cksum: code shrink
[oweals/busybox.git] / coreutils / comm.c
index 2354bac0be5a8b1535394c952b6ac0393e048459..4bee7767752edc54f7533351054ec27bc7b9cf1f 100644 (file)
  *
  * Copyright (C) 2005 by Robert Sullivan <cogito.ergo.cogito@gmail.com>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- * 02111-1307 USA
- *
+ * 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.
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include "busybox.h"
+//applet:IF_COMM(APPLET(comm, BB_DIR_USR_BIN, BB_SUID_DROP))
 
-#define COMM_OPT_1 0x01
-#define COMM_OPT_2 0x02
-#define COMM_OPT_3 0x04
+//kbuild:lib-$(CONFIG_COMM) += comm.o
 
-/* These three variables control behaviour if non-zero */
+//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"
 
-static int only_file_1;
-static int only_file_2;
-static int both;
+#include "libbb.h"
+
+#define COMM_OPT_1 (1 << 0)
+#define COMM_OPT_2 (1 << 1)
+#define COMM_OPT_3 (1 << 2)
 
 /* writeline outputs the input given, appropriately aligned according to class */
 static void writeline(char *line, int class)
 {
-       switch (class) {
-               case 1:
-                       if (!only_file_1)
-                               return;
-                       break;
-               case 2:
-                       if (!only_file_2)
-                               return;
-                       if (only_file_1)
-                               putchar('\t');
-                       break;
-               case 3:
-                       if (!both)
-                               return;
-                       if (only_file_1)
-                               putchar('\t');
-                       if (only_file_2)
-                               putchar('\t');
-                       break;
+       int flags = option_mask32;
+       if (class == 0) {
+               if (flags & COMM_OPT_1)
+                       return;
+       } else if (class == 1) {
+               if (flags & COMM_OPT_2)
+                       return;
+               if (!(flags & COMM_OPT_1))
+                       putchar('\t');
+       } else /*if (class == 2)*/ {
+               if (flags & COMM_OPT_3)
+                       return;
+               if (!(flags & COMM_OPT_1))
+                       putchar('\t');
+               if (!(flags & COMM_OPT_2))
+                       putchar('\t');
        }
-       fputs(line, stdout);
+       puts(line);
 }
 
-/* This is the real core of the program - lines are compared here */
-static int cmp_files(char **infiles)
+int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int comm_main(int argc UNUSED_PARAM, char **argv)
 {
-       char thisline[2][100];
-       FILE *streams[2];
-       int i = 0;
+       char *thisline[2];
+       FILE *stream[2];
+       int i;
+       int order;
 
-       for (i = 0; i < 2; i++) {
-               streams[i] = (strcmp(infiles[i], "=") == 0 ? stdin : fopen(infiles[i], "r"));
-               fgets(thisline[i], 100, streams[i]);
-       }
+       getopt32(argv, "^" "123" "\0" "=2");
+       argv += optind;
 
-       while (thisline[0] || thisline[1]) {
-               int order = 0;
-               int tl0_len = strlen(thisline[0]);
-               int tl1_len = strlen(thisline[1]);
+       for (i = 0; i < 2; ++i) {
+               stream[i] = xfopen_stdin(argv[i]);
+       }
 
-               if (!thisline[0])
-                       order = 1;
-               else if (!thisline[1])
-                       order = -1;
-               else {
-                       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) && (!feof(streams[0])) && (!feof(streams[1])))
-                       writeline(thisline[1], 3);
-               else if ((order > 0) && (!feof(streams[1])))
-                       writeline(thisline[1], 2);
-               else if ((order < 0) && (!feof(streams[0])))
-                       writeline(thisline[0], 1);
-
-               if (feof(streams[0]) && feof(streams[1])) {
-                       fclose(streams[0]);
-                       fclose(streams[1]);
-                       break;
-
-               } else if (feof(streams[0])) {
-                       while (!feof(streams[1])) {
-                               if (order < 0)
-                                       writeline(thisline[1], 2);
-                               fgets(thisline[1], 100, streams[1]);
-                       }
-                       fclose(streams[0]);
-                       fclose(streams[1]);
+               i = !thisline[0] + (!thisline[1] << 1);
+               if (i)
                        break;
+               order = strcmp(thisline[0], thisline[1]);
 
-               } else if (feof(streams[1])) {
-                       while (!feof(streams[0])) {
-                               if (order > 0)
-                                       writeline(thisline[0], 1);
-                               fgets(thisline[0], 100, streams[0]);
-                       }
-                       fclose(streams[0]);
-                       fclose(streams[1]);
-                       break;
+               if (order >= 0)
+                       writeline(thisline[1], order ? 1 : 2);
+               else
+                       writeline(thisline[0], 0);
+       }
 
-               } else {
-                       if (order >= 0)
-                               fgets(thisline[1], 100, streams[1]);
-                       if (order <= 0)
-                               fgets(thisline[0], 100, 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);
                }
        }
 
-       return 0;
-}
-
-int comm_main(int argc, char **argv)
-{
-       unsigned long flags;
-
-       flags = bb_getopt_ulflags(argc, argv, "123");
-
-       if (optind + 2 != argc)
-               bb_show_usage();
-
-       only_file_1 = !(flags & COMM_OPT_1);
-       only_file_2 = !(flags & COMM_OPT_2);
-       both = !(flags & COMM_OPT_3);
+       if (ENABLE_FEATURE_CLEAN_UP) {
+               fclose(stream[0]);
+               fclose(stream[1]);
+       }
 
-       exit(cmp_files(argv + optind) == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+       return EXIT_SUCCESS;
 }