X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=coreutils%2Fcomm.c;h=221cbfbcc844320258aeac7f5b149b9931e3d581;hb=5b492ee87614632e7bf5cd68fe0ce87dad30002a;hp=b6412968d356768fb85ec6157ba7794ec8be5510;hpb=06af2165288cd6516b89001ec9e24992619230e0;p=oweals%2Fbusybox.git diff --git a/coreutils/comm.c b/coreutils/comm.c index b6412968d..221cbfbcc 100644 --- a/coreutils/comm.c +++ b/coreutils/comm.c @@ -7,121 +7,94 @@ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ -#include "busybox.h" +#include "libbb.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; +#define COMM_OPT_1 (1 << 0) +#define COMM_OPT_2 (1 << 1) +#define COMM_OPT_3 (1 << 2) /* writeline outputs the input given, appropriately aligned according to class */ static void writeline(char *line, int class) { + int flags = option_mask32; if (class == 0) { - if (!only_file_1) + if (flags & COMM_OPT_1) return; } else if (class == 1) { - if (!only_file_2) + if (flags & COMM_OPT_2) return; - if (only_file_1) + if (!(flags & COMM_OPT_1)) putchar('\t'); - } - else /*if (class == 2)*/ { - if (!both) + } else /*if (class == 2)*/ { + if (flags & COMM_OPT_3) return; - if (only_file_1) + if (!(flags & COMM_OPT_1)) putchar('\t'); - if (only_file_2) + if (!(flags & COMM_OPT_2)) putchar('\t'); } - fputs(line, stdout); + puts(line); } -/* This is the real core of the program - lines are compared here */ -static void cmp_files(char **infiles) +int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int comm_main(int argc UNUSED_PARAM, char **argv) { -#define LINE_LEN 100 -#define BB_EOF_0 0x1 -#define BB_EOF_1 0x2 - char thisline[2][LINE_LEN]; - FILE *streams[2]; + char *thisline[2]; + FILE *stream[2]; int i; + int order; + + opt_complementary = "=2"; + getopt32(argv, "123"); + argv += optind; for (i = 0; i < 2; ++i) { - streams[i] = ((infiles[i][0] == '=' && infiles[i][1]) ? stdin : xfopen(infiles[i], "r")); - fgets(thisline[i], LINE_LEN, streams[i]); + stream[i] = xfopen_stdin(argv[i]); } - while (*thisline[0] || *thisline[1]) { - int order = 0; - - 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; + order = 0; + thisline[1] = thisline[0] = NULL; + while (1) { + if (order <= 0) { + free(thisline[0]); + thisline[0] = xmalloc_fgetline(stream[0]); + } + if (order >= 0) { + free(thisline[1]); + thisline[1] = xmalloc_fgetline(stream[1]); } - if (order == 0 && !i) - writeline(thisline[1], 2); - else if (order > 0 && !(i & BB_EOF_1)) - writeline(thisline[1], 1); - else if (order < 0 && !(i & BB_EOF_0)) - writeline(thisline[0], 0); - - if (i & BB_EOF_0 & BB_EOF_1) { + i = !thisline[0] + (!thisline[1] << 1); + if (i) break; + order = strcmp(thisline[0], thisline[1]); - } 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]); - } - break; + if (order >= 0) + writeline(thisline[1], order ? 1 : 2); + else + writeline(thisline[0], 0); + } - } else { - if (order >= 0) - fgets(thisline[1], LINE_LEN, streams[1]); - if (order <= 0) - fgets(thisline[0], LINE_LEN, streams[0]); + /* EOF at least on one of the streams */ + i &= 1; + if (thisline[i]) { + /* stream[i] is not at EOF yet */ + /* we did not print thisline[i] yet */ + char *p = thisline[i]; + writeline(p, i); + while (1) { + free(p); + p = xmalloc_fgetline(stream[i]); + if (!p) + break; + writeline(p, i); } } - fclose(streams[0]); - fclose(streams[1]); -} - -int comm_main(int argc, char **argv); -int comm_main(int argc, char **argv) -{ - unsigned long flags; - - flags = getopt32(argc, argv, "123"); - - if (optind + 2 != argc) - bb_show_usage(); - - only_file_1 = !(flags & COMM_OPT_1); - only_file_2 = !(flags & COMM_OPT_2); - both = !(flags & COMM_OPT_3); + if (ENABLE_FEATURE_CLEAN_UP) { + fclose(stream[0]); + fclose(stream[1]); + } - cmp_files(argv + optind); - exit(EXIT_SUCCESS); + return EXIT_SUCCESS; }