1 /* comm -- compare two sorted files line by line.
2 Copyright (C) 86, 90, 91, 1995-2000 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>
27 #include "linebuffer.h"
29 #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 and 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. */
45 /* Nonzero if the LC_COLLATE locale is hard. */
46 static int hard_LC_COLLATE
;
49 /* If nonzero, print lines that are found only in file 1. */
50 static int only_file_1
;
52 /* If nonzero, print lines that are found only in file 2. */
53 static int only_file_2
;
55 /* If nonzero, print lines that are found in both files. */
58 static struct option
const long_options
[] =
60 {GETOPT_HELP_OPTION_DECL
},
61 {GETOPT_VERSION_OPTION_DECL
},
71 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
76 Usage: %s [OPTION]... LEFT_FILE RIGHT_FILE\n\
80 Compare sorted files LEFT_FILE and RIGHT_FILE line by line.\n\
82 -1 suppress lines unique to left file\n\
83 -2 suppress lines unique to right file\n\
84 -3 suppress lines unique to both files\n\
85 --help display this help and exit\n\
86 --version output version information and exit\n\
88 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
90 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
93 /* Output the line in linebuffer LINE to stream STREAM
94 provided the switches say it should be output.
95 CLASS is 1 for a line found only in file 1,
96 2 for a line only in file 2, 3 for a line in both. */
99 writeline (const struct linebuffer
*line
, FILE *stream
, int class)
111 /* Print a TAB if we are printing lines from file 1. */
119 /* Print a TAB if we are printing lines from file 1. */
122 /* Print a TAB if we are printing lines from file 2. */
128 fwrite (line
->buffer
, sizeof (char), line
->length
, stream
);
131 /* Compare INFILES[0] and INFILES[1].
132 If either is "-", use the standard input for that file.
133 Assume that each input file is sorted;
134 merge them and output the result.
135 Return 0 if successful, 1 if any errors occur. */
138 compare_files (char **infiles
)
140 /* For each file, we have one linebuffer in lb1. */
141 struct linebuffer lb1
[2];
143 /* thisline[i] points to the linebuffer holding the next available line
144 in file i, or is NULL if there are no lines left in that file. */
145 struct linebuffer
*thisline
[2];
147 /* streams[i] holds the input stream for file i. */
152 /* Initialize the storage. */
153 for (i
= 0; i
< 2; i
++)
155 initbuffer (&lb1
[i
]);
156 thisline
[i
] = &lb1
[i
];
157 streams
[i
] = (STREQ (infiles
[i
], "-") ? stdin
: fopen (infiles
[i
], "r"));
160 error (0, errno
, "%s", infiles
[i
]);
164 thisline
[i
] = readline (thisline
[i
], streams
[i
]);
167 while (thisline
[0] || thisline
[1])
171 /* Compare the next available lines of the two files. */
175 else if (!thisline
[1])
181 order
= memcoll (thisline
[0]->buffer
, thisline
[0]->length
- 1,
182 thisline
[1]->buffer
, thisline
[1]->length
- 1);
186 size_t len
= min (thisline
[0]->length
, thisline
[1]->length
) - 1;
187 order
= memcmp (thisline
[0]->buffer
, thisline
[1]->buffer
, len
);
189 order
= thisline
[0]->length
- thisline
[1]->length
;
193 /* Output the line that is lesser. */
195 writeline (thisline
[1], stdout
, 3);
197 writeline (thisline
[1], stdout
, 2);
199 writeline (thisline
[0], stdout
, 1);
201 /* Step the file the line came from.
202 If the files match, step both files. */
204 thisline
[1] = readline (thisline
[1], streams
[1]);
206 thisline
[0] = readline (thisline
[0], streams
[0]);
209 /* Free all storage and close all input streams. */
210 for (i
= 0; i
< 2; i
++)
212 free (lb1
[i
].buffer
);
213 if (ferror (streams
[i
]) || fclose (streams
[i
]) == EOF
)
215 error (0, errno
, "%s", infiles
[i
]);
223 main (int argc
, char **argv
)
227 program_name
= argv
[0];
228 setlocale (LC_ALL
, "");
229 bindtextdomain (PACKAGE
, LOCALEDIR
);
230 textdomain (PACKAGE
);
232 atexit (close_stdout
);
235 hard_LC_COLLATE
= hard_locale (LC_COLLATE
);
242 while ((c
= getopt_long (argc
, argv
, "123", long_options
, NULL
)) != -1)
260 case_GETOPT_HELP_CHAR
;
262 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
268 if (optind
+ 2 != argc
)
271 exit (compare_files (argv
+ optind
) == 0
272 ? EXIT_SUCCESS
: EXIT_FAILURE
);