Hello all.
Is it possible for me to invoke a script through a program?
For example. I would like to create a script that copies all files in a directory, create a new directory and place the copied files into those directories.
I would like to invoke this script only in a certain situation.
This is all done in Linux.
Any help would be greatly appreciated. Thanks.
6 replies to this topic
#1
Posted 26 November 2011 - 01:08 PM
|
|
|
#2
Posted 26 November 2011 - 05:05 PM
The system command can do something like this.
#4
Posted 27 November 2011 - 04:26 AM
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.
#5
Posted 27 November 2011 - 05:15 AM
Did I said that fork invokes a script?
I think i'm able to write a code for printing "Hello, World!". Proud of that!
#6
Posted 27 November 2011 - 03:24 PM
Though system is a simple fix, it's dangerous if you screw up the command line, and also shell-dependent. fork is a bit more complicated but will get the job done and you can pick which shell you want to use. On the other hand, you could just code it yourself.
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string>
#include <cstring>
#include <cstdio>
#include <cerrno>
// Write this yourself, preferably something more efficient than copying the
// file byte by byte.
bool copy_file(const char *from, const char *to)
{
FILE *ifd, *ofd;
ifd = fopen(from, "rb");
if( !ifd )
return false;
ofd = fopen(to, "wb");
if( !ofd )
{
fclose(ifd);
return false;
}
while( !feof(ifd) )
putc(getc(ifd), ofd);
bool success = !ferror(ifd) || !ferror(ofd);
fclose(ifd);
fclose(ofd);
return success;
}
bool ensure_dir_exists(const char *name, int perm = 0700)
{
struct stat dir_info;
// Check to see if the directory exists.
if( stat(name, &dir_info) == 0 )
{
// Something with the same name as the "name" already exists. If that
// something is a directory, then all's well since we can just use that.
// If not, we're screwed.
return (dir_info.st_mode & S_IFDIR) != 0;
}
else
// Failed to stat "name" so it doesn't exist; create the directory. Note
// that perm defaults to 0700, which means that only the owner can touch
// the new directory, and others can't access it at all. Change the
// permissions using the flags in sys/stat.h to your liking.
return mkdir(name, perm) == 0;
}
// Here we assume all parameters are valid. The onus is on you to check that.
// Recursively copies from "from" to "to", creating "to" if it doesn't exist.
// Returns the number of files copied, -1 on failure.
int copy_dir(const char *from, const char *to, bool recursive)
{
// If the destination directory doesn't exist, create it.
if( !ensure_dir_exists(to) )
{
printf("[DEBUG]: Failed to copy <%s> : Destination <%s> couldn't be created.\n",
from, to);
return -1;
}
// Open the source directory for reading, bail out if it failed.
DIR *dp = opendir(from);
if( !dp )
{
printf("[DEBUG]: Failed to copy <%s> : Not a directory or access denied.\n", from);
return -1;
}
std::string source = from;
std::string dest = to;
int files_copied = 0;
struct dirent *entry;
// Iterate through the entire directory.
for( entry = readdir(dp); entry != NULL; entry = readdir(dp) )
{
// Copy entry name.
std::string ent_name = entry->d_name;
// Because . and .. can appear in this directory entry list, we have to
// make sure we don't try to copy those. That'd be bad.
if( (ent_name == ".") || (ent_name == "..") )
continue;
// Create path names for the objects we're going to copy.
std::string tmpf = source + "/" + ent_name;
std::string tmpt = dest + "/" + ent_name;
const char * const new_from = tmpf.c_str();
const char * const new_to = tmpt.c_str();
printf("[DEBUG]: %s --> %s\n", new_from, new_to);
// Get the file information for this directory entry.
struct stat file_info;
if( stat(new_from, &file_info) != 0 )
{
printf("[DEBUG]: Failed to stat <%s>. Reason: %s\n", new_from, strerror(errno));
closedir(dp);
return -1;
}
// Is it a directory?
if( file_info.st_mode & S_IFDIR )
{
// If we're not recursively copying, ignore this directory.
if( !recursive )
continue;
// Okay, we have a regular directory that we want to copy. Note that
// the destination directory might not exist, so we might have to
// create it.
if( !ensure_dir_exists(new_to) )
{
printf("[DEBUG]: Failed to create destination directory <%s>\n", new_to);
closedir(dp);
return -1;
}
// If we get here we're guaranteed that the source and destination
// directories exist. Copy them over.
int subdir_copy = copy_dir(new_from, new_to, recursive);
// Check to see if the subdirectory copying worked. If it failed,
// return -1. Otherwise, add to the count of files we copied.
if( subdir_copy >= 0 )
files_copied += subdir_copy;
else
{
printf("[DEBUG]: Subdir copy of <%s> failed.\n", new_from);
closedir(dp);
return -1;
}
}
else
{
// Copy the file over, bail out if it returns false.
if( copy_file(new_from, new_to) )
++files_copied;
else
{
printf("[DEBUG]: Failed to copy file <%s> to <%s>.\n", new_from, new_to);
closedir(dp);
return -1;
}
}
}
closedir(dp);
return files_copied;
}
int main(int argc, char **argv)
{
int rval = copy_dir(argv[1], argv[2], true);
return rval >= 0 ? 0 : 1;
}
sudo rm -rf /
#7
Posted 27 November 2011 - 08:04 PM
AKMafia001 said:
Did I said that fork invokes a script?
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









