From 49f7c2be366f0b918b68a4cc8e4335ebab0a15ba Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Sun, 30 Apr 2023 02:12:02 -0400 Subject: fix ls and add start of ed --- src/commands/cat.c | 51 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) (limited to 'src/commands/cat.c') 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 #include +#include #include #include @@ -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; } -- cgit v1.2.3-freya