diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-15 06:45:14 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-15 06:45:14 +0000 |
| commit | bb13079c8e9ca7bd86193a220baae1befb053fd6 (patch) | |
| tree | 81b489bcc56d1fb12d30ae05adcb68d2acc59288 /coreutils | |
| parent | 51f1b6c0e0160a3c836bc6700c3fa2a43601dfac (diff) | |
| download | busybox-w32-bb13079c8e9ca7bd86193a220baae1befb053fd6.tar.gz busybox-w32-bb13079c8e9ca7bd86193a220baae1befb053fd6.tar.bz2 busybox-w32-bb13079c8e9ca7bd86193a220baae1befb053fd6.zip | |
comm: almost total rewrite; add testsuite
function old new delta
writeline 128 125 -3
comm_main 540 239 -301
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-304) Total: -304 bytes
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/comm.c | 95 |
1 files changed, 41 insertions, 54 deletions
diff --git a/coreutils/comm.c b/coreutils/comm.c index b62c98772..221cbfbcc 100644 --- a/coreutils/comm.c +++ b/coreutils/comm.c | |||
| @@ -14,8 +14,9 @@ | |||
| 14 | #define COMM_OPT_3 (1 << 2) | 14 | #define COMM_OPT_3 (1 << 2) |
| 15 | 15 | ||
| 16 | /* writeline outputs the input given, appropriately aligned according to class */ | 16 | /* writeline outputs the input given, appropriately aligned according to class */ |
| 17 | static void writeline(char *line, int class, int flags) | 17 | static void writeline(char *line, int class) |
| 18 | { | 18 | { |
| 19 | int flags = option_mask32; | ||
| 19 | if (class == 0) { | 20 | if (class == 0) { |
| 20 | if (flags & COMM_OPT_1) | 21 | if (flags & COMM_OPT_1) |
| 21 | return; | 22 | return; |
| @@ -32,81 +33,67 @@ static void writeline(char *line, int class, int flags) | |||
| 32 | if (!(flags & COMM_OPT_2)) | 33 | if (!(flags & COMM_OPT_2)) |
| 33 | putchar('\t'); | 34 | putchar('\t'); |
| 34 | } | 35 | } |
| 35 | fputs(line, stdout); | 36 | puts(line); |
| 36 | } | 37 | } |
| 37 | 38 | ||
| 38 | int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 39 | int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 39 | int comm_main(int argc UNUSED_PARAM, char **argv) | 40 | int comm_main(int argc UNUSED_PARAM, char **argv) |
| 40 | { | 41 | { |
| 41 | #define LINE_LEN 100 | 42 | char *thisline[2]; |
| 42 | #define BB_EOF_0 0x1 | 43 | FILE *stream[2]; |
| 43 | #define BB_EOF_1 0x2 | ||
| 44 | char thisline[2][LINE_LEN]; | ||
| 45 | FILE *streams[2]; | ||
| 46 | int i; | 44 | int i; |
| 47 | unsigned flags; | 45 | int order; |
| 48 | 46 | ||
| 49 | opt_complementary = "=2"; | 47 | opt_complementary = "=2"; |
| 50 | flags = getopt32(argv, "123"); | 48 | getopt32(argv, "123"); |
| 51 | argv += optind; | 49 | argv += optind; |
| 52 | 50 | ||
| 53 | for (i = 0; i < 2; ++i) { | 51 | for (i = 0; i < 2; ++i) { |
| 54 | streams[i] = (argv[i][0] == '-' && !argv[i][1]) ? stdin : xfopen(argv[i], "r"); | 52 | stream[i] = xfopen_stdin(argv[i]); |
| 55 | fgets(thisline[i], LINE_LEN, streams[i]); | ||
| 56 | } | 53 | } |
| 57 | 54 | ||
| 58 | /* This is the real core of the program - lines are compared here */ | 55 | order = 0; |
| 59 | 56 | thisline[1] = thisline[0] = NULL; | |
| 60 | while (*thisline[0] || *thisline[1]) { | 57 | while (1) { |
| 61 | int order = 0; | 58 | if (order <= 0) { |
| 62 | 59 | free(thisline[0]); | |
| 63 | i = 0; | 60 | thisline[0] = xmalloc_fgetline(stream[0]); |
| 64 | if (feof(streams[0])) i |= BB_EOF_0; | 61 | } |
| 65 | if (feof(streams[1])) i |= BB_EOF_1; | 62 | if (order >= 0) { |
| 66 | 63 | free(thisline[1]); | |
| 67 | if (!*thisline[0]) | 64 | thisline[1] = xmalloc_fgetline(stream[1]); |
| 68 | order = 1; | ||
| 69 | else if (!*thisline[1]) | ||
| 70 | order = -1; | ||
| 71 | else { | ||
| 72 | int tl0_len, tl1_len; | ||
| 73 | tl0_len = strlen(thisline[0]); | ||
| 74 | tl1_len = strlen(thisline[1]); | ||
| 75 | order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len); | ||
| 76 | if (!order) | ||
| 77 | order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len; | ||
| 78 | } | 65 | } |
| 79 | 66 | ||
| 80 | if (order == 0 && !i) | 67 | i = !thisline[0] + (!thisline[1] << 1); |
| 81 | writeline(thisline[1], 2, flags); | 68 | if (i) |
| 82 | else if (order > 0 && !(i & BB_EOF_1)) | ||
| 83 | writeline(thisline[1], 1, flags); | ||
| 84 | else if (order < 0 && !(i & BB_EOF_0)) | ||
| 85 | writeline(thisline[0], 0, flags); | ||
| 86 | |||
| 87 | if (i & BB_EOF_0 & BB_EOF_1) { | ||
| 88 | break; | 69 | break; |
| 70 | order = strcmp(thisline[0], thisline[1]); | ||
| 89 | 71 | ||
| 90 | } else if (i) { | 72 | if (order >= 0) |
| 91 | i = (i & BB_EOF_0 ? 1 : 0); | 73 | writeline(thisline[1], order ? 1 : 2); |
| 92 | while (!feof(streams[i])) { | 74 | else |
| 93 | if ((order < 0 && i) || (order > 0 && !i)) | 75 | writeline(thisline[0], 0); |
| 94 | writeline(thisline[i], i, flags); | 76 | } |
| 95 | fgets(thisline[i], LINE_LEN, streams[i]); | ||
| 96 | } | ||
| 97 | break; | ||
| 98 | 77 | ||
| 99 | } else { | 78 | /* EOF at least on one of the streams */ |
| 100 | if (order >= 0) | 79 | i &= 1; |
| 101 | fgets(thisline[1], LINE_LEN, streams[1]); | 80 | if (thisline[i]) { |
| 102 | if (order <= 0) | 81 | /* stream[i] is not at EOF yet */ |
| 103 | fgets(thisline[0], LINE_LEN, streams[0]); | 82 | /* we did not print thisline[i] yet */ |
| 83 | char *p = thisline[i]; | ||
| 84 | writeline(p, i); | ||
| 85 | while (1) { | ||
| 86 | free(p); | ||
| 87 | p = xmalloc_fgetline(stream[i]); | ||
| 88 | if (!p) | ||
| 89 | break; | ||
| 90 | writeline(p, i); | ||
| 104 | } | 91 | } |
| 105 | } | 92 | } |
| 106 | 93 | ||
| 107 | if (ENABLE_FEATURE_CLEAN_UP) { | 94 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 108 | fclose(streams[0]); | 95 | fclose(stream[0]); |
| 109 | fclose(streams[1]); | 96 | fclose(stream[1]); |
| 110 | } | 97 | } |
| 111 | 98 | ||
| 112 | return EXIT_SUCCESS; | 99 | return EXIT_SUCCESS; |
