2005-05-12 04:42:49 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini comm implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Robert Sullivan <cogito.ergo.cogito@gmail.com>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2005-05-12 04:42:49 +05:30
|
|
|
*/
|
2016-11-23 19:16:56 +05:30
|
|
|
//config:config COMM
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "comm (3.9 kb)"
|
2016-11-23 19:16:56 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: comm is used to compare two files line by line and return
|
|
|
|
//config: a three-column output.
|
2016-11-23 19:16:56 +05:30
|
|
|
|
|
|
|
//applet:IF_COMM(APPLET(comm, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_COMM) += comm.o
|
2005-05-12 04:42:49 +05:30
|
|
|
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage:#define comm_trivial_usage
|
|
|
|
//usage: "[-123] FILE1 FILE2"
|
|
|
|
//usage:#define comm_full_usage "\n\n"
|
|
|
|
//usage: "Compare FILE1 with FILE2\n"
|
|
|
|
//usage: "\n -1 Suppress lines unique to FILE1"
|
|
|
|
//usage: "\n -2 Suppress lines unique to FILE2"
|
|
|
|
//usage: "\n -3 Suppress lines common to both files"
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2005-05-12 04:42:49 +05:30
|
|
|
|
2007-04-07 06:15:27 +05:30
|
|
|
#define COMM_OPT_1 (1 << 0)
|
|
|
|
#define COMM_OPT_2 (1 << 1)
|
|
|
|
#define COMM_OPT_3 (1 << 2)
|
2005-05-12 04:42:49 +05:30
|
|
|
|
|
|
|
/* writeline outputs the input given, appropriately aligned according to class */
|
2008-07-15 12:15:14 +05:30
|
|
|
static void writeline(char *line, int class)
|
2005-05-13 03:55:41 +05:30
|
|
|
{
|
2008-07-15 12:15:14 +05:30
|
|
|
int flags = option_mask32;
|
2005-05-13 06:27:30 +05:30
|
|
|
if (class == 0) {
|
2007-04-07 06:15:27 +05:30
|
|
|
if (flags & COMM_OPT_1)
|
2005-05-13 06:28:18 +05:30
|
|
|
return;
|
2005-05-13 06:27:30 +05:30
|
|
|
} else if (class == 1) {
|
2007-04-07 06:15:27 +05:30
|
|
|
if (flags & COMM_OPT_2)
|
2005-05-13 04:20:12 +05:30
|
|
|
return;
|
2007-04-07 06:15:27 +05:30
|
|
|
if (!(flags & COMM_OPT_1))
|
2005-05-13 04:20:12 +05:30
|
|
|
putchar('\t');
|
2007-04-07 06:15:27 +05:30
|
|
|
} else /*if (class == 2)*/ {
|
|
|
|
if (flags & COMM_OPT_3)
|
2005-05-13 04:20:12 +05:30
|
|
|
return;
|
2007-04-07 06:15:27 +05:30
|
|
|
if (!(flags & COMM_OPT_1))
|
2005-05-13 04:20:12 +05:30
|
|
|
putchar('\t');
|
2007-04-07 06:15:27 +05:30
|
|
|
if (!(flags & COMM_OPT_2))
|
2005-05-13 04:20:12 +05:30
|
|
|
putchar('\t');
|
2005-05-12 04:42:49 +05:30
|
|
|
}
|
2008-07-15 12:15:14 +05:30
|
|
|
puts(line);
|
2005-05-12 04:42:49 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int comm_main(int argc UNUSED_PARAM, char **argv)
|
2005-05-13 03:55:41 +05:30
|
|
|
{
|
2008-07-15 12:15:14 +05:30
|
|
|
char *thisline[2];
|
|
|
|
FILE *stream[2];
|
2005-05-13 06:27:30 +05:30
|
|
|
int i;
|
2008-07-15 12:15:14 +05:30
|
|
|
int order;
|
2007-04-07 06:15:27 +05:30
|
|
|
|
|
|
|
opt_complementary = "=2";
|
2008-07-15 12:15:14 +05:30
|
|
|
getopt32(argv, "123");
|
2007-04-07 06:15:27 +05:30
|
|
|
argv += optind;
|
2005-05-13 03:55:41 +05:30
|
|
|
|
2005-05-13 06:27:30 +05:30
|
|
|
for (i = 0; i < 2; ++i) {
|
2008-07-15 12:15:14 +05:30
|
|
|
stream[i] = xfopen_stdin(argv[i]);
|
2005-05-12 04:42:49 +05:30
|
|
|
}
|
|
|
|
|
2008-07-15 12:15:14 +05:30
|
|
|
order = 0;
|
|
|
|
thisline[1] = thisline[0] = NULL;
|
|
|
|
while (1) {
|
|
|
|
if (order <= 0) {
|
|
|
|
free(thisline[0]);
|
|
|
|
thisline[0] = xmalloc_fgetline(stream[0]);
|
|
|
|
}
|
|
|
|
if (order >= 0) {
|
|
|
|
free(thisline[1]);
|
|
|
|
thisline[1] = xmalloc_fgetline(stream[1]);
|
2005-05-12 04:42:49 +05:30
|
|
|
}
|
2005-05-13 03:55:41 +05:30
|
|
|
|
2008-07-15 12:15:14 +05:30
|
|
|
i = !thisline[0] + (!thisline[1] << 1);
|
|
|
|
if (i)
|
2005-05-12 04:42:49 +05:30
|
|
|
break;
|
2008-07-15 12:15:14 +05:30
|
|
|
order = strcmp(thisline[0], thisline[1]);
|
2005-05-12 04:42:49 +05:30
|
|
|
|
2008-07-15 12:15:14 +05:30
|
|
|
if (order >= 0)
|
|
|
|
writeline(thisline[1], order ? 1 : 2);
|
|
|
|
else
|
|
|
|
writeline(thisline[0], 0);
|
|
|
|
}
|
2005-05-13 03:55:41 +05:30
|
|
|
|
2008-07-15 12:15:14 +05:30
|
|
|
/* EOF at least on one of the streams */
|
|
|
|
i &= 1;
|
|
|
|
if (thisline[i]) {
|
|
|
|
/* stream[i] is not at EOF yet */
|
|
|
|
/* we did not print thisline[i] yet */
|
|
|
|
char *p = thisline[i];
|
|
|
|
writeline(p, i);
|
|
|
|
while (1) {
|
|
|
|
free(p);
|
|
|
|
p = xmalloc_fgetline(stream[i]);
|
|
|
|
if (!p)
|
|
|
|
break;
|
|
|
|
writeline(p, i);
|
2005-05-12 04:42:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-07 06:15:27 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
2008-07-15 12:15:14 +05:30
|
|
|
fclose(stream[0]);
|
|
|
|
fclose(stream[1]);
|
2007-04-07 06:15:27 +05:30
|
|
|
}
|
2005-05-12 04:42:49 +05:30
|
|
|
|
2007-04-07 06:15:27 +05:30
|
|
|
return EXIT_SUCCESS;
|
2005-05-12 04:42:49 +05:30
|
|
|
}
|