summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Frysinger2005-05-13 00:57:30 +0000
committerMike Frysinger2005-05-13 00:57:30 +0000
commit867a6080fe48b8d23c3845dcb875ba969eeee066 (patch)
treeabff926319a94390fb6aeb1dc516dab28a519958
parentdad4cf7e6307b5b6032d928f0c897235b3e1042d (diff)
downloadbusybox-867a6080fe48b8d23c3845dcb875ba969eeee066.zip
busybox-867a6080fe48b8d23c3845dcb875ba969eeee066.tar.gz
remove duplicated code and rework algorithms to end up with smaller code
-rw-r--r--coreutils/comm.c78
1 files changed, 38 insertions, 40 deletions
diff --git a/coreutils/comm.c b/coreutils/comm.c
index b0384fe..7471c3b 100644
--- a/coreutils/comm.c
+++ b/coreutils/comm.c
@@ -40,15 +40,16 @@ static int both;
/* writeline outputs the input given, appropriately aligned according to class */
static void writeline(char *line, int class)
{
- if (class == 1 && !only_file_1)
+ if (class == 0) {
+ if (!only_file_1)
return;
- else if (class == 2) {
+ } else if (class == 1) {
if (!only_file_2)
return;
if (only_file_1)
putchar('\t');
}
- else if (class == 3) {
+ else /*if (class == 2)*/ {
if (!both)
return;
if (only_file_1)
@@ -60,73 +61,69 @@ static void writeline(char *line, int class)
}
/* This is the real core of the program - lines are compared here */
-static int cmp_files(char **infiles)
+static void cmp_files(char **infiles)
{
- char thisline[2][100];
+#define LINE_LEN 100
+#define BB_EOF_0 0x1
+#define BB_EOF_1 0x2
+ char thisline[2][LINE_LEN];
FILE *streams[2];
- int i = 0;
+ int i;
- for (i = 0; i < 2; i++) {
- streams[i] = (strcmp(infiles[i], "=") == 0 ? stdin : bb_xfopen(infiles[i], "r"));
- fgets(thisline[i], 100, streams[i]);
+ for (i = 0; i < 2; ++i) {
+ streams[i] = ((infiles[i][0] == '=' && infiles[1][1]) ? stdin : bb_xfopen(infiles[i], "r"));
+ fgets(thisline[i], LINE_LEN, streams[i]);
}
while (thisline[0] || thisline[1]) {
int order = 0;
- int tl0_len = strlen(thisline[0]);
- int tl1_len = strlen(thisline[1]);
+
+ i = 0;
+ if (feof(streams[0])) i |= BB_EOF_0;
+ if (feof(streams[1])) i |= BB_EOF_1;
if (!thisline[0])
order = 1;
else if (!thisline[1])
order = -1;
else {
+ int tl0_len, tl1_len;
+ tl0_len = strlen(thisline[0]);
+ tl1_len = strlen(thisline[1]);
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;
}
- if ((order == 0) && (!feof(streams[0])) && (!feof(streams[1])))
- writeline(thisline[1], 3);
- else if ((order > 0) && (!feof(streams[1])))
+ if (order == 0 && !i)
writeline(thisline[1], 2);
- else if ((order < 0) && (!feof(streams[0])))
- writeline(thisline[0], 1);
-
- if (feof(streams[0]) && feof(streams[1])) {
- fclose(streams[0]);
- fclose(streams[1]);
- break;
+ else if (order > 0 && !(i & BB_EOF_1))
+ writeline(thisline[1], 1);
+ else if (order < 0 && !(i & BB_EOF_0))
+ writeline(thisline[0], 0);
- } else if (feof(streams[0])) {
- while (!feof(streams[1])) {
- if (order < 0)
- writeline(thisline[1], 2);
- fgets(thisline[1], 100, streams[1]);
- }
- fclose(streams[0]);
- fclose(streams[1]);
+ if (i & BB_EOF_0 & BB_EOF_1) {
break;
- } else if (feof(streams[1])) {
- while (!feof(streams[0])) {
- if (order > 0)
- writeline(thisline[0], 1);
- fgets(thisline[0], 100, streams[0]);
+ } 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]);
}
- fclose(streams[0]);
- fclose(streams[1]);
break;
} else {
if (order >= 0)
- fgets(thisline[1], 100, streams[1]);
+ fgets(thisline[1], LINE_LEN, streams[1]);
if (order <= 0)
- fgets(thisline[0], 100, streams[0]);
+ fgets(thisline[0], LINE_LEN, streams[0]);
}
}
- return 0;
+ fclose(streams[0]);
+ fclose(streams[1]);
}
int comm_main(int argc, char **argv)
@@ -142,5 +139,6 @@ int comm_main(int argc, char **argv)
only_file_2 = !(flags & COMM_OPT_2);
both = !(flags & COMM_OPT_3);
- exit(cmp_files(argv + optind) == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+ cmp_files(argv + optind);
+ exit(EXIT_SUCCESS);
}