#include #include #include #include #include #include #include #include int filename_ok(const char *); int main(int argc, char *argv[]) { DIR *d; struct dirent *de; char fn[PATH_MAX]; if (argc != 2) { fprintf(stderr, "usage: %s \n", argv[0]); fprintf(stderr, "deletes all files that don't start with 3 digits in \n"); return 1; } if ((d = opendir(argv[1])) == NULL) { perror("opendir"); return 1; } while ((de = readdir(d)) != NULL) { if (!filename_ok(de->d_name)) { sprintf(fn, "%s%s%s", argv[1], argv[1][strlen(argv[1]) - 1] == '/' ? "" : "/", de->d_name); if (unlink(fn) < 0) { if (errno == EISDIR) continue; perror("unlink"); } } } closedir(d); return 0; } int filename_ok(const char *file) { if (file == NULL) { fprintf(stderr, "I hate myself and want to die.\n"); return 1; } if (!strcmp(file, "..") || !strcmp(file, ".")) return 1; if (strlen(file) < 3) return 0; if (!isdigit(file[0]) || !isdigit(file[1]) || !isdigit(file[2])) return 0; return 1; }