static void traverse_file(char *filename) { struct stat buf; int st; int link = 0; int r; st = lstat(filename, &buf); if (!st && S_ISLNK(buf.st_mode)) { /* is a symbolic link */ link = 1; st = stat(filename, &buf); } if (st || !S_ISDIR(buf.st_mode)) { file_action(filename); return; } /* is a directory */ if (cmd.recursive<=1 && link==1) { /* ignore link */ if (cmd.verbose>=0) { fprintf(stderr, "%s: %s: directory is a symbolic link -- ignored\n", cmd.name, filename); symlink_warnings++; } return; } if (cmd.recursive==0) { /* ignore */ if (cmd.verbose>=0) { fprintf(stderr, "%s: %s: is a directory -- ignored\n", cmd.name, filename); isreg_warnings++; } return; } r = known_inode(buf.st_ino, buf.st_dev); if (r != -1) { /* already traversed */ if (cmd.verbose>0) { fprintf(stderr, "Already visited directory %s -- skipped.\n", filename); } return; } add_inode(buf.st_ino, buf.st_dev, 1); /* recursively traverse directory */ { char **filelist; int count; get_filelist(filename, &filelist, &count); traverse_files(filelist, count); free_filelist(filelist, count); } return; }
void read_dir(const char * directory, void (*file_action)(const char *)) { uint length; struct dirent *dirent; DIR *dir; dir = opendir(directory); if (dir != NULL) { while ((dirent = readdir(dir)) != NULL) { length = strlen (dirent->d_name); if (length >= 4) { if (strcmp (".bmp", &(dirent->d_name[length - 4])) == 0) { char path[PATH_MAX + 1]; realpath(dirent->d_name, path); file_action(path); } } } closedir(dir); } }