Does anyone know how to copy a directory, including all subdirectories across to another folder The code I've written so far copies over text files and directories, but it doesn't copy all the sub-directories. Before going any further, I have seen questions like this before, but there don't cover what I need to know, or there covered in other languages.
Sorry if my code is a mess.
Anyway, here is my code:
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define BUFFERSIZE 4096
#define COPYMODE 0644
#define FILE_MODE S_IRWXU
DIR * directory;
struct dirent * entry;
DIR * directoryTwo;
struct dirent * entryTwo;
struct stat info;
// copyFile
void copyFile(const char * src, const char * dst)
{
FILE * file1, *file2;
file1 = fopen(src, "r");
if(file1 == NULL)
{
perror("Cannot open source file");
fclose(file1);
exit(-1);
}
file2 = fopen(dst, "w");
if(file2 == NULL) {
perror("cannot open destination file");
fclose(file2);
exit(-1);
}
char currentchar;
while((currentchar = getc(file1)) != EOF) {
fputc(currentchar, file2);
}
fclose(file1);
fclose(file2);
}
void goThoughFile(const char * srcFilePath, const char * dst)
{
char srcFile[260];
char dstFile[260];
directory = opendir(srcFilePath);
if(directory == NULL) {
printf("Error");
exit(1);
}
while((entry = readdir(directory)) != NULL)
{
if(strstr(entry->d_name, "DS_Store") || strstr(entry->d_name, ".localized") ||
strstr(".", &entry->d_name[0]) || strstr("..", &entry->d_name[0]))
continue;
else if(entry->d_type == DT_REG)
{
sprintf(srcFile, "%s/%s", srcFilePath, entry->d_name);
sprintf(dstFile, "%s/%s", dst, entry->d_name);
copyFile(srcFile, dstFile);
}
if(entry->d_type == DT_DIR)
{
chdir(dst);
mkdir(entry->d_name, S_IRWXU);
sprintf(dstFile, "%s%s/%s", dst, entry->d_name, "/..");
chdir(dstFile);
mkdir(entry->d_name, S_IRWXU);
}
}
}
int main()
{
goThoughFile(someFilePath, anotherFilePath);
return 0;
}