#include #include #include #include #include "dirwalk.h" /* ripped off from K&R 2nd ed */ void dirwalk(char *dir, void (*fnp)(char*)) { char buf[1024]; struct dirent *dp; DIR *dfd; char *fn; dfd = opendir(dir); if(dfd == NULL){ fprintf(stderr, "dirwalk: no such directory: %s\n", dir); return; } while((dp = readdir(dfd)) != NULL){ fn = dp->d_name; if(strcmp(fn, ".") == 0 || strcmp(fn, "..") == 0) continue; if(strlen(dir) + strlen(fn) + 2 > sizeof buf) fprintf(stderr, "dirwalk: filename too long: %s", buf); else{ snprintf(buf, sizeof buf, "%s/%s", dir, fn); (*fnp)(buf); } } closedir(dfd); }