diff options
author | Bert <ber.t@gmx.com> | 2011-04-08 10:23:42 +0200 |
---|---|---|
committer | Bert <ber.t@gmx.com> | 2011-04-08 10:23:42 +0200 |
commit | a90bd1c833b3475e434bd2de95ab9dd0347f1780 (patch) | |
tree | a7500bd675b360719eaf0f9e4b3727b864991864 /main.c | |
parent | e9996882cb55c5b6974a3448f29bda32d6aa373d (diff) | |
download | nsxiv-a90bd1c833b3475e434bd2de95ab9dd0347f1780.tar.zst |
Refactored recursive directory util functions
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 36 |
1 files changed, 24 insertions, 12 deletions
@@ -92,11 +92,15 @@ int load_image(int new) { return ret; } +int fncmp(const void *a, const void *b) { + return strcoll(*((char* const*) a), *((char* const*) b)); +} + int main(int argc, char **argv) { - int i, j; + int i, start; const char *filename; - char **fnames; struct stat fstats; + r_dir_t dir; parse_options(argc, argv); @@ -121,18 +125,26 @@ int main(int argc, char **argv) { } else { for (i = 0; i < options->filecnt; ++i) { filename = options->filenames[i]; - if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) { - if (options->recursive && (fnames = read_dir_rec(filename))) { - for (j = 0; fnames[j]; ++j) { - if (!check_append(fnames[j])) - free(fnames[j]); - } - free(fnames); - } else { + + if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) { + check_append(filename); + } else { + if (!options->recursive) { warn("ignoring directory: %s", filename); + continue; } - } else { - check_append(filename); + if (r_opendir(&dir, filename)) { + warn("could not open directory: %s", filename); + continue; + } + start = fileidx; + while ((filename = r_readdir(&dir))) { + if (!check_append(filename)) + free((void*) filename); + } + r_closedir(&dir); + if (fileidx - start > 1) + qsort(filenames + start, fileidx - start, sizeof(char*), fncmp); } } } |