diff options
Diffstat (limited to 'src/commands/tail.c')
-rw-r--r-- | src/commands/tail.c | 83 |
1 files changed, 60 insertions, 23 deletions
diff --git a/src/commands/tail.c b/src/commands/tail.c index 4856bc0..07b3d2b 100644 --- a/src/commands/tail.c +++ b/src/commands/tail.c @@ -10,18 +10,27 @@ static struct { } flags; static size_t tail_file_lines(FILE* file, unsigned int count, size_t skip) { - char* ring[count]; + + char** ring; + int* ring_len; + int index, read; + unsigned int size, i; + size_t len; + char* line; + + ring = malloc(sizeof(char*) * count); memset(ring, 0, sizeof(char*) * count); - int ring_len[count]; + ring_len = malloc(sizeof(int) * count); - int index = 0; - unsigned int size = 0; + index = 0; + size = 0; - int read; fseek(file, skip, SEEK_SET); - size_t len = skip; - char* line = NULL; + + len = skip; + line = NULL; + while ((read = getline(&line, &len, file)) != -1) { if (ring[index] != NULL) free(ring[index]); @@ -37,8 +46,8 @@ static size_t tail_file_lines(FILE* file, unsigned int count, size_t skip) { index += count - size; index %= count; - - for (unsigned int i = 0; i < size; i++) { + + for (i = 0; i < size; i++) { fwrite(ring[index], ring_len[index], 1, stdout); free(ring[index]); index += 1; @@ -47,20 +56,28 @@ static size_t tail_file_lines(FILE* file, unsigned int count, size_t skip) { free(line); fclose(file); + free(ring); + free(ring_len); return len; } static size_t tail_file_chars(FILE* file, unsigned int count, size_t skip) { - char ring[count]; + + char* ring; + int index; + unsigned int size, i; + int read, c; + + ring = malloc(sizeof(char) * count); memset(ring, 0, count); - int index = 0; - unsigned int size = 0; + index = 0; + size = 0; fseek(file, skip, SEEK_SET); - int read = skip; - int c; + read = skip; + while((c = getc(file)) != EOF) { ring[index] = c; index++; @@ -72,7 +89,7 @@ static size_t tail_file_chars(FILE* file, unsigned int count, size_t skip) { index += count - size; index %= count; - for (unsigned int i = 0; i < size; i++) { + for (i = 0; i < size; i++) { putchar(ring[index]); index += 1; index %= count; @@ -108,10 +125,14 @@ static void print_header(char* path, bool many) { } static void tail_file(char* path, bool many) { - FILE* file = get_file(path, "r"); + + FILE* file; + size_t skip; + + file = get_file(path, "r"); print_header(path, many); - size_t skip = 0; + skip = 0; while (true) { if (flags.lines) { skip = tail_file_lines(file, flags.count, skip); @@ -127,22 +148,32 @@ static void tail_file(char* path, bool many) { static int short_arg(char c, char* next) { switch (c) { case 'c': { + long int bkm; + flags.lines = false; + check_arg(next); - long int bkm = get_blkm(next); + bkm = get_blkm(next); + if (bkm < 1) { error("bkm cannot be less than 1"); } + flags.count = bkm; return ARG_USED; } case 'n': { + long int bkm; + flags.lines = true; + check_arg(next); - long int bkm = get_blkm(next); + bkm = get_blkm(next); + if (bkm < 1) { error("bkm cannot be less than 1"); } + flags.count = bkm; return ARG_USED; } @@ -156,11 +187,15 @@ static int short_arg(char c, char* next) { flags.print_as_grow = true; break; case 's': { + long int sec; + check_arg(next); - long int sec = get_number(next); + sec = get_number(next); + if (sec < 1) { error("wait seconds cannot be less than 1"); } + flags.grow_wait = sec; return ARG_USED; } @@ -172,6 +207,8 @@ static int short_arg(char c, char* next) { COMMAND(tail) { + int start, count, i; + flags.count = 10; flags.dont_print_headers = false; flags.print_headers = false; @@ -179,9 +216,9 @@ COMMAND(tail) { flags.print_as_grow = false; flags.grow_wait = 10; - int start = parse_args(argc, argv, help, short_arg, NULL); + start = parse_args(argc, argv, help, short_arg, NULL); - int count = argc - start; + count = argc - start; if (count < 1) { tail_file_lines(stdin, 10, 0); @@ -193,7 +230,7 @@ COMMAND(tail) { return EXIT_SUCCESS; } - for (int i = 0; i < count; i++) { + for (i = 0; i < count; i++) { tail_file(argv[start + i], true); } |