- add libbb function str_tolower to convert a string to lowercase.
[oweals/busybox.git] / coreutils / comm.c
index 7471c3b318ff6e3a2919aee5ff46639ff51d526a..28e3982f2b3074b559ebcb3d8bf4a4987615fffb 100644 (file)
@@ -4,64 +4,39 @@
  *
  * 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 tarball for details.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
 #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)
-               return;
+               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
@@ -69,22 +44,29 @@ 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[1][1]) ? stdin : bb_xfopen(infiles[i], "r"));
+               streams[i] = (argv[i][0] == '-' && !argv[i][1]) ? stdin : xfopen(argv[i], "r");
                fgets(thisline[i], LINE_LEN, streams[i]);
        }
 
-       while (thisline[0] || thisline[1]) {
+       /* 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])
+               if (!*thisline[0])
                        order = 1;
-               else if (!thisline[1])
+               else if (!*thisline[1])
                        order = -1;
                else {
                        int tl0_len, tl1_len;
@@ -96,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;
@@ -109,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;
@@ -122,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;
 }