(do_link): Produce the same sort of one-line output for
[coreutils.git] / src / comm.c
blob5020902850595117f94197ac98093e48565a9ac1
1 /* comm -- compare two sorted files line by line.
2 Copyright (C) 86, 90, 91, 1995-1999 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 "linebuffer.h"
27 #include "error.h"
28 #include "hard-locale.h"
29 #include "memcoll.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "comm"
34 #define AUTHORS "Richard Stallman and David MacKenzie"
36 /* Undefine, to avoid warning about redefinition on some systems. */
37 #undef min
38 #define min(x, y) ((x) < (y) ? (x) : (y))
40 /* The name this program was run with. */
41 char *program_name;
43 #ifdef ENABLE_NLS
44 /* Nonzero if the LC_COLLATE locale is hard. */
45 static int hard_LC_COLLATE;
46 #endif
48 /* If nonzero, print lines that are found only in file 1. */
49 static int only_file_1;
51 /* If nonzero, print lines that are found only in file 2. */
52 static int only_file_2;
54 /* If nonzero, print lines that are found in both files. */
55 static int both;
57 static struct option const long_options[] =
59 {GETOPT_HELP_OPTION_DECL},
60 {GETOPT_VERSION_OPTION_DECL},
61 {0, 0, 0, 0}
66 void
67 usage (int status)
69 if (status != 0)
70 fprintf (stderr, _("Try `%s --help' for more information.\n"),
71 program_name);
72 else
74 printf (_("\
75 Usage: %s [OPTION]... LEFT_FILE RIGHT_FILE\n\
76 "),
77 program_name);
78 printf (_("\
79 Compare sorted files LEFT_FILE and RIGHT_FILE line by line.\n\
80 \n\
81 -1 suppress lines unique to left file\n\
82 -2 suppress lines unique to right file\n\
83 -3 suppress lines unique to both files\n\
84 --help display this help and exit\n\
85 --version output version information and exit\n\
86 "));
87 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
89 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
92 /* Output the line in linebuffer LINE to stream STREAM
93 provided the switches say it should be output.
94 CLASS is 1 for a line found only in file 1,
95 2 for a line only in file 2, 3 for a line in both. */
97 static void
98 writeline (const struct linebuffer *line, FILE *stream, int class)
100 switch (class)
102 case 1:
103 if (!only_file_1)
104 return;
105 break;
107 case 2:
108 if (!only_file_2)
109 return;
110 /* Skip the tab stop for case 1, if we are printing case 1. */
111 if (only_file_1)
112 putc ('\t', stream);
113 break;
115 case 3:
116 if (!both)
117 return;
118 /* Skip the tab stop for case 1, if we are printing case 1. */
119 if (only_file_1)
120 putc ('\t', stream);
121 /* Skip the tab stop for case 2, if we are printing case 2. */
122 if (only_file_2)
123 putc ('\t', stream);
124 break;
127 fwrite (line->buffer, sizeof (char), line->length, stream);
130 /* Compare INFILES[0] and INFILES[1].
131 If either is "-", use the standard input for that file.
132 Assume that each input file is sorted;
133 merge them and output the result.
134 Return 0 if successful, 1 if any errors occur. */
136 static int
137 compare_files (char **infiles)
139 /* For each file, we have one linebuffer in lb1. */
140 struct linebuffer lb1[2];
142 /* thisline[i] points to the linebuffer holding the next available line
143 in file i, or is NULL if there are no lines left in that file. */
144 struct linebuffer *thisline[2];
146 /* streams[i] holds the input stream for file i. */
147 FILE *streams[2];
149 int i, ret = 0;
151 /* Initialize the storage. */
152 for (i = 0; i < 2; i++)
154 initbuffer (&lb1[i]);
155 thisline[i] = &lb1[i];
156 streams[i] = (STREQ (infiles[i], "-") ? stdin : fopen (infiles[i], "r"));
157 if (!streams[i])
159 error (0, errno, "%s", infiles[i]);
160 return 1;
163 thisline[i] = readline (thisline[i], streams[i]);
166 while (thisline[0] || thisline[1])
168 int order;
170 /* Compare the next available lines of the two files. */
172 if (!thisline[0])
173 order = 1;
174 else if (!thisline[1])
175 order = -1;
176 else
178 #ifdef ENABLE_NLS
179 if (hard_LC_COLLATE)
180 order = memcoll (thisline[0]->buffer, thisline[0]->length - 1,
181 thisline[1]->buffer, thisline[1]->length - 1);
182 else
183 #endif
185 size_t len = min (thisline[0]->length, thisline[1]->length) - 1;
186 order = memcmp (thisline[0]->buffer, thisline[1]->buffer, len);
187 if (order == 0)
188 order = thisline[0]->length - thisline[1]->length;
192 /* Output the line that is lesser. */
193 if (order == 0)
194 writeline (thisline[1], stdout, 3);
195 else if (order > 0)
196 writeline (thisline[1], stdout, 2);
197 else
198 writeline (thisline[0], stdout, 1);
200 /* Step the file the line came from.
201 If the files match, step both files. */
202 if (order >= 0)
203 thisline[1] = readline (thisline[1], streams[1]);
204 if (order <= 0)
205 thisline[0] = readline (thisline[0], streams[0]);
208 /* Free all storage and close all input streams. */
209 for (i = 0; i < 2; i++)
211 free (lb1[i].buffer);
212 if (ferror (streams[i]) || fclose (streams[i]) == EOF)
214 error (0, errno, "%s", infiles[i]);
215 ret = 1;
218 if (ferror (stdout) || fclose (stdout) == EOF)
220 error (0, errno, _("write error"));
221 ret = 1;
223 return ret;
227 main (int argc, char **argv)
229 int c;
231 program_name = argv[0];
232 setlocale (LC_ALL, "");
233 bindtextdomain (PACKAGE, LOCALEDIR);
234 textdomain (PACKAGE);
236 #ifdef ENABLE_NLS
237 hard_LC_COLLATE = hard_locale (LC_COLLATE);
238 #endif
240 only_file_1 = 1;
241 only_file_2 = 1;
242 both = 1;
244 while ((c = getopt_long (argc, argv, "123", long_options, NULL)) != -1)
245 switch (c)
247 case 0:
248 break;
250 case '1':
251 only_file_1 = 0;
252 break;
254 case '2':
255 only_file_2 = 0;
256 break;
258 case '3':
259 both = 0;
260 break;
262 case_GETOPT_HELP_CHAR;
264 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
266 default:
267 usage (1);
270 if (optind + 2 != argc)
271 usage (1);
273 exit (compare_files (argv + optind) == 0
274 ? EXIT_SUCCESS : EXIT_FAILURE);