int walk_recur(char *dname, regex_t * reg, int spec) { struct dirent *dent; DIR *dir; struct stat st; char fn[FILENAME_MAX]; int res = WALK_OK; int len = strlen(dname); if (len >= FILENAME_MAX - 1) return WALK_NAMETOOLONG; strcpy(fn, dname); fn[len++] = '/'; if (!(dir = opendir(dname))) { warn("can't open %s", dname); return WALK_BADIO; } errno = 0; while ((dent = readdir(dir))) { if (!(spec & WS_DOTFILES) && dent->d_name[0] == '.') continue; if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; strncpy(fn + len, dent->d_name, FILENAME_MAX - len); if (lstat(fn, &st) == -1) { warn("Can't stat %s", fn); res = WALK_BADIO; continue; } /* don't follow symlink unless told so */ if (S_ISLNK(st.st_mode) && !(spec & WS_FOLLOWLINK)) continue; /* will be false for symlinked dirs */ if (S_ISDIR(st.st_mode)) { /* recursively follow dirs */ if ((spec & WS_RECURSIVE)) walk_recur(fn, reg, spec); if (!(spec & WS_MATCHDIRS)) continue; } /* pattern match */ if (!regexec(reg, fn, 0, 0, 0)) { pcaplist[num_of_pcaps] = (char *)malloc(256); memcpy(pcaplist[num_of_pcaps], fn, 256); num_of_pcaps++; puts(fn); } } if (dir) closedir(dir); return res ? res : errno ? WALK_BADIO : WALK_OK; }
int walk_dir(char *dname, char *pattern, int spec) { regex_t r; int res; if (regcomp(&r, pattern, REG_EXTENDED | REG_NOSUB)) return WALK_BADPATTERN; res = walk_recur(dname, &r, spec); regfree(&r); return res; }
int walk_dir(char *dname, char *pattern, int spec, void (*hashFile)(char *file)) { regex_t r; int res; if (regcomp(&r, pattern, REG_EXTENDED | REG_NOSUB)) return WALK_BADPATTERN; // printf("%s",dname); res = walk_recur(dname, &r, spec,hashFile); regfree(&r); return res; }
int walk_recur(char *dname, regex_t *reg, int spec, void (*hashFile)(char *file)) { struct dirent *dent; DIR *dir; struct stat st; char fn[FILENAME_MAX]; int res = WALK_OK; int len = (int)strlen(dname); if (len >= FILENAME_MAX - 1) return WALK_NAMETOOLONG; strcpy(fn, dname); fn[len++] = '/'; if (!(dir = opendir(dname))) { warn("can't open %s", dname); return WALK_BADIO; } errno = 0; while ((dent = readdir(dir))) { if (!(spec & WS_DOTFILES) && dent->d_name[0] == '.') continue; if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; strncpy(fn + len, dent->d_name, FILENAME_MAX - len); if (lstat(fn, &st) == -1) { warn("Can't stat %s", fn); res = WALK_BADIO; continue; } /* don't follow symlink unless told so */ if (S_ISLNK(st.st_mode) && !(spec & WS_FOLLOWLINK)) continue; /* will be false for symlinked dirs */ if (S_ISDIR(st.st_mode)) { /* recursively follow dirs */ if ((spec & WS_RECURSIVE)) walk_recur(fn, reg, spec,hashFile); if (!(spec & WS_MATCHDIRS)) continue; } //printf("%s\n",fn); if(S_ISREG(st.st_mode)){ printf("Considering file %s\n",fn); hashFile(fn); //printf("ISRED IS %d \n",S_IFREG); } /* pattern match */ if (!regexec(reg, fn, 0, 0, 0)){ // puts(fn); } } if (dir) closedir(dir); return res ? res : errno ? WALK_BADIO : WALK_OK; }