Tizen 2.0 Release
[external/tizen-coreutils.git] / src / comm.c
1 /* comm -- compare two sorted files line by line.
2    Copyright (C) 86, 90, 91, 1995-2005 Free Software Foundation, Inc.
3
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.
8
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.
13
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Richard Stallman and David MacKenzie. */
19 \f
20 #include <config.h>
21
22 #include <getopt.h>
23 #include <sys/types.h>
24 #include "system.h"
25 #include "linebuffer.h"
26 #include "error.h"
27 #include "hard-locale.h"
28 #include "quote.h"
29 #include "stdio--.h"
30 #include "xmemcoll.h"
31
32 /* The official name of this program (e.g., no `g' prefix).  */
33 #define PROGRAM_NAME "comm"
34
35 #define AUTHORS "Richard Stallman", "David MacKenzie"
36
37 /* Undefine, to avoid warning about redefinition on some systems.  */
38 #undef min
39 #define min(x, y) ((x) < (y) ? (x) : (y))
40
41 /* The name this program was run with. */
42 char *program_name;
43
44 /* True if the LC_COLLATE locale is hard.  */
45 static bool hard_LC_COLLATE;
46
47 /* If true, print lines that are found only in file 1. */
48 static bool only_file_1;
49
50 /* If true, print lines that are found only in file 2. */
51 static bool only_file_2;
52
53 /* If true, print lines that are found in both files. */
54 static bool both;
55
56 static struct option const long_options[] =
57 {
58   {GETOPT_HELP_OPTION_DECL},
59   {GETOPT_VERSION_OPTION_DECL},
60   {NULL, 0, NULL, 0}
61 };
62
63 \f
64
65 void
66 usage (int status)
67 {
68   if (status != EXIT_SUCCESS)
69     fprintf (stderr, _("Try `%s --help' for more information.\n"),
70              program_name);
71   else
72     {
73       printf (_("\
74 Usage: %s [OPTION]... FILE1 FILE2\n\
75 "),
76               program_name);
77       fputs (_("\
78 Compare sorted files FILE1 and FILE2 line by line.\n\
79 "), stdout);
80       fputs (_("\
81 \n\
82 With no options, produce three-column output.  Column one contains\n\
83 lines unique to FILE1, column two contains lines unique to FILE2,\n\
84 and column three contains lines common to both files.\n\
85 "), stdout);
86       fputs (_("\
87 \n\
88   -1              suppress lines unique to FILE1\n\
89   -2              suppress lines unique to FILE2\n\
90   -3              suppress lines that appear in both files\n\
91 "), stdout);
92       fputs (HELP_OPTION_DESCRIPTION, stdout);
93       fputs (VERSION_OPTION_DESCRIPTION, stdout);
94       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
95     }
96   exit (status);
97 }
98
99 /* Output the line in linebuffer LINE to stream STREAM
100    provided the switches say it should be output.
101    CLASS is 1 for a line found only in file 1,
102    2 for a line only in file 2, 3 for a line in both. */
103
104 static void
105 writeline (const struct linebuffer *line, FILE *stream, int class)
106 {
107   switch (class)
108     {
109     case 1:
110       if (!only_file_1)
111         return;
112       break;
113
114     case 2:
115       if (!only_file_2)
116         return;
117       /* Print a TAB if we are printing lines from file 1.  */
118       if (only_file_1)
119         putc ('\t', stream);
120       break;
121
122     case 3:
123       if (!both)
124         return;
125       /* Print a TAB if we are printing lines from file 1.  */
126       if (only_file_1)
127         putc ('\t', stream);
128       /* Print a TAB if we are printing lines from file 2.  */
129       if (only_file_2)
130         putc ('\t', stream);
131       break;
132     }
133
134   fwrite (line->buffer, sizeof (char), line->length, stream);
135 }
136
137 /* Compare INFILES[0] and INFILES[1].
138    If either is "-", use the standard input for that file.
139    Assume that each input file is sorted;
140    merge them and output the result.  */
141
142 static void
143 compare_files (char **infiles)
144 {
145   /* For each file, we have one linebuffer in lb1.  */
146   struct linebuffer lb1[2];
147
148   /* thisline[i] points to the linebuffer holding the next available line
149      in file i, or is NULL if there are no lines left in that file.  */
150   struct linebuffer *thisline[2];
151
152   /* streams[i] holds the input stream for file i.  */
153   FILE *streams[2];
154
155   int i;
156
157   /* Initialize the storage. */
158   for (i = 0; i < 2; i++)
159     {
160       initbuffer (&lb1[i]);
161       thisline[i] = &lb1[i];
162       streams[i] = (STREQ (infiles[i], "-") ? stdin : fopen (infiles[i], "r"));
163       if (!streams[i])
164         error (EXIT_FAILURE, errno, "%s", infiles[i]);
165
166       thisline[i] = readlinebuffer (thisline[i], streams[i]);
167       if (ferror (streams[i]))
168         error (EXIT_FAILURE, errno, "%s", infiles[i]);
169     }
170
171   while (thisline[0] || thisline[1])
172     {
173       int order;
174
175       /* Compare the next available lines of the two files.  */
176
177       if (!thisline[0])
178         order = 1;
179       else if (!thisline[1])
180         order = -1;
181       else
182         {
183           if (hard_LC_COLLATE)
184             order = xmemcoll (thisline[0]->buffer, thisline[0]->length - 1,
185                               thisline[1]->buffer, thisline[1]->length - 1);
186           else
187             {
188               size_t len = min (thisline[0]->length, thisline[1]->length) - 1;
189               order = memcmp (thisline[0]->buffer, thisline[1]->buffer, len);
190               if (order == 0)
191                 order = (thisline[0]->length < thisline[1]->length
192                          ? -1
193                          : thisline[0]->length != thisline[1]->length);
194             }
195         }
196
197       /* Output the line that is lesser. */
198       if (order == 0)
199         writeline (thisline[1], stdout, 3);
200       else if (order > 0)
201         writeline (thisline[1], stdout, 2);
202       else
203         writeline (thisline[0], stdout, 1);
204
205       /* Step the file the line came from.
206          If the files match, step both files.  */
207       if (order >= 0)
208         {
209           thisline[1] = readlinebuffer (thisline[1], streams[1]);
210           if (ferror (streams[1]))
211             error (EXIT_FAILURE, errno, "%s", infiles[1]);
212         }
213       if (order <= 0)
214         {
215           thisline[0] = readlinebuffer (thisline[0], streams[0]);
216           if (ferror (streams[0]))
217             error (EXIT_FAILURE, errno, "%s", infiles[0]);
218         }
219     }
220
221   for (i = 0; i < 2; i++)
222     if (fclose (streams[i]) != 0)
223       error (EXIT_FAILURE, errno, "%s", infiles[i]);
224 }
225
226 int
227 main (int argc, char **argv)
228 {
229   int c;
230
231   initialize_main (&argc, &argv);
232   program_name = argv[0];
233   setlocale (LC_ALL, "");
234   bindtextdomain (PACKAGE, LOCALEDIR);
235   textdomain (PACKAGE);
236   hard_LC_COLLATE = hard_locale (LC_COLLATE);
237
238   atexit (close_stdout);
239
240   only_file_1 = true;
241   only_file_2 = true;
242   both = true;
243
244   while ((c = getopt_long (argc, argv, "123", long_options, NULL)) != -1)
245     switch (c)
246       {
247       case '1':
248         only_file_1 = false;
249         break;
250
251       case '2':
252         only_file_2 = false;
253         break;
254
255       case '3':
256         both = false;
257         break;
258
259       case_GETOPT_HELP_CHAR;
260
261       case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
262
263       default:
264         usage (EXIT_FAILURE);
265       }
266
267   if (argc - optind < 2)
268     {
269       if (argc <= optind)
270         error (0, 0, _("missing operand"));
271       else
272         error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
273       usage (EXIT_FAILURE);
274     }
275
276   if (2 < argc - optind)
277     {
278       error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
279       usage (EXIT_FAILURE);
280     }
281
282   compare_files (argv + optind);
283
284   exit (EXIT_SUCCESS);
285 }