diff options
author | Rob Landley <rob@landley.net> | 2019-07-24 17:31:28 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2019-07-24 17:31:28 -0500 |
commit | b14b5d9d8d6e3ae14814436d518785ba8773bf80 (patch) | |
tree | 9971bec14825e371fa8c05db68cf298b25d9d034 | |
parent | 9f5155ecad75b61db83181fa4fcdda1709e420b7 (diff) | |
download | toybox-b14b5d9d8d6e3ae14814436d518785ba8773bf80.tar.gz |
Rewrite tac to make better use of lib functions.
-rw-r--r-- | toys/other/tac.c | 48 |
1 files changed, 16 insertions, 32 deletions
diff --git a/toys/other/tac.c b/toys/other/tac.c index 06e97205..2a9e003d 100644 --- a/toys/other/tac.c +++ b/toys/other/tac.c @@ -13,44 +13,28 @@ config TAC Output lines in reverse order. */ +#define FOR_tac #include "toys.h" -static void do_tac(int fd, char *name) -{ - struct arg_list *list = NULL; - FILE *fp; - - if (fd == -1) { - perror_msg_raw(name); - return; - } - - // Read in lines. - fp = xfdopen(fd, "r"); - for (;;) { - char *line = NULL; - size_t allocated_length; +GLOBALS( + struct double_list *dl; +) - if (getline(&line, &allocated_length, fp) <= 0) break; - - struct arg_list *temp = xmalloc(sizeof(struct arg_list)); - temp->next = list; - temp->arg = line; - list = temp; - } - fclose(fp); - - // Play them back. - while (list) { - struct arg_list *temp = list->next; - xprintf("%s", list->arg); - free(list->arg); - free(list); - list = temp; +static void do_tac(char **pline, long len) +{ + if (pline) { + dlist_add(&TT.dl, *pline); + *pline = 0; + } else while (TT.dl) { + struct double_list *dl = dlist_lpop(&TT.dl); + + xprintf("%s", dl->data); + free(dl->data); + free(dl); } } void tac_main(void) { - loopfiles_rw(toys.optargs, O_RDONLY, 0, do_tac); + loopfiles_lines(toys.optargs, do_tac); } |