int main(int argc, char *argv[]) { int c, i; while ((c = getopt(argc, argv, "0")) != -1) switch (c) { case '0': sep = '\0'; break; default: usage: fprintf(stderr, "Usage: mdirs [-0] dirs...\n"); exit(1); } if (argc == optind) goto usage; char toplevel[PATH_MAX]; if (!getcwd(toplevel, sizeof toplevel)) { perror("mdirs: getcwd"); exit(-1); } for (i = 0; i < argc; i++) { mdirs(argv[i]); if (chdir(toplevel) < 0) { perror("mdirs: chdir"); exit(-1); } } return 0; }
void mdirs(char *fpath) { DIR *dir; struct dirent *d; struct stat st; dir = opendir(fpath); if (!dir) return; if (chdir(fpath) < 0) { closedir(dir); return; } int dotonly = 0; if (stat("cur", &st) == 0 && S_ISDIR(st.st_mode) && stat("new", &st) == 0 && S_ISDIR(st.st_mode)) { pwd(); dotonly = 1; // Maildir++ } while ((d = readdir(dir))) { #if defined(DT_DIR) && defined(DT_UNKNOWN) if (d->d_type != DT_DIR && d->d_type != DT_UNKNOWN) continue; #endif if (d->d_name[0] == '.' && d->d_name[1] == 0) continue; if (d->d_name[0] == '.' && d->d_name[1] == '.' && d->d_name[2] == 0) continue; if (dotonly && d->d_name[0] != '.') continue; mdirs(d->d_name); } if (chdir("..") < 0) exit(-1); closedir(dir); }
/* generic routine to do things on all files in the tree */ void dosubs(int *level, int width, int depth) { int d; char dir[256]; (*level)++; mdirs(width); printf("Starting %d\n", *level); /* only decend if not at top level */ if (*level < depth) { for ( d = 0 ; d < width ; d++ ) { sprintf(dir, "%x", d); chdir(dir); dosubs(level, width, depth); chdir(".."); } } printf("Ending %d\n", *level); (*level)--; }