I am very new to C programming and there is little helpful information anywhere on how to do recursive copying of the contents of a directory in C (not C++,C#, Objective-C,
This won't solve your problem, but I spotted a possible error in your code. I don't see the recursion happening.
You see, this code may work fine if you only have one subfolder. But what do you do, if there's a subfolder in that subfolder?
You need a recursive directory search function. This may look like this:
void SearchDirectory(const char *name) {
DIR *dir = opendir(name); //Assuming absolute pathname here.
if(dir) {
char Path[256], *EndPtr = Path;
struct dirent *e;
strcpy(Path, name); //Copies the current path to the 'Path' variable.
EndPtr += strlen(name); //Moves the EndPtr to the ending position.
while((e = readdir(dir)) != NULL) { //Iterates through the entire directory.
struct stat info; //Helps us know about stuff
strcpy(EndPtr, e->d_name); //Copies the current filename to the end of the path, overwriting it with each loop.
if(!stat(Path, &info)) { //stat returns zero on success.
if(S_ISDIR(info.st_mode)) { //Are we dealing with a directory?
//Make corresponding directory in the target folder here.
SearchDirectory(Path); //Calls this function AGAIN, this time with the sub-name.
} else if(S_ISREG(info.st_mode) { //Or did we find a regular file?
//Run Copy routine
}
}
}
}
}
This code will call the function SearchDirectory
for as many times as there are subdirectories in the current folder. If the architecture runs too deep you may run out of memory and the program may crash, so please beware.
Once it cannot find any more directories in the folder (it has reached the 'leaf'-folders), it will iterate over the remaining files and then end the function, popping the last function call from the stack, allowing the previous call to SearchDirectory
to continue searching.
After all is said and done, you'll have a full recursive copy.
P.S: Sorry for the spoiler, I read your comment only after I was done typing.