1 /* vi: set sw=4 ts=4: */
3 * od implementation for busybox
4 * Based on code from util-linux v 2.11l
7 * The Regents of the University of California. All rights reserved.
9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 * Original copyright notice is retained at the end of this file.
14 //usage:#define od_trivial_usage
15 //usage: "[-aBbcDdeFfHhIiLlOovXx] " IF_DESKTOP("[-t TYPE] ") "[FILE]"
16 //usage:#define od_full_usage "\n\n"
17 //usage: "Write an unambiguous representation, octal bytes by default, of FILE\n"
18 //usage: "(or stdin) to stdout"
22 /* This one provides -t (busybox's own build script needs it) */
23 #include "od_bloaty.c"
29 odoffset(dumper_t *dumper, int argc, char ***argvp)
36 * The offset syntax of od(1) was genuinely bizarre. First, if
37 * it started with a plus it had to be an offset. Otherwise, if
38 * there were at least two arguments, a number or lower-case 'x'
39 * followed by a number makes it an offset. By default it was
40 * octal; if it started with 'x' or '0x' it was hex. If it ended
41 * in a '.', it was decimal. If a 'b' or 'B' was appended, it
42 * multiplied the number by 512 or 1024 byte units. There was
43 * no way to assign a block count to a hex offset.
45 * We assumes it's a file if the offset is bad.
50 /* hey someone is probably piping to us ... */
57 && ((p[0] != 'x') || !isxdigit(p[1])))))
62 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
67 if (p[0] == 'x' && isxdigit(p[1])) {
70 } else if (p[0] == '0' && p[1] == 'x') {
75 /* skip over the number */
77 for (num = p; isxdigit(*p); ++p)
80 for (num = p; isdigit(*p); ++p)
83 /* check for no number */
87 /* if terminates with a '.', base is decimal */
94 dumper->dump_skip = strtol(num, &end, base ? base : 8);
96 /* if end isn't the same as p, we got a non-octal digit */
98 dumper->dump_skip = 0;
102 dumper->dump_skip *= 512;
104 } else if (*p == 'B') {
105 dumper->dump_skip *= 1024;
110 dumper->dump_skip = 0;
114 * If the offset uses a non-octal base, the base of
115 * the offset is changed as well. This isn't pretty,
118 #define TYPE_OFFSET 7
128 dumper->fshead->nextfu->fmt[TYPE_OFFSET]
129 = dumper->fshead->nextfs->nextfu->fmt[TYPE_OFFSET]
137 static const char *const add_strings[] = {
138 "16/1 \"%3_u \" \"\\n\"", /* a */
139 "8/2 \" %06o \" \"\\n\"", /* B, o */
140 "16/1 \"%03o \" \"\\n\"", /* b */
141 "16/1 \"%3_c \" \"\\n\"", /* c */
142 "8/2 \" %05u \" \"\\n\"", /* d */
143 "4/4 \" %010u \" \"\\n\"", /* D */
144 "2/8 \" %21.14e \" \"\\n\"", /* e (undocumented in od), F */
145 "4/4 \" %14.7e \" \"\\n\"", /* f */
146 "4/4 \" %08x \" \"\\n\"", /* H, X */
147 "8/2 \" %04x \" \"\\n\"", /* h, x */
148 "4/4 \" %11d \" \"\\n\"", /* I, L, l */
149 "8/2 \" %6d \" \"\\n\"", /* i */
150 "4/4 \" %011o \" \"\\n\"", /* O */
153 static const char od_opts[] ALIGN1 = "aBbcDdeFfHhIiLlOoXxv";
155 static const char od_o2si[] ALIGN1 = {
158 9, 0xa, 0xb, 0xa, 0xa,
162 int od_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
163 int od_main(int argc, char **argv)
168 dumper_t *dumper = alloc_dumper();
170 while ((ch = getopt(argc, argv, od_opts)) > 0) {
172 dumper->dump_vflag = ALL;
173 } else if (((p = strchr(od_opts, ch)) != NULL) && (*p != '\0')) {
176 bb_dump_add(dumper, "\"%07.7_Ao\n\"");
177 bb_dump_add(dumper, "\"%07.7_ao \"");
179 bb_dump_add(dumper, "\" \"");
181 bb_dump_add(dumper, add_strings[(int)od_o2si[(p - od_opts)]]);
182 } else { /* P, p, s, w, or other unhandled */
186 if (!dumper->fshead) {
187 bb_dump_add(dumper, "\"%07.7_Ao\n\"");
188 bb_dump_add(dumper, "\"%07.7_ao \" 8/2 \"%06o \" \"\\n\"");
194 odoffset(dumper, argc, &argv);
196 return bb_dump_dump(dumper, argv);
198 #endif /* ENABLE_DESKTOP */
201 * Copyright (c) 1990 The Regents of the University of California.
202 * All rights reserved.
204 * Redistribution and use in source and binary forms, with or without
205 * modification, are permitted provided that the following conditions
207 * 1. Redistributions of source code must retain the above copyright
208 * notice, this list of conditions and the following disclaimer.
209 * 2. Redistributions in binary form must reproduce the above copyright
210 * notice, this list of conditions and the following disclaimer in the
211 * documentation and/or other materials provided with the distribution.
212 * 3. Neither the name of the University nor the names of its contributors
213 * may be used to endorse or promote products derived from this software
214 * without specific prior written permission.
216 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
217 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
220 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
223 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
224 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
225 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF