.
[coreutils.git] / src / comm.c
blobe7d241d153a3b4bc5a61792d4fc0f391ce873212
1 /* comm -- compare two sorted files line by line.
2 Copyright (C) 1986, 1990, 1991, 1995 Free Software Foundation, Inc.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Richard Stallman and David MacKenzie. */
20 #include <config.h>
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"
30 /* Undefine, to avoid warning about redefinition on some systems. */
31 #undef min
32 #define min(x, y) ((x) < (y) ? (x) : (y))
34 /* The name this program was run with. */
35 char *program_name;
37 /* If nonzero, print lines that are found only in file 1. */
38 static int only_file_1;
40 /* If nonzero, print lines that are found only in file 2. */
41 static int only_file_2;
43 /* If nonzero, print lines that are found in both files. */
44 static int both;
46 /* If non-zero, display usage information and exit. */
47 static int show_help;
49 /* If non-zero, print the version on standard output then exit. */
50 static int show_version;
52 static struct option const long_options[] =
54 {"help", no_argument, &show_help, 1},
55 {"version", no_argument, &show_version, 1},
56 {0, 0, 0, 0}
59 static int compare_files ();
60 static void writeline ();
61 static void usage ();
63 void
64 main (argc, argv)
65 int argc;
66 char *argv[];
68 int c;
70 program_name = argv[0];
72 only_file_1 = 1;
73 only_file_2 = 1;
74 both = 1;
76 while ((c = getopt_long (argc, argv, "123", long_options, (int *) 0)) != EOF)
77 switch (c)
79 case 0:
80 break;
82 case '1':
83 only_file_1 = 0;
84 break;
86 case '2':
87 only_file_2 = 0;
88 break;
90 case '3':
91 both = 0;
92 break;
94 default:
95 usage (1);
98 if (show_version)
100 printf ("comm - %s\n", version_string);
101 exit (0);
104 if (show_help)
105 usage (0);
107 if (optind + 2 != argc)
108 usage (1);
110 exit (compare_files (argv + optind));
113 /* Compare INFILES[0] and INFILES[1].
114 If either is "-", use the standard input for that file.
115 Assume that each input file is sorted;
116 merge them and output the result.
117 Return 0 if successful, 1 if any errors occur. */
119 static int
120 compare_files (infiles)
121 char **infiles;
123 /* For each file, we have one linebuffer in lb1. */
124 struct linebuffer lb1[2];
126 /* thisline[i] points to the linebuffer holding the next available line
127 in file i, or is NULL if there are no lines left in that file. */
128 struct linebuffer *thisline[2];
130 /* streams[i] holds the input stream for file i. */
131 FILE *streams[2];
133 int i, ret = 0;
135 /* Initialize the storage. */
136 for (i = 0; i < 2; i++)
138 initbuffer (&lb1[i]);
139 thisline[i] = &lb1[i];
140 streams[i] = strcmp (infiles[i], "-")
141 ? fopen (infiles[i], "r") : stdin;
142 if (!streams[i])
144 error (0, errno, "%s", infiles[i]);
145 return 1;
148 thisline[i] = readline (thisline[i], streams[i]);
151 while (thisline[0] || thisline[1])
153 int order;
155 /* Compare the next available lines of the two files. */
157 if (!thisline[0])
158 order = 1;
159 else if (!thisline[1])
160 order = -1;
161 else
163 /* Cannot use bcmp -- it only returns a boolean value. */
164 order = memcmp (thisline[0]->buffer, thisline[1]->buffer,
165 min (thisline[0]->length, thisline[1]->length));
166 if (order == 0)
167 order = thisline[0]->length - thisline[1]->length;
170 /* Output the line that is lesser. */
171 if (order == 0)
172 writeline (thisline[1], stdout, 3);
173 else if (order > 0)
174 writeline (thisline[1], stdout, 2);
175 else
176 writeline (thisline[0], stdout, 1);
178 /* Step the file the line came from.
179 If the files match, step both files. */
180 if (order >= 0)
181 thisline[1] = readline (thisline[1], streams[1]);
182 if (order <= 0)
183 thisline[0] = readline (thisline[0], streams[0]);
186 /* Free all storage and close all input streams. */
187 for (i = 0; i < 2; i++)
189 free (lb1[i].buffer);
190 if (ferror (streams[i]) || fclose (streams[i]) == EOF)
192 error (0, errno, "%s", infiles[i]);
193 ret = 1;
196 if (ferror (stdout) || fclose (stdout) == EOF)
198 error (0, errno, "write error");
199 ret = 1;
201 return ret;
204 /* Output the line in linebuffer LINE to stream STREAM
205 provided the switches say it should be output.
206 CLASS is 1 for a line found only in file 1,
207 2 for a line only in file 2, 3 for a line in both. */
209 static void
210 writeline (line, stream, class)
211 struct linebuffer *line;
212 FILE *stream;
213 int class;
215 switch (class)
217 case 1:
218 if (!only_file_1)
219 return;
220 break;
222 case 2:
223 if (!only_file_2)
224 return;
225 /* Skip the tab stop for case 1, if we are printing case 1. */
226 if (only_file_1)
227 putc ('\t', stream);
228 break;
230 case 3:
231 if (!both)
232 return;
233 /* Skip the tab stop for case 1, if we are printing case 1. */
234 if (only_file_1)
235 putc ('\t', stream);
236 /* Skip the tab stop for case 2, if we are printing case 2. */
237 if (only_file_2)
238 putc ('\t', stream);
239 break;
242 fwrite (line->buffer, sizeof (char), line->length, stream);
243 putc ('\n', stream);
246 static void
247 usage (status)
248 int status;
250 if (status != 0)
251 fprintf (stderr, "Try `%s --help' for more information.\n",
252 program_name);
253 else
255 printf ("\
256 Usage: %s [OPTION]... LEFT_FILE RIGHT_FILE\n\
258 program_name);
259 printf ("\
260 Compare sorted files LEFT_FILE and RIGHT_FILE line by line.\n\
262 -1 suppress lines unique to left file\n\
263 -2 suppress lines unique to right file\n\
264 -3 suppress lines unique to both files\n\
265 --help display this help and exit\n\
266 --version output version information and exit\n\
269 exit (status);