Factor out some common strings to make translation easier.
[platform/upstream/coreutils.git] / src / comm.c
1 /* comm -- compare two sorted files line by line.
2    Copyright (C) 86, 90, 91, 1995-2001 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by Richard Stallman and David MacKenzie. */
19 \f
20 #include <config.h>
21
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"
31
32 /* The official name of this program (e.g., no `g' prefix).  */
33 #define PROGRAM_NAME "comm"
34
35 #define AUTHORS N_ ("Richard Stallman and 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 #ifdef ENABLE_NLS
45 /* Nonzero if the LC_COLLATE locale is hard.  */
46 static int hard_LC_COLLATE;
47 #endif
48
49 /* If nonzero, print lines that are found only in file 1. */
50 static int only_file_1;
51
52 /* If nonzero, print lines that are found only in file 2. */
53 static int only_file_2;
54
55 /* If nonzero, print lines that are found in both files. */
56 static int both;
57
58 static struct option const long_options[] =
59 {
60   {GETOPT_HELP_OPTION_DECL},
61   {GETOPT_VERSION_OPTION_DECL},
62   {0, 0, 0, 0}
63 };
64
65 \f
66
67 void
68 usage (int status)
69 {
70   if (status != 0)
71     fprintf (stderr, _("Try `%s --help' for more information.\n"),
72              program_name);
73   else
74     {
75       printf (_("\
76 Usage: %s [OPTION]... LEFT_FILE RIGHT_FILE\n\
77 "),
78               program_name);
79       fputs (_("\
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 that appear in both files\n\
85 "), stdout);
86       fputs (_("\
87       --help      display this help and exit\n\
88       --version   output version information and exit\n\
89 "), stdout);
90       puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
91     }
92   exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
93 }
94
95 /* Output the line in linebuffer LINE to stream STREAM
96    provided the switches say it should be output.
97    CLASS is 1 for a line found only in file 1,
98    2 for a line only in file 2, 3 for a line in both. */
99
100 static void
101 writeline (const struct linebuffer *line, FILE *stream, int class)
102 {
103   switch (class)
104     {
105     case 1:
106       if (!only_file_1)
107         return;
108       break;
109
110     case 2:
111       if (!only_file_2)
112         return;
113       /* Print a TAB if we are printing lines from file 1.  */
114       if (only_file_1)
115         putc ('\t', stream);
116       break;
117
118     case 3:
119       if (!both)
120         return;
121       /* Print a TAB if we are printing lines from file 1.  */
122       if (only_file_1)
123         putc ('\t', stream);
124       /* Print a TAB if we are printing lines from file 2.  */
125       if (only_file_2)
126         putc ('\t', stream);
127       break;
128     }
129
130   fwrite (line->buffer, sizeof (char), line->length, stream);
131 }
132
133 /* Compare INFILES[0] and INFILES[1].
134    If either is "-", use the standard input for that file.
135    Assume that each input file is sorted;
136    merge them and output the result.
137    Return 0 if successful, 1 if any errors occur. */
138
139 static int
140 compare_files (char **infiles)
141 {
142   /* For each file, we have one linebuffer in lb1.  */
143   struct linebuffer lb1[2];
144
145   /* thisline[i] points to the linebuffer holding the next available line
146      in file i, or is NULL if there are no lines left in that file.  */
147   struct linebuffer *thisline[2];
148
149   /* streams[i] holds the input stream for file i.  */
150   FILE *streams[2];
151
152   int i, ret = 0;
153
154   /* Initialize the storage. */
155   for (i = 0; i < 2; i++)
156     {
157       initbuffer (&lb1[i]);
158       thisline[i] = &lb1[i];
159       streams[i] = (STREQ (infiles[i], "-") ? stdin : fopen (infiles[i], "r"));
160       if (!streams[i])
161         {
162           error (0, errno, "%s", infiles[i]);
163           return 1;
164         }
165
166       thisline[i] = readline (thisline[i], streams[i]);
167     }
168
169   while (thisline[0] || thisline[1])
170     {
171       int order;
172
173       /* Compare the next available lines of the two files.  */
174
175       if (!thisline[0])
176         order = 1;
177       else if (!thisline[1])
178         order = -1;
179       else
180         {
181 #ifdef ENABLE_NLS
182           if (hard_LC_COLLATE)
183             order = memcoll (thisline[0]->buffer, thisline[0]->length - 1,
184                              thisline[1]->buffer, thisline[1]->length - 1);
185           else
186 #endif
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             }
193         }
194
195       /* Output the line that is lesser. */
196       if (order == 0)
197         writeline (thisline[1], stdout, 3);
198       else if (order > 0)
199         writeline (thisline[1], stdout, 2);
200       else
201         writeline (thisline[0], stdout, 1);
202
203       /* Step the file the line came from.
204          If the files match, step both files.  */
205       if (order >= 0)
206         thisline[1] = readline (thisline[1], streams[1]);
207       if (order <= 0)
208         thisline[0] = readline (thisline[0], streams[0]);
209     }
210
211   /* Free all storage and close all input streams. */
212   for (i = 0; i < 2; i++)
213     {
214       free (lb1[i].buffer);
215       if (ferror (streams[i]) || fclose (streams[i]) == EOF)
216         {
217           error (0, errno, "%s", infiles[i]);
218           ret = 1;
219         }
220     }
221   return ret;
222 }
223
224 int
225 main (int argc, char **argv)
226 {
227   int c;
228
229   program_name = argv[0];
230   setlocale (LC_ALL, "");
231   bindtextdomain (PACKAGE, LOCALEDIR);
232   textdomain (PACKAGE);
233
234   atexit (close_stdout);
235
236 #ifdef ENABLE_NLS
237   hard_LC_COLLATE = hard_locale (LC_COLLATE);
238 #endif
239
240   only_file_1 = 1;
241   only_file_2 = 1;
242   both = 1;
243
244   while ((c = getopt_long (argc, argv, "123", long_options, NULL)) != -1)
245     switch (c)
246       {
247       case 0:
248         break;
249
250       case '1':
251         only_file_1 = 0;
252         break;
253
254       case '2':
255         only_file_2 = 0;
256         break;
257
258       case '3':
259         both = 0;
260         break;
261
262       case_GETOPT_HELP_CHAR;
263
264       case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
265
266       default:
267         usage (1);
268       }
269
270   if (optind + 2 != argc)
271     usage (1);
272
273   exit (compare_files (argv + optind) == 0
274         ? EXIT_SUCCESS : EXIT_FAILURE);
275 }