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 | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 17 | static void writeline(char *line, int class) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 18 | { |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 19 | int flags = option_mask32; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 20 | if (class == 0) { |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 21 | if (flags & COMM_OPT_1) |
Mike Frysinger | 0fb397e | 2005-05-13 00:58:18 +0000 | [diff] [blame] | 22 | return; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 23 | } else if (class == 1) { |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 24 | if (flags & COMM_OPT_2) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 25 | return; |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 26 | if (!(flags & COMM_OPT_1)) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 27 | putchar('\t'); |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 28 | } else /*if (class == 2)*/ { |
| 29 | if (flags & COMM_OPT_3) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 30 | return; |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 31 | if (!(flags & COMM_OPT_1)) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 32 | putchar('\t'); |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 33 | if (!(flags & COMM_OPT_2)) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 34 | putchar('\t'); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 35 | } |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 36 | puts(line); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 39 | int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 40 | int comm_main(int argc UNUSED_PARAM, char **argv) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 41 | { |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 42 | char *thisline[2]; |
| 43 | FILE *stream[2]; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 44 | int i; |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 45 | int order; |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 46 | |
| 47 | opt_complementary = "=2"; |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 48 | getopt32(argv, "123"); |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 49 | argv += optind; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 50 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 51 | for (i = 0; i < 2; ++i) { |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 52 | stream[i] = xfopen_stdin(argv[i]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 55 | order = 0; |
| 56 | thisline[1] = thisline[0] = NULL; |
| 57 | while (1) { |
| 58 | if (order <= 0) { |
| 59 | free(thisline[0]); |
| 60 | thisline[0] = xmalloc_fgetline(stream[0]); |
| 61 | } |
| 62 | if (order >= 0) { |
| 63 | free(thisline[1]); |
| 64 | thisline[1] = xmalloc_fgetline(stream[1]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 65 | } |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 67 | i = !thisline[0] + (!thisline[1] << 1); |
| 68 | if (i) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 69 | break; |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 70 | order = strcmp(thisline[0], thisline[1]); |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 71 | |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 72 | if (order >= 0) |
| 73 | writeline(thisline[1], order ? 1 : 2); |
| 74 | else |
| 75 | writeline(thisline[0], 0); |
| 76 | } |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 78 | /* EOF at least on one of the streams */ |
| 79 | i &= 1; |
| 80 | if (thisline[i]) { |
| 81 | /* stream[i] is not at EOF yet */ |
| 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); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 94 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 95 | fclose(stream[0]); |
| 96 | fclose(stream[1]); |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 97 | } |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 98 | |
Denis Vlasenko | bb5b01c | 2007-04-07 00:45:27 +0000 | [diff] [blame] | 99 | return EXIT_SUCCESS; |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 100 | } |