- add libbb function str_tolower to convert a string to lowercase.
[oweals/busybox.git] / coreutils / comm.c
index 7524a7b2552f9e138ccce2b8c3533b043bb9448b..28e3982f2b3074b559ebcb3d8bf4a4987615fffb 100644 (file)
@@ -9,41 +9,34 @@
 
 #include "busybox.h"
 
-#define COMM_OPT_1 0x01
-#define COMM_OPT_2 0x02
-#define COMM_OPT_3 0x04
-
-/* These three variables control behaviour if non-zero */
-
-static int only_file_1;
-static int only_file_2;
-static int both;
+#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)
+static void writeline(char *line, int class, int flags)
 {
        if (class == 0) {
-               if (!only_file_1)
+               if (flags & COMM_OPT_1)
                        return;
        } else if (class == 1) {
-               if (!only_file_2)
+               if (flags & COMM_OPT_2)
                        return;
-               if (only_file_1)
+               if (!(flags & COMM_OPT_1))
                        putchar('\t');
-       }
-       else /*if (class == 2)*/ {
-               if (!both)
+       } else /*if (class == 2)*/ {
+               if (flags & COMM_OPT_3)
                        return;
-               if (only_file_1)
+               if (!(flags & COMM_OPT_1))
                        putchar('\t');
-               if (only_file_2)
+               if (!(flags & COMM_OPT_2))
                        putchar('\t');
        }
        fputs(line, stdout);
 }
 
-/* This is the real core of the program - lines are compared here */
-static void cmp_files(char **infiles)
+int comm_main(int argc, char **argv);
+int comm_main(int argc, char **argv)
 {
 #define LINE_LEN 100
 #define BB_EOF_0 0x1
@@ -51,12 +44,19 @@ static void cmp_files(char **infiles)
        char thisline[2][LINE_LEN];
        FILE *streams[2];
        int i;
+       unsigned flags;
+
+       opt_complementary = "=2";
+       flags = getopt32(argc, argv, "123");
+       argv += optind;
 
        for (i = 0; i < 2; ++i) {
-               streams[i] = ((infiles[i][0] == '=' && infiles[i][1]) ? stdin : xfopen(infiles[i], "r"));
+               streams[i] = (argv[i][0] == '-' && !argv[i][1]) ? stdin : xfopen(argv[i], "r");
                fgets(thisline[i], LINE_LEN, streams[i]);
        }
 
+       /* This is the real core of the program - lines are compared here */
+
        while (*thisline[0] || *thisline[1]) {
                int order = 0;
 
@@ -78,11 +78,11 @@ static void cmp_files(char **infiles)
                }
 
                if (order == 0 && !i)
-                       writeline(thisline[1], 2);
+                       writeline(thisline[1], 2, flags);
                else if (order > 0 && !(i & BB_EOF_1))
-                       writeline(thisline[1], 1);
+                       writeline(thisline[1], 1, flags);
                else if (order < 0 && !(i & BB_EOF_0))
-                       writeline(thisline[0], 0);
+                       writeline(thisline[0], 0, flags);
 
                if (i & BB_EOF_0 & BB_EOF_1) {
                        break;
@@ -91,7 +91,7 @@ static void cmp_files(char **infiles)
                        i = (i & BB_EOF_0 ? 1 : 0);
                        while (!feof(streams[i])) {
                                if ((order < 0 && i) || (order > 0 && !i))
-                                       writeline(thisline[i], i);
+                                       writeline(thisline[i], i, flags);
                                fgets(thisline[i], LINE_LEN, streams[i]);
                        }
                        break;
@@ -104,23 +104,10 @@ static void cmp_files(char **infiles)
                }
        }
 
-       fclose(streams[0]);
-       fclose(streams[1]);
-}
-
-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(streams[0]);
+               fclose(streams[1]);
+       }
 
-       cmp_files(argv + optind);
-       exit(EXIT_SUCCESS);
+       return EXIT_SUCCESS;
 }