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>
|
|
|
|
*
|
2006-04-05 00:56:37 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2005-05-12 04:42:49 +05:30
|
|
|
*/
|
|
|
|
|
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 */
|
2007-04-07 06:15:27 +05:30
|
|
|
static void writeline(char *line, int class, int flags)
|
2005-05-13 03:55:41 +05:30
|
|
|
{
|
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
|
|
|
}
|
2005-05-13 03:55:41 +05:30
|
|
|
fputs(line, stdout);
|
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
|
|
|
{
|
2005-05-13 06:27:30 +05:30
|
|
|
#define LINE_LEN 100
|
|
|
|
#define BB_EOF_0 0x1
|
|
|
|
#define BB_EOF_1 0x2
|
|
|
|
char thisline[2][LINE_LEN];
|
2005-05-12 04:42:49 +05:30
|
|
|
FILE *streams[2];
|
2005-05-13 06:27:30 +05:30
|
|
|
int i;
|
2007-04-07 06:15:27 +05:30
|
|
|
unsigned flags;
|
|
|
|
|
|
|
|
opt_complementary = "=2";
|
2007-08-18 21:02:12 +05:30
|
|
|
flags = 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) {
|
2007-04-07 06:15:27 +05:30
|
|
|
streams[i] = (argv[i][0] == '-' && !argv[i][1]) ? stdin : xfopen(argv[i], "r");
|
2005-05-13 06:27:30 +05:30
|
|
|
fgets(thisline[i], LINE_LEN, streams[i]);
|
2005-05-12 04:42:49 +05:30
|
|
|
}
|
|
|
|
|
2007-04-07 06:15:27 +05:30
|
|
|
/* This is the real core of the program - lines are compared here */
|
|
|
|
|
2006-04-05 00:56:37 +05:30
|
|
|
while (*thisline[0] || *thisline[1]) {
|
2005-05-12 04:42:49 +05:30
|
|
|
int order = 0;
|
2005-05-13 06:27:30 +05:30
|
|
|
|
|
|
|
i = 0;
|
|
|
|
if (feof(streams[0])) i |= BB_EOF_0;
|
|
|
|
if (feof(streams[1])) i |= BB_EOF_1;
|
2005-05-13 03:55:41 +05:30
|
|
|
|
2006-04-05 00:56:37 +05:30
|
|
|
if (!*thisline[0])
|
2005-05-12 04:42:49 +05:30
|
|
|
order = 1;
|
2006-04-05 00:56:37 +05:30
|
|
|
else if (!*thisline[1])
|
2005-05-12 04:42:49 +05:30
|
|
|
order = -1;
|
|
|
|
else {
|
2005-05-13 06:27:30 +05:30
|
|
|
int tl0_len, tl1_len;
|
|
|
|
tl0_len = strlen(thisline[0]);
|
|
|
|
tl1_len = strlen(thisline[1]);
|
2005-05-12 04:42:49 +05:30
|
|
|
order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len);
|
|
|
|
if (!order)
|
|
|
|
order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len;
|
|
|
|
}
|
2005-05-13 03:55:41 +05:30
|
|
|
|
2005-05-13 06:27:30 +05:30
|
|
|
if (order == 0 && !i)
|
2007-04-07 06:15:27 +05:30
|
|
|
writeline(thisline[1], 2, flags);
|
2005-05-13 06:27:30 +05:30
|
|
|
else if (order > 0 && !(i & BB_EOF_1))
|
2007-04-07 06:15:27 +05:30
|
|
|
writeline(thisline[1], 1, flags);
|
2005-05-13 06:27:30 +05:30
|
|
|
else if (order < 0 && !(i & BB_EOF_0))
|
2007-04-07 06:15:27 +05:30
|
|
|
writeline(thisline[0], 0, flags);
|
2005-05-13 03:55:41 +05:30
|
|
|
|
2005-05-13 06:27:30 +05:30
|
|
|
if (i & BB_EOF_0 & BB_EOF_1) {
|
2005-05-12 04:42:49 +05:30
|
|
|
break;
|
|
|
|
|
2005-05-13 06:27:30 +05:30
|
|
|
} else if (i) {
|
|
|
|
i = (i & BB_EOF_0 ? 1 : 0);
|
|
|
|
while (!feof(streams[i])) {
|
|
|
|
if ((order < 0 && i) || (order > 0 && !i))
|
2007-04-07 06:15:27 +05:30
|
|
|
writeline(thisline[i], i, flags);
|
2005-05-13 06:27:30 +05:30
|
|
|
fgets(thisline[i], LINE_LEN, streams[i]);
|
2005-05-12 04:42:49 +05:30
|
|
|
}
|
|
|
|
break;
|
2005-05-13 03:55:41 +05:30
|
|
|
|
|
|
|
} else {
|
2005-05-12 04:42:49 +05:30
|
|
|
if (order >= 0)
|
2005-05-13 06:27:30 +05:30
|
|
|
fgets(thisline[1], LINE_LEN, streams[1]);
|
2005-05-12 04:42:49 +05:30
|
|
|
if (order <= 0)
|
2005-05-13 06:27:30 +05:30
|
|
|
fgets(thisline[0], LINE_LEN, streams[0]);
|
2005-05-12 04:42:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-07 06:15:27 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
|
|
|
fclose(streams[0]);
|
|
|
|
fclose(streams[1]);
|
|
|
|
}
|
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
|
|
|
}
|