Use PROGRAM_NAME in place of string in parse_long_options call.
[platform/upstream/coreutils.git] / src / comm.c
1 /* comm -- compare two sorted files line by line.
2    Copyright (C) 86, 90, 91, 1995-1999 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 "linebuffer.h"
27 #include "long-options.h"
28 #include "error.h"
29
30 /* The official name of this program (e.g., no `g' prefix).  */
31 #define PROGRAM_NAME "comm"
32
33 /* Undefine, to avoid warning about redefinition on some systems.  */
34 #undef min
35 #define min(x, y) ((x) < (y) ? (x) : (y))
36
37 /* The name this program was run with. */
38 char *program_name;
39
40 /* If nonzero, print lines that are found only in file 1. */
41 static int only_file_1;
42
43 /* If nonzero, print lines that are found only in file 2. */
44 static int only_file_2;
45
46 /* If nonzero, print lines that are found in both files. */
47 static int both;
48
49 static struct option const long_options[] =
50 {
51   {0, 0, 0, 0}
52 };
53
54 \f
55
56 void
57 usage (int status)
58 {
59   if (status != 0)
60     fprintf (stderr, _("Try `%s --help' for more information.\n"),
61              program_name);
62   else
63     {
64       printf (_("\
65 Usage: %s [OPTION]... LEFT_FILE RIGHT_FILE\n\
66 "),
67               program_name);
68       printf (_("\
69 Compare sorted files LEFT_FILE and RIGHT_FILE line by line.\n\
70 \n\
71   -1              suppress lines unique to left file\n\
72   -2              suppress lines unique to right file\n\
73   -3              suppress lines unique to both files\n\
74       --help      display this help and exit\n\
75       --version   output version information and exit\n\
76 "));
77       puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
78     }
79   exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
80 }
81
82 /* Output the line in linebuffer LINE to stream STREAM
83    provided the switches say it should be output.
84    CLASS is 1 for a line found only in file 1,
85    2 for a line only in file 2, 3 for a line in both. */
86
87 static void
88 writeline (struct linebuffer *line, FILE *stream, int class)
89 {
90   switch (class)
91     {
92     case 1:
93       if (!only_file_1)
94         return;
95       break;
96
97     case 2:
98       if (!only_file_2)
99         return;
100       /* Skip the tab stop for case 1, if we are printing case 1.  */
101       if (only_file_1)
102         putc ('\t', stream);
103       break;
104
105     case 3:
106       if (!both)
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       /* Skip the tab stop for case 2, if we are printing case 2.  */
112       if (only_file_2)
113         putc ('\t', stream);
114       break;
115     }
116
117   fwrite (line->buffer, sizeof (char), line->length, stream);
118   putc ('\n', stream);
119 }
120
121 /* Compare INFILES[0] and INFILES[1].
122    If either is "-", use the standard input for that file.
123    Assume that each input file is sorted;
124    merge them and output the result.
125    Return 0 if successful, 1 if any errors occur. */
126
127 static int
128 compare_files (char **infiles)
129 {
130   /* For each file, we have one linebuffer in lb1.  */
131   struct linebuffer lb1[2];
132
133   /* thisline[i] points to the linebuffer holding the next available line
134      in file i, or is NULL if there are no lines left in that file.  */
135   struct linebuffer *thisline[2];
136
137   /* streams[i] holds the input stream for file i.  */
138   FILE *streams[2];
139
140   int i, ret = 0;
141
142   /* Initialize the storage. */
143   for (i = 0; i < 2; i++)
144     {
145       initbuffer (&lb1[i]);
146       thisline[i] = &lb1[i];
147       streams[i] = (STREQ (infiles[i], "-") ? stdin : fopen (infiles[i], "r"));
148       if (!streams[i])
149         {
150           error (0, errno, "%s", infiles[i]);
151           return 1;
152         }
153
154       thisline[i] = readline (thisline[i], streams[i]);
155     }
156
157   while (thisline[0] || thisline[1])
158     {
159       int order;
160
161       /* Compare the next available lines of the two files.  */
162
163       if (!thisline[0])
164         order = 1;
165       else if (!thisline[1])
166         order = -1;
167       else
168         {
169           /* Cannot use bcmp -- it only returns a boolean value. */
170           order = memcmp (thisline[0]->buffer, thisline[1]->buffer,
171                           min (thisline[0]->length, thisline[1]->length));
172           if (order == 0)
173             order = thisline[0]->length - thisline[1]->length;
174         }
175
176       /* Output the line that is lesser. */
177       if (order == 0)
178         writeline (thisline[1], stdout, 3);
179       else if (order > 0)
180         writeline (thisline[1], stdout, 2);
181       else
182         writeline (thisline[0], stdout, 1);
183
184       /* Step the file the line came from.
185          If the files match, step both files.  */
186       if (order >= 0)
187         thisline[1] = readline (thisline[1], streams[1]);
188       if (order <= 0)
189         thisline[0] = readline (thisline[0], streams[0]);
190     }
191
192   /* Free all storage and close all input streams. */
193   for (i = 0; i < 2; i++)
194     {
195       free (lb1[i].buffer);
196       if (ferror (streams[i]) || fclose (streams[i]) == EOF)
197         {
198           error (0, errno, "%s", infiles[i]);
199           ret = 1;
200         }
201     }
202   if (ferror (stdout) || fclose (stdout) == EOF)
203     {
204       error (0, errno, _("write error"));
205       ret = 1;
206     }
207   return ret;
208 }
209
210 int
211 main (int argc, char **argv)
212 {
213   int c;
214
215   program_name = argv[0];
216   setlocale (LC_ALL, "");
217   bindtextdomain (PACKAGE, LOCALEDIR);
218   textdomain (PACKAGE);
219
220   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
221                       "Richard Stallman and David MacKenzie", usage);
222
223   only_file_1 = 1;
224   only_file_2 = 1;
225   both = 1;
226
227   while ((c = getopt_long (argc, argv, "123", long_options, NULL)) != -1)
228     switch (c)
229       {
230       case 0:
231         break;
232
233       case '1':
234         only_file_1 = 0;
235         break;
236
237       case '2':
238         only_file_2 = 0;
239         break;
240
241       case '3':
242         both = 0;
243         break;
244
245       default:
246         usage (1);
247       }
248
249   if (optind + 2 != argc)
250     usage (1);
251
252   exit (compare_files (argv + optind) == 0
253         ? EXIT_SUCCESS : EXIT_FAILURE);
254 }