Skip to content

Instantly share code, notes, and snippets.

@lstipakov
Last active July 20, 2016 15:55
Show Gist options
  • Save lstipakov/b723ffd120f84b635dd00df50c8cccd5 to your computer and use it in GitHub Desktop.
Save lstipakov/b723ffd120f84b635dd00df50c8cccd5 to your computer and use it in GitHub Desktop.
void processDir(const char* d) {
DIR* dir = NULL;
chdir(d);
dir = opendir("./");
/*
char newdir[PATH_MAX];
strcpy(newdir, "./");
strcat(newdir, d);
dir = opendir(newdir);
printf("\npid %d, dir %s\n", getpid(), newdir);
*/
if (dir == NULL) {
printf("Cannot open directory %s, err: %d\n", d, errno);
return;
}
struct dirent* ent = NULL;
struct stat st;
char attrs[11]; // drwxrwxrwx
while ((ent = readdir(dir)) != NULL) {
strcpy(attrs, "----------");
lstat(ent->d_name, &st);
fill_attrs(attrs, st.st_mode);
printf("%s %10ld %s", attrs, st.st_size, ent->d_name);
if (S_ISLNK(st.st_mode)) {
char buf[PATH_MAX + 1];
readlink(ent->d_name, buf, PATH_MAX + 1);
struct stat st1;
if (stat(buf, &st1) == -1) {
printf(" symlink broken");
} else {
printf(" symlink");
}
}
if ((S_ISDIR(st.st_mode) != 0) &&
(strncmp(ent->d_name, ".", 1) != 0) &&
(strncmp(ent->d_name, "..", 2) != 0)) {
if (fork() == 0) {
// child
processDir(ent->d_name);
break;
} else {
//printf("\nDir: %s\n", ent->d_name);
int status;
wait(&status);
//break;
}
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment