.
[coreutils.git] / src / comm.c
blob94606ad009b899bb2dab5a0354e14ecee1c75d7c
1 /* comm -- compare two sorted files line by line.
2 Copyright (C) 1986, 1990, 1991, 1995 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
16 Foundation, 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 "version.h"
28 #include "error.h"
30 /* Undefine, to avoid warning about redefinition on some systems. */
31 #undef min
32 #define min(x, y) ((x) < (y) ? (x) : (y))
34 /* The name this program was run with. */
35 char *program_name;
37 /* If nonzero, print lines that are found only in file 1. */
38 static int only_file_1;
40 /* If nonzero, print lines that are found only in file 2. */
41 static int only_file_2;
43 /* If nonzero, print lines that are found in both files. */
44 static int both;
46 /* If non-zero, display usage information and exit. */
47 static int show_help;
49 /* If non-zero, print the version on standard output then exit. */
50 static int show_version;
52 static struct option const long_options[] =
54 {"help", no_argument, &show_help, 1},
55 {"version", no_argument, &show_version, 1},
56 {0, 0, 0, 0}
61 static void
62 usage (status)
63 int status;
65 if (status != 0)
66 fprintf (stderr, "Try `%s --help' for more information.\n",
67 program_name);
68 else
70 printf ("\
71 Usage: %s [OPTION]... LEFT_FILE RIGHT_FILE\n\
73 program_name);
74 printf ("\
75 Compare sorted files LEFT_FILE and RIGHT_FILE line by line.\n\
76 \n\
77 -1 suppress lines unique to left file\n\
78 -2 suppress lines unique to right file\n\
79 -3 suppress lines unique to both files\n\
80 --help display this help and exit\n\
81 --version output version information and exit\n\
82 ");
84 exit (status);
87 /* Output the line in linebuffer LINE to stream STREAM
88 provided the switches say it should be output.
89 CLASS is 1 for a line found only in file 1,
90 2 for a line only in file 2, 3 for a line in both. */
92 static void
93 writeline (line, stream, class)
94 struct linebuffer *line;
95 FILE *stream;
96 int class;
98 switch (class)
100 case 1:
101 if (!only_file_1)
102 return;
103 break;
105 case 2:
106 if (!only_file_2)
107 return;
108 /* Skip the tab stop for case 1, if we are printing case 1. */
109 if (only_file_1)
110 putc ('\t', stream);
111 break;
113 case 3:
114 if (!both)
115 return;
116 /* Skip the tab stop for case 1, if we are printing case 1. */
117 if (only_file_1)
118 putc ('\t', stream);
119 /* Skip the tab stop for case 2, if we are printing case 2. */
120 if (only_file_2)
121 putc ('\t', stream);
122 break;
125 fwrite (line->buffer, sizeof (char), line->length, stream);
126 putc ('\n', 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 (infiles)
137 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] = strcmp (infiles[i], "-")
157 ? fopen (infiles[i], "r") : stdin;
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 /* Cannot use bcmp -- it only returns a boolean value. */
180 order = memcmp (thisline[0]->buffer, thisline[1]->buffer,
181 min (thisline[0]->length, thisline[1]->length));
182 if (order == 0)
183 order = thisline[0]->length - thisline[1]->length;
186 /* Output the line that is lesser. */
187 if (order == 0)
188 writeline (thisline[1], stdout, 3);
189 else if (order > 0)
190 writeline (thisline[1], stdout, 2);
191 else
192 writeline (thisline[0], stdout, 1);
194 /* Step the file the line came from.
195 If the files match, step both files. */
196 if (order >= 0)
197 thisline[1] = readline (thisline[1], streams[1]);
198 if (order <= 0)
199 thisline[0] = readline (thisline[0], streams[0]);
202 /* Free all storage and close all input streams. */
203 for (i = 0; i < 2; i++)
205 free (lb1[i].buffer);
206 if (ferror (streams[i]) || fclose (streams[i]) == EOF)
208 error (0, errno, "%s", infiles[i]);
209 ret = 1;
212 if (ferror (stdout) || fclose (stdout) == EOF)
214 error (0, errno, "write error");
215 ret = 1;
217 return ret;
220 void
221 main (argc, argv)
222 int argc;
223 char *argv[];
225 int c;
227 program_name = argv[0];
229 only_file_1 = 1;
230 only_file_2 = 1;
231 both = 1;
233 while ((c = getopt_long (argc, argv, "123", long_options, (int *) 0)) != EOF)
234 switch (c)
236 case 0:
237 break;
239 case '1':
240 only_file_1 = 0;
241 break;
243 case '2':
244 only_file_2 = 0;
245 break;
247 case '3':
248 both = 0;
249 break;
251 default:
252 usage (1);
255 if (show_version)
257 printf ("comm - %s\n", version_string);
258 exit (0);
261 if (show_help)
262 usage (0);
264 if (optind + 2 != argc)
265 usage (1);
267 exit (compare_files (argv + optind));