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 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | * 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <unistd.h> |
| 28 | #include "busybox.h" |
| 29 | |
| 30 | #define COMM_OPT_1 0x01 |
| 31 | #define COMM_OPT_2 0x02 |
| 32 | #define COMM_OPT_3 0x04 |
| 33 | |
| 34 | /* These three variables control behaviour if non-zero */ |
| 35 | |
| 36 | static int only_file_1; |
| 37 | static int only_file_2; |
| 38 | static int both; |
| 39 | |
| 40 | /* writeline outputs the input given, appropriately aligned according to class */ |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 41 | static void writeline(char *line, int class) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 42 | { |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 43 | if (class == 0) { |
| 44 | if (!only_file_1) |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 45 | return; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 46 | } else if (class == 1) { |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 47 | if (!only_file_2) |
| 48 | return; |
| 49 | if (only_file_1) |
| 50 | putchar('\t'); |
| 51 | } |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 52 | else /*if (class == 2)*/ { |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 53 | if (!both) |
| 54 | return; |
| 55 | if (only_file_1) |
| 56 | putchar('\t'); |
| 57 | if (only_file_2) |
| 58 | putchar('\t'); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 59 | } |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 60 | fputs(line, stdout); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* This is the real core of the program - lines are compared here */ |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 64 | static void cmp_files(char **infiles) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 65 | { |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 66 | #define LINE_LEN 100 |
| 67 | #define BB_EOF_0 0x1 |
| 68 | #define BB_EOF_1 0x2 |
| 69 | char thisline[2][LINE_LEN]; |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 70 | FILE *streams[2]; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 71 | int i; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 72 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 73 | for (i = 0; i < 2; ++i) { |
| 74 | streams[i] = ((infiles[i][0] == '=' && infiles[1][1]) ? stdin : bb_xfopen(infiles[i], "r")); |
| 75 | fgets(thisline[i], LINE_LEN, streams[i]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | while (thisline[0] || thisline[1]) { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 79 | int order = 0; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 80 | |
| 81 | i = 0; |
| 82 | if (feof(streams[0])) i |= BB_EOF_0; |
| 83 | if (feof(streams[1])) i |= BB_EOF_1; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 84 | |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 85 | if (!thisline[0]) |
| 86 | order = 1; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 87 | else if (!thisline[1]) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 88 | order = -1; |
| 89 | else { |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 90 | int tl0_len, tl1_len; |
| 91 | tl0_len = strlen(thisline[0]); |
| 92 | tl1_len = strlen(thisline[1]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 93 | order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len); |
| 94 | if (!order) |
| 95 | order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len; |
| 96 | } |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 97 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 98 | if (order == 0 && !i) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 99 | writeline(thisline[1], 2); |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 100 | else if (order > 0 && !(i & BB_EOF_1)) |
| 101 | writeline(thisline[1], 1); |
| 102 | else if (order < 0 && !(i & BB_EOF_0)) |
| 103 | writeline(thisline[0], 0); |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 104 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 105 | if (i & BB_EOF_0 & BB_EOF_1) { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 106 | break; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 107 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 108 | } else if (i) { |
| 109 | i = (i & BB_EOF_0 ? 1 : 0); |
| 110 | while (!feof(streams[i])) { |
| 111 | if ((order < 0 && i) || (order > 0 && !i)) |
| 112 | writeline(thisline[i], i); |
| 113 | fgets(thisline[i], LINE_LEN, streams[i]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 114 | } |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 115 | break; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 116 | |
| 117 | } else { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 118 | if (order >= 0) |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 119 | fgets(thisline[1], LINE_LEN, streams[1]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 120 | if (order <= 0) |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 121 | fgets(thisline[0], LINE_LEN, streams[0]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 125 | fclose(streams[0]); |
| 126 | fclose(streams[1]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 129 | int comm_main(int argc, char **argv) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 130 | { |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 131 | unsigned long flags; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 132 | |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 133 | flags = bb_getopt_ulflags(argc, argv, "123"); |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 134 | |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 135 | if (optind + 2 != argc) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 136 | bb_show_usage(); |
| 137 | |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 138 | only_file_1 = !(flags & COMM_OPT_1); |
| 139 | only_file_2 = !(flags & COMM_OPT_2); |
| 140 | both = !(flags & COMM_OPT_3); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 141 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame^] | 142 | cmp_files(argv + optind); |
| 143 | exit(EXIT_SUCCESS); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 144 | } |