1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-20 02:36:16 +02:00
Files
coreutils/src/comm.c

286 lines
7.1 KiB
C
Raw Normal View History

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"
#include "error.h"
#include "hard-locale.h"
#include "quote.h"
#include "stdio--.h"
#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"
#define AUTHORS "Richard Stallman", "David MacKenzie"
1999-04-03 05:22:05 +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;
/* True if the LC_COLLATE locale is hard. */
static bool hard_LC_COLLATE;
/* If true, print lines that are found only in file 1. */
static bool only_file_1;
1992-11-08 02:50:43 +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
/* 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[] =
{
{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)
{
if (status != EXIT_SUCCESS)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
{
printf (_("\
Usage: %s [OPTION]... FILE1 FILE2\n\
"),
program_name);
fputs (_("\
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 (_("\
\n\
-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\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
}
exit (status);
}
1992-11-08 02:50:43 +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
static void
1999-08-22 08:48:45 +00:00
writeline (const struct linebuffer *line, FILE *stream, int class)
{
switch (class)
1993-05-22 05:06:39 +00:00
{
case 1:
if (!only_file_1)
return;
break;
1993-04-29 05:26:22 +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. */
if (only_file_1)
putc ('\t', stream);
break;
1993-04-29 05:26:22 +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. */
if (only_file_1)
putc ('\t', stream);
2000-08-19 11:09:03 +00:00
/* Print a TAB if we are printing lines from file 2. */
if (only_file_2)
putc ('\t', stream);
break;
}
1992-11-08 02:50:43 +00:00
fwrite (line->buffer, sizeof (char), line->length, stream);
1992-11-08 02:50:43 +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;
merge them and output the result. */
1992-11-08 02:50:43 +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];
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];
streams[i] = (STREQ (infiles[i], "-") ? stdin : fopen (infiles[i], "r"));
1992-11-08 02:50:43 +00:00
if (!streams[i])
error (EXIT_FAILURE, errno, "%s", infiles[i]);
1992-11-08 02:50:43 +00:00
thisline[i] = readlinebuffer (thisline[i], streams[i]);
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
{
if (hard_LC_COLLATE)
order = xmemcoll (thisline[0]->buffer, thisline[0]->length - 1,
thisline[1]->buffer, thisline[1]->length - 1);
else
{
size_t len = min (thisline[0]->length, thisline[1]->length) - 1;
order = memcmp (thisline[0]->buffer, thisline[1]->buffer, len);
if (order == 0)
order = (thisline[0]->length < thisline[1]->length
? -1
: thisline[0]->length != thisline[1]->length);
}
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)
{
thisline[1] = readlinebuffer (thisline[1], streams[1]);
if (ferror (streams[1]))
error (EXIT_FAILURE, errno, "%s", infiles[1]);
}
1992-11-08 02:50:43 +00:00
if (order <= 0)
{
thisline[0] = readlinebuffer (thisline[0], streams[0]);
if (ferror (streams[0]))
error (EXIT_FAILURE, errno, "%s", infiles[0]);
}
1992-11-08 02:50:43 +00:00
}
for (i = 0; i < 2; i++)
if (fclose (streams[i]) != 0)
error (EXIT_FAILURE, errno, "%s", infiles[i]);
1992-11-08 02:50:43 +00:00
}
int
1995-08-05 03:36:29 +00:00
main (int argc, char **argv)
1992-11-08 02:50:43 +00:00
{
int c;
1992-11-08 02:50:43 +00:00
2003-06-17 18:13:23 +00:00
initialize_main (&argc, &argv);
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
hard_LC_COLLATE = hard_locale (LC_COLLATE);
1992-11-08 02:50:43 +00:00
atexit (close_stdout);
only_file_1 = true;
only_file_2 = true;
both = true;
1992-11-08 02:50:43 +00:00
while ((c = getopt_long (argc, argv, "123", long_options, NULL)) != -1)
switch (c)
{
case '1':
only_file_1 = false;
break;
case '2':
only_file_2 = false;
break;
case '3':
both = false;
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (EXIT_FAILURE);
}
if (argc - optind < 2)
{
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]));
usage (EXIT_FAILURE);
}
compare_files (argv + optind);
exit (EXIT_SUCCESS);
1992-11-08 02:50:43 +00:00
}