diff options
author | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2002-11-10 21:52:59 +0000 |
---|---|---|
committer | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2002-11-10 21:52:59 +0000 |
commit | 1eb18f9bbc87465b2ad5c84a7ac92b6a3bd8d14d (patch) | |
tree | 30df899fb218076eec8151e2c3b752a38ba02fb3 /coreutils | |
parent | 17ac7ef8f3a52ca86d465a68ca95228ecfc89b29 (diff) | |
download | busybox-w32-1eb18f9bbc87465b2ad5c84a7ac92b6a3bd8d14d.tar.gz busybox-w32-1eb18f9bbc87465b2ad5c84a7ac92b6a3bd8d14d.tar.bz2 busybox-w32-1eb18f9bbc87465b2ad5c84a7ac92b6a3bd8d14d.zip |
Prevent a segfault if no argument, by Geoffrey Lee <glee@bluesat.unsw.edu.au>
git-svn-id: svn://busybox.net/trunk/busybox@5902 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'coreutils')
-rw-r--r-- | coreutils/od.c | 288 |
1 files changed, 64 insertions, 224 deletions
diff --git a/coreutils/od.c b/coreutils/od.c index a29ed758e..471bae45b 100644 --- a/coreutils/od.c +++ b/coreutils/od.c | |||
@@ -1,9 +1,9 @@ | |||
1 | /* | 1 | /* |
2 | * od implementation for busybox | 2 | * Mini xargs implementation for busybox |
3 | * Based on code from util-linux v 2.11l | ||
4 | * | 3 | * |
5 | * Copyright (c) 1990 | 4 | * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen |
6 | * The Regents of the University of California. All rights reserved. | 5 | * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org> |
6 | * Remixed by Mark Whitley <markw@codepoet.org> | ||
7 | * | 7 | * |
8 | * This program is free software; you can redistribute it and/or modify | 8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by | 9 | * it under the terms of the GNU General Public License as published by |
@@ -19,248 +19,88 @@ | |||
19 | * along with this program; if not, write to the Free Software | 19 | * along with this program; if not, write to the Free Software |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 | * | 21 | * |
22 | * Original copyright notice is retained at the end of this file. | ||
23 | */ | 22 | */ |
24 | 23 | ||
25 | #include <ctype.h> | 24 | #include <stdio.h> |
26 | #include <getopt.h> | ||
27 | #include <stdlib.h> | 25 | #include <stdlib.h> |
28 | #include "dump.h" | 26 | #include <string.h> |
29 | #include "busybox.h" | 27 | #include "busybox.h" |
30 | 28 | ||
31 | extern FS *fshead; /* head of format strings */ | 29 | int xargs_main(int argc, char **argv) |
32 | extern int blocksize; /* data block size */ | ||
33 | extern int length; /* max bytes to read */ | ||
34 | |||
35 | #define ishexdigit(c) \ | ||
36 | ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) | ||
37 | |||
38 | static void | ||
39 | odoffset(int argc, char ***argvp) | ||
40 | { | 30 | { |
41 | extern off_t skip; | 31 | char *cmd_to_be_executed; |
42 | register char *num, *p; | 32 | char *file_to_act_on; |
43 | int base; | 33 | int i; |
44 | char *end; | 34 | int len; |
45 | 35 | ||
46 | /* | 36 | /* |
47 | * The offset syntax of od(1) was genuinely bizarre. First, if | 37 | * No options are supported in this version of xargs; no getopt. |
48 | * it started with a plus it had to be an offset. Otherwise, if | ||
49 | * there were at least two arguments, a number or lower-case 'x' | ||
50 | * followed by a number makes it an offset. By default it was | ||
51 | * octal; if it started with 'x' or '0x' it was hex. If it ended | ||
52 | * in a '.', it was decimal. If a 'b' or 'B' was appended, it | ||
53 | * multiplied the number by 512 or 1024 byte units. There was | ||
54 | * no way to assign a block count to a hex offset. | ||
55 | * | 38 | * |
56 | * We assumes it's a file if the offset is bad. | 39 | * Re: The missing -t flag: Most programs that produce output also print |
57 | */ | 40 | * the filename, so xargs doesn't really need to do it again. Supporting |
58 | p = **argvp; | 41 | * the -t flag =greatly= bloats up the size of this app and the memory it |
59 | if (*p != '+' && (argc < 2 || | 42 | * uses because you have to buffer all the input file strings in memory. If |
60 | (!isdigit(p[0]) && (p[0] != 'x' || !ishexdigit(p[1]))))) | 43 | * you really want to see the filenames that xargs will act on, just run it |
61 | return; | 44 | * once with no args and xargs will echo the filename. Simple. |
62 | |||
63 | base = 0; | ||
64 | /* | ||
65 | * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and | ||
66 | * set base. | ||
67 | */ | 45 | */ |
68 | if (p[0] == '+') | ||
69 | ++p; | ||
70 | if (p[0] == 'x' && ishexdigit(p[1])) { | ||
71 | ++p; | ||
72 | base = 16; | ||
73 | } else if (p[0] == '0' && p[1] == 'x') { | ||
74 | p += 2; | ||
75 | base = 16; | ||
76 | } | ||
77 | |||
78 | /* skip over the number */ | ||
79 | if (base == 16) | ||
80 | for (num = p; ishexdigit(*p); ++p); | ||
81 | else | ||
82 | for (num = p; isdigit(*p); ++p); | ||
83 | |||
84 | /* check for no number */ | ||
85 | if (num == p) | ||
86 | return; | ||
87 | 46 | ||
88 | /* if terminates with a '.', base is decimal */ | 47 | argv++; |
89 | if (*p == '.') { | 48 | len = argc; /* arg = count for ' ' + trailing '\0' */ |
90 | if (base) | 49 | /* Store the command to be executed (taken from the command line) */ |
91 | return; | 50 | if (argc == 1) { |
92 | base = 10; | 51 | /* default behavior is to echo all the filenames */ |
52 | argv[0] = "/bin/echo"; | ||
53 | len++; /* space for trailing '\0' */ | ||
54 | } else { | ||
55 | argc--; | ||
56 | } | ||
57 | /* concatenate all the arguments passed to xargs together */ | ||
58 | for (i = 0; i < argc; i++) | ||
59 | len += strlen(argv[i]); | ||
60 | cmd_to_be_executed = xmalloc (len); | ||
61 | for (i = len = 0; i < argc; i++) { | ||
62 | len = sprintf(cmd_to_be_executed + len, "%s ", argv[i]); | ||
93 | } | 63 | } |
94 | 64 | ||
95 | skip = strtol(num, &end, base ? base : 8); | 65 | /* Now, read in one line at a time from stdin, and store this |
66 | * line to be used later as an argument to the command */ | ||
67 | while ((file_to_act_on = get_line_from_file(stdin)) !=NULL) { | ||
96 | 68 | ||
97 | /* if end isn't the same as p, we got a non-octal digit */ | 69 | FILE *cmd_output; |
98 | if (end != p) | 70 | char *output_line; |
99 | skip = 0; | 71 | char *execstr; |
100 | else { | ||
101 | if (*p) { | ||
102 | if (*p == 'b') | ||
103 | skip *= 512; | ||
104 | else if (*p == 'B') | ||
105 | skip *= 1024; | ||
106 | ++p; | ||
107 | } | ||
108 | if (*p) | ||
109 | skip = 0; | ||
110 | else { | ||
111 | ++*argvp; | ||
112 | /* | ||
113 | * If the offset uses a non-octal base, the base of | ||
114 | * the offset is changed as well. This isn't pretty, | ||
115 | * but it's easy. | ||
116 | */ | ||
117 | #define TYPE_OFFSET 7 | ||
118 | if (base == 16) { | ||
119 | fshead->nextfu->fmt[TYPE_OFFSET] = 'x'; | ||
120 | fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x'; | ||
121 | } else if (base == 10) { | ||
122 | fshead->nextfu->fmt[TYPE_OFFSET] = 'd'; | ||
123 | fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd'; | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | 72 | ||
129 | static void odprecede(void) | 73 | /* eat the newline off the filename. */ |
130 | { | 74 | chomp(file_to_act_on); |
131 | static int first = 1; | ||
132 | 75 | ||
133 | if (first) { | 76 | /* eat blank lines */ |
134 | first = 0; | 77 | if (file_to_act_on[0] == 0) |
135 | add("\"%07.7_Ao\n\""); | 78 | continue; |
136 | add("\"%07.7_ao \""); | ||
137 | } else | ||
138 | add("\" \""); | ||
139 | } | ||
140 | 79 | ||
141 | int od_main(int argc, char **argv) | 80 | /* assemble the command and execute it */ |
142 | { | 81 | bb_asprintf(&execstr, "%s%s", cmd_to_be_executed, file_to_act_on); |
143 | int ch; | 82 | |
144 | extern enum _vflag vflag; | 83 | cmd_output = popen(execstr, "r"); |
145 | vflag = FIRST; | 84 | if (cmd_output == NULL) |
146 | length = -1; | 85 | perror_msg_and_die("popen"); |
147 | 86 | ||
148 | while ((ch = getopt(argc, argv, "aBbcDdeFfHhIiLlOoPpswvXx")) != EOF) | 87 | /* harvest the output */ |
149 | switch (ch) { | 88 | while ((output_line = get_line_from_file(cmd_output)) != NULL) { |
150 | case 'a': | 89 | fputs(output_line, stdout); |
151 | odprecede(); | 90 | free(output_line); |
152 | add("16/1 \"%3_u \" \"\\n\""); | ||
153 | break; | ||
154 | case 'B': | ||
155 | case 'o': | ||
156 | odprecede(); | ||
157 | add("8/2 \" %06o \" \"\\n\""); | ||
158 | break; | ||
159 | case 'b': | ||
160 | odprecede(); | ||
161 | add("16/1 \"%03o \" \"\\n\""); | ||
162 | break; | ||
163 | case 'c': | ||
164 | odprecede(); | ||
165 | add("16/1 \"%3_c \" \"\\n\""); | ||
166 | break; | ||
167 | case 'd': | ||
168 | odprecede(); | ||
169 | add("8/2 \" %05u \" \"\\n\""); | ||
170 | break; | ||
171 | case 'D': | ||
172 | odprecede(); | ||
173 | add("4/4 \" %010u \" \"\\n\""); | ||
174 | break; | ||
175 | case 'e': /* undocumented in od */ | ||
176 | case 'F': | ||
177 | odprecede(); | ||
178 | add("2/8 \" %21.14e \" \"\\n\""); | ||
179 | break; | ||
180 | |||
181 | case 'f': | ||
182 | odprecede(); | ||
183 | add("4/4 \" %14.7e \" \"\\n\""); | ||
184 | break; | ||
185 | case 'H': | ||
186 | case 'X': | ||
187 | odprecede(); | ||
188 | add("4/4 \" %08x \" \"\\n\""); | ||
189 | break; | ||
190 | case 'h': | ||
191 | case 'x': | ||
192 | odprecede(); | ||
193 | add("8/2 \" %04x \" \"\\n\""); | ||
194 | break; | ||
195 | case 'I': | ||
196 | case 'L': | ||
197 | case 'l': | ||
198 | odprecede(); | ||
199 | add("4/4 \" %11d \" \"\\n\""); | ||
200 | break; | ||
201 | case 'i': | ||
202 | odprecede(); | ||
203 | add("8/2 \" %6d \" \"\\n\""); | ||
204 | break; | ||
205 | case 'O': | ||
206 | odprecede(); | ||
207 | add("4/4 \" %011o \" \"\\n\""); | ||
208 | break; | ||
209 | case 'v': | ||
210 | vflag = ALL; | ||
211 | break; | ||
212 | case 'P': | ||
213 | case 'p': | ||
214 | case 's': | ||
215 | case 'w': | ||
216 | case '?': | ||
217 | default: | ||
218 | error_msg("od: od(1) has been deprecated for hexdump(1).\n"); | ||
219 | if (ch != '?') { | ||
220 | error_msg("od: hexdump(1) compatibility doesn't support the -%c option%s\n", | ||
221 | ch, ch == 's' ? "; see strings(1)." : "."); | ||
222 | } | ||
223 | show_usage(); | ||
224 | } | 91 | } |
225 | 92 | ||
226 | if (!fshead) { | 93 | /* clean up */ |
227 | add("\"%07.7_Ao\n\""); | 94 | pclose(cmd_output); |
228 | add("\"%07.7_ao \" 8/2 \"%06o \" \"\\n\""); | 95 | free(execstr); |
96 | free(file_to_act_on); | ||
229 | } | 97 | } |
230 | 98 | ||
231 | argc -= optind; | 99 | #ifdef CONFIG_FEATURE_CLEAN_UP |
232 | argv += optind; | 100 | free(cmd_to_be_executed); |
233 | 101 | #endif | |
234 | odoffset(argc, &argv); | ||
235 | 102 | ||
236 | return(dump(argv)); | 103 | return 0; |
237 | } | 104 | } |
238 | 105 | ||
239 | /*- | 106 | /* vi: set sw=4 ts=4: */ |
240 | * Copyright (c) 1990 The Regents of the University of California. | ||
241 | * All rights reserved. | ||
242 | * | ||
243 | * Redistribution and use in source and binary forms, with or without | ||
244 | * modification, are permitted provided that the following conditions | ||
245 | * are met: | ||
246 | * 1. Redistributions of source code must retain the above copyright | ||
247 | * notice, this list of conditions and the following disclaimer. | ||
248 | * 2. Redistributions in binary form must reproduce the above copyright | ||
249 | * notice, this list of conditions and the following disclaimer in the | ||
250 | * documentation and/or other materials provided with the distribution. | ||
251 | * 3. Neither the name of the University nor the names of its contributors | ||
252 | * may be used to endorse or promote products derived from this software | ||
253 | * without specific prior written permission. | ||
254 | * | ||
255 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
256 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
257 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
258 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
259 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
260 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
261 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
262 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
263 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
264 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
265 | * SUCH DAMAGE. | ||
266 | */ | ||