1 /* comm -- compare two sorted files line by line.
2 Copyright (C) 86, 90, 91, 1995-1999 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)
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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Richard Stallman and David MacKenzie. */
24 #include <sys/types.h>
26 #include "linebuffer.h"
27 #include "long-options.h"
30 /* The official name of this program (e.g., no `g' prefix). */
31 #define PROGRAM_NAME "comm"
33 /* Undefine, to avoid warning about redefinition on some systems. */
35 #define min(x, y) ((x) < (y) ? (x) : (y))
37 /* The name this program was run with. */
40 /* If nonzero, print lines that are found only in file 1. */
41 static int only_file_1;
43 /* If nonzero, print lines that are found only in file 2. */
44 static int only_file_2;
46 /* If nonzero, print lines that are found in both files. */
49 static struct option const long_options[] =
60 fprintf (stderr, _("Try `%s --help' for more information.\n"),
65 Usage: %s [OPTION]... LEFT_FILE RIGHT_FILE\n\
69 Compare sorted files LEFT_FILE and RIGHT_FILE line by line.\n\
71 -1 suppress lines unique to left file\n\
72 -2 suppress lines unique to right file\n\
73 -3 suppress lines unique to both files\n\
74 --help display this help and exit\n\
75 --version output version information and exit\n\
77 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
79 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
82 /* Output the line in linebuffer LINE to stream STREAM
83 provided the switches say it should be output.
84 CLASS is 1 for a line found only in file 1,
85 2 for a line only in file 2, 3 for a line in both. */
88 writeline (struct linebuffer *line, FILE *stream, int class)
100 /* Skip the tab stop for case 1, if we are printing case 1. */
108 /* Skip the tab stop for case 1, if we are printing case 1. */
111 /* Skip the tab stop for case 2, if we are printing case 2. */
117 fwrite (line->buffer, sizeof (char), line->length, stream);
121 /* Compare INFILES[0] and INFILES[1].
122 If either is "-", use the standard input for that file.
123 Assume that each input file is sorted;
124 merge them and output the result.
125 Return 0 if successful, 1 if any errors occur. */
128 compare_files (char **infiles)
130 /* For each file, we have one linebuffer in lb1. */
131 struct linebuffer lb1[2];
133 /* thisline[i] points to the linebuffer holding the next available line
134 in file i, or is NULL if there are no lines left in that file. */
135 struct linebuffer *thisline[2];
137 /* streams[i] holds the input stream for file i. */
142 /* Initialize the storage. */
143 for (i = 0; i < 2; i++)
145 initbuffer (&lb1[i]);
146 thisline[i] = &lb1[i];
147 streams[i] = (STREQ (infiles[i], "-") ? stdin : fopen (infiles[i], "r"));
150 error (0, errno, "%s", infiles[i]);
154 thisline[i] = readline (thisline[i], streams[i]);
157 while (thisline[0] || thisline[1])
161 /* Compare the next available lines of the two files. */
165 else if (!thisline[1])
169 /* Cannot use bcmp -- it only returns a boolean value. */
170 order = memcmp (thisline[0]->buffer, thisline[1]->buffer,
171 min (thisline[0]->length, thisline[1]->length));
173 order = thisline[0]->length - thisline[1]->length;
176 /* Output the line that is lesser. */
178 writeline (thisline[1], stdout, 3);
180 writeline (thisline[1], stdout, 2);
182 writeline (thisline[0], stdout, 1);
184 /* Step the file the line came from.
185 If the files match, step both files. */
187 thisline[1] = readline (thisline[1], streams[1]);
189 thisline[0] = readline (thisline[0], streams[0]);
192 /* Free all storage and close all input streams. */
193 for (i = 0; i < 2; i++)
195 free (lb1[i].buffer);
196 if (ferror (streams[i]) || fclose (streams[i]) == EOF)
198 error (0, errno, "%s", infiles[i]);
202 if (ferror (stdout) || fclose (stdout) == EOF)
204 error (0, errno, _("write error"));
211 main (int argc, char **argv)
215 program_name = argv[0];
216 setlocale (LC_ALL, "");
217 bindtextdomain (PACKAGE, LOCALEDIR);
218 textdomain (PACKAGE);
220 parse_long_options (argc, argv, "comm", GNU_PACKAGE, VERSION,
221 "Richard Stallman and David MacKenzie", usage);
227 while ((c = getopt_long (argc, argv, "123", long_options, NULL)) != -1)
249 if (optind + 2 != argc)
252 exit (compare_files (argv + optind) == 0
253 ? EXIT_SUCCESS : EXIT_FAILURE);