1992-11-08 02:50:43 +00:00
|
|
|
|
/* comm -- compare two sorted files line by line.
|
2005-03-28 17:54:13 +00:00
|
|
|
|
Copyright (C) 86, 90, 91, 1995-2005 Free Software Foundation, Inc.
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
1997-01-25 06:11:22 +00:00
|
|
|
|
along with this program; if not, write to the Free Software Foundation,
|
2005-05-14 07:58:31 +00:00
|
|
|
|
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
/* Written by Richard Stallman and David MacKenzie. */
|
|
|
|
|
|
|
1993-10-21 22:08:53 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
1992-11-08 02:50:43 +00:00
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
#include "system.h"
|
|
|
|
|
|
#include "linebuffer.h"
|
1994-12-16 05:41:05 +00:00
|
|
|
|
#include "error.h"
|
1999-07-04 10:22:25 +00:00
|
|
|
|
#include "hard-locale.h"
|
2004-06-21 15:03:35 +00:00
|
|
|
|
#include "quote.h"
|
2005-07-03 07:16:23 +00:00
|
|
|
|
#include "stdio--.h"
|
2002-01-22 08:11:04 +00:00
|
|
|
|
#include "xmemcoll.h"
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1999-04-03 05:01:48 +00:00
|
|
|
|
/* The official name of this program (e.g., no `g' prefix). */
|
|
|
|
|
|
#define PROGRAM_NAME "comm"
|
|
|
|
|
|
|
2003-10-18 10:05:47 +00:00
|
|
|
|
#define AUTHORS "Richard Stallman", "David MacKenzie"
|
1999-04-03 05:22:05 +00:00
|
|
|
|
|
1995-06-24 21:10:12 +00:00
|
|
|
|
/* Undefine, to avoid warning about redefinition on some systems. */
|
|
|
|
|
|
#undef min
|
1992-11-08 02:50:43 +00:00
|
|
|
|
#define min(x, y) ((x) < (y) ? (x) : (y))
|
|
|
|
|
|
|
1993-04-29 05:26:22 +00:00
|
|
|
|
/* The name this program was run with. */
|
|
|
|
|
|
char *program_name;
|
|
|
|
|
|
|
2004-07-30 05:25:17 +00:00
|
|
|
|
/* True if the LC_COLLATE locale is hard. */
|
|
|
|
|
|
static bool hard_LC_COLLATE;
|
1999-07-04 10:22:25 +00:00
|
|
|
|
|
2004-07-30 05:25:17 +00:00
|
|
|
|
/* If true, print lines that are found only in file 1. */
|
|
|
|
|
|
static bool only_file_1;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
2004-07-30 05:25:17 +00:00
|
|
|
|
/* If true, print lines that are found only in file 2. */
|
|
|
|
|
|
static bool only_file_2;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
2004-07-30 05:25:17 +00:00
|
|
|
|
/* If true, print lines that are found in both files. */
|
|
|
|
|
|
static bool both;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1993-04-29 05:26:22 +00:00
|
|
|
|
static struct option const long_options[] =
|
|
|
|
|
|
{
|
1999-04-04 15:44:26 +00:00
|
|
|
|
{GETOPT_HELP_OPTION_DECL},
|
|
|
|
|
|
{GETOPT_VERSION_OPTION_DECL},
|
2005-03-28 17:54:13 +00:00
|
|
|
|
{NULL, 0, NULL, 0}
|
1993-04-29 05:26:22 +00:00
|
|
|
|
};
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-01-14 18:25:16 +00:00
|
|
|
|
void
|
1995-08-05 03:36:29 +00:00
|
|
|
|
usage (int status)
|
1995-07-12 03:01:33 +00:00
|
|
|
|
{
|
2004-01-21 23:03:34 +00:00
|
|
|
|
if (status != EXIT_SUCCESS)
|
1995-08-07 14:57:29 +00:00
|
|
|
|
fprintf (stderr, _("Try `%s --help' for more information.\n"),
|
1995-07-12 03:01:33 +00:00
|
|
|
|
program_name);
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
1995-08-07 14:57:29 +00:00
|
|
|
|
printf (_("\
|
2004-02-21 09:21:15 +00:00
|
|
|
|
Usage: %s [OPTION]... FILE1 FILE2\n\
|
1995-08-07 14:57:29 +00:00
|
|
|
|
"),
|
1995-07-12 03:01:33 +00:00
|
|
|
|
program_name);
|
2001-11-23 19:58:23 +00:00
|
|
|
|
fputs (_("\
|
2004-02-21 09:21:15 +00:00
|
|
|
|
Compare sorted files FILE1 and FILE2 line by line.\n\
|
|
|
|
|
|
"), stdout);
|
|
|
|
|
|
fputs (_("\
|
|
|
|
|
|
\n\
|
|
|
|
|
|
With no options, produce three-column output. Column one contains\n\
|
|
|
|
|
|
lines unique to FILE1, column two contains lines unique to FILE2,\n\
|
|
|
|
|
|
and column three contains lines common to both files.\n\
|
|
|
|
|
|
"), stdout);
|
|
|
|
|
|
fputs (_("\
|
1995-07-12 03:01:33 +00:00
|
|
|
|
\n\
|
2004-02-21 09:21:15 +00:00
|
|
|
|
-1 suppress lines unique to FILE1\n\
|
|
|
|
|
|
-2 suppress lines unique to FILE2\n\
|
2001-05-05 12:54:37 +00:00
|
|
|
|
-3 suppress lines that appear in both files\n\
|
2001-11-23 19:58:23 +00:00
|
|
|
|
"), stdout);
|
2001-12-01 17:41:25 +00:00
|
|
|
|
fputs (HELP_OPTION_DESCRIPTION, stdout);
|
|
|
|
|
|
fputs (VERSION_OPTION_DESCRIPTION, stdout);
|
2002-07-02 09:06:33 +00:00
|
|
|
|
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
|
1995-07-12 03:01:33 +00:00
|
|
|
|
}
|
2004-01-21 23:03:34 +00:00
|
|
|
|
exit (status);
|
1995-07-12 03:01:33 +00:00
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1995-07-12 03:01:33 +00:00
|
|
|
|
/* Output the line in linebuffer LINE to stream STREAM
|
|
|
|
|
|
provided the switches say it should be output.
|
|
|
|
|
|
CLASS is 1 for a line found only in file 1,
|
|
|
|
|
|
2 for a line only in file 2, 3 for a line in both. */
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1995-07-12 03:01:33 +00:00
|
|
|
|
static void
|
1999-08-22 08:48:45 +00:00
|
|
|
|
writeline (const struct linebuffer *line, FILE *stream, int class)
|
1995-07-12 03:01:33 +00:00
|
|
|
|
{
|
|
|
|
|
|
switch (class)
|
1993-05-22 05:06:39 +00:00
|
|
|
|
{
|
1995-07-12 03:01:33 +00:00
|
|
|
|
case 1:
|
|
|
|
|
|
if (!only_file_1)
|
|
|
|
|
|
return;
|
|
|
|
|
|
break;
|
1993-04-29 05:26:22 +00:00
|
|
|
|
|
1995-07-12 03:01:33 +00:00
|
|
|
|
case 2:
|
|
|
|
|
|
if (!only_file_2)
|
|
|
|
|
|
return;
|
2000-08-19 11:09:03 +00:00
|
|
|
|
/* Print a TAB if we are printing lines from file 1. */
|
1995-07-12 03:01:33 +00:00
|
|
|
|
if (only_file_1)
|
1998-06-29 15:57:45 +00:00
|
|
|
|
putc ('\t', stream);
|
1995-07-12 03:01:33 +00:00
|
|
|
|
break;
|
1993-04-29 05:26:22 +00:00
|
|
|
|
|
1995-07-12 03:01:33 +00:00
|
|
|
|
case 3:
|
|
|
|
|
|
if (!both)
|
|
|
|
|
|
return;
|
2000-08-19 11:09:03 +00:00
|
|
|
|
/* Print a TAB if we are printing lines from file 1. */
|
1995-07-12 03:01:33 +00:00
|
|
|
|
if (only_file_1)
|
1998-06-29 15:57:45 +00:00
|
|
|
|
putc ('\t', stream);
|
2000-08-19 11:09:03 +00:00
|
|
|
|
/* Print a TAB if we are printing lines from file 2. */
|
1995-07-12 03:01:33 +00:00
|
|
|
|
if (only_file_2)
|
1998-06-29 15:57:45 +00:00
|
|
|
|
putc ('\t', stream);
|
1995-07-12 03:01:33 +00:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1998-06-29 15:57:45 +00:00
|
|
|
|
fwrite (line->buffer, sizeof (char), line->length, stream);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
1995-07-12 03:01:33 +00:00
|
|
|
|
|
1992-11-08 02:50:43 +00:00
|
|
|
|
/* Compare INFILES[0] and INFILES[1].
|
|
|
|
|
|
If either is "-", use the standard input for that file.
|
|
|
|
|
|
Assume that each input file is sorted;
|
2005-04-11 20:06:13 +00:00
|
|
|
|
merge them and output the result. */
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
2005-04-11 20:06:13 +00:00
|
|
|
|
static void
|
1995-08-05 03:36:29 +00:00
|
|
|
|
compare_files (char **infiles)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
|
|
|
|
|
/* For each file, we have one linebuffer in lb1. */
|
|
|
|
|
|
struct linebuffer lb1[2];
|
|
|
|
|
|
|
|
|
|
|
|
/* thisline[i] points to the linebuffer holding the next available line
|
|
|
|
|
|
in file i, or is NULL if there are no lines left in that file. */
|
|
|
|
|
|
struct linebuffer *thisline[2];
|
|
|
|
|
|
|
|
|
|
|
|
/* streams[i] holds the input stream for file i. */
|
|
|
|
|
|
FILE *streams[2];
|
|
|
|
|
|
|
2004-07-30 05:25:17 +00:00
|
|
|
|
int i;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
/* Initialize the storage. */
|
|
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
initbuffer (&lb1[i]);
|
|
|
|
|
|
thisline[i] = &lb1[i];
|
2005-07-03 07:16:23 +00:00
|
|
|
|
streams[i] = (STREQ (infiles[i], "-") ? stdin : fopen (infiles[i], "r"));
|
1992-11-08 02:50:43 +00:00
|
|
|
|
if (!streams[i])
|
2005-04-11 20:06:13 +00:00
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infiles[i]);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
2003-06-18 07:37:29 +00:00
|
|
|
|
thisline[i] = readlinebuffer (thisline[i], streams[i]);
|
2005-04-11 20:06:13 +00:00
|
|
|
|
if (ferror (streams[i]))
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infiles[i]);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (thisline[0] || thisline[1])
|
|
|
|
|
|
{
|
|
|
|
|
|
int order;
|
|
|
|
|
|
|
|
|
|
|
|
/* Compare the next available lines of the two files. */
|
|
|
|
|
|
|
|
|
|
|
|
if (!thisline[0])
|
|
|
|
|
|
order = 1;
|
|
|
|
|
|
else if (!thisline[1])
|
|
|
|
|
|
order = -1;
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2004-12-02 06:54:16 +00:00
|
|
|
|
if (hard_LC_COLLATE)
|
2002-01-22 08:11:04 +00:00
|
|
|
|
order = xmemcoll (thisline[0]->buffer, thisline[0]->length - 1,
|
|
|
|
|
|
thisline[1]->buffer, thisline[1]->length - 1);
|
1999-07-04 10:22:25 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
1999-11-04 23:11:03 +00:00
|
|
|
|
size_t len = min (thisline[0]->length, thisline[1]->length) - 1;
|
|
|
|
|
|
order = memcmp (thisline[0]->buffer, thisline[1]->buffer, len);
|
1999-07-04 10:22:25 +00:00
|
|
|
|
if (order == 0)
|
2002-01-21 22:00:32 +00:00
|
|
|
|
order = (thisline[0]->length < thisline[1]->length
|
|
|
|
|
|
? -1
|
|
|
|
|
|
: thisline[0]->length != thisline[1]->length);
|
1999-07-04 10:22:25 +00:00
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Output the line that is lesser. */
|
|
|
|
|
|
if (order == 0)
|
|
|
|
|
|
writeline (thisline[1], stdout, 3);
|
|
|
|
|
|
else if (order > 0)
|
|
|
|
|
|
writeline (thisline[1], stdout, 2);
|
|
|
|
|
|
else
|
|
|
|
|
|
writeline (thisline[0], stdout, 1);
|
|
|
|
|
|
|
|
|
|
|
|
/* Step the file the line came from.
|
|
|
|
|
|
If the files match, step both files. */
|
|
|
|
|
|
if (order >= 0)
|
2003-09-23 12:09:33 +00:00
|
|
|
|
{
|
|
|
|
|
|
thisline[1] = readlinebuffer (thisline[1], streams[1]);
|
2005-04-11 20:06:13 +00:00
|
|
|
|
if (ferror (streams[1]))
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infiles[1]);
|
2003-09-23 12:09:33 +00:00
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
|
if (order <= 0)
|
2003-09-23 12:09:33 +00:00
|
|
|
|
{
|
|
|
|
|
|
thisline[0] = readlinebuffer (thisline[0], streams[0]);
|
2005-04-11 20:06:13 +00:00
|
|
|
|
if (ferror (streams[0]))
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infiles[0]);
|
2003-09-23 12:09:33 +00:00
|
|
|
|
}
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++)
|
2005-04-11 20:06:13 +00:00
|
|
|
|
if (fclose (streams[i]) != 0)
|
|
|
|
|
|
error (EXIT_FAILURE, errno, "%s", infiles[i]);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
1996-03-21 22:41:04 +00:00
|
|
|
|
int
|
1995-08-05 03:36:29 +00:00
|
|
|
|
main (int argc, char **argv)
|
1992-11-08 02:50:43 +00:00
|
|
|
|
{
|
1995-07-12 03:01:33 +00:00
|
|
|
|
int c;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
2003-06-17 18:13:23 +00:00
|
|
|
|
initialize_main (&argc, &argv);
|
1995-07-12 03:01:33 +00:00
|
|
|
|
program_name = argv[0];
|
1996-03-09 20:19:13 +00:00
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
|
|
textdomain (PACKAGE);
|
2002-04-30 09:23:38 +00:00
|
|
|
|
hard_LC_COLLATE = hard_locale (LC_COLLATE);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
2000-05-20 22:04:16 +00:00
|
|
|
|
atexit (close_stdout);
|
|
|
|
|
|
|
2004-07-30 05:25:17 +00:00
|
|
|
|
only_file_1 = true;
|
|
|
|
|
|
only_file_2 = true;
|
|
|
|
|
|
both = true;
|
1992-11-08 02:50:43 +00:00
|
|
|
|
|
1997-02-01 02:00:04 +00:00
|
|
|
|
while ((c = getopt_long (argc, argv, "123", long_options, NULL)) != -1)
|
1995-07-12 03:01:33 +00:00
|
|
|
|
switch (c)
|
|
|
|
|
|
{
|
|
|
|
|
|
case '1':
|
2004-07-30 05:25:17 +00:00
|
|
|
|
only_file_1 = false;
|
1995-07-12 03:01:33 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case '2':
|
2004-07-30 05:25:17 +00:00
|
|
|
|
only_file_2 = false;
|
1995-07-12 03:01:33 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case '3':
|
2004-07-30 05:25:17 +00:00
|
|
|
|
both = false;
|
1995-07-12 03:01:33 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
1999-04-04 15:44:26 +00:00
|
|
|
|
case_GETOPT_HELP_CHAR;
|
|
|
|
|
|
|
2003-10-18 10:05:47 +00:00
|
|
|
|
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
|
1999-04-04 15:44:26 +00:00
|
|
|
|
|
1995-07-12 03:01:33 +00:00
|
|
|
|
default:
|
2002-08-31 08:52:10 +00:00
|
|
|
|
usage (EXIT_FAILURE);
|
1995-07-12 03:01:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2004-06-21 15:03:35 +00:00
|
|
|
|
if (argc - optind < 2)
|
2003-05-10 14:17:46 +00:00
|
|
|
|
{
|
2004-06-21 15:03:35 +00:00
|
|
|
|
if (argc <= optind)
|
|
|
|
|
|
error (0, 0, _("missing operand"));
|
|
|
|
|
|
else
|
|
|
|
|
|
error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
|
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (2 < argc - optind)
|
|
|
|
|
|
{
|
|
|
|
|
|
error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
|
2003-05-10 14:17:46 +00:00
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
|
}
|
1995-07-12 03:01:33 +00:00
|
|
|
|
|
2005-04-11 20:06:13 +00:00
|
|
|
|
compare_files (argv + optind);
|
|
|
|
|
|
|
|
|
|
|
|
exit (EXIT_SUCCESS);
|
1992-11-08 02:50:43 +00:00
|
|
|
|
}
|