I am writing a code to traverse through the directory using depth first algorithm. The problem is the program is not displaying anything and gives Segmentation Fault error. I tried to debug it but it was worthless as I am still learning stuff. So now I need the help of experts. Here is the code:
void func(char path[]);
int main(int argc, char *argv) {
char buf[255];
scanf("%s",buf);
func(buf);
return 0;
}
void func(char path[]) {
DIR *dirp;
struct stat states;
struct dirent *direntp;
printf("Inside\n");
dirp=opendir(path);
stat(path, &states);
while ((direntp=readdir(dirp)) != NULL) {
if (S_ISDIR(states.st_mode)) {
printf("Calling Func\n");
func(direntp->d_name);
chdir("..");
} else if (!S_ISDIR(states.st_mode)) {
printf(" %s\n", direntp->d_name);
} else if (!strcmp(direntp->d_name, ".") || !strcmp(direntp->d_name, "..")) {
continue;
}
}
return ;
}
Inside func, in front of the while, put that:
and, btw, its
int main (int argc, char *argv[])