From 6f05e777280cd36a8ccaf20182c4e8a0554bb563 Mon Sep 17 00:00:00 2001 From: Bert Münnich Date: Tue, 19 Mar 2013 21:11:29 +0100 Subject: New options: -[io], read/write files from/to stdin/out Fixes issue #84 --- main.c | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index e6d6700..cda0c4d 100644 --- a/main.c +++ b/main.c @@ -622,7 +622,7 @@ int main(int argc, char **argv) exit(EXIT_SUCCESS); } - if (options->filecnt == 0) { + if (options->filecnt == 0 && !options->from_stdin) { print_usage(); exit(EXIT_FAILURE); } @@ -635,7 +635,6 @@ int main(int argc, char **argv) files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t)); fileidx = 0; - /* build file list: */ if (options->from_stdin) { filename = NULL; while ((len = get_line(&filename, &n, stdin)) > 0) { @@ -645,34 +644,34 @@ int main(int argc, char **argv) } if (filename != NULL) free(filename); - } else { - for (i = 0; i < options->filecnt; i++) { - filename = options->filenames[i]; + } + + for (i = 0; i < options->filecnt; i++) { + filename = options->filenames[i]; - if (stat(filename, &fstats) < 0) { - warn("could not stat file: %s", filename); + if (stat(filename, &fstats) < 0) { + warn("could not stat file: %s", filename); + continue; + } + if (!S_ISDIR(fstats.st_mode)) { + check_add_file(filename); + } else { + if (!options->recursive) { + warn("ignoring directory: %s", filename); continue; } - if (!S_ISDIR(fstats.st_mode)) { + if (r_opendir(&dir, filename) < 0) { + warn("could not open directory: %s", filename); + continue; + } + start = fileidx; + while ((filename = r_readdir(&dir)) != NULL) { check_add_file(filename); - } else { - if (!options->recursive) { - warn("ignoring directory: %s", filename); - continue; - } - if (r_opendir(&dir, filename) < 0) { - warn("could not open directory: %s", filename); - continue; - } - start = fileidx; - while ((filename = r_readdir(&dir)) != NULL) { - check_add_file(filename); - free((void*) filename); - } - r_closedir(&dir); - if (fileidx - start > 1) - qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp); + free((void*) filename); } + r_closedir(&dir); + if (fileidx - start > 1) + qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp); } } -- cgit v1.2.3-54-g00ecf