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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 10 | //usage:#define comm_trivial_usage |
| 11 | //usage: "[-123] FILE1 FILE2" |
| 12 | //usage:#define comm_full_usage "\n\n" |
| 13 | //usage: "Compare FILE1 with FILE2\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 14 | //usage: "\n -1 Suppress lines unique to FILE1" |
| 15 | //usage: "\n -2 Suppress lines unique to FILE2" |
| 16 | //usage: "\n -3 Suppress lines common to both files" |
| 17 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 19 | |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 20 | #define COMM_OPT_1 (1 << 0) |
| 21 | #define COMM_OPT_2 (1 << 1) |
| 22 | #define COMM_OPT_3 (1 << 2) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 23 | |
| 24 | /* writeline outputs the input given, appropriately aligned according to class */ |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 25 | static void writeline(char *line, int class) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 26 | { |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 27 | int flags = option_mask32; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 28 | if (class == 0) { |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 29 | if (flags & COMM_OPT_1) |
Mike Frysinger | 0fb397e | 2005-05-13 00:58:18 +0000 | [diff] [blame] | 30 | return; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 31 | } else if (class == 1) { |
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 | return; |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 34 | if (!(flags & COMM_OPT_1)) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 35 | putchar('\t'); |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 36 | } else /*if (class == 2)*/ { |
| 37 | if (flags & COMM_OPT_3) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 38 | return; |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 39 | if (!(flags & COMM_OPT_1)) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 40 | putchar('\t'); |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 41 | if (!(flags & COMM_OPT_2)) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 42 | putchar('\t'); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 43 | } |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 44 | puts(line); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 47 | int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 48 | int comm_main(int argc UNUSED_PARAM, char **argv) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 49 | { |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 50 | char *thisline[2]; |
| 51 | FILE *stream[2]; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 52 | int i; |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 53 | int order; |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 54 | |
| 55 | opt_complementary = "=2"; |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 56 | getopt32(argv, "123"); |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 57 | argv += optind; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 58 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 59 | for (i = 0; i < 2; ++i) { |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 60 | stream[i] = xfopen_stdin(argv[i]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 63 | order = 0; |
| 64 | thisline[1] = thisline[0] = NULL; |
| 65 | while (1) { |
| 66 | if (order <= 0) { |
| 67 | free(thisline[0]); |
| 68 | thisline[0] = xmalloc_fgetline(stream[0]); |
| 69 | } |
| 70 | if (order >= 0) { |
| 71 | free(thisline[1]); |
| 72 | thisline[1] = xmalloc_fgetline(stream[1]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 73 | } |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 75 | i = !thisline[0] + (!thisline[1] << 1); |
| 76 | if (i) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 77 | break; |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 78 | order = strcmp(thisline[0], thisline[1]); |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 79 | |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 80 | if (order >= 0) |
| 81 | writeline(thisline[1], order ? 1 : 2); |
| 82 | else |
| 83 | writeline(thisline[0], 0); |
| 84 | } |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 85 | |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 86 | /* EOF at least on one of the streams */ |
| 87 | i &= 1; |
| 88 | if (thisline[i]) { |
| 89 | /* stream[i] is not at EOF yet */ |
| 90 | /* we did not print thisline[i] yet */ |
| 91 | char *p = thisline[i]; |
| 92 | writeline(p, i); |
| 93 | while (1) { |
| 94 | free(p); |
| 95 | p = xmalloc_fgetline(stream[i]); |
| 96 | if (!p) |
| 97 | break; |
| 98 | writeline(p, i); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 102 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 103 | fclose(stream[0]); |
| 104 | fclose(stream[1]); |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 105 | } |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 106 | |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 107 | return EXIT_SUCCESS; |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 108 | } |