From 9f5155ecad75b61db83181fa4fcdda1709e420b7 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 23 Jul 2019 21:47:47 -0700 Subject: tac: switch to getline(). --- toys/other/tac.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'toys/other/tac.c') diff --git a/toys/other/tac.c b/toys/other/tac.c index d5f72fd2..06e97205 100644 --- a/toys/other/tac.c +++ b/toys/other/tac.c @@ -18,20 +18,27 @@ config TAC static void do_tac(int fd, char *name) { struct arg_list *list = NULL; - char *c; + FILE *fp; - // Read in lines + if (fd == -1) { + perror_msg_raw(name); + return; + } + + // Read in lines. + fp = xfdopen(fd, "r"); for (;;) { - struct arg_list *temp; - long len; + char *line = NULL; + size_t allocated_length; - if (!(c = get_rawline(fd, &len, '\n'))) break; + if (getline(&line, &allocated_length, fp) <= 0) break; - temp = xmalloc(sizeof(struct arg_list)); + struct arg_list *temp = xmalloc(sizeof(struct arg_list)); temp->next = list; - temp->arg = c; + temp->arg = line; list = temp; } + fclose(fp); // Play them back. while (list) { @@ -45,5 +52,5 @@ static void do_tac(int fd, char *name) void tac_main(void) { - loopfiles(toys.optargs, do_tac); + loopfiles_rw(toys.optargs, O_RDONLY, 0, do_tac); } -- cgit v1.2.3