use a bunch of if statements since it is a few bytes smaller than a switch; also...
[oweals/busybox.git] / coreutils / comm.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini comm implementation for busybox
4  *
5  * Copyright (C) 2005 by Robert Sullivan <cogito.ergo.cogito@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include "busybox.h"
29
30 #define COMM_OPT_1 0x01
31 #define COMM_OPT_2 0x02
32 #define COMM_OPT_3 0x04
33
34 /* These three variables control behaviour if non-zero */
35
36 static int only_file_1;
37 static int only_file_2;
38 static int both;
39
40 /* writeline outputs the input given, appropriately aligned according to class */
41 static void writeline(char *line, int class)
42 {
43         if (class == 1 && !only_file_1)
44                 return;
45         else if (class == 2) {
46                 if (!only_file_2)
47                         return;
48                 if (only_file_1)
49                         putchar('\t');
50         }
51         else if (class == 3) {
52                 if (!both)
53                         return;
54                 if (only_file_1)
55                         putchar('\t');
56                 if (only_file_2)
57                         putchar('\t');
58         }
59         fputs(line, stdout);
60 }
61
62 /* This is the real core of the program - lines are compared here */
63 static int cmp_files(char **infiles)
64 {
65         char thisline[2][100];
66         FILE *streams[2];
67         int i = 0;
68
69         for (i = 0; i < 2; i++) {
70                 streams[i] = (strcmp(infiles[i], "=") == 0 ? stdin : bb_xfopen(infiles[i], "r"));
71                 fgets(thisline[i], 100, streams[i]);
72         }
73
74         while (thisline[0] || thisline[1]) {
75                 int order = 0;
76                 int tl0_len = strlen(thisline[0]);
77                 int tl1_len = strlen(thisline[1]);
78
79                 if (!thisline[0])
80                         order = 1;
81                 else if (!thisline[1])
82                         order = -1;
83                 else {
84                         order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len);
85                         if (!order)
86                                 order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len;
87                 }
88
89                 if ((order == 0) && (!feof(streams[0])) && (!feof(streams[1])))
90                         writeline(thisline[1], 3);
91                 else if ((order > 0) && (!feof(streams[1])))
92                         writeline(thisline[1], 2);
93                 else if ((order < 0) && (!feof(streams[0])))
94                         writeline(thisline[0], 1);
95
96                 if (feof(streams[0]) && feof(streams[1])) {
97                         fclose(streams[0]);
98                         fclose(streams[1]);
99                         break;
100
101                 } else if (feof(streams[0])) {
102                         while (!feof(streams[1])) {
103                                 if (order < 0)
104                                         writeline(thisline[1], 2);
105                                 fgets(thisline[1], 100, streams[1]);
106                         }
107                         fclose(streams[0]);
108                         fclose(streams[1]);
109                         break;
110
111                 } else if (feof(streams[1])) {
112                         while (!feof(streams[0])) {
113                                 if (order > 0)
114                                         writeline(thisline[0], 1);
115                                 fgets(thisline[0], 100, streams[0]);
116                         }
117                         fclose(streams[0]);
118                         fclose(streams[1]);
119                         break;
120
121                 } else {
122                         if (order >= 0)
123                                 fgets(thisline[1], 100, streams[1]);
124                         if (order <= 0)
125                                 fgets(thisline[0], 100, streams[0]);
126                 }
127         }
128
129         return 0;
130 }
131
132 int comm_main(int argc, char **argv)
133 {
134         unsigned long flags;
135
136         flags = bb_getopt_ulflags(argc, argv, "123");
137
138         if (optind + 2 != argc)
139                 bb_show_usage();
140
141         only_file_1 = !(flags & COMM_OPT_1);
142         only_file_2 = !(flags & COMM_OPT_2);
143         both = !(flags & COMM_OPT_3);
144
145         exit(cmp_files(argv + optind) == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
146 }