---
 toys/other/tac.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
From 87f31b744a5bb54f09afa8afa28950d6e673adb8 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Tue, 23 Jul 2019 21:47:47 -0700
Subject: [PATCH 1/2] tac: switch to getline().

---
 toys/other/tac.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

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);
 }
-- 
2.22.0.657.g960e92d24f-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to