Code:
#include <stdio.h> // Standard input/output routines
#include <dirent.h> // readdir(), etc.
#include <sys/stat.h> // stat(), etc.
#include <string.h> // strstr(), etc.
#include <unistd.h> // getcwd(), etc.
#include <sys/types.h>
#include <fcntl.h>
#define MAX_DIR_PATH 1024 // Maximal full path length we support
void
sysbackup(char* origPath, char* backupPath)
{
DIR *dir; // Pointer to the scanned directory
struct dirent *entry; // Pointer to one directory entry
struct dirent *lastentry;
char cwd[MAX_DIR_PATH + 1]; // Current working directory
struct stat dir_stat; // Used by stat()
int f_from; //stream of source file
int f_to; //stream of target file
char *buf[MAX_DIR_PATH+1]; // input buffer
// First, save the path of current working directory
printf("test \n");
if (!getcwd (cwd, MAX_DIR_PATH + 1))
{
perror ("getcwd:");
return;
}
// Open the directory to read
dir = opendir (".");
if (!dir)
{
fprintf (stderr, "Cannot read directory '%s': ", cwd);
perror ("");
return;
}
/*=======================================================
*
* Scan the directory, traversing each sub-directory, and each file / directory name.
*
========================================================*/
while ((entry = readdir (dir)))
{
printf("while \n");
// Check if the given entry is a directory
if (stat (entry->d_name, &dir_stat) == -1)
{
perror ("stat:");
continue;
}
// Skip the "." and ".." entries, to avoid loops
if (strcmp (entry->d_name, ".") == 0)
continue;
if (strcmp (entry->d_name, "..") == 0)
continue;
/* Is this a directory? */
if (S_ISDIR (dir_stat.st_mode))
{
printf("isdir \n");
lastentry=entry;
/* Change into the new directory */
if (chdir (entry->d_name) == -1)
{
fprintf (stderr, "Cannot chdir into '%s': ", entry->d_name);
perror ("");
continue;
}
mkdir(strcat(backupPath, lastentry->d_name), 777);
/* check this directory */
sysbackup(strcat(origPath, lastentry->d_name), backupPath);
/* Finally, restore the original working directory. */
if (chdir ("..") == -1)
{
fprintf (stderr, "Cannot chdir back to '%s': ", cwd);
perror ("");
return;
}
} else //if (S_ISREG (dir_stat.st_mode)) //If not directory assume its a file
{
printf("isreg \n");
f_from=open(strcat(origPath, entry->d_name), 777);
if (!f_from)
{
fprintf(stderr, "Cannot open source file1: ");
perror("");
return;
}
f_to=creat(strcat(backupPath, entry->d_name), 777);
if (!f_to)
{
fprintf(stderr, "Cannot open target file: ");
perror("");
return;
}
while (read(f_from, buf, MAX_DIR_PATH+1))
{
if (write(f_to, buf, MAX_DIR_PATH+1) == EOF)
{ /* error writing data */
fprintf(stderr, "Error writing to target file2: %d:", f_to);
perror("");
return;
}
}
}
}
}// End sysbackup()
void
stdbackup(char* origPath, char* backupPath)
{
DIR *dir; // Pointer to the scanned directory
struct dirent *entry; // Pointer to one directory entry
struct dirent *lastentry;
char cwd[MAX_DIR_PATH + 1]; // Current working directory
struct stat dir_stat; // Used by stat()
FILE* f_from; //stream of source file
FILE* f_to; //stream of target file
char buf[MAX_DIR_PATH+1]; // input buffer
// First, save the path of current working directory
if (!getcwd (cwd, MAX_DIR_PATH + 1))
{
perror ("getcwd:");
return;
}
// Open the directory to read
dir = opendir (".");
if (!dir)
{
fprintf (stderr, "Cannot read directory '%s': ", cwd);
perror ("");
return;
}
/*=======================================================
*
* Scan the directory, traversing each sub-directory, and each file / directory name.
*
========================================================*/
while ((entry = readdir (dir)))
{
// Check if the given entry is a directory
if (stat (entry->d_name, &dir_stat) == -1)
{
perror ("stat:");
continue;
}
// Skip the "." and ".." entries, to avoid loops
if (strcmp (entry->d_name, ".") == 0)
continue;
if (strcmp (entry->d_name, "..") == 0)
continue;
/* Is this a directory? */
if (S_ISDIR (dir_stat.st_mode))
{
lastentry=entry;
/* Change into the new directory */
if (chdir (entry->d_name) == -1)
{
fprintf (stderr, "Cannot chdir into '%s': ", entry->d_name);
perror ("");
continue;
}
mkdir(strcat(backupPath, lastentry->d_name), 777);
/* check this directory */
stdbackup(strcat(origPath, lastentry->d_name), backupPath);
/* Finally, restore the original working directory. */
if (chdir ("..") == -1)
{
fprintf (stderr, "Cannot chdir back to '%s': ", cwd);
perror ("");
return;
}
} else //If not directory assume its a file
{
f_from=fopen(strcat(origPath, entry->d_name), "r");
if (!f_from)
{
fprintf(stderr, "Cannot open source file: ");
perror("");
return;
}
f_to=fopen(strcat(backupPath, entry->d_name), "w+");
if (!f_to)
{
fprintf(stderr, "Cannot open target file: ");
perror("");
return;
}
while (fgets(buf, MAX_DIR_PATH+1, f_from))
{
if (fputs(buf, f_to) == EOF)
{ /* error writing data */
fprintf(stderr, "Error writing to target file: ");
perror("");
return;
}
}
}
}
}// End stdbackup()
int main (int argc, char **argv)
{
char *option; // Option -sys or -std
char *dir_path; // directory path
struct stat dir_stat; // Used by stat()
char dir_to_copy[MAX_DIR_PATH + 1];
char cwd[MAX_DIR_PATH + 1];
if (argc != 3 || !argv[1] || !argv[2])
{
fprintf (stderr, "Usage: %s <option> <directory path>\n",
argv[0]);
return(-1);
}
option = argv[1];
dir_path = argv[2];
// Make sure the given path refers to a directory
if (stat (dir_path, &dir_stat) == -1)
{
perror ("stat:");
return(-1);
}
if (!S_ISDIR (dir_stat.st_mode))
{
fprintf (stderr, "'%s' is not a directory\n", dir_path);
return(-1);
}
// Change into the given directory
if (chdir (dir_path) == -1)
{
fprintf (stderr, "Cannot change to directory '%s': ", dir_path);
perror ("");
return(-1);
}
getcwd (cwd, MAX_DIR_PATH + 1);
if (strcmp(option, "-std") == 0)
{
mkdir(strcat(dir_to_copy, "BackStd"), 777);
stdbackup(cwd, dir_to_copy);
} else if (strcmp (option, "-sys") == 0)
{
mkdir(strcat(dir_to_copy, "BackSys"), 777);
sysbackup(cwd, dir_to_copy);
} else
{
fprintf (stderr, "'%s' is not a valid option, try again with -std or -sys\n", option);
return(-1);
}
return 0;
}