2005-05-11 23:12:49 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini comm implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Robert Sullivan <cogito.ergo.cogito@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 of the License, 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
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
* 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "busybox.h"
|
|
|
|
|
|
|
|
#define COMM_OPT_1 0x01
|
|
|
|
#define COMM_OPT_2 0x02
|
|
|
|
#define COMM_OPT_3 0x04
|
|
|
|
|
|
|
|
/* These three variables control behaviour if non-zero */
|
|
|
|
|
|
|
|
static int only_file_1;
|
|
|
|
static int only_file_2;
|
|
|
|
static int both;
|
|
|
|
|
|
|
|
/* writeline outputs the input given, appropriately aligned according to class */
|
2005-05-12 22:41:13 +00:00
|
|
|
static void writeline(char *line, int class)
|
2005-05-12 22:25:41 +00:00
|
|
|
{
|
2005-05-13 00:57:30 +00:00
|
|
|
if (class == 0) {
|
|
|
|
if (!only_file_1)
|
2005-05-13 00:58:18 +00:00
|
|
|
return;
|
2005-05-13 00:57:30 +00:00
|
|
|
} else if (class == 1) {
|
2005-05-12 22:50:12 +00:00
|
|
|
if (!only_file_2)
|
|
|
|
return;
|
|
|
|
if (only_file_1)
|
|
|
|
putchar('\t');
|
|
|
|
}
|
2005-05-13 00:57:30 +00:00
|
|
|
else /*if (class == 2)*/ {
|
2005-05-12 22:50:12 +00:00
|
|
|
if (!both)
|
|
|
|
return;
|
|
|
|
if (only_file_1)
|
|
|
|
putchar('\t');
|
|
|
|
if (only_file_2)
|
|
|
|
putchar('\t');
|
2005-05-11 23:12:49 +00:00
|
|
|
}
|
2005-05-12 22:25:41 +00:00
|
|
|
fputs(line, stdout);
|
2005-05-11 23:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is the real core of the program - lines are compared here */
|
2005-05-13 00:57:30 +00:00
|
|
|
static void cmp_files(char **infiles)
|
2005-05-12 22:25:41 +00:00
|
|
|
{
|
2005-05-13 00:57:30 +00:00
|
|
|
#define LINE_LEN 100
|
|
|
|
#define BB_EOF_0 0x1
|
|
|
|
#define BB_EOF_1 0x2
|
|
|
|
char thisline[2][LINE_LEN];
|
2005-05-11 23:12:49 +00:00
|
|
|
FILE *streams[2];
|
2005-05-13 00:57:30 +00:00
|
|
|
int i;
|
2005-05-12 22:25:41 +00:00
|
|
|
|
2005-05-13 00:57:30 +00:00
|
|
|
for (i = 0; i < 2; ++i) {
|
2005-05-13 03:09:20 +00:00
|
|
|
streams[i] = ((infiles[i][0] == '=' && infiles[i][1]) ? stdin : bb_xfopen(infiles[i], "r"));
|
2005-05-13 00:57:30 +00:00
|
|
|
fgets(thisline[i], LINE_LEN, streams[i]);
|
2005-05-11 23:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (thisline[0] || thisline[1]) {
|
|
|
|
int order = 0;
|
2005-05-13 00:57:30 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
if (feof(streams[0])) i |= BB_EOF_0;
|
|
|
|
if (feof(streams[1])) i |= BB_EOF_1;
|
2005-05-12 22:25:41 +00:00
|
|
|
|
2005-05-11 23:12:49 +00:00
|
|
|
if (!thisline[0])
|
|
|
|
order = 1;
|
2005-05-12 22:25:41 +00:00
|
|
|
else if (!thisline[1])
|
2005-05-11 23:12:49 +00:00
|
|
|
order = -1;
|
|
|
|
else {
|
2005-05-13 00:57:30 +00:00
|
|
|
int tl0_len, tl1_len;
|
|
|
|
tl0_len = strlen(thisline[0]);
|
|
|
|
tl1_len = strlen(thisline[1]);
|
2005-05-11 23:12:49 +00:00
|
|
|
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-12 22:25:41 +00:00
|
|
|
|
2005-05-13 00:57:30 +00:00
|
|
|
if (order == 0 && !i)
|
2005-05-11 23:12:49 +00:00
|
|
|
writeline(thisline[1], 2);
|
2005-05-13 00:57:30 +00:00
|
|
|
else if (order > 0 && !(i & BB_EOF_1))
|
|
|
|
writeline(thisline[1], 1);
|
|
|
|
else if (order < 0 && !(i & BB_EOF_0))
|
|
|
|
writeline(thisline[0], 0);
|
2005-05-12 22:25:41 +00:00
|
|
|
|
2005-05-13 00:57:30 +00:00
|
|
|
if (i & BB_EOF_0 & BB_EOF_1) {
|
2005-05-11 23:12:49 +00:00
|
|
|
break;
|
|
|
|
|
2005-05-13 00:57:30 +00:00
|
|
|
} else if (i) {
|
|
|
|
i = (i & BB_EOF_0 ? 1 : 0);
|
|
|
|
while (!feof(streams[i])) {
|
|
|
|
if ((order < 0 && i) || (order > 0 && !i))
|
|
|
|
writeline(thisline[i], i);
|
|
|
|
fgets(thisline[i], LINE_LEN, streams[i]);
|
2005-05-11 23:12:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2005-05-12 22:25:41 +00:00
|
|
|
|
|
|
|
} else {
|
2005-05-11 23:12:49 +00:00
|
|
|
if (order >= 0)
|
2005-05-13 00:57:30 +00:00
|
|
|
fgets(thisline[1], LINE_LEN, streams[1]);
|
2005-05-11 23:12:49 +00:00
|
|
|
if (order <= 0)
|
2005-05-13 00:57:30 +00:00
|
|
|
fgets(thisline[0], LINE_LEN, streams[0]);
|
2005-05-11 23:12:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-13 00:57:30 +00:00
|
|
|
fclose(streams[0]);
|
|
|
|
fclose(streams[1]);
|
2005-05-11 23:12:49 +00:00
|
|
|
}
|
|
|
|
|
2005-05-12 22:41:13 +00:00
|
|
|
int comm_main(int argc, char **argv)
|
2005-05-12 22:25:41 +00:00
|
|
|
{
|
2005-05-12 22:41:13 +00:00
|
|
|
unsigned long flags;
|
2005-05-12 22:25:41 +00:00
|
|
|
|
2005-05-12 22:41:13 +00:00
|
|
|
flags = bb_getopt_ulflags(argc, argv, "123");
|
2005-05-12 22:25:41 +00:00
|
|
|
|
2005-05-12 22:41:13 +00:00
|
|
|
if (optind + 2 != argc)
|
2005-05-12 22:25:41 +00:00
|
|
|
bb_show_usage();
|
|
|
|
|
2005-05-12 22:41:13 +00:00
|
|
|
only_file_1 = !(flags & COMM_OPT_1);
|
|
|
|
only_file_2 = !(flags & COMM_OPT_2);
|
|
|
|
both = !(flags & COMM_OPT_3);
|
2005-05-11 23:12:49 +00:00
|
|
|
|
2005-05-13 00:57:30 +00:00
|
|
|
cmp_files(argv + optind);
|
|
|
|
exit(EXIT_SUCCESS);
|
2005-05-11 23:12:49 +00:00
|
|
|
}
|