*** empty log message ***
[coreutils.git] / src / comm.c
blob7da3de20bd8cecb2c438bda2d5e7441c767ef41e
1 /* comm -- compare two sorted files line by line.
2 Copyright (C) 86, 90, 91, 1995-2002 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 Foundation,
16 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 "closeout.h"
27 #include "linebuffer.h"
28 #include "error.h"
29 #include "hard-locale.h"
30 #include "xmemcoll.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "comm"
35 #define AUTHORS N_ ("Richard Stallman and David MacKenzie")
37 /* Undefine, to avoid warning about redefinition on some systems. */
38 #undef min
39 #define min(x, y) ((x) < (y) ? (x) : (y))
41 /* The name this program was run with. */
42 char *program_name;
44 /* Nonzero if the LC_COLLATE locale is hard. */
45 static int hard_LC_COLLATE;
47 /* If nonzero, print lines that are found only in file 1. */
48 static int only_file_1;
50 /* If nonzero, print lines that are found only in file 2. */
51 static int only_file_2;
53 /* If nonzero, print lines that are found in both files. */
54 static int both;
56 static struct option const long_options[] =
58 {GETOPT_HELP_OPTION_DECL},
59 {GETOPT_VERSION_OPTION_DECL},
60 {0, 0, 0, 0}
65 void
66 usage (int status)
68 if (status != 0)
69 fprintf (stderr, _("Try `%s --help' for more information.\n"),
70 program_name);
71 else
73 printf (_("\
74 Usage: %s [OPTION]... LEFT_FILE RIGHT_FILE\n\
75 "),
76 program_name);
77 fputs (_("\
78 Compare sorted files LEFT_FILE and RIGHT_FILE line by line.\n\
79 \n\
80 -1 suppress lines unique to left file\n\
81 -2 suppress lines unique to right file\n\
82 -3 suppress lines that appear in both files\n\
83 "), stdout);
84 fputs (HELP_OPTION_DESCRIPTION, stdout);
85 fputs (VERSION_OPTION_DESCRIPTION, stdout);
86 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
88 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
91 /* Output the line in linebuffer LINE to stream STREAM
92 provided the switches say it should be output.
93 CLASS is 1 for a line found only in file 1,
94 2 for a line only in file 2, 3 for a line in both. */
96 static void
97 writeline (const struct linebuffer *line, FILE *stream, int class)
99 switch (class)
101 case 1:
102 if (!only_file_1)
103 return;
104 break;
106 case 2:
107 if (!only_file_2)
108 return;
109 /* Print a TAB if we are printing lines from file 1. */
110 if (only_file_1)
111 putc ('\t', stream);
112 break;
114 case 3:
115 if (!both)
116 return;
117 /* Print a TAB if we are printing lines from file 1. */
118 if (only_file_1)
119 putc ('\t', stream);
120 /* Print a TAB if we are printing lines from file 2. */
121 if (only_file_2)
122 putc ('\t', stream);
123 break;
126 fwrite (line->buffer, sizeof (char), line->length, stream);
129 /* Compare INFILES[0] and INFILES[1].
130 If either is "-", use the standard input for that file.
131 Assume that each input file is sorted;
132 merge them and output the result.
133 Return 0 if successful, 1 if any errors occur. */
135 static int
136 compare_files (char **infiles)
138 /* For each file, we have one linebuffer in lb1. */
139 struct linebuffer lb1[2];
141 /* thisline[i] points to the linebuffer holding the next available line
142 in file i, or is NULL if there are no lines left in that file. */
143 struct linebuffer *thisline[2];
145 /* streams[i] holds the input stream for file i. */
146 FILE *streams[2];
148 int i, ret = 0;
150 /* Initialize the storage. */
151 for (i = 0; i < 2; i++)
153 initbuffer (&lb1[i]);
154 thisline[i] = &lb1[i];
155 streams[i] = (STREQ (infiles[i], "-") ? stdin : fopen (infiles[i], "r"));
156 if (!streams[i])
158 error (0, errno, "%s", infiles[i]);
159 return 1;
162 thisline[i] = readline (thisline[i], streams[i]);
165 while (thisline[0] || thisline[1])
167 int order;
169 /* Compare the next available lines of the two files. */
171 if (!thisline[0])
172 order = 1;
173 else if (!thisline[1])
174 order = -1;
175 else
177 if (HAVE_SETLOCALE && hard_LC_COLLATE)
178 order = xmemcoll (thisline[0]->buffer, thisline[0]->length - 1,
179 thisline[1]->buffer, thisline[1]->length - 1);
180 else
182 size_t len = min (thisline[0]->length, thisline[1]->length) - 1;
183 order = memcmp (thisline[0]->buffer, thisline[1]->buffer, len);
184 if (order == 0)
185 order = (thisline[0]->length < thisline[1]->length
186 ? -1
187 : thisline[0]->length != thisline[1]->length);
191 /* Output the line that is lesser. */
192 if (order == 0)
193 writeline (thisline[1], stdout, 3);
194 else if (order > 0)
195 writeline (thisline[1], stdout, 2);
196 else
197 writeline (thisline[0], stdout, 1);
199 /* Step the file the line came from.
200 If the files match, step both files. */
201 if (order >= 0)
202 thisline[1] = readline (thisline[1], streams[1]);
203 if (order <= 0)
204 thisline[0] = readline (thisline[0], streams[0]);
207 /* Free all storage and close all input streams. */
208 for (i = 0; i < 2; i++)
210 free (lb1[i].buffer);
211 if (ferror (streams[i]) || fclose (streams[i]) == EOF)
213 error (0, errno, "%s", infiles[i]);
214 ret = 1;
217 return ret;
221 main (int argc, char **argv)
223 int c;
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);
233 only_file_1 = 1;
234 only_file_2 = 1;
235 both = 1;
237 while ((c = getopt_long (argc, argv, "123", long_options, NULL)) != -1)
238 switch (c)
240 case 0:
241 break;
243 case '1':
244 only_file_1 = 0;
245 break;
247 case '2':
248 only_file_2 = 0;
249 break;
251 case '3':
252 both = 0;
253 break;
255 case_GETOPT_HELP_CHAR;
257 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
259 default:
260 usage (1);
263 if (optind + 2 != argc)
264 usage (1);
266 exit (compare_files (argv + optind) == 0
267 ? EXIT_SUCCESS : EXIT_FAILURE);