From b14b5d9d8d6e3ae14814436d518785ba8773bf80 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 24 Jul 2019 17:31:28 -0500 Subject: Rewrite tac to make better use of lib functions. --- toys/other/tac.c | 48 ++++++++++++++++-------------------------------- 1 file changed, 16 insertions(+), 32 deletions(-) (limited to 'toys/other') 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); } -- cgit v1.2.3