convert the rest to use proper_name -- manually
[coreutils.git] / src / comm.c
blobff33b71ce6f6e34d678d2ad47162d8236d3aad1a
1 /* comm -- compare two sorted files line by line.
2 Copyright (C) 86, 90, 91, 1995-2005 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 3 of the License, or
7 (at your option) 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, see <http://www.gnu.org/licenses/>. */
17 /* Written by Richard Stallman and David MacKenzie. */
19 #include <config.h>
21 #include <getopt.h>
22 #include <sys/types.h>
23 #include "system.h"
24 #include "linebuffer.h"
25 #include "error.h"
26 #include "hard-locale.h"
27 #include "quote.h"
28 #include "stdio--.h"
29 #include "xmemcoll.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "comm"
34 #define AUTHORS \
35 proper_name ("Richard Stallman"), \
36 proper_name ("David MacKenzie")
38 /* Undefine, to avoid warning about redefinition on some systems. */
39 #undef min
40 #define min(x, y) ((x) < (y) ? (x) : (y))
42 /* The name this program was run with. */
43 char *program_name;
45 /* True if the LC_COLLATE locale is hard. */
46 static bool hard_LC_COLLATE;
48 /* If true, print lines that are found only in file 1. */
49 static bool only_file_1;
51 /* If true, print lines that are found only in file 2. */
52 static bool only_file_2;
54 /* If true, print lines that are found in both files. */
55 static bool both;
57 static struct option const long_options[] =
59 {GETOPT_HELP_OPTION_DECL},
60 {GETOPT_VERSION_OPTION_DECL},
61 {NULL, 0, NULL, 0}
66 void
67 usage (int status)
69 if (status != EXIT_SUCCESS)
70 fprintf (stderr, _("Try `%s --help' for more information.\n"),
71 program_name);
72 else
74 printf (_("\
75 Usage: %s [OPTION]... FILE1 FILE2\n\
76 "),
77 program_name);
78 fputs (_("\
79 Compare sorted files FILE1 and FILE2 line by line.\n\
80 "), stdout);
81 fputs (_("\
82 \n\
83 With no options, produce three-column output. Column one contains\n\
84 lines unique to FILE1, column two contains lines unique to FILE2,\n\
85 and column three contains lines common to both files.\n\
86 "), stdout);
87 fputs (_("\
88 \n\
89 -1 suppress lines unique to FILE1\n\
90 -2 suppress lines unique to FILE2\n\
91 -3 suppress lines that appear in both files\n\
92 "), stdout);
93 fputs (HELP_OPTION_DESCRIPTION, stdout);
94 fputs (VERSION_OPTION_DESCRIPTION, stdout);
95 emit_bug_reporting_address ();
97 exit (status);
100 /* Output the line in linebuffer LINE to stream STREAM
101 provided the switches say it should be output.
102 CLASS is 1 for a line found only in file 1,
103 2 for a line only in file 2, 3 for a line in both. */
105 static void
106 writeline (const struct linebuffer *line, FILE *stream, int class)
108 switch (class)
110 case 1:
111 if (!only_file_1)
112 return;
113 break;
115 case 2:
116 if (!only_file_2)
117 return;
118 /* Print a TAB if we are printing lines from file 1. */
119 if (only_file_1)
120 putc ('\t', stream);
121 break;
123 case 3:
124 if (!both)
125 return;
126 /* Print a TAB if we are printing lines from file 1. */
127 if (only_file_1)
128 putc ('\t', stream);
129 /* Print a TAB if we are printing lines from file 2. */
130 if (only_file_2)
131 putc ('\t', stream);
132 break;
135 fwrite (line->buffer, sizeof (char), line->length, stream);
138 /* Compare INFILES[0] and INFILES[1].
139 If either is "-", use the standard input for that file.
140 Assume that each input file is sorted;
141 merge them and output the result. */
143 static void
144 compare_files (char **infiles)
146 /* For each file, we have one linebuffer in lb1. */
147 struct linebuffer lb1[2];
149 /* thisline[i] points to the linebuffer holding the next available line
150 in file i, or is NULL if there are no lines left in that file. */
151 struct linebuffer *thisline[2];
153 /* streams[i] holds the input stream for file i. */
154 FILE *streams[2];
156 int i;
158 /* Initialize the storage. */
159 for (i = 0; i < 2; i++)
161 initbuffer (&lb1[i]);
162 thisline[i] = &lb1[i];
163 streams[i] = (STREQ (infiles[i], "-") ? stdin : fopen (infiles[i], "r"));
164 if (!streams[i])
165 error (EXIT_FAILURE, errno, "%s", infiles[i]);
167 thisline[i] = readlinebuffer (thisline[i], streams[i]);
168 if (ferror (streams[i]))
169 error (EXIT_FAILURE, errno, "%s", infiles[i]);
172 while (thisline[0] || thisline[1])
174 int order;
176 /* Compare the next available lines of the two files. */
178 if (!thisline[0])
179 order = 1;
180 else if (!thisline[1])
181 order = -1;
182 else
184 if (hard_LC_COLLATE)
185 order = xmemcoll (thisline[0]->buffer, thisline[0]->length - 1,
186 thisline[1]->buffer, thisline[1]->length - 1);
187 else
189 size_t len = min (thisline[0]->length, thisline[1]->length) - 1;
190 order = memcmp (thisline[0]->buffer, thisline[1]->buffer, len);
191 if (order == 0)
192 order = (thisline[0]->length < thisline[1]->length
193 ? -1
194 : thisline[0]->length != thisline[1]->length);
198 /* Output the line that is lesser. */
199 if (order == 0)
200 writeline (thisline[1], stdout, 3);
201 else if (order > 0)
202 writeline (thisline[1], stdout, 2);
203 else
204 writeline (thisline[0], stdout, 1);
206 /* Step the file the line came from.
207 If the files match, step both files. */
208 if (order >= 0)
210 thisline[1] = readlinebuffer (thisline[1], streams[1]);
211 if (ferror (streams[1]))
212 error (EXIT_FAILURE, errno, "%s", infiles[1]);
214 if (order <= 0)
216 thisline[0] = readlinebuffer (thisline[0], streams[0]);
217 if (ferror (streams[0]))
218 error (EXIT_FAILURE, errno, "%s", infiles[0]);
222 for (i = 0; i < 2; i++)
223 if (fclose (streams[i]) != 0)
224 error (EXIT_FAILURE, errno, "%s", infiles[i]);
228 main (int argc, char **argv)
230 int c;
232 initialize_main (&argc, &argv);
233 program_name = argv[0];
234 setlocale (LC_ALL, "");
235 bindtextdomain (PACKAGE, LOCALEDIR);
236 textdomain (PACKAGE);
237 hard_LC_COLLATE = hard_locale (LC_COLLATE);
239 atexit (close_stdout);
241 only_file_1 = true;
242 only_file_2 = true;
243 both = true;
245 while ((c = getopt_long (argc, argv, "123", long_options, NULL)) != -1)
246 switch (c)
248 case '1':
249 only_file_1 = false;
250 break;
252 case '2':
253 only_file_2 = false;
254 break;
256 case '3':
257 both = false;
258 break;
260 case_GETOPT_HELP_CHAR;
262 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
264 default:
265 usage (EXIT_FAILURE);
268 if (argc - optind < 2)
270 if (argc <= optind)
271 error (0, 0, _("missing operand"));
272 else
273 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
274 usage (EXIT_FAILURE);
277 if (2 < argc - optind)
279 error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
280 usage (EXIT_FAILURE);
283 compare_files (argv + optind);
285 exit (EXIT_SUCCESS);