diff options
author | Mike Frysinger <vapier@gentoo.org> | 2005-05-13 00:57:30 +0000 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2005-05-13 00:57:30 +0000 |
commit | 867a6080fe48b8d23c3845dcb875ba969eeee066 (patch) | |
tree | abff926319a94390fb6aeb1dc516dab28a519958 | |
parent | dad4cf7e6307b5b6032d928f0c897235b3e1042d (diff) | |
download | busybox-w32-867a6080fe48b8d23c3845dcb875ba969eeee066.tar.gz busybox-w32-867a6080fe48b8d23c3845dcb875ba969eeee066.tar.bz2 busybox-w32-867a6080fe48b8d23c3845dcb875ba969eeee066.zip |
remove duplicated code and rework algorithms to end up with smaller code
-rw-r--r-- | coreutils/comm.c | 78 |
1 files changed, 38 insertions, 40 deletions
diff --git a/coreutils/comm.c b/coreutils/comm.c index b0384fe87..7471c3b31 100644 --- a/coreutils/comm.c +++ b/coreutils/comm.c | |||
@@ -40,15 +40,16 @@ static int both; | |||
40 | /* writeline outputs the input given, appropriately aligned according to class */ | 40 | /* writeline outputs the input given, appropriately aligned according to class */ |
41 | static void writeline(char *line, int class) | 41 | static void writeline(char *line, int class) |
42 | { | 42 | { |
43 | if (class == 1 && !only_file_1) | 43 | if (class == 0) { |
44 | if (!only_file_1) | ||
44 | return; | 45 | return; |
45 | else if (class == 2) { | 46 | } else if (class == 1) { |
46 | if (!only_file_2) | 47 | if (!only_file_2) |
47 | return; | 48 | return; |
48 | if (only_file_1) | 49 | if (only_file_1) |
49 | putchar('\t'); | 50 | putchar('\t'); |
50 | } | 51 | } |
51 | else if (class == 3) { | 52 | else /*if (class == 2)*/ { |
52 | if (!both) | 53 | if (!both) |
53 | return; | 54 | return; |
54 | if (only_file_1) | 55 | if (only_file_1) |
@@ -60,73 +61,69 @@ static void writeline(char *line, int class) | |||
60 | } | 61 | } |
61 | 62 | ||
62 | /* This is the real core of the program - lines are compared here */ | 63 | /* This is the real core of the program - lines are compared here */ |
63 | static int cmp_files(char **infiles) | 64 | static void cmp_files(char **infiles) |
64 | { | 65 | { |
65 | char thisline[2][100]; | 66 | #define LINE_LEN 100 |
67 | #define BB_EOF_0 0x1 | ||
68 | #define BB_EOF_1 0x2 | ||
69 | char thisline[2][LINE_LEN]; | ||
66 | FILE *streams[2]; | 70 | FILE *streams[2]; |
67 | int i = 0; | 71 | int i; |
68 | 72 | ||
69 | for (i = 0; i < 2; i++) { | 73 | for (i = 0; i < 2; ++i) { |
70 | streams[i] = (strcmp(infiles[i], "=") == 0 ? stdin : bb_xfopen(infiles[i], "r")); | 74 | streams[i] = ((infiles[i][0] == '=' && infiles[1][1]) ? stdin : bb_xfopen(infiles[i], "r")); |
71 | fgets(thisline[i], 100, streams[i]); | 75 | fgets(thisline[i], LINE_LEN, streams[i]); |
72 | } | 76 | } |
73 | 77 | ||
74 | while (thisline[0] || thisline[1]) { | 78 | while (thisline[0] || thisline[1]) { |
75 | int order = 0; | 79 | int order = 0; |
76 | int tl0_len = strlen(thisline[0]); | 80 | |
77 | int tl1_len = strlen(thisline[1]); | 81 | i = 0; |
82 | if (feof(streams[0])) i |= BB_EOF_0; | ||
83 | if (feof(streams[1])) i |= BB_EOF_1; | ||
78 | 84 | ||
79 | if (!thisline[0]) | 85 | if (!thisline[0]) |
80 | order = 1; | 86 | order = 1; |
81 | else if (!thisline[1]) | 87 | else if (!thisline[1]) |
82 | order = -1; | 88 | order = -1; |
83 | else { | 89 | else { |
90 | int tl0_len, tl1_len; | ||
91 | tl0_len = strlen(thisline[0]); | ||
92 | tl1_len = strlen(thisline[1]); | ||
84 | order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len); | 93 | order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len); |
85 | if (!order) | 94 | if (!order) |
86 | order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len; | 95 | order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len; |
87 | } | 96 | } |
88 | 97 | ||
89 | if ((order == 0) && (!feof(streams[0])) && (!feof(streams[1]))) | 98 | if (order == 0 && !i) |
90 | writeline(thisline[1], 3); | ||
91 | else if ((order > 0) && (!feof(streams[1]))) | ||
92 | writeline(thisline[1], 2); | 99 | writeline(thisline[1], 2); |
93 | else if ((order < 0) && (!feof(streams[0]))) | 100 | else if (order > 0 && !(i & BB_EOF_1)) |
94 | writeline(thisline[0], 1); | 101 | writeline(thisline[1], 1); |
95 | 102 | else if (order < 0 && !(i & BB_EOF_0)) | |
96 | if (feof(streams[0]) && feof(streams[1])) { | 103 | writeline(thisline[0], 0); |
97 | fclose(streams[0]); | ||
98 | fclose(streams[1]); | ||
99 | break; | ||
100 | 104 | ||
101 | } else if (feof(streams[0])) { | 105 | if (i & BB_EOF_0 & BB_EOF_1) { |
102 | while (!feof(streams[1])) { | ||
103 | if (order < 0) | ||
104 | writeline(thisline[1], 2); | ||
105 | fgets(thisline[1], 100, streams[1]); | ||
106 | } | ||
107 | fclose(streams[0]); | ||
108 | fclose(streams[1]); | ||
109 | break; | 106 | break; |
110 | 107 | ||
111 | } else if (feof(streams[1])) { | 108 | } else if (i) { |
112 | while (!feof(streams[0])) { | 109 | i = (i & BB_EOF_0 ? 1 : 0); |
113 | if (order > 0) | 110 | while (!feof(streams[i])) { |
114 | writeline(thisline[0], 1); | 111 | if ((order < 0 && i) || (order > 0 && !i)) |
115 | fgets(thisline[0], 100, streams[0]); | 112 | writeline(thisline[i], i); |
113 | fgets(thisline[i], LINE_LEN, streams[i]); | ||
116 | } | 114 | } |
117 | fclose(streams[0]); | ||
118 | fclose(streams[1]); | ||
119 | break; | 115 | break; |
120 | 116 | ||
121 | } else { | 117 | } else { |
122 | if (order >= 0) | 118 | if (order >= 0) |
123 | fgets(thisline[1], 100, streams[1]); | 119 | fgets(thisline[1], LINE_LEN, streams[1]); |
124 | if (order <= 0) | 120 | if (order <= 0) |
125 | fgets(thisline[0], 100, streams[0]); | 121 | fgets(thisline[0], LINE_LEN, streams[0]); |
126 | } | 122 | } |
127 | } | 123 | } |
128 | 124 | ||
129 | return 0; | 125 | fclose(streams[0]); |
126 | fclose(streams[1]); | ||
130 | } | 127 | } |
131 | 128 | ||
132 | int comm_main(int argc, char **argv) | 129 | int comm_main(int argc, char **argv) |
@@ -142,5 +139,6 @@ int comm_main(int argc, char **argv) | |||
142 | only_file_2 = !(flags & COMM_OPT_2); | 139 | only_file_2 = !(flags & COMM_OPT_2); |
143 | both = !(flags & COMM_OPT_3); | 140 | both = !(flags & COMM_OPT_3); |
144 | 141 | ||
145 | exit(cmp_files(argv + optind) == 0 ? EXIT_SUCCESS : EXIT_FAILURE); | 142 | cmp_files(argv + optind); |
143 | exit(EXIT_SUCCESS); | ||
146 | } | 144 | } |