diff options
author | Tyler Murphy <tylerm@tylerm.dev> | 2023-05-01 18:43:32 -0400 |
---|---|---|
committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-05-01 18:43:32 -0400 |
commit | 82e55dde69dc89fdf06e751b75449f35836282a1 (patch) | |
tree | 29e49ae19cba7f6a7c2987c381ebe4b253edc053 /src/commands/head.c | |
parent | move back to c99 (diff) | |
download | lazysphere-82e55dde69dc89fdf06e751b75449f35836282a1.tar.gz lazysphere-82e55dde69dc89fdf06e751b75449f35836282a1.tar.bz2 lazysphere-82e55dde69dc89fdf06e751b75449f35836282a1.zip |
refactor and xargs
Diffstat (limited to 'src/commands/head.c')
-rw-r--r-- | src/commands/head.c | 124 |
1 files changed, 54 insertions, 70 deletions
diff --git a/src/commands/head.c b/src/commands/head.c index 018da5a..44bc45b 100644 --- a/src/commands/head.c +++ b/src/commands/head.c @@ -1,17 +1,17 @@ #include "../command.h" -struct Flags { +static struct { int count; bool lines; bool print_headers; bool dont_print_headers; -}; +} flags; -static void head_file_lines(FILE* file, struct Flags* flags) { +static void head_file_lines(FILE* file) { size_t len = 0; char* line = NULL; - int count = flags->count; + int count = flags.count; while(count > 0 && getline(&line, &len, file) != -1) { printf("%s", line); count--; @@ -21,9 +21,9 @@ static void head_file_lines(FILE* file, struct Flags* flags) { fclose(file); } -static void head_file_chars(FILE* file, struct Flags* flags) { +static void head_file_chars(FILE* file) { char c; - int count = flags->count; + int count = flags.count; while(count > 0 && (c = getc(file)) != EOF) { putchar(c); count--; @@ -32,7 +32,7 @@ static void head_file_chars(FILE* file, struct Flags* flags) { fclose(file); } -static void help() { +static void help(void) { printf("Usage: head [OPTIONS] [FILE]...\n\n"); printf("Print first 10 lines of FILEs (or stdin)\n"); printf("With more than one FILE, precede each with a filename header.\n\n"); @@ -41,19 +41,11 @@ static void help() { printf("\t\t\t(b:*512 k:*1024 m:*1024^2)\n"); printf("\t-q\t\tNever print headers\n"); printf("\t-v\t\tAlways print headers\n"); - exit(EXIT_SUCCESS); } -static char* next_arg(int argc, char** argv, int index) { - if (index >= argc) { - error("error: expected another argument after option"); - } - return argv[index]; -} - -static void print_header(char* path, struct Flags* flags, bool many) { - if (flags->dont_print_headers) return; - if (!many && !flags->print_headers) return; +static void print_header(char* path, bool many) { + if (flags.dont_print_headers) return; + if (!many && !flags.print_headers) return; if (streql("-", path)) { printf("\n==> standard input <==\n"); } else { @@ -61,81 +53,73 @@ static void print_header(char* path, struct Flags* flags, bool many) { } } -static void head_file(char* path, struct Flags* flags, bool many) { +static void head_file(char* path, bool many) { FILE* file = get_file(path, "r"); - print_header(path, flags, many); - if (flags->lines) { - head_file_lines(file, flags); + print_header(path, many); + if (flags.lines) { + head_file_lines(file); } else { - head_file_chars(file, flags); + head_file_chars(file); + } +} + +static int short_arg(char c, char* next) { + switch(c) { + case 'c': { + flags.lines = false; + check_arg(next); + long int bkm = get_blkm(next); + if (bkm < 1) { + error("error: bkm cannot be less than 1"); + } + flags.count = bkm; + return ARG_USED; + } + case 'n': { + flags.lines = true; + check_arg(next); + long int bkm = get_blkm(next); + if (bkm < 1) { + error("error: bkm cannot be less than 1"); + } + flags.count = bkm; + return ARG_USED; + } + case 'q': + flags.dont_print_headers = true; + break; + case 'v': + flags.print_headers = true; + break; + default: { + error("error: unknown option -%c", c); + } } + return ARG_UNUSED; } COMMAND(head) { - struct Flags flags; flags.count = 10; flags.lines = true; flags.print_headers = false; flags.dont_print_headers = false; - int start = 0; - for (int i = 0; i < argc; i++) { - if (!prefix("-", argv[i])) break; - if (streql(argv[0], "--help")) help(); - start++; - int i_s = i; - for (size_t j = 1; j < strlen(argv[i_s]); j++) { - char c = argv[i_s][j]; - switch(c) { - case 'c': { - start++; - flags.lines = false; - char* arg = next_arg(argc, argv, ++i); - long int bkm = get_blkm(arg); - if (bkm < 1) { - error("error: bkm cannot be less than 1"); - } - flags.count = bkm; - break; - } - case 'n': { - start++; - flags.lines = true; - char* arg = next_arg(argc, argv, ++i); - long int bkm = get_blkm(arg); - if (bkm < 1) { - error("error: bkm cannot be less than 1"); - } - flags.count = bkm; - break; - } - case 'q': - flags.dont_print_headers = true; - break; - case 'v': - flags.print_headers = true; - break; - default: { - error("error: unknown option -%c", c); - } - } - } - } + int start = parse_args(argc, argv, help, short_arg, NULL); int count = argc - start; if (count < 1) { - head_file_lines(stdin, &flags); + head_file_lines(stdin); return EXIT_SUCCESS; } if (count == 1) { - head_file(argv[start], &flags, false); + head_file(argv[start], false); return EXIT_SUCCESS; } for (int i = 0; i < count; i++) { - head_file(argv[start + i], &flags, true); + head_file(argv[start + i], true); } return EXIT_SUCCESS; |