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 | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 41 | static void writeline (char *line, int class) |
| 42 | { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 43 | switch (class) { |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 44 | case 1: |
| 45 | if (!only_file_1) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 46 | return; |
| 47 | break; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 48 | case 2: |
| 49 | if (!only_file_2) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 50 | return; |
| 51 | if (only_file_1) |
| 52 | putchar('\t'); |
| 53 | break; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 54 | case 3: |
| 55 | if (!both) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 56 | return; |
| 57 | if (only_file_1) |
| 58 | putchar('\t'); |
| 59 | if (only_file_2) |
| 60 | putchar('\t'); |
| 61 | break; |
| 62 | } |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 63 | fputs(line, stdout); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* This is the real core of the program - lines are compared here */ |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 67 | static int cmp_files(char **infiles) |
| 68 | { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 69 | char thisline[2][100]; |
| 70 | FILE *streams[2]; |
| 71 | int i = 0; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 72 | |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 73 | for (i = 0; i < 2; i++) { |
| 74 | streams[i] = (strcmp(infiles[i], "=") == 0 ? stdin : fopen(infiles[i], "r")); |
| 75 | fgets(thisline[i], 100, streams[i]); |
| 76 | } |
| 77 | |
| 78 | while (thisline[0] || thisline[1]) { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 79 | int order = 0; |
| 80 | int tl0_len = strlen(thisline[0]); |
| 81 | int tl1_len = strlen(thisline[1]); |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 82 | |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 83 | if (!thisline[0]) |
| 84 | order = 1; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 85 | else if (!thisline[1]) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 86 | order = -1; |
| 87 | else { |
| 88 | order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len); |
| 89 | if (!order) |
| 90 | order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len; |
| 91 | } |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 92 | |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 93 | if ((order == 0) && (!feof(streams[0])) && (!feof(streams[1]))) |
| 94 | writeline(thisline[1], 3); |
| 95 | else if ((order > 0) && (!feof(streams[1]))) |
| 96 | writeline(thisline[1], 2); |
| 97 | else if ((order < 0) && (!feof(streams[0]))) |
| 98 | writeline(thisline[0], 1); |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 99 | |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 100 | if (feof(streams[0]) && feof(streams[1])) { |
| 101 | fclose(streams[0]); |
| 102 | fclose(streams[1]); |
| 103 | break; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 104 | |
| 105 | } else if (feof(streams[0])) { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 106 | while (!feof(streams[1])) { |
| 107 | if (order < 0) |
| 108 | writeline(thisline[1], 2); |
| 109 | fgets(thisline[1], 100, streams[1]); |
| 110 | } |
| 111 | fclose(streams[0]); |
| 112 | fclose(streams[1]); |
| 113 | break; |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 114 | |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 115 | } else if (feof(streams[1])) { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 116 | while (!feof(streams[0])) { |
| 117 | if (order > 0) |
| 118 | writeline(thisline[0], 1); |
| 119 | fgets(thisline[0], 100, streams[0]); |
| 120 | } |
| 121 | fclose(streams[0]); |
| 122 | fclose(streams[1]); |
| 123 | break; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 124 | |
| 125 | } else { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 126 | if (order >= 0) |
| 127 | fgets(thisline[1], 100, streams[1]); |
| 128 | if (order <= 0) |
| 129 | fgets(thisline[0], 100, streams[0]); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 136 | int comm_main (int argc, char **argv) |
| 137 | { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 138 | unsigned long opt; |
| 139 | only_file_1 = 1; |
| 140 | only_file_2 = 1; |
| 141 | both = 1; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 142 | |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 143 | opt = bb_getopt_ulflags(argc, argv, "123"); |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame^] | 144 | |
| 145 | if ((opt & 0x80000000UL) || (optind == argc)) |
| 146 | bb_show_usage(); |
| 147 | |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 148 | if (opt & COMM_OPT_1) |
| 149 | only_file_1 = 0; |
| 150 | if (opt & COMM_OPT_2) |
| 151 | only_file_2 = 0; |
| 152 | if (opt & COMM_OPT_3) |
| 153 | both = 0; |
| 154 | |
| 155 | exit(cmp_files(argv + optind) == 0 ? EXIT_SUCCESS : EXIT_FAILURE); |
| 156 | } |