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 | |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 10 | #include "busybox.h" |
| 11 | |
| 12 | #define COMM_OPT_1 0x01 |
| 13 | #define COMM_OPT_2 0x02 |
| 14 | #define COMM_OPT_3 0x04 |
| 15 | |
| 16 | /* These three variables control behaviour if non-zero */ |
| 17 | |
| 18 | static int only_file_1; |
| 19 | static int only_file_2; |
| 20 | static int both; |
| 21 | |
| 22 | /* writeline outputs the input given, appropriately aligned according to class */ |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 23 | static void writeline(char *line, int class) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 24 | { |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 25 | if (class == 0) { |
| 26 | if (!only_file_1) |
Mike Frysinger | 0fb397e | 2005-05-13 00:58:18 +0000 | [diff] [blame] | 27 | return; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 28 | } else if (class == 1) { |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 29 | if (!only_file_2) |
| 30 | return; |
| 31 | if (only_file_1) |
| 32 | putchar('\t'); |
| 33 | } |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 34 | else /*if (class == 2)*/ { |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 35 | if (!both) |
| 36 | return; |
| 37 | if (only_file_1) |
| 38 | putchar('\t'); |
| 39 | if (only_file_2) |
| 40 | putchar('\t'); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 41 | } |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 42 | fputs(line, stdout); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* This is the real core of the program - lines are compared here */ |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 46 | static void cmp_files(char **infiles) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 47 | { |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 48 | #define LINE_LEN 100 |
| 49 | #define BB_EOF_0 0x1 |
| 50 | #define BB_EOF_1 0x2 |
| 51 | char thisline[2][LINE_LEN]; |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 52 | FILE *streams[2]; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 53 | int i; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 54 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 55 | for (i = 0; i < 2; ++i) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 56 | streams[i] = ((infiles[i][0] == '=' && infiles[i][1]) ? stdin : xfopen(infiles[i], "r")); |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 57 | fgets(thisline[i], LINE_LEN, streams[i]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 58 | } |
| 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) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 81 | writeline(thisline[1], 2); |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 82 | else if (order > 0 && !(i & BB_EOF_1)) |
| 83 | writeline(thisline[1], 1); |
| 84 | else if (order < 0 && !(i & BB_EOF_0)) |
| 85 | writeline(thisline[0], 0); |
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)) |
| 94 | writeline(thisline[i], i); |
| 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 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 107 | fclose(streams[0]); |
| 108 | fclose(streams[1]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 111 | int comm_main(int argc, char **argv) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 112 | { |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 113 | unsigned long flags; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 114 | |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 115 | flags = bb_getopt_ulflags(argc, argv, "123"); |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 116 | |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 117 | if (optind + 2 != argc) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 118 | bb_show_usage(); |
| 119 | |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 120 | only_file_1 = !(flags & COMM_OPT_1); |
| 121 | only_file_2 = !(flags & COMM_OPT_2); |
| 122 | both = !(flags & COMM_OPT_3); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 123 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 124 | cmp_files(argv + optind); |
| 125 | exit(EXIT_SUCCESS); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 126 | } |