(jm_FILE_SYSTEM_USAGE): Add `[]' between use of
[coreutils.git] / src / comm.c
blob28e9655a2e1a1a74e00202ec3a71dbfe020e9196
1 /* comm -- compare two sorted files line by line.
2 Copyright (C) 86, 90, 91, 1995-2000 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 "memcoll.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "comm"
35 #define AUTHORS "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 #ifdef ENABLE_NLS
45 /* Nonzero if the LC_COLLATE locale is hard. */
46 static int hard_LC_COLLATE;
47 #endif
49 /* If nonzero, print lines that are found only in file 1. */
50 static int only_file_1;
52 /* If nonzero, print lines that are found only in file 2. */
53 static int only_file_2;
55 /* If nonzero, print lines that are found in both files. */
56 static int both;
58 static struct option const long_options[] =
60 {GETOPT_HELP_OPTION_DECL},
61 {GETOPT_VERSION_OPTION_DECL},
62 {0, 0, 0, 0}
67 void
68 usage (int status)
70 if (status != 0)
71 fprintf (stderr, _("Try `%s --help' for more information.\n"),
72 program_name);
73 else
75 printf (_("\
76 Usage: %s [OPTION]... LEFT_FILE RIGHT_FILE\n\
77 "),
78 program_name);
79 printf (_("\
80 Compare sorted files LEFT_FILE and RIGHT_FILE line by line.\n\
81 \n\
82 -1 suppress lines unique to left file\n\
83 -2 suppress lines unique to right file\n\
84 -3 suppress lines unique to both files\n\
85 --help display this help and exit\n\
86 --version output version information and exit\n\
87 "));
88 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
90 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
93 /* Output the line in linebuffer LINE to stream STREAM
94 provided the switches say it should be output.
95 CLASS is 1 for a line found only in file 1,
96 2 for a line only in file 2, 3 for a line in both. */
98 static void
99 writeline (const struct linebuffer *line, FILE *stream, int class)
101 switch (class)
103 case 1:
104 if (!only_file_1)
105 return;
106 break;
108 case 2:
109 if (!only_file_2)
110 return;
111 /* Print a TAB if we are printing lines from file 1. */
112 if (only_file_1)
113 putc ('\t', stream);
114 break;
116 case 3:
117 if (!both)
118 return;
119 /* Print a TAB if we are printing lines from file 1. */
120 if (only_file_1)
121 putc ('\t', stream);
122 /* Print a TAB if we are printing lines from file 2. */
123 if (only_file_2)
124 putc ('\t', stream);
125 break;
128 fwrite (line->buffer, sizeof (char), line->length, stream);
131 /* Compare INFILES[0] and INFILES[1].
132 If either is "-", use the standard input for that file.
133 Assume that each input file is sorted;
134 merge them and output the result.
135 Return 0 if successful, 1 if any errors occur. */
137 static int
138 compare_files (char **infiles)
140 /* For each file, we have one linebuffer in lb1. */
141 struct linebuffer lb1[2];
143 /* thisline[i] points to the linebuffer holding the next available line
144 in file i, or is NULL if there are no lines left in that file. */
145 struct linebuffer *thisline[2];
147 /* streams[i] holds the input stream for file i. */
148 FILE *streams[2];
150 int i, ret = 0;
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"));
158 if (!streams[i])
160 error (0, errno, "%s", infiles[i]);
161 return 1;
164 thisline[i] = readline (thisline[i], streams[i]);
167 while (thisline[0] || thisline[1])
169 int order;
171 /* Compare the next available lines of the two files. */
173 if (!thisline[0])
174 order = 1;
175 else if (!thisline[1])
176 order = -1;
177 else
179 #ifdef ENABLE_NLS
180 if (hard_LC_COLLATE)
181 order = memcoll (thisline[0]->buffer, thisline[0]->length - 1,
182 thisline[1]->buffer, thisline[1]->length - 1);
183 else
184 #endif
186 size_t len = min (thisline[0]->length, thisline[1]->length) - 1;
187 order = memcmp (thisline[0]->buffer, thisline[1]->buffer, len);
188 if (order == 0)
189 order = thisline[0]->length - thisline[1]->length;
193 /* Output the line that is lesser. */
194 if (order == 0)
195 writeline (thisline[1], stdout, 3);
196 else if (order > 0)
197 writeline (thisline[1], stdout, 2);
198 else
199 writeline (thisline[0], stdout, 1);
201 /* Step the file the line came from.
202 If the files match, step both files. */
203 if (order >= 0)
204 thisline[1] = readline (thisline[1], streams[1]);
205 if (order <= 0)
206 thisline[0] = readline (thisline[0], streams[0]);
209 /* Free all storage and close all input streams. */
210 for (i = 0; i < 2; i++)
212 free (lb1[i].buffer);
213 if (ferror (streams[i]) || fclose (streams[i]) == EOF)
215 error (0, errno, "%s", infiles[i]);
216 ret = 1;
219 return ret;
223 main (int argc, char **argv)
225 int c;
227 program_name = argv[0];
228 setlocale (LC_ALL, "");
229 bindtextdomain (PACKAGE, LOCALEDIR);
230 textdomain (PACKAGE);
232 atexit (close_stdout);
234 #ifdef ENABLE_NLS
235 hard_LC_COLLATE = hard_locale (LC_COLLATE);
236 #endif
238 only_file_1 = 1;
239 only_file_2 = 1;
240 both = 1;
242 while ((c = getopt_long (argc, argv, "123", long_options, NULL)) != -1)
243 switch (c)
245 case 0:
246 break;
248 case '1':
249 only_file_1 = 0;
250 break;
252 case '2':
253 only_file_2 = 0;
254 break;
256 case '3':
257 both = 0;
258 break;
260 case_GETOPT_HELP_CHAR;
262 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
264 default:
265 usage (1);
268 if (optind + 2 != argc)
269 usage (1);
271 exit (compare_files (argv + optind) == 0
272 ? EXIT_SUCCESS : EXIT_FAILURE);