/* scandir (optional, sort) files under directory "pathname" */ int _scandir(char *pathname) { DIR *dirp; struct dirent **namelist; char *currentpath; long max_pathname, namelen, orig_offset; int count; dirp = opendir(pathname); if (dirp == NULL) { perror(":: opendir "); return RET_ERROR; } max_pathname = dirent_buf_size(dirp); namelen = strlen(pathname) + max_pathname + 1; currentpath = malloc(namelen); if (currentpath == NULL) { perror(":: malloc "); closedir(dirp); return RET_ERROR; } memset(currentpath, 0, namelen); orig_offset = sprintf(currentpath, "%s/", pathname); // count = scandir(currentpath, &namelist, NULL, alphasort); count = scandir(currentpath, &namelist, NULL, NULL); if (count == -1) perror(":: scandir "); else { while (count--) { if (is_dot_or_dotdot(namelist[count]->d_name)) { continue; free(namelist[count]); } sprintf(currentpath + orig_offset, "%s", namelist[count]->d_name); printf("%s\n", currentpath); free(namelist[count]); #ifdef DIRLIST_RECURSIVE if (is_directory(currentpath)) _scandir(currentpath); #endif } free(namelist); } closedir(dirp); free(currentpath); return RET_OKAY; }
int scandir (const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) { int (*_scandir) (const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)); char *new_path = NULL; int ret; _scandir = (int (*)(const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **))) dlsym (RTLD_NEXT, "scandir"); new_path = redirect_path (dirp); ret = _scandir (new_path, namelist, filter, compar); free (new_path); return ret; }