.
[coreutils.git] / src / comm.c
blobe9db88874713cc3f493aa5d7cc4b87ccea12f5d4
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 nonzero, display usage information and exit. */
47 static int show_help;
49 /* If nonzero, 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 (int status)
64 if (status != 0)
65 fprintf (stderr, _("Try `%s --help' for more information.\n"),
66 program_name);
67 else
69 printf (_("\
70 Usage: %s [OPTION]... LEFT_FILE RIGHT_FILE\n\
71 "),
72 program_name);
73 printf (_("\
74 Compare sorted files LEFT_FILE and RIGHT_FILE line by line.\n\
75 \n\
76 -1 suppress lines unique to left file\n\
77 -2 suppress lines unique to right file\n\
78 -3 suppress lines unique to both files\n\
79 --help display this help and exit\n\
80 --version output version information and exit\n\
81 "));
83 exit (status);
86 /* Output the line in linebuffer LINE to stream STREAM
87 provided the switches say it should be output.
88 CLASS is 1 for a line found only in file 1,
89 2 for a line only in file 2, 3 for a line in both. */
91 static void
92 writeline (struct linebuffer *line, FILE *stream, int class)
94 switch (class)
96 case 1:
97 if (!only_file_1)
98 return;
99 break;
101 case 2:
102 if (!only_file_2)
103 return;
104 /* Skip the tab stop for case 1, if we are printing case 1. */
105 if (only_file_1)
106 putc ('\t', stream);
107 break;
109 case 3:
110 if (!both)
111 return;
112 /* Skip the tab stop for case 1, if we are printing case 1. */
113 if (only_file_1)
114 putc ('\t', stream);
115 /* Skip the tab stop for case 2, if we are printing case 2. */
116 if (only_file_2)
117 putc ('\t', stream);
118 break;
121 fwrite (line->buffer, sizeof (char), line->length, stream);
122 putc ('\n', stream);
125 /* Compare INFILES[0] and INFILES[1].
126 If either is "-", use the standard input for that file.
127 Assume that each input file is sorted;
128 merge them and output the result.
129 Return 0 if successful, 1 if any errors occur. */
131 static int
132 compare_files (char **infiles)
134 /* For each file, we have one linebuffer in lb1. */
135 struct linebuffer lb1[2];
137 /* thisline[i] points to the linebuffer holding the next available line
138 in file i, or is NULL if there are no lines left in that file. */
139 struct linebuffer *thisline[2];
141 /* streams[i] holds the input stream for file i. */
142 FILE *streams[2];
144 int i, ret = 0;
146 /* Initialize the storage. */
147 for (i = 0; i < 2; i++)
149 initbuffer (&lb1[i]);
150 thisline[i] = &lb1[i];
151 streams[i] = strcmp (infiles[i], "-")
152 ? fopen (infiles[i], "r") : stdin;
153 if (!streams[i])
155 error (0, errno, "%s", infiles[i]);
156 return 1;
159 thisline[i] = readline (thisline[i], streams[i]);
162 while (thisline[0] || thisline[1])
164 int order;
166 /* Compare the next available lines of the two files. */
168 if (!thisline[0])
169 order = 1;
170 else if (!thisline[1])
171 order = -1;
172 else
174 /* Cannot use bcmp -- it only returns a boolean value. */
175 order = memcmp (thisline[0]->buffer, thisline[1]->buffer,
176 min (thisline[0]->length, thisline[1]->length));
177 if (order == 0)
178 order = thisline[0]->length - thisline[1]->length;
181 /* Output the line that is lesser. */
182 if (order == 0)
183 writeline (thisline[1], stdout, 3);
184 else if (order > 0)
185 writeline (thisline[1], stdout, 2);
186 else
187 writeline (thisline[0], stdout, 1);
189 /* Step the file the line came from.
190 If the files match, step both files. */
191 if (order >= 0)
192 thisline[1] = readline (thisline[1], streams[1]);
193 if (order <= 0)
194 thisline[0] = readline (thisline[0], streams[0]);
197 /* Free all storage and close all input streams. */
198 for (i = 0; i < 2; i++)
200 free (lb1[i].buffer);
201 if (ferror (streams[i]) || fclose (streams[i]) == EOF)
203 error (0, errno, "%s", infiles[i]);
204 ret = 1;
207 if (ferror (stdout) || fclose (stdout) == EOF)
209 error (0, errno, _("write error"));
210 ret = 1;
212 return ret;
215 void
216 main (int argc, char **argv)
218 int c;
220 program_name = argv[0];
221 setlocale (LC_ALL, "");
222 bindtextdomain (PACKAGE, LOCALEDIR);
223 textdomain (PACKAGE);
225 only_file_1 = 1;
226 only_file_2 = 1;
227 both = 1;
229 while ((c = getopt_long (argc, argv, "123", long_options, (int *) 0)) != EOF)
230 switch (c)
232 case 0:
233 break;
235 case '1':
236 only_file_1 = 0;
237 break;
239 case '2':
240 only_file_2 = 0;
241 break;
243 case '3':
244 both = 0;
245 break;
247 default:
248 usage (1);
251 if (show_version)
253 printf ("comm - %s\n", version_string);
254 exit (0);
257 if (show_help)
258 usage (0);
260 if (optind + 2 != argc)
261 usage (1);
263 exit (compare_files (argv + optind));