diff options
Diffstat (limited to '')
-rw-r--r-- | src/commands/cat.c | 51 |
1 files changed, 23 insertions, 28 deletions
diff --git a/src/commands/cat.c b/src/commands/cat.c index 8e22706..dd1a732 100644 --- a/src/commands/cat.c +++ b/src/commands/cat.c @@ -2,6 +2,7 @@ #include <ctype.h> #include <errno.h> +#include <stdio.h> #include <string.h> #include <sys/stat.h> @@ -90,18 +91,26 @@ COMMAND(cat) { flags.change_tabs = false; flags.end_lines_dollar = false; + int start = 0; + for (int i = 0; i < argc; i++) { if (!prefix("-", argv[i])) { continue; } + if (streql("-", argv[i])) { + break; + } + if (streql("--help", argv[i])) { help(); } - size_t len = strlen(argv[i] + 1); - for (size_t j = 0; j < len; j++) { - char c = argv[i][j + 1]; + start++; + + size_t len = strlen(argv[i]); + for (size_t j = 1; j < len; j++) { + char c = argv[i][j]; switch (c) { case 'n': flags.number_lines = true; @@ -131,33 +140,19 @@ COMMAND(cat) { } } - bool files = false; - for (int i = 0; i < argc; i++) { - if (streql("-", argv[i])) { - files = true; - cat_file(stdin, flags); - } else if (prefix("-", argv[i])) { - continue; - } else { - files = true; - struct stat s; - if (stat(argv[i], &s) < 0) { - printf("error: failed to read %s: %s\n", argv[i], strerror(errno)); - continue; - } - if (!S_ISREG(s.st_mode)) { - printf("error: %s is not a file\n", argv[i]); - continue; - } - FILE* in = get_file(argv[i], "r"); - cat_file(in, flags); - fclose(in); - } - } + int arg_len = argc - start; - if (!files) { + if (arg_len < 1) { cat_file(stdin, flags); + return EXIT_SUCCESS; } - + + for (int i = start; i < argc; i++) { + FILE* in = get_file(argv[i], "r"); + cat_file(in, flags); + if (in != stdin) + fclose(in); + } + return EXIT_SUCCESS; } |