Also use FLAG().
---
 tests/comm.test   |  9 +++++++++
 toys/posix/comm.c | 28 ++++++++++++++--------------
 2 files changed, 23 insertions(+), 14 deletions(-)
 create mode 100755 tests/comm.test
From 5adcdb8ae4a91da7b52facddee583d4c2be5957b Mon Sep 17 00:00:00 2001
From: Elliott Hughes <e...@google.com>
Date: Sat, 25 Sep 2021 19:10:43 -0700
Subject: [PATCH] comm: stop using get_line().

Also use FLAG().
---
 tests/comm.test   |  9 +++++++++
 toys/posix/comm.c | 28 ++++++++++++++--------------
 2 files changed, 23 insertions(+), 14 deletions(-)
 create mode 100755 tests/comm.test

diff --git a/tests/comm.test b/tests/comm.test
new file mode 100755
index 00000000..63d9c847
--- /dev/null
+++ b/tests/comm.test
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+[ -f testing.sh ] && . testing.sh
+
+#testing "name" "command" "result" "infile" "stdin"
+
+for i in a b c ; do echo $i >> lhs ; done
+for i in c d e ; do echo $i >> rhs ; done
+testing "comm" "comm lhs rhs" "a\nb\n\t\tc\n\td\n\te\n" "" ""
diff --git a/toys/posix/comm.c b/toys/posix/comm.c
index c5c8f851..62d23743 100644
--- a/toys/posix/comm.c
+++ b/toys/posix/comm.c
@@ -27,29 +27,29 @@ config COMM
 
 static void writeline(const char *line, int col)
 {
-  if (col == 0 && toys.optflags & FLAG_1) return;
+  if (col == 0 && FLAG(1)) return;
   else if (col == 1) {
-    if (toys.optflags & FLAG_2) return;
-    if (!(toys.optflags & FLAG_1)) putchar('\t');
+    if (FLAG(2)) return;
+    if (!FLAG(1)) putchar('\t');
   } else if (col == 2) {
-    if (toys.optflags & FLAG_3) return;
-    if (!(toys.optflags & FLAG_1)) putchar('\t');
-    if (!(toys.optflags & FLAG_2)) putchar('\t');
+    if (FLAG(3)) return;
+    if (!FLAG(1)) putchar('\t');
+    if (!FLAG(2)) putchar('\t');
   }
   puts(line);
 }
 
 void comm_main(void)
 {
-  int file[2];
+  FILE *file[2];
   char *line[2];
   int i;
 
   if (toys.optflags == 7) return;
 
   for (i = 0; i < 2; i++) {
-    file[i] = xopenro(toys.optargs[i]);
-    line[i] = get_line(file[i]);
+    file[i] = xfopen(toys.optargs[i], "r");
+    line[i] = xgetline(file[i]);
   }
 
   while (line[0] && line[1]) {
@@ -59,22 +59,22 @@ void comm_main(void)
       writeline(line[0], 2);
       for (i = 0; i < 2; i++) {
         free(line[i]);
-        line[i] = get_line(file[i]);
+        line[i] = xgetline(file[i]);
       }
     } else {
       i = order < 0 ? 0 : 1;
       writeline(line[i], i);
       free(line[i]);
-      line[i] = get_line(file[i]);
+      line[i] = xgetline(file[i]);
     }
   }
 
-  /* print rest of the longer file */
+  // Print rest of the longer file.
   for (i = line[0] ? 0 : 1; line[i];) {
     writeline(line[i], i);
     free(line[i]);
-    line[i] = get_line(file[i]);
+    line[i] = xgetline(file[i]);
   }
 
-  if (CFG_TOYBOX_FREE) for (i = 0; i < 2; i++) xclose(file[i]);
+  if (CFG_TOYBOX_FREE) for (i = 0; i < 2; i++) fclose(file[i]);
 }
-- 
2.33.0.685.g46640cef36-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to