diff options
author | Rob Landley <rob@landley.net> | 2005-05-11 23:12:49 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2005-05-11 23:12:49 +0000 |
commit | 2acfd7bd2640655138260e0661990e12a99c57d3 (patch) | |
tree | e162cc6fca7b67b684492c758a43fb9463782a7d | |
parent | b662f0d58ba40cbbc1cca849fb6e8665688deb5e (diff) | |
download | busybox-w32-2acfd7bd2640655138260e0661990e12a99c57d3.tar.gz busybox-w32-2acfd7bd2640655138260e0661990e12a99c57d3.tar.bz2 busybox-w32-2acfd7bd2640655138260e0661990e12a99c57d3.zip |
Small comm implementatin from Rob Sullivan. Needed to build perl.
-rw-r--r-- | AUTHORS | 3 | ||||
-rw-r--r-- | coreutils/Config.in | 7 | ||||
-rw-r--r-- | coreutils/comm.c | 155 | ||||
-rw-r--r-- | include/applets.h | 3 | ||||
-rw-r--r-- | include/usage.h | 9 |
5 files changed, 177 insertions, 0 deletions
@@ -128,6 +128,9 @@ Pavel Roskin <proski@gnu.org> | |||
128 | Gyepi Sam <gyepi@praxis-sw.com> | 128 | Gyepi Sam <gyepi@praxis-sw.com> |
129 | Remote logging feature for syslogd | 129 | Remote logging feature for syslogd |
130 | 130 | ||
131 | Rob Sullivan <cogito.ergo.cogito@gmail.com> | ||
132 | comm | ||
133 | |||
131 | Linus Torvalds <torvalds@transmeta.com> | 134 | Linus Torvalds <torvalds@transmeta.com> |
132 | mkswap, fsck.minix, mkfs.minix | 135 | mkswap, fsck.minix, mkfs.minix |
133 | 136 | ||
diff --git a/coreutils/Config.in b/coreutils/Config.in index b292e2568..6d19909a8 100644 --- a/coreutils/Config.in +++ b/coreutils/Config.in | |||
@@ -59,6 +59,13 @@ config CONFIG_CMP | |||
59 | cmp is used to compare two files and returns the result | 59 | cmp is used to compare two files and returns the result |
60 | to standard output. | 60 | to standard output. |
61 | 61 | ||
62 | config CONFIG_COMM | ||
63 | bool "comm" | ||
64 | default n | ||
65 | help | ||
66 | comm is used to compare two files line by line and return | ||
67 | a three-column output. | ||
68 | |||
62 | config CONFIG_CP | 69 | config CONFIG_CP |
63 | bool "cp" | 70 | bool "cp" |
64 | default n | 71 | default n |
diff --git a/coreutils/comm.c b/coreutils/comm.c new file mode 100644 index 000000000..3e81a5aa3 --- /dev/null +++ b/coreutils/comm.c | |||
@@ -0,0 +1,155 @@ | |||
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 */ | ||
41 | static void writeline (char *line, int class) { | ||
42 | switch (class) { | ||
43 | case 1: if (!only_file_1) | ||
44 | return; | ||
45 | break; | ||
46 | case 2: if (!only_file_2) | ||
47 | return; | ||
48 | if (only_file_1) | ||
49 | putchar('\t'); | ||
50 | break; | ||
51 | case 3: if (!both) | ||
52 | return; | ||
53 | if (only_file_1) | ||
54 | putchar('\t'); | ||
55 | if (only_file_2) | ||
56 | putchar('\t'); | ||
57 | break; | ||
58 | } | ||
59 | fputs(line, stdout); | ||
60 | } | ||
61 | |||
62 | /* This is the real core of the program - lines are compared here */ | ||
63 | static int cmp_files(char **infiles) { | ||
64 | |||
65 | char thisline[2][100]; | ||
66 | FILE *streams[2]; | ||
67 | int i = 0; | ||
68 | |||
69 | for (i = 0; i < 2; i++) { | ||
70 | streams[i] = (strcmp(infiles[i], "=") == 0 ? stdin : fopen(infiles[i], "r")); | ||
71 | fgets(thisline[i], 100, streams[i]); | ||
72 | } | ||
73 | |||
74 | while (thisline[0] || thisline[1]) { | ||
75 | |||
76 | int order = 0; | ||
77 | int tl0_len = strlen(thisline[0]); | ||
78 | int tl1_len = strlen(thisline[1]); | ||
79 | if (!thisline[0]) | ||
80 | order = 1; | ||
81 | else if (!thisline[1]) | ||
82 | order = -1; | ||
83 | else { | ||
84 | order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len); | ||
85 | if (!order) | ||
86 | order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len; | ||
87 | } | ||
88 | |||
89 | if ((order == 0) && (!feof(streams[0])) && (!feof(streams[1]))) | ||
90 | writeline(thisline[1], 3); | ||
91 | else if ((order > 0) && (!feof(streams[1]))) | ||
92 | writeline(thisline[1], 2); | ||
93 | else if ((order < 0) && (!feof(streams[0]))) | ||
94 | writeline(thisline[0], 1); | ||
95 | |||
96 | if (feof(streams[0]) && feof(streams[1])) { | ||
97 | fclose(streams[0]); | ||
98 | fclose(streams[1]); | ||
99 | break; | ||
100 | } | ||
101 | else if (feof(streams[0])) { | ||
102 | |||
103 | while (!feof(streams[1])) { | ||
104 | if (order < 0) | ||
105 | writeline(thisline[1], 2); | ||
106 | fgets(thisline[1], 100, streams[1]); | ||
107 | } | ||
108 | fclose(streams[0]); | ||
109 | fclose(streams[1]); | ||
110 | break; | ||
111 | } | ||
112 | else if (feof(streams[1])) { | ||
113 | |||
114 | while (!feof(streams[0])) { | ||
115 | if (order > 0) | ||
116 | writeline(thisline[0], 1); | ||
117 | fgets(thisline[0], 100, streams[0]); | ||
118 | } | ||
119 | fclose(streams[0]); | ||
120 | fclose(streams[1]); | ||
121 | break; | ||
122 | } | ||
123 | else { | ||
124 | if (order >= 0) | ||
125 | fgets(thisline[1], 100, streams[1]); | ||
126 | if (order <= 0) | ||
127 | fgets(thisline[0], 100, streams[0]); | ||
128 | } | ||
129 | } | ||
130 | |||
131 | return 0; | ||
132 | } | ||
133 | |||
134 | int comm_main (int argc, char **argv) { | ||
135 | |||
136 | unsigned long opt; | ||
137 | only_file_1 = 1; | ||
138 | only_file_2 = 1; | ||
139 | both = 1; | ||
140 | |||
141 | opt = bb_getopt_ulflags(argc, argv, "123"); | ||
142 | |||
143 | if ((opt & 0x80000000UL) || (optind == argc)) { | ||
144 | bb_show_usage(); | ||
145 | } | ||
146 | |||
147 | if (opt & COMM_OPT_1) | ||
148 | only_file_1 = 0; | ||
149 | if (opt & COMM_OPT_2) | ||
150 | only_file_2 = 0; | ||
151 | if (opt & COMM_OPT_3) | ||
152 | both = 0; | ||
153 | |||
154 | exit(cmp_files(argv + optind) == 0 ? EXIT_SUCCESS : EXIT_FAILURE); | ||
155 | } | ||
diff --git a/include/applets.h b/include/applets.h index bc5b36ae3..4ef5ed77c 100644 --- a/include/applets.h +++ b/include/applets.h | |||
@@ -113,6 +113,9 @@ | |||
113 | #ifdef CONFIG_CMP | 113 | #ifdef CONFIG_CMP |
114 | APPLET(cmp, cmp_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER) | 114 | APPLET(cmp, cmp_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER) |
115 | #endif | 115 | #endif |
116 | #ifdef CONFIG_COMM | ||
117 | APPLET(comm, comm_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER) | ||
118 | #endif | ||
116 | #ifdef CONFIG_CP | 119 | #ifdef CONFIG_CP |
117 | APPLET(cp, cp_main, _BB_DIR_BIN, _BB_SUID_NEVER) | 120 | APPLET(cp, cp_main, _BB_DIR_BIN, _BB_SUID_NEVER) |
118 | #endif | 121 | #endif |
diff --git a/include/usage.h b/include/usage.h index c68ae2e0d..ce8d407e3 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -222,6 +222,15 @@ | |||
222 | "\t\t for all differing bytes\n" \ | 222 | "\t\t for all differing bytes\n" \ |
223 | "\t-s\tquiet mode - do not print" | 223 | "\t-s\tquiet mode - do not print" |
224 | 224 | ||
225 | #define comm_trivial_usage \ | ||
226 | "[-123] FILE1 FILE2" | ||
227 | #define comm_full_usage \ | ||
228 | "Compare files. Compares FILE1 to FILE2, or to stdin if - is specified.\n\n" \ | ||
229 | "Options:\n" \ | ||
230 | "\t-1\tSuppress lines unique to FILE1\n" \ | ||
231 | "\t-2\tSuppress lines unique to FILE2\n" \ | ||
232 | "\t-3\tSuppress lines common to both files" | ||
233 | |||
225 | #define cp_trivial_usage \ | 234 | #define cp_trivial_usage \ |
226 | "[OPTION]... SOURCE DEST" | 235 | "[OPTION]... SOURCE DEST" |
227 | #define cp_full_usage \ | 236 | #define cp_full_usage \ |