I am trying to write a function given a current working directory will print all the contents of that directory as well as the contents in the sub directories in the cwd.
void printDir(char *cwd)
{
printf("file path after printdir call: %s\n",cwd);
DIR *dirPtr = opendir(cwd);
struct dirent *dirEnt;
char *slash = "/";
char *dot = ".";
char *dotdot = "..";
char *pathPrefix = malloc(sizeof(cwd) + sizeof(slash)+1);
pathPrefix = strcat(cwd,slash);
if (dirPtr != NULL)
{
while((dirEnt = readdir(dirPtr)) != NULL)
{
char *temp = dirEnt->d_name;
if (strcmp(temp,dot) != 0 && strcmp(temp,dotdot) != 0)
{
char *tempFullPath = malloc(sizeof(pathPrefix) + sizeof(temp) + 1);
tempFullPath = strcpy(tempFullPath, cwd);
strcat(tempFullPath, temp);
printf("file path before we try to openthedir: %s\n",tempFullPath);
DIR *tempSubDirPtr = opendir(tempFullPath);
printf("filePath after we try to open this shit: %s\n",tempFullPath2);
if (tempSubDirPtr != NULL)
{
printf("file path right before a recursive call: %s\n",tempFullPath);
closedir(tempSubDirPtr);
printDir(tempFullPath);
}
printf("%s\n",tempFullPath);
}
}
}
else
{
With the debugging printf()'s the console output is:
current working directory string after getcwd: /home/TTU/canorman/testUtil
file path after printdir call: /home/TTU/canorman/testUtil
file path before we try to openthedir: /home/TTU/canorman/testUtil/find.c
filePath after we try to open this shit: /home/TTU/canorman/testUtil/find.c
/home/TTU/canorman/testUtil/find.c
file path before we try to openthedir: /home/TTU/canorman/testUtil/find2.c
filePath after we try to open this shit: /home/TTU/canorman/testUtil/find2.c
/home/TTU/canorman/testUtil/find2.c
file path before we try to openthedir: /home/TTU/canorman/testUtil/find
filePath after we try to open this shit: /home/TTU/canorman/testUtil/find
/home/TTU/canorman/testUtil/find
file path before we try to openthedir: /home/TTU/canorman/testUtil/testDir
filePath after we try to open this shit: /home/TTU/canorman/testUA
file path right before a recursive call: /home/TTU/canorman/testUA
file path after printdir call: /home/TTU/canorman/testUA
Could not open specified working directory
/home/TTU/canorman/testUA/
So as you can see in the last few lines of the output that the file string goes from
/home/TTU/canorman/testUtil/testDir
to
/home/TTU/canorman/testUA
I can't find anything in the opendir(3) man pages about this happening. Any ideas as to why this going on.