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. */
149 /* Initialize the storage. */
150 for (i = 0; i < 2; i++)
152 initbuffer (&lb1[i]);
153 thisline[i] = &lb1[i];
154 streams[i] = (STREQ (infiles[i], "-") ? stdin : fopen (infiles[i], "r"));
157 error (0, errno, "%s", infiles[i]);
161 thisline[i] = readlinebuffer (thisline[i], streams[i]);
164 while (thisline[0] || thisline[1])
168 /* Compare the next available lines of the two files. */
172 else if (!thisline[1])
176 if (HAVE_SETLOCALE && hard_LC_COLLATE)
177 order = xmemcoll (thisline[0]->buffer, thisline[0]->length - 1,
178 thisline[1]->buffer, thisline[1]->length - 1);
181 size_t len = min (thisline[0]->length, thisline[1]->length) - 1;
182 order = memcmp (thisline[0]->buffer, thisline[1]->buffer, len);
184 order = (thisline[0]->length < thisline[1]->length
186 : thisline[0]->length != thisline[1]->length);
190 /* Output the line that is lesser. */
192 writeline (thisline[1], stdout, 3);
194 writeline (thisline[1], stdout, 2);
196 writeline (thisline[0], stdout, 1);
198 /* Step the file the line came from.
199 If the files match, step both files. */
201 thisline[1] = readlinebuffer (thisline[1], streams[1]);
203 thisline[0] = readlinebuffer (thisline[0], streams[0]);
206 /* Free all storage and close all input streams. */
207 for (i = 0; i < 2; i++)
209 free (lb1[i].buffer);
210 if (ferror (streams[i]) || fclose (streams[i]) == EOF)
212 error (0, errno, "%s", infiles[i]);
220 main (int argc, char **argv)
224 initialize_main (&argc, &argv);
225 program_name = argv[0];
226 setlocale (LC_ALL, "");
227 bindtextdomain (PACKAGE, LOCALEDIR);
228 textdomain (PACKAGE);
229 hard_LC_COLLATE = hard_locale (LC_COLLATE);
231 atexit (close_stdout);
237 while ((c = getopt_long (argc, argv, "123", long_options, NULL)) != -1)
255 case_GETOPT_HELP_CHAR;
257 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
260 usage (EXIT_FAILURE);
263 if (optind + 2 != argc)
265 error (0, 0, _("too few arguments"));
266 usage (EXIT_FAILURE);
269 exit (compare_files (argv + optind) == 0
270 ? EXIT_SUCCESS : EXIT_FAILURE);