1 /* comm -- compare two sorted files line by line.
2 Copyright (C) 86, 90, 91, 1995-2003 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"
28 #include "hard-locale.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "comm"
34 #define WRITTEN_BY _("Written by Richard Stallman and David MacKenzie.")
36 /* Undefine, to avoid warning about redefinition on some systems. */
38 #define min(x, y) ((x) < (y) ? (x) : (y))
40 /* The name this program was run with. */
43 /* Nonzero if the LC_COLLATE locale is hard. */
44 static int hard_LC_COLLATE;
46 /* If nonzero, print lines that are found only in file 1. */
47 static int only_file_1;
49 /* If nonzero, print lines that are found only in file 2. */
50 static int only_file_2;
52 /* If nonzero, print lines that are found in both files. */
55 static struct option const long_options[] =
57 {GETOPT_HELP_OPTION_DECL},
58 {GETOPT_VERSION_OPTION_DECL},
68 fprintf (stderr, _("Try `%s --help' for more information.\n"),
73 Usage: %s [OPTION]... LEFT_FILE RIGHT_FILE\n\
77 Compare sorted files LEFT_FILE and RIGHT_FILE line by line.\n\
79 -1 suppress lines unique to left file\n\
80 -2 suppress lines unique to right file\n\
81 -3 suppress lines that appear in both files\n\
83 fputs (HELP_OPTION_DESCRIPTION, stdout);
84 fputs (VERSION_OPTION_DESCRIPTION, stdout);
85 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
87 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
90 /* Output the line in linebuffer LINE to stream STREAM
91 provided the switches say it should be output.
92 CLASS is 1 for a line found only in file 1,
93 2 for a line only in file 2, 3 for a line in both. */
96 writeline (const struct linebuffer *line, FILE *stream, int class)
108 /* Print a TAB if we are printing lines from file 1. */
116 /* Print a TAB if we are printing lines from file 1. */
119 /* Print a TAB if we are printing lines from file 2. */
125 fwrite (line->buffer, sizeof (char), line->length, stream);
128 /* Compare INFILES[0] and INFILES[1].
129 If either is "-", use the standard input for that file.
130 Assume that each input file is sorted;
131 merge them and output the result.
132 Return 0 if successful, 1 if any errors occur. */
135 compare_files (char **infiles)
137 /* For each file, we have one linebuffer in lb1. */
138 struct linebuffer lb1[2];
140 /* thisline[i] points to the linebuffer holding the next available line
141 in file i, or is NULL if there are no lines left in that file. */
142 struct linebuffer *thisline[2];
144 /* streams[i] holds the input stream for file i. */
147 /* errno values for each stream. */
152 /* Initialize the storage. */
153 for (i = 0; i < 2; i++)
155 initbuffer (&lb1[i]);
156 thisline[i] = &lb1[i];
157 streams[i] = (STREQ (infiles[i], "-") ? stdin : fopen (infiles[i], "r"));
160 error (0, errno, "%s", infiles[i]);
164 thisline[i] = readlinebuffer (thisline[i], streams[i]);
165 saved_errno[i] = errno;
168 while (thisline[0] || thisline[1])
172 /* Compare the next available lines of the two files. */
176 else if (!thisline[1])
180 if (HAVE_SETLOCALE && hard_LC_COLLATE)
181 order = xmemcoll (thisline[0]->buffer, thisline[0]->length - 1,
182 thisline[1]->buffer, thisline[1]->length - 1);
185 size_t len = min (thisline[0]->length, thisline[1]->length) - 1;
186 order = memcmp (thisline[0]->buffer, thisline[1]->buffer, len);
188 order = (thisline[0]->length < thisline[1]->length
190 : thisline[0]->length != thisline[1]->length);
194 /* Output the line that is lesser. */
196 writeline (thisline[1], stdout, 3);
198 writeline (thisline[1], stdout, 2);
200 writeline (thisline[0], stdout, 1);
202 /* Step the file the line came from.
203 If the files match, step both files. */
206 thisline[1] = readlinebuffer (thisline[1], streams[1]);
207 saved_errno[1] = errno;
211 thisline[0] = readlinebuffer (thisline[0], streams[0]);
212 saved_errno[0] = errno;
216 /* Free all storage and close all input streams. */
217 for (i = 0; i < 2; i++)
219 free (lb1[i].buffer);
220 if (ferror (streams[i]))
222 error (0, saved_errno[i], "%s", infiles[i]);
225 if (fclose (streams[i]) != 0)
227 error (0, errno, "%s", infiles[i]);
235 main (int argc, char **argv)
239 initialize_main (&argc, &argv);
240 program_name = argv[0];
241 setlocale (LC_ALL, "");
242 bindtextdomain (PACKAGE, LOCALEDIR);
243 textdomain (PACKAGE);
244 hard_LC_COLLATE = hard_locale (LC_COLLATE);
246 atexit (close_stdout);
252 while ((c = getopt_long (argc, argv, "123", long_options, NULL)) != -1)
270 case_GETOPT_HELP_CHAR;
272 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
275 usage (EXIT_FAILURE);
278 if (optind + 2 != argc)
280 error (0, 0, _("too few arguments"));
281 usage (EXIT_FAILURE);
284 exit (compare_files (argv + optind) == 0
285 ? EXIT_SUCCESS : EXIT_FAILURE);