1 /* comm -- compare two sorted files line by line.
2 Copyright (C) 86, 90, 91, 1995-2004 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)
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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Richard Stallman and David MacKenzie. */
24 #include <sys/types.h>
26 #include "linebuffer.h"
28 #include "hard-locale.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "comm"
35 #define AUTHORS "Richard Stallman", "David MacKenzie"
37 /* Undefine, to avoid warning about redefinition on some systems. */
39 #define min(x, y) ((x) < (y) ? (x) : (y))
41 /* The name this program was run with. */
44 /* True if the LC_COLLATE locale is hard. */
45 static bool hard_LC_COLLATE
;
47 /* If true, print lines that are found only in file 1. */
48 static bool only_file_1
;
50 /* If true, print lines that are found only in file 2. */
51 static bool only_file_2
;
53 /* If true, print lines that are found in both files. */
56 static struct option
const long_options
[] =
58 {GETOPT_HELP_OPTION_DECL
},
59 {GETOPT_VERSION_OPTION_DECL
},
68 if (status
!= EXIT_SUCCESS
)
69 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
74 Usage: %s [OPTION]... FILE1 FILE2\n\
78 Compare sorted files FILE1 and FILE2 line by line.\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\
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\
92 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
93 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
94 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
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. */
105 writeline (const struct linebuffer
*line
, FILE *stream
, int class)
117 /* Print a TAB if we are printing lines from file 1. */
125 /* Print a TAB if we are printing lines from file 1. */
128 /* Print a TAB if we are printing lines from file 2. */
134 fwrite (line
->buffer
, sizeof (char), line
->length
, stream
);
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 Return true if successful. */
144 compare_files (char **infiles
)
146 /* For each file, we have one linebuffer in lb1. */
147 struct linebuffer lb1
[2];
149 /* thisline[i] points to the linebuffer holding the next available line
150 in file i, or is NULL if there are no lines left in that file. */
151 struct linebuffer
*thisline
[2];
153 /* streams[i] holds the input stream for file i. */
156 /* errno values for each stream. */
162 /* Initialize the storage. */
163 for (i
= 0; i
< 2; i
++)
165 initbuffer (&lb1
[i
]);
166 thisline
[i
] = &lb1
[i
];
167 streams
[i
] = (STREQ (infiles
[i
], "-") ? stdin
: fopen (infiles
[i
], "r"));
170 error (0, errno
, "%s", infiles
[i
]);
174 thisline
[i
] = readlinebuffer (thisline
[i
], streams
[i
]);
175 saved_errno
[i
] = errno
;
178 while (thisline
[0] || thisline
[1])
182 /* Compare the next available lines of the two files. */
186 else if (!thisline
[1])
191 order
= xmemcoll (thisline
[0]->buffer
, thisline
[0]->length
- 1,
192 thisline
[1]->buffer
, thisline
[1]->length
- 1);
195 size_t len
= min (thisline
[0]->length
, thisline
[1]->length
) - 1;
196 order
= memcmp (thisline
[0]->buffer
, thisline
[1]->buffer
, len
);
198 order
= (thisline
[0]->length
< thisline
[1]->length
200 : thisline
[0]->length
!= thisline
[1]->length
);
204 /* Output the line that is lesser. */
206 writeline (thisline
[1], stdout
, 3);
208 writeline (thisline
[1], stdout
, 2);
210 writeline (thisline
[0], stdout
, 1);
212 /* Step the file the line came from.
213 If the files match, step both files. */
216 thisline
[1] = readlinebuffer (thisline
[1], streams
[1]);
217 saved_errno
[1] = errno
;
221 thisline
[0] = readlinebuffer (thisline
[0], streams
[0]);
222 saved_errno
[0] = errno
;
226 /* Free all storage and close all input streams. */
227 for (i
= 0; i
< 2; i
++)
229 free (lb1
[i
].buffer
);
230 if (ferror (streams
[i
]))
232 error (0, saved_errno
[i
], "%s", infiles
[i
]);
235 if (fclose (streams
[i
]) != 0)
237 error (0, errno
, "%s", infiles
[i
]);
245 main (int argc
, char **argv
)
249 initialize_main (&argc
, &argv
);
250 program_name
= argv
[0];
251 setlocale (LC_ALL
, "");
252 bindtextdomain (PACKAGE
, LOCALEDIR
);
253 textdomain (PACKAGE
);
254 hard_LC_COLLATE
= hard_locale (LC_COLLATE
);
256 atexit (close_stdout
);
262 while ((c
= getopt_long (argc
, argv
, "123", long_options
, NULL
)) != -1)
277 case_GETOPT_HELP_CHAR
;
279 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
282 usage (EXIT_FAILURE
);
285 if (argc
- optind
< 2)
288 error (0, 0, _("missing operand"));
290 error (0, 0, _("missing operand after %s"), quote (argv
[argc
- 1]));
291 usage (EXIT_FAILURE
);
294 if (2 < argc
- optind
)
296 error (0, 0, _("extra operand %s"), quote (argv
[optind
+ 2]));
297 usage (EXIT_FAILURE
);
300 exit (compare_files (argv
+ optind
)
301 ? EXIT_SUCCESS
: EXIT_FAILURE
);