summaryrefslogtreecommitdiff
path: root/src/commands/tail.c
diff options
context:
space:
mode:
authorTyler Murphy <tylerm@tylerm.dev>2023-05-01 18:43:32 -0400
committerTyler Murphy <tylerm@tylerm.dev>2023-05-01 18:43:32 -0400
commit82e55dde69dc89fdf06e751b75449f35836282a1 (patch)
tree29e49ae19cba7f6a7c2987c381ebe4b253edc053 /src/commands/tail.c
parentmove back to c99 (diff)
downloadlazysphere-82e55dde69dc89fdf06e751b75449f35836282a1.tar.gz
lazysphere-82e55dde69dc89fdf06e751b75449f35836282a1.tar.bz2
lazysphere-82e55dde69dc89fdf06e751b75449f35836282a1.zip
refactor and xargs
Diffstat (limited to '')
-rw-r--r--src/commands/tail.c142
1 files changed, 63 insertions, 79 deletions
diff --git a/src/commands/tail.c b/src/commands/tail.c
index 118a771..a611842 100644
--- a/src/commands/tail.c
+++ b/src/commands/tail.c
@@ -1,13 +1,13 @@
#include "../command.h"
-struct Flags {
+static struct {
bool lines;
int count;
bool print_headers;
bool dont_print_headers;
bool print_as_grow;
int grow_wait;
-};
+} flags;
static size_t tail_file_lines(FILE* file, unsigned int count, size_t skip) {
char* ring[count];
@@ -83,7 +83,7 @@ static size_t tail_file_chars(FILE* file, unsigned int count, size_t skip) {
return read;
}
-static void help() {
+static void help(void) {
printf("Usage: tail [OPTIONS] [FILE]...\n\n");
printf("Print last 10 lines of FILEs (or stdin) to.\n");
printf("With more than one FILE, precede each with a filename header.\n\n");
@@ -97,16 +97,9 @@ static void help() {
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 {
@@ -114,26 +107,72 @@ static void print_header(char* path, struct Flags* flags, bool many) {
}
}
-static void tail_file(char* path, struct Flags* flags, bool many) {
+static void tail_file(char* path, bool many) {
FILE* file = get_file(path, "r");
- print_header(path, flags, many);
+ print_header(path, many);
size_t skip = 0;
while (true) {
- if (flags->lines) {
- skip = tail_file_lines(file, flags->count, skip);
+ if (flags.lines) {
+ skip = tail_file_lines(file, flags.count, skip);
} else {
- skip = tail_file_chars(file, flags->count, skip);
+ skip = tail_file_chars(file, flags.count, skip);
}
- if (!flags->print_as_grow) break;
- sleep(flags->grow_wait);
+ if (!flags.print_as_grow) break;
+ sleep(flags.grow_wait);
get_file(path, "r");
};
}
+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;
+ case 'f':
+ flags.print_as_grow = true;
+ break;
+ case 's': {
+ check_arg(next);
+ long int sec = get_number(next);
+ if (sec < 1) {
+ error("error: wait seconds cannot be less than 1");
+ }
+ flags.grow_wait = sec;
+ return ARG_USED;
+ }
+ default: {
+ error("error: unknown option -%c", c);
+ }
+ }
+ return ARG_UNUSED;
+}
+
COMMAND(tail) {
- struct Flags flags;
flags.count = 10;
flags.dont_print_headers = false;
flags.print_headers = false;
@@ -141,62 +180,7 @@ COMMAND(tail) {
flags.print_as_grow = false;
flags.grow_wait = 10;
- 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;
- case 'f':
- flags.print_as_grow = true;
- break;
- case 's': {
- start++;
- char* arg = next_arg(argc, argv, ++i);
- long int sec = get_number(arg);
- if (sec < 1) {
- error("error: wait seconds cannot be less than 1");
- }
- flags.grow_wait = sec;
- break;
- }
- default: {
- error("error: unknown option -%c", c);
- }
- }
- }
- }
+ int start = parse_args(argc, argv, help, short_arg, NULL);
int count = argc - start;
@@ -206,12 +190,12 @@ COMMAND(tail) {
}
if (count == 1) {
- tail_file(argv[start], &flags, false);
+ tail_file(argv[start], false);
return EXIT_SUCCESS;
}
for (int i = 0; i < count; i++) {
- tail_file(argv[start + i], &flags, true);
+ tail_file(argv[start + i], true);
}
return EXIT_SUCCESS;