| @@ -19,7 +19,6 @@ |
| 19 | 19 | | #include <stdlib.h> |
| 20 | 20 | | #include <stdio.h> |
| 21 | 21 | | #include <string.h> |
| 22 | | − | #include <dirent.h> |
| 23 | 22 | | #include <sys/select.h> |
| 24 | 23 | | #include <sys/stat.h> |
| 25 | 24 | | #include <sys/time.h> |
| @@ -48,7 +47,6 @@ typedef enum { |
| 48 | 47 | | |
| 49 | 48 | | void update_title(); |
| 50 | 49 | | int check_append(const char*); |
| 51 | | − | void read_dir_rec(const char*); |
| 52 | 50 | | void run(); |
| 53 | 51 | | |
| 54 | 52 | | appmode_t mode; |
| @@ -56,7 +54,6 @@ img_t img; |
| 56 | 54 | | tns_t tns; |
| 57 | 55 | | win_t win; |
| 58 | 56 | | |
| 59 | | − | #define DNAME_CNT 512 |
| 60 | 57 | | #define FNAME_CNT 1024 |
| 61 | 58 | | const char **filenames; |
| 62 | 59 | | int filecnt, fileidx; |
| @@ -96,8 +93,9 @@ int load_image(int new) { |
| 96 | 93 | | } |
| 97 | 94 | | |
| 98 | 95 | | int main(int argc, char **argv) { |
| 99 | | − | int i; |
| 96 | + | int i, j; |
| 100 | 97 | | const char *filename; |
| 98 | + | char **fnames; |
| 101 | 99 | | struct stat fstats; |
| 102 | 100 | | |
| 103 | 101 | | parse_options(argc, argv); |
| @@ -124,10 +122,15 @@ int main(int argc, char **argv) { |
| 124 | 122 | | for (i = 0; i < options->filecnt; ++i) { |
| 125 | 123 | | filename = options->filenames[i]; |
| 126 | 124 | | if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) { |
| 127 | | − | if (options->recursive) |
| 128 | | − | read_dir_rec(filename); |
| 129 | | − | else |
| 125 | + | if (options->recursive && (fnames = read_dir_rec(filename))) { |
| 126 | + | for (j = 0; fnames[j]; ++j) { |
| 127 | + | if (!check_append(fnames[j])) |
| 128 | + | free(fnames[j]); |
| 129 | + | } |
| 130 | + | free(fnames); |
| 131 | + | } else { |
| 130 | 132 | | warn("ignoring directory: %s", filename); |
| 133 | + | } |
| 131 | 134 | | } else { |
| 132 | 135 | | check_append(filename); |
| 133 | 136 | | } |
| @@ -215,75 +218,6 @@ int check_append(const char *filename) { |
| 215 | 218 | | } |
| 216 | 219 | | } |
| 217 | 220 | | |
| 218 | | − | int fncmp(const void *a, const void *b) { |
| 219 | | − | return strcoll(*((char* const*) a), *((char* const*) b)); |
| 220 | | − | } |
| 221 | | − | |
| 222 | | − | void read_dir_rec(const char *dirname) { |
| 223 | | − | char *filename; |
| 224 | | − | const char **dirnames; |
| 225 | | − | int dircnt, diridx; |
| 226 | | − | int fcnt, fstart; |
| 227 | | − | unsigned char first; |
| 228 | | − | size_t len; |
| 229 | | − | DIR *dir; |
| 230 | | − | struct dirent *dentry; |
| 231 | | − | struct stat fstats; |
| 232 | | − | |
| 233 | | − | if (!dirname) |
| 234 | | − | return; |
| 235 | | − | |
| 236 | | − | dircnt = DNAME_CNT; |
| 237 | | − | diridx = first = 1; |
| 238 | | − | dirnames = (const char**) s_malloc(dircnt * sizeof(const char*)); |
| 239 | | − | dirnames[0] = dirname; |
| 240 | | − | |
| 241 | | − | fcnt = 0; |
| 242 | | − | fstart = fileidx; |
| 243 | | − | |
| 244 | | − | while (diridx > 0) { |
| 245 | | − | dirname = dirnames[--diridx]; |
| 246 | | − | if (!(dir = opendir(dirname))) { |
| 247 | | − | warn("could not open directory: %s", dirname); |
| 248 | | − | } else { |
| 249 | | − | while ((dentry = readdir(dir))) { |
| 250 | | − | if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, "..")) |
| 251 | | − | continue; |
| 252 | | − | |
| 253 | | − | len = strlen(dirname) + strlen(dentry->d_name) + 2; |
| 254 | | − | filename = (char*) s_malloc(len * sizeof(char)); |
| 255 | | − | snprintf(filename, len, "%s%s%s", dirname, |
| 256 | | − | dirname[strlen(dirname)-1] == '/' ? "" : "/", dentry->d_name); |
| 257 | | − | |
| 258 | | − | if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) { |
| 259 | | − | if (diridx == dircnt) { |
| 260 | | − | dircnt *= 2; |
| 261 | | − | dirnames = (const char**) s_realloc(dirnames, |
| 262 | | − | dircnt * sizeof(const char*)); |
| 263 | | − | } |
| 264 | | − | dirnames[diridx++] = filename; |
| 265 | | − | } else { |
| 266 | | − | if (check_append(filename)) |
| 267 | | − | ++fcnt; |
| 268 | | − | else |
| 269 | | − | free(filename); |
| 270 | | − | } |
| 271 | | − | } |
| 272 | | − | closedir(dir); |
| 273 | | − | } |
| 274 | | − | |
| 275 | | − | if (!first) |
| 276 | | − | free((void*) dirname); |
| 277 | | − | else |
| 278 | | − | first = 0; |
| 279 | | − | } |
| 280 | | − | |
| 281 | | − | if (fcnt > 1) |
| 282 | | − | qsort(filenames + fstart, fcnt, sizeof(char*), fncmp); |
| 283 | | − | |
| 284 | | − | free(dirnames); |
| 285 | | − | } |
| 286 | | − | |
| 287 | 221 | | #ifdef EXT_COMMANDS |
| 288 | 222 | | int run_command(const char *cline, Bool reload) { |
| 289 | 223 | | int fncnt, fnlen; |