I am trying to implement the unix command in my program "ls -R" without the system call.
Right now, what my recursion code does is like, go to the folder (either ./ or a specified one) and look at the files. If it's a folder, print the name of the folder and call the function again into that folder. Then it does the same thing for that folder.
The problem is that the output is messed up. When it gets into a folder, it doesn't go back to the like above and prints the remaining files on the last folder. It just prints them after it goes into a folder like this:
Code:
: sls -R
sls.c backup2sls.c forfun:
ffscandir.c ffscandir.c~ newf:
blankfold:
soetxt.txt
ffa.out
backupsls.c sls.man a.out 212public sfouchelink 212shared sls makefile fouche
The current folder has forfun/, 212public/ and 212shared as symb links, and forfun/newf/ has that soetxt.txt file, and blankfold/ has nothing in it
This is the code so far:
Code:
void rcommand(char *dirPath) {
...
...
while((d = readdir(dp)) != NULL)
{
if(!strcmp(d->d_name,".") || !strcmp(d->d_name,"..")) {
continue;
}
//Hiden files/folders
if (!acomm){
if (*(d->d_name) == '.')
continue;
}
if (d->d_type & DT_DIR) {
printf("%s:\n", d->d_name);
strcpy(temp, dirPath);
strcat(temp, "/");
strcat(temp, d->d_name);
rcommand(temp);
} else {
printf("%s ", d->d_name);
}
}
printf("\n");
closedir(dp);
return;
}