cksum: code shrink
[oweals/busybox.git] / coreutils / comm.c
index 67088e06c1dfdd56f060be1f28d2f25f1b1ed910..4bee7767752edc54f7533351054ec27bc7b9cf1f 100644 (file)
@@ -4,8 +4,26 @@
  *
  * Copyright (C) 2005 by Robert Sullivan <cogito.ergo.cogito@gmail.com>
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
+//config:config COMM
+//config:      bool "comm (3.9 kb)"
+//config:      default y
+//config:      help
+//config:      comm is used to compare two files line by line and return
+//config:      a three-column output.
+
+//applet:IF_COMM(APPLET(comm, BB_DIR_USR_BIN, BB_SUID_DROP))
+
+//kbuild:lib-$(CONFIG_COMM) += comm.o
+
+//usage:#define comm_trivial_usage
+//usage:       "[-123] FILE1 FILE2"
+//usage:#define comm_full_usage "\n\n"
+//usage:       "Compare FILE1 with FILE2\n"
+//usage:     "\n       -1      Suppress lines unique to FILE1"
+//usage:     "\n       -2      Suppress lines unique to FILE2"
+//usage:     "\n       -3      Suppress lines common to both files"
 
 #include "libbb.h"
 
@@ -14,8 +32,9 @@
 #define COMM_OPT_3 (1 << 2)
 
 /* writeline outputs the input given, appropriately aligned according to class */
-static void writeline(char *line, int class, int flags)
+static void writeline(char *line, int class)
 {
+       int flags = option_mask32;
        if (class == 0) {
                if (flags & COMM_OPT_1)
                        return;
@@ -32,81 +51,66 @@ static void writeline(char *line, int class, int flags)
                if (!(flags & COMM_OPT_2))
                        putchar('\t');
        }
-       fputs(line, stdout);
+       puts(line);
 }
 
 int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int comm_main(int argc, char **argv)
+int comm_main(int argc UNUSED_PARAM, char **argv)
 {
-#define LINE_LEN 100
-#define BB_EOF_0 0x1
-#define BB_EOF_1 0x2
-       char thisline[2][LINE_LEN];
-       FILE *streams[2];
+       char *thisline[2];
+       FILE *stream[2];
        int i;
-       unsigned flags;
+       int order;
 
-       opt_complementary = "=2";
-       flags = getopt32(argv, "123");
+       getopt32(argv, "^" "123" "\0" "=2");
        argv += optind;
 
        for (i = 0; i < 2; ++i) {
-               streams[i] = (argv[i][0] == '-' && !argv[i][1]) ? stdin : xfopen(argv[i], "r");
-               fgets(thisline[i], LINE_LEN, streams[i]);
+               stream[i] = xfopen_stdin(argv[i]);
        }
 
-       /* This is the real core of the program - lines are compared here */
-
-       while (*thisline[0] || *thisline[1]) {
-               int order = 0;
-
-               i = 0;
-               if (feof(streams[0])) i |= BB_EOF_0;
-               if (feof(streams[1])) i |= BB_EOF_1;
-
-               if (!*thisline[0])
-                       order = 1;
-               else if (!*thisline[1])
-                       order = -1;
-               else {
-                       int tl0_len, tl1_len;
-                       tl0_len = strlen(thisline[0]);
-                       tl1_len = strlen(thisline[1]);
-                       order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len);
-                       if (!order)
-                               order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len;
+       order = 0;
+       thisline[1] = thisline[0] = NULL;
+       while (1) {
+               if (order <= 0) {
+                       free(thisline[0]);
+                       thisline[0] = xmalloc_fgetline(stream[0]);
+               }
+               if (order >= 0) {
+                       free(thisline[1]);
+                       thisline[1] = xmalloc_fgetline(stream[1]);
                }
 
-               if (order == 0 && !i)
-                       writeline(thisline[1], 2, flags);
-               else if (order > 0 && !(i & BB_EOF_1))
-                       writeline(thisline[1], 1, flags);
-               else if (order < 0 && !(i & BB_EOF_0))
-                       writeline(thisline[0], 0, flags);
-
-               if (i & BB_EOF_0 & BB_EOF_1) {
+               i = !thisline[0] + (!thisline[1] << 1);
+               if (i)
                        break;
+               order = strcmp(thisline[0], thisline[1]);
 
-               } else if (i) {
-                       i = (i & BB_EOF_0 ? 1 : 0);
-                       while (!feof(streams[i])) {
-                               if ((order < 0 && i) || (order > 0 && !i))
-                                       writeline(thisline[i], i, flags);
-                               fgets(thisline[i], LINE_LEN, streams[i]);
-                       }
-                       break;
+               if (order >= 0)
+                       writeline(thisline[1], order ? 1 : 2);
+               else
+                       writeline(thisline[0], 0);
+       }
 
-               } else {
-                       if (order >= 0)
-                               fgets(thisline[1], LINE_LEN, streams[1]);
-                       if (order <= 0)
-                               fgets(thisline[0], LINE_LEN, streams[0]);
+       /* EOF at least on one of the streams */
+       i &= 1;
+       if (thisline[i]) {
+               /* stream[i] is not at EOF yet */
+               /* we did not print thisline[i] yet */
+               char *p = thisline[i];
+               writeline(p, i);
+               while (1) {
+                       free(p);
+                       p = xmalloc_fgetline(stream[i]);
+                       if (!p)
+                               break;
+                       writeline(p, i);
                }
        }
 
        if (ENABLE_FEATURE_CLEAN_UP) {
-               fclose(streams[0]);
-               fclose(streams[1]);
+               fclose(stream[0]);
+               fclose(stream[1]);
        }
 
        return EXIT_SUCCESS;