(add_tabstop): Give correct size when reallocating tab_list buffer.
[platform/upstream/coreutils.git] / src / comm.c
1 /* comm -- compare two sorted files line by line.
2    Copyright (C) 1986, 1990, 1991, 1995 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 /* Written by Richard Stallman and David MacKenzie. */
19 \f
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <sys/types.h>
25 #include "system.h"
26 #include "linebuffer.h"
27 #include "version.h"
28 #include "error.h"
29
30 #define min(x, y) ((x) < (y) ? (x) : (y))
31
32 /* The name this program was run with. */
33 char *program_name;
34
35 /* If nonzero, print lines that are found only in file 1. */
36 static int only_file_1;
37
38 /* If nonzero, print lines that are found only in file 2. */
39 static int only_file_2;
40
41 /* If nonzero, print lines that are found in both files. */
42 static int both;
43
44 /* If non-zero, display usage information and exit.  */
45 static int show_help;
46
47 /* If non-zero, print the version on standard output then exit.  */
48 static int show_version;
49
50 static struct option const long_options[] =
51 {
52   {"help", no_argument, &show_help, 1},
53   {"version", no_argument, &show_version, 1},
54   {0, 0, 0, 0}
55 };
56
57 static int compare_files ();
58 static void writeline ();
59 static void usage ();
60 \f
61 void
62 main (argc, argv)
63      int argc;
64      char *argv[];
65 {
66   int c;
67
68   program_name = argv[0];
69
70   only_file_1 = 1;
71   only_file_2 = 1;
72   both = 1;
73
74   while ((c = getopt_long (argc, argv, "123", long_options, (int *) 0)) != EOF)
75     switch (c)
76       {
77       case 0:
78         break;
79
80       case '1':
81         only_file_1 = 0;
82         break;
83
84       case '2':
85         only_file_2 = 0;
86         break;
87
88       case '3':
89         both = 0;
90         break;
91
92       default:
93         usage (1);
94       }
95
96   if (show_version)
97     {
98       printf ("comm - %s\n", version_string);
99       exit (0);
100     }
101
102   if (show_help)
103     usage (0);
104
105   if (optind + 2 != argc)
106     usage (1);
107
108   exit (compare_files (argv + optind));
109 }
110 \f
111 /* Compare INFILES[0] and INFILES[1].
112    If either is "-", use the standard input for that file.
113    Assume that each input file is sorted;
114    merge them and output the result.
115    Return 0 if successful, 1 if any errors occur. */
116
117 static int
118 compare_files (infiles)
119      char **infiles;
120 {
121   /* For each file, we have one linebuffer in lb1.  */
122   struct linebuffer lb1[2];
123
124   /* thisline[i] points to the linebuffer holding the next available line
125      in file i, or is NULL if there are no lines left in that file.  */
126   struct linebuffer *thisline[2];
127
128   /* streams[i] holds the input stream for file i.  */
129   FILE *streams[2];
130
131   int i, ret = 0;
132
133   /* Initialize the storage. */
134   for (i = 0; i < 2; i++)
135     {
136       initbuffer (&lb1[i]);
137       thisline[i] = &lb1[i];
138       streams[i] = strcmp (infiles[i], "-")
139         ? fopen (infiles[i], "r") : stdin;
140       if (!streams[i])
141         {
142           error (0, errno, "%s", infiles[i]);
143           return 1;
144         }
145
146       thisline[i] = readline (thisline[i], streams[i]);
147     }
148
149   while (thisline[0] || thisline[1])
150     {
151       int order;
152
153       /* Compare the next available lines of the two files.  */
154
155       if (!thisline[0])
156         order = 1;
157       else if (!thisline[1])
158         order = -1;
159       else
160         {
161           /* Cannot use bcmp -- it only returns a boolean value. */
162           order = memcmp (thisline[0]->buffer, thisline[1]->buffer,
163                           min (thisline[0]->length, thisline[1]->length));
164           if (order == 0)
165             order = thisline[0]->length - thisline[1]->length;
166         }
167
168       /* Output the line that is lesser. */
169       if (order == 0)
170         writeline (thisline[1], stdout, 3);
171       else if (order > 0)
172         writeline (thisline[1], stdout, 2);
173       else
174         writeline (thisline[0], stdout, 1);
175
176       /* Step the file the line came from.
177          If the files match, step both files.  */
178       if (order >= 0)
179         thisline[1] = readline (thisline[1], streams[1]);
180       if (order <= 0)
181         thisline[0] = readline (thisline[0], streams[0]);
182     }
183
184   /* Free all storage and close all input streams. */
185   for (i = 0; i < 2; i++)
186     {
187       free (lb1[i].buffer);
188       if (ferror (streams[i]) || fclose (streams[i]) == EOF)
189         {
190           error (0, errno, "%s", infiles[i]);
191           ret = 1;
192         }
193     }
194   if (ferror (stdout) || fclose (stdout) == EOF)
195     {
196       error (0, errno, "write error");
197       ret = 1;
198     }
199   return ret;
200 }
201
202 /* Output the line in linebuffer LINE to stream STREAM
203    provided the switches say it should be output.
204    CLASS is 1 for a line found only in file 1,
205    2 for a line only in file 2, 3 for a line in both. */
206
207 static void
208 writeline (line, stream, class)
209      struct linebuffer *line;
210      FILE *stream;
211      int class;
212 {
213   switch (class)
214     {
215     case 1:
216       if (!only_file_1)
217         return;
218       break;
219
220     case 2:
221       if (!only_file_2)
222         return;
223       /* Skip the tab stop for case 1, if we are printing case 1.  */
224       if (only_file_1)
225         putc ('\t', stream);
226       break;
227
228     case 3:
229       if (!both)
230         return;
231       /* Skip the tab stop for case 1, if we are printing case 1.  */
232       if (only_file_1)
233         putc ('\t', stream);
234       /* Skip the tab stop for case 2, if we are printing case 2.  */
235       if (only_file_2)
236         putc ('\t', stream);
237       break;
238     }
239
240   fwrite (line->buffer, sizeof (char), line->length, stream);
241   putc ('\n', stream);
242 }
243 \f
244 static void
245 usage (status)
246      int status;
247 {
248   if (status != 0)
249     fprintf (stderr, "Try `%s --help' for more information.\n",
250              program_name);
251   else
252     {
253       printf ("\
254 Usage: %s [OPTION]... LEFT_FILE RIGHT_FILE\n\
255 ",
256               program_name);
257       printf ("\
258 \n\
259   -1              suppress lines unique to left file\n\
260   -2              suppress lines unique to right file\n\
261   -3              suppress lines unique to both files\n\
262       --help      display this help and exit\n\
263       --version   output version information and exit\n\
264 ");
265     }
266   exit (status);
267 }