fix segfault if user only specifies 1 file
[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         switch (class) {
44                 case 1:
45                         if (!only_file_1)
46                                 return;
47                         break;
48                 case 2:
49                         if (!only_file_2)
50                                 return;
51                         if (only_file_1)
52                                 putchar('\t');
53                         break;
54                 case 3:
55                         if (!both)
56                                 return;
57                         if (only_file_1)
58                                 putchar('\t');
59                         if (only_file_2)
60                                 putchar('\t');
61                         break;
62         }
63         fputs(line, stdout);
64 }
65
66 /* This is the real core of the program - lines are compared here */
67 static int cmp_files(char **infiles)
68 {
69         char thisline[2][100];
70         FILE *streams[2];
71         int i = 0;
72
73         for (i = 0; i < 2; i++) {
74                 streams[i] = (strcmp(infiles[i], "=") == 0 ? stdin : fopen(infiles[i], "r"));
75                 fgets(thisline[i], 100, streams[i]);
76         }
77
78         while (thisline[0] || thisline[1]) {
79                 int order = 0;
80                 int tl0_len = strlen(thisline[0]);
81                 int tl1_len = strlen(thisline[1]);
82
83                 if (!thisline[0])
84                         order = 1;
85                 else if (!thisline[1])
86                         order = -1;
87                 else {
88                         order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len);
89                         if (!order)
90                                 order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len;
91                 }
92
93                 if ((order == 0) && (!feof(streams[0])) && (!feof(streams[1])))
94                         writeline(thisline[1], 3);
95                 else if ((order > 0) && (!feof(streams[1])))
96                         writeline(thisline[1], 2);
97                 else if ((order < 0) && (!feof(streams[0])))
98                         writeline(thisline[0], 1);
99
100                 if (feof(streams[0]) && feof(streams[1])) {
101                         fclose(streams[0]);
102                         fclose(streams[1]);
103                         break;
104
105                 } else if (feof(streams[0])) {
106                         while (!feof(streams[1])) {
107                                 if (order < 0)
108                                         writeline(thisline[1], 2);
109                                 fgets(thisline[1], 100, streams[1]);
110                         }
111                         fclose(streams[0]);
112                         fclose(streams[1]);
113                         break;
114
115                 } else if (feof(streams[1])) {
116                         while (!feof(streams[0])) {
117                                 if (order > 0)
118                                         writeline(thisline[0], 1);
119                                 fgets(thisline[0], 100, streams[0]);
120                         }
121                         fclose(streams[0]);
122                         fclose(streams[1]);
123                         break;
124
125                 } else {
126                         if (order >= 0)
127                                 fgets(thisline[1], 100, streams[1]);
128                         if (order <= 0)
129                                 fgets(thisline[0], 100, streams[0]);
130                 }
131         }
132
133         return 0;
134 }
135
136 int comm_main (int argc, char **argv)
137 {
138         unsigned long opt;
139         only_file_1 = 1;
140         only_file_2 = 1;
141         both = 1;
142
143         opt = bb_getopt_ulflags(argc, argv, "123");
144
145         if (optind != argc + 2)
146                 bb_show_usage();
147
148         if (opt & COMM_OPT_1)
149                 only_file_1 = 0;
150         if (opt & COMM_OPT_2)
151                 only_file_2 = 0;
152         if (opt & COMM_OPT_3)
153                 both = 0;
154
155         exit(cmp_files(argv + optind) == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
156 }