Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini comm implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2005 by Robert Sullivan <cogito.ergo.cogito@gmail.com> |
| 6 | * |
Rob Landley | e104d5f | 2006-04-04 19:26:37 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 11 | |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 12 | #define COMM_OPT_1 (1 << 0) |
| 13 | #define COMM_OPT_2 (1 << 1) |
| 14 | #define COMM_OPT_3 (1 << 2) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 15 | |
| 16 | /* writeline outputs the input given, appropriately aligned according to class */ |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 17 | static void writeline(char *line, int class, int flags) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 18 | { |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 19 | if (class == 0) { |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 20 | if (flags & COMM_OPT_1) |
Mike Frysinger | 0fb397e | 2005-05-13 00:58:18 +0000 | [diff] [blame] | 21 | return; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 22 | } else if (class == 1) { |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 23 | if (flags & COMM_OPT_2) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 24 | return; |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 25 | if (!(flags & COMM_OPT_1)) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 26 | putchar('\t'); |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 27 | } else /*if (class == 2)*/ { |
| 28 | if (flags & COMM_OPT_3) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 29 | return; |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 30 | if (!(flags & COMM_OPT_1)) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 31 | putchar('\t'); |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 32 | if (!(flags & COMM_OPT_2)) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 33 | putchar('\t'); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 34 | } |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 35 | fputs(line, stdout); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame^] | 38 | int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 39 | int comm_main(int argc, char **argv) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 40 | { |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 41 | #define LINE_LEN 100 |
| 42 | #define BB_EOF_0 0x1 |
| 43 | #define BB_EOF_1 0x2 |
| 44 | char thisline[2][LINE_LEN]; |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 45 | FILE *streams[2]; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 46 | int i; |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 47 | unsigned flags; |
| 48 | |
| 49 | opt_complementary = "=2"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 50 | flags = getopt32(argv, "123"); |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 51 | argv += optind; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 52 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 53 | for (i = 0; i < 2; ++i) { |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 54 | streams[i] = (argv[i][0] == '-' && !argv[i][1]) ? stdin : xfopen(argv[i], "r"); |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 55 | fgets(thisline[i], LINE_LEN, streams[i]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 58 | /* This is the real core of the program - lines are compared here */ |
| 59 | |
Rob Landley | e104d5f | 2006-04-04 19:26:37 +0000 | [diff] [blame] | 60 | while (*thisline[0] || *thisline[1]) { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 61 | int order = 0; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 62 | |
| 63 | i = 0; |
| 64 | if (feof(streams[0])) i |= BB_EOF_0; |
| 65 | if (feof(streams[1])) i |= BB_EOF_1; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 66 | |
Rob Landley | e104d5f | 2006-04-04 19:26:37 +0000 | [diff] [blame] | 67 | if (!*thisline[0]) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 68 | order = 1; |
Rob Landley | e104d5f | 2006-04-04 19:26:37 +0000 | [diff] [blame] | 69 | else if (!*thisline[1]) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 70 | order = -1; |
| 71 | else { |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 72 | int tl0_len, tl1_len; |
| 73 | tl0_len = strlen(thisline[0]); |
| 74 | tl1_len = strlen(thisline[1]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 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 | } |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 79 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 80 | if (order == 0 && !i) |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 81 | writeline(thisline[1], 2, flags); |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 82 | else if (order > 0 && !(i & BB_EOF_1)) |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 83 | writeline(thisline[1], 1, flags); |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 84 | else if (order < 0 && !(i & BB_EOF_0)) |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 85 | writeline(thisline[0], 0, flags); |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 86 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 87 | if (i & BB_EOF_0 & BB_EOF_1) { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 88 | break; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 89 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 90 | } else if (i) { |
| 91 | i = (i & BB_EOF_0 ? 1 : 0); |
| 92 | while (!feof(streams[i])) { |
| 93 | if ((order < 0 && i) || (order > 0 && !i)) |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 94 | writeline(thisline[i], i, flags); |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 95 | fgets(thisline[i], LINE_LEN, streams[i]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 96 | } |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 97 | break; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 98 | |
| 99 | } else { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 100 | if (order >= 0) |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 101 | fgets(thisline[1], LINE_LEN, streams[1]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 102 | if (order <= 0) |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 103 | fgets(thisline[0], LINE_LEN, streams[0]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 107 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 108 | fclose(streams[0]); |
| 109 | fclose(streams[1]); |
| 110 | } |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 111 | |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 112 | return EXIT_SUCCESS; |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 113 | } |