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