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 | |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <unistd.h> |
| 14 | #include "busybox.h" |
| 15 | |
| 16 | #define COMM_OPT_1 0x01 |
| 17 | #define COMM_OPT_2 0x02 |
| 18 | #define COMM_OPT_3 0x04 |
| 19 | |
| 20 | /* These three variables control behaviour if non-zero */ |
| 21 | |
| 22 | static int only_file_1; |
| 23 | static int only_file_2; |
| 24 | static int both; |
| 25 | |
| 26 | /* writeline outputs the input given, appropriately aligned according to class */ |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 27 | static void writeline(char *line, int class) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 28 | { |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 29 | if (class == 0) { |
| 30 | if (!only_file_1) |
Mike Frysinger | 0fb397e | 2005-05-13 00:58:18 +0000 | [diff] [blame] | 31 | return; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 32 | } else if (class == 1) { |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 33 | if (!only_file_2) |
| 34 | return; |
| 35 | if (only_file_1) |
| 36 | putchar('\t'); |
| 37 | } |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 38 | else /*if (class == 2)*/ { |
Mike Frysinger | dad4cf7 | 2005-05-12 22:50:12 +0000 | [diff] [blame] | 39 | if (!both) |
| 40 | return; |
| 41 | if (only_file_1) |
| 42 | putchar('\t'); |
| 43 | if (only_file_2) |
| 44 | putchar('\t'); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 45 | } |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 46 | fputs(line, stdout); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /* This is the real core of the program - lines are compared here */ |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 50 | static void cmp_files(char **infiles) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 51 | { |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 52 | #define LINE_LEN 100 |
| 53 | #define BB_EOF_0 0x1 |
| 54 | #define BB_EOF_1 0x2 |
| 55 | char thisline[2][LINE_LEN]; |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 56 | FILE *streams[2]; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 57 | int i; |
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) { |
Mike Frysinger | 1ece21b | 2005-05-13 03:09:20 +0000 | [diff] [blame] | 60 | streams[i] = ((infiles[i][0] == '=' && infiles[i][1]) ? stdin : bb_xfopen(infiles[i], "r")); |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 61 | fgets(thisline[i], LINE_LEN, streams[i]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Rob Landley | e104d5f | 2006-04-04 19:26:37 +0000 | [diff] [blame] | 64 | while (*thisline[0] || *thisline[1]) { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 65 | int order = 0; |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 66 | |
| 67 | i = 0; |
| 68 | if (feof(streams[0])) i |= BB_EOF_0; |
| 69 | if (feof(streams[1])) i |= BB_EOF_1; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 70 | |
Rob Landley | e104d5f | 2006-04-04 19:26:37 +0000 | [diff] [blame] | 71 | if (!*thisline[0]) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 72 | order = 1; |
Rob Landley | e104d5f | 2006-04-04 19:26:37 +0000 | [diff] [blame] | 73 | else if (!*thisline[1]) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 74 | order = -1; |
| 75 | else { |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 76 | int tl0_len, tl1_len; |
| 77 | tl0_len = strlen(thisline[0]); |
| 78 | tl1_len = strlen(thisline[1]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 79 | order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len); |
| 80 | if (!order) |
| 81 | order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len; |
| 82 | } |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 83 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 84 | if (order == 0 && !i) |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 85 | writeline(thisline[1], 2); |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 86 | else if (order > 0 && !(i & BB_EOF_1)) |
| 87 | writeline(thisline[1], 1); |
| 88 | else if (order < 0 && !(i & BB_EOF_0)) |
| 89 | writeline(thisline[0], 0); |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 90 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 91 | if (i & BB_EOF_0 & BB_EOF_1) { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 92 | break; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 93 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 94 | } else if (i) { |
| 95 | i = (i & BB_EOF_0 ? 1 : 0); |
| 96 | while (!feof(streams[i])) { |
| 97 | if ((order < 0 && i) || (order > 0 && !i)) |
| 98 | writeline(thisline[i], i); |
| 99 | fgets(thisline[i], LINE_LEN, streams[i]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 100 | } |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 101 | break; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 102 | |
| 103 | } else { |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 104 | if (order >= 0) |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 105 | fgets(thisline[1], LINE_LEN, streams[1]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 106 | if (order <= 0) |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 107 | fgets(thisline[0], LINE_LEN, streams[0]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 111 | fclose(streams[0]); |
| 112 | fclose(streams[1]); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 115 | int comm_main(int argc, char **argv) |
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 | unsigned long flags; |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 118 | |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 119 | flags = bb_getopt_ulflags(argc, argv, "123"); |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 120 | |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 121 | if (optind + 2 != argc) |
Mike Frysinger | 0d605e9 | 2005-05-12 22:25:41 +0000 | [diff] [blame] | 122 | bb_show_usage(); |
| 123 | |
Mike Frysinger | b3a6ec3 | 2005-05-12 22:41:13 +0000 | [diff] [blame] | 124 | only_file_1 = !(flags & COMM_OPT_1); |
| 125 | only_file_2 = !(flags & COMM_OPT_2); |
| 126 | both = !(flags & COMM_OPT_3); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 127 | |
Mike Frysinger | 867a608 | 2005-05-13 00:57:30 +0000 | [diff] [blame] | 128 | cmp_files(argv + optind); |
| 129 | exit(EXIT_SUCCESS); |
Rob Landley | 2acfd7b | 2005-05-11 23:12:49 +0000 | [diff] [blame] | 130 | } |