1 /* comm -- compare two sorted files line by line.
2 Copyright (C) 1986-2024 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 3 of the License, or
7 (at your option) 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, see <https://www.gnu.org/licenses/>. */
17 /* Written by Richard Stallman and David MacKenzie. */
22 #include <sys/types.h>
24 #include "linebuffer.h"
26 #include "hard-locale.h"
32 /* The official name of this program (e.g., no 'g' prefix). */
33 #define PROGRAM_NAME "comm"
36 proper_name ("Richard M. Stallman"), \
37 proper_name ("David MacKenzie")
39 /* True if the LC_COLLATE locale is hard. */
40 static bool hard_LC_COLLATE
;
42 /* If true, print lines that are found only in file 1. */
43 static bool only_file_1
;
45 /* If true, print lines that are found only in file 2. */
46 static bool only_file_2
;
48 /* If true, print lines that are found in both files. */
51 /* If nonzero, we have seen at least one unpairable line. */
52 static bool seen_unpairable
;
54 /* If nonzero, we have warned about disorder in that file. */
55 static bool issued_disorder_warning
[2];
58 static unsigned char delim
= '\n';
60 /* If true, print a summary. */
61 static bool total_option
;
63 /* If nonzero, check that the input is correctly ordered. */
71 /* Output columns will be delimited with this string, which may be set
72 on the command-line with --output-delimiter=STR. */
73 static char const *col_sep
= "\t";
74 static size_t col_sep_len
= 0;
76 /* For long options that have no equivalent short option, use a
77 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
80 CHECK_ORDER_OPTION
= CHAR_MAX
+ 1,
82 OUTPUT_DELIMITER_OPTION
,
86 static struct option
const long_options
[] =
88 {"check-order", no_argument
, nullptr, CHECK_ORDER_OPTION
},
89 {"nocheck-order", no_argument
, nullptr, NOCHECK_ORDER_OPTION
},
90 {"output-delimiter", required_argument
, nullptr, OUTPUT_DELIMITER_OPTION
},
91 {"total", no_argument
, nullptr, TOTAL_OPTION
},
92 {"zero-terminated", no_argument
, nullptr, 'z'},
93 {GETOPT_HELP_OPTION_DECL
},
94 {GETOPT_VERSION_OPTION_DECL
},
95 {nullptr, 0, nullptr, 0}
102 if (status
!= EXIT_SUCCESS
)
107 Usage: %s [OPTION]... FILE1 FILE2\n\
111 Compare sorted files FILE1 and FILE2 line by line.\n\
115 When FILE1 or FILE2 (not both) is -, read standard input.\n\
119 With no options, produce three-column output. Column one contains\n\
120 lines unique to FILE1, column two contains lines unique to FILE2,\n\
121 and column three contains lines common to both files.\n\
125 -1 suppress column 1 (lines unique to FILE1)\n\
126 -2 suppress column 2 (lines unique to FILE2)\n\
127 -3 suppress column 3 (lines that appear in both files)\n\
131 --check-order check that the input is correctly sorted, even\n\
132 if all input lines are pairable\n\
133 --nocheck-order do not check that the input is correctly sorted\n\
136 --output-delimiter=STR separate columns with STR\n\
139 --total output a summary\n\
142 -z, --zero-terminated line delimiter is NUL, not newline\n\
144 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
145 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
148 Comparisons honor the rules specified by 'LC_COLLATE'.\n\
153 %s -12 file1 file2 Print only lines present in both file1 and file2.\n\
154 %s -3 file1 file2 Print lines in file1 not in file2, and vice versa.\n\
156 program_name
, program_name
);
157 emit_ancillary_info (PROGRAM_NAME
);
162 /* Output the line in linebuffer LINE to stdout
163 provided the switches say it should be output.
164 CLASS is 1 for a line found only in file 1,
165 2 for a line only in file 2, 3 for a line in both. */
168 writeline (struct linebuffer
const *line
, int class)
181 fwrite (col_sep
, 1, col_sep_len
, stdout
);
188 fwrite (col_sep
, 1, col_sep_len
, stdout
);
190 fwrite (col_sep
, 1, col_sep_len
, stdout
);
194 fwrite (line
->buffer
, sizeof (char), line
->length
, stdout
);
200 /* Check that successive input lines PREV and CURRENT from input file
201 WHATFILE are presented in order.
203 If the user specified --nocheck-order, the check is not made.
204 If the user specified --check-order, the problem is fatal.
205 Otherwise (the default), the message is simply a warning.
207 A message is printed at most once per input file.
209 This function was copied (nearly) verbatim from 'src/join.c'. */
212 check_order (struct linebuffer
const *prev
,
213 struct linebuffer
const *current
,
217 if (check_input_order
!= CHECK_ORDER_DISABLED
218 && ((check_input_order
== CHECK_ORDER_ENABLED
) || seen_unpairable
))
220 if (!issued_disorder_warning
[whatfile
- 1])
225 order
= xmemcoll (prev
->buffer
, prev
->length
- 1,
226 current
->buffer
, current
->length
- 1);
228 order
= memcmp2 (prev
->buffer
, prev
->length
- 1,
229 current
->buffer
, current
->length
- 1);
233 error ((check_input_order
== CHECK_ORDER_ENABLED
235 0, _("file %d is not in sorted order"), whatfile
);
237 /* If we get to here, the message was just a warning, but we
238 want only to issue it once. */
239 issued_disorder_warning
[whatfile
- 1] = true;
245 /* Compare INFILES[0] and INFILES[1].
246 If either is "-", use the standard input for that file.
247 Assume that each input file is sorted;
248 merge them and output the result.
249 Exit the program when done. */
251 static _Noreturn
void
252 compare_files (char **infiles
)
254 /* For each file, we have four linebuffers in lba. */
255 struct linebuffer lba
[2][4];
257 /* thisline[i] points to the linebuffer holding the next available line
258 in file i, or is null if there are no lines left in that file. */
259 struct linebuffer
*thisline
[2];
261 /* all_line[i][alt[i][0]] also points to the linebuffer holding the
262 current line in file i. We keep two buffers of history around so we
263 can look two lines back when we get to the end of a file. */
264 struct linebuffer
*all_line
[2][4];
266 /* This is used to rotate through the buffers for each input file. */
269 /* streams[i] holds the input stream for file i. */
272 /* Counters for the summary. */
273 uintmax_t total
[] = {0, 0, 0};
277 /* Initialize the storage. */
278 for (i
= 0; i
< 2; i
++)
280 for (j
= 0; j
< 4; j
++)
282 initbuffer (&lba
[i
][j
]);
283 all_line
[i
][j
] = &lba
[i
][j
];
288 streams
[i
] = (STREQ (infiles
[i
], "-") ? stdin
: fopen (infiles
[i
], "r"));
290 error (EXIT_FAILURE
, errno
, "%s", quotef (infiles
[i
]));
292 fadvise (streams
[i
], FADVISE_SEQUENTIAL
);
294 thisline
[i
] = readlinebuffer_delim (all_line
[i
][alt
[i
][0]], streams
[i
],
296 if (ferror (streams
[i
]))
297 error (EXIT_FAILURE
, errno
, "%s", quotef (infiles
[i
]));
300 while (thisline
[0] || thisline
[1])
303 bool fill_up
[2] = { false, false };
305 /* Compare the next available lines of the two files. */
309 else if (!thisline
[1])
314 order
= xmemcoll (thisline
[0]->buffer
, thisline
[0]->length
- 1,
315 thisline
[1]->buffer
, thisline
[1]->length
- 1);
318 size_t len
= MIN (thisline
[0]->length
, thisline
[1]->length
) - 1;
319 order
= memcmp (thisline
[0]->buffer
, thisline
[1]->buffer
, len
);
321 order
= ((thisline
[0]->length
> thisline
[1]->length
)
322 - (thisline
[0]->length
< thisline
[1]->length
));
326 /* Output the line that is lesser. */
329 /* Line is seen in both files. */
331 writeline (thisline
[1], 3);
335 seen_unpairable
= true;
338 /* Line is seen in file 1 only. */
340 writeline (thisline
[0], 1);
344 /* Line is seen in file 2 only. */
346 writeline (thisline
[1], 2);
350 /* Step the file the line came from.
351 If the files match, step both files. */
357 for (i
= 0; i
< 2; i
++)
360 /* Rotate the buffers for this file. */
361 alt
[i
][2] = alt
[i
][1];
362 alt
[i
][1] = alt
[i
][0];
363 alt
[i
][0] = (alt
[i
][0] + 1) & 0x03;
365 thisline
[i
] = readlinebuffer_delim (all_line
[i
][alt
[i
][0]],
369 check_order (all_line
[i
][alt
[i
][1]], thisline
[i
], i
+ 1);
371 /* If this is the end of the file we may need to re-check
372 the order of the previous two lines, since we might have
373 discovered an unpairable match since we checked before. */
374 else if (all_line
[i
][alt
[i
][2]]->buffer
)
375 check_order (all_line
[i
][alt
[i
][2]],
376 all_line
[i
][alt
[i
][1]], i
+ 1);
378 if (ferror (streams
[i
]))
379 error (EXIT_FAILURE
, errno
, "%s", quotef (infiles
[i
]));
385 for (i
= 0; i
< 2; i
++)
386 if (fclose (streams
[i
]) != 0)
387 error (EXIT_FAILURE
, errno
, "%s", quotef (infiles
[i
]));
391 /* Print the summary, minding the column and line delimiters. */
392 char buf1
[INT_BUFSIZE_BOUND (uintmax_t)];
393 char buf2
[INT_BUFSIZE_BOUND (uintmax_t)];
394 char buf3
[INT_BUFSIZE_BOUND (uintmax_t)];
395 if (col_sep_len
== 1)
396 { /* Separate to handle NUL char. */
397 printf ("%s%c%s%c%s%c%s%c",
398 umaxtostr (total
[0], buf1
), *col_sep
,
399 umaxtostr (total
[1], buf2
), *col_sep
,
400 umaxtostr (total
[2], buf3
), *col_sep
,
405 printf ("%s%s%s%s%s%s%s%c",
406 umaxtostr (total
[0], buf1
), col_sep
,
407 umaxtostr (total
[1], buf2
), col_sep
,
408 umaxtostr (total
[2], buf3
), col_sep
,
413 if (issued_disorder_warning
[0] || issued_disorder_warning
[1])
414 error (EXIT_FAILURE
, 0, _("input is not in sorted order"));
416 /* Exit here to pacify gcc -fsanitizer=leak. */
421 main (int argc
, char **argv
)
425 initialize_main (&argc
, &argv
);
426 set_program_name (argv
[0]);
427 setlocale (LC_ALL
, "");
428 bindtextdomain (PACKAGE
, LOCALEDIR
);
429 textdomain (PACKAGE
);
430 hard_LC_COLLATE
= hard_locale (LC_COLLATE
);
432 atexit (close_stdout
);
438 seen_unpairable
= false;
439 issued_disorder_warning
[0] = issued_disorder_warning
[1] = false;
440 check_input_order
= CHECK_ORDER_DEFAULT
;
441 total_option
= false;
443 while ((c
= getopt_long (argc
, argv
, "123z", long_options
, nullptr)) != -1)
462 case NOCHECK_ORDER_OPTION
:
463 check_input_order
= CHECK_ORDER_DISABLED
;
466 case CHECK_ORDER_OPTION
:
467 check_input_order
= CHECK_ORDER_ENABLED
;
470 case OUTPUT_DELIMITER_OPTION
:
471 if (col_sep_len
&& !STREQ (col_sep
, optarg
))
472 error (EXIT_FAILURE
, 0, _("multiple output delimiters specified"));
474 col_sep_len
= *optarg
? strlen (optarg
) : 1;
481 case_GETOPT_HELP_CHAR
;
483 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
486 usage (EXIT_FAILURE
);
492 if (argc
- optind
< 2)
495 error (0, 0, _("missing operand"));
497 error (0, 0, _("missing operand after %s"), quote (argv
[argc
- 1]));
498 usage (EXIT_FAILURE
);
501 if (2 < argc
- optind
)
503 error (0, 0, _("extra operand %s"), quote (argv
[optind
+ 2]));
504 usage (EXIT_FAILURE
);
507 compare_files (argv
+ optind
);