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 //config: bool "od (11 kb)"
17 //config: od is used to dump binary files in octal and other formats.
19 //applet:IF_OD(APPLET(od, BB_DIR_USR_BIN, BB_SUID_DROP))
21 //kbuild:lib-$(CONFIG_OD) += od.o
23 //usage:#if !ENABLE_DESKTOP
24 //usage:#define od_trivial_usage
25 //usage: "[-abcdeFfhiloxsv] [FILE]"
26 // We also support -BDOHXIL, but they are not documented in coreutils 9.1
27 // manpage/help, so don't show them either.
28 //usage:#define od_full_usage "\n\n"
29 //usage: "Print FILE (or stdin) unambiguously, as octal bytes by default"
34 /* This one provides -t (busybox's own build script needs it) */
35 #include "od_bloaty.c"
41 odoffset(dumper_t
*dumper
, int argc
, char ***argvp
)
48 * The offset syntax of od(1) was genuinely bizarre. First, if
49 * it started with a plus it had to be an offset. Otherwise, if
50 * there were at least two arguments, a number or lower-case 'x'
51 * followed by a number makes it an offset. By default it was
52 * octal; if it started with 'x' or '0x' it was hex. If it ended
53 * in a '.', it was decimal. If a 'b' or 'B' was appended, it
54 * multiplied the number by 512 or 1024 byte units. There was
55 * no way to assign a block count to a hex offset.
57 * We assumes it's a file if the offset is bad.
62 /* hey someone is probably piping to us ... */
69 && ((p
[0] != 'x') || !isxdigit(p
[1])))))
74 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
79 if (p
[0] == 'x' && isxdigit(p
[1])) {
82 } else if (p
[0] == '0' && p
[1] == 'x') {
87 /* skip over the number */
89 for (num
= p
; isxdigit(*p
); ++p
)
92 for (num
= p
; isdigit(*p
); ++p
)
95 /* check for no number */
99 /* if terminates with a '.', base is decimal */
106 dumper
->dump_skip
= strtol(num
, &end
, base
? base
: 8);
108 /* if end isn't the same as p, we got a non-octal digit */
110 dumper
->dump_skip
= 0;
114 dumper
->dump_skip
*= 512;
116 } else if (*p
== 'B') {
117 dumper
->dump_skip
*= 1024;
122 dumper
->dump_skip
= 0;
126 * If the offset uses a non-octal base, the base of
127 * the offset is changed as well. This isn't pretty,
130 #define TYPE_OFFSET 7
140 dumper
->fshead
->nextfu
->fmt
[TYPE_OFFSET
]
141 = dumper
->fshead
->nextfs
->nextfu
->fmt
[TYPE_OFFSET
]
150 // A format string contains format units separated by [optional] whitespace.
151 // A format unit contains up to three items: an iteration count, a byte count,
153 // The iteration count is an optional integer (default 1).
154 // Each format is applied iteration count times.
155 // The byte count is an optional integer. It defines the number
156 // of bytes to be interpreted by each iteration of the format.
157 // If an iteration count and/or a byte count is specified, a slash must be
158 // placed after the iteration count and/or before the byte count
159 // to disambiguate them.
160 // The printf-style format is required and must be surrounded by " "s.
161 // (Below, each string contains two format units)
162 static const char *const add_strings
[] ALIGN_PTR
= {
163 "16/1 \" %3_u\"" "\"\n\"", /* 0: a */
164 "8/2 \" %06o\"" "\"\n\"", /* 1: B (undocumented in od), o */
165 "16/1 \" %03o\"" "\"\n\"", /* 2: b */
166 "16/1 \" %3_c\"" "\"\n\"", /* 3: c */
167 "8/2 \" %5u\"" "\"\n\"", /* 4: d */
168 "4/4 \" %10u\"" "\"\n\"", /* 5: D */
169 "2/8 \" %24.14e\"" "\"\n\"", /* 6: e (undocumented in od), F */
170 "4/4 \" %15.7e\"" "\"\n\"", /* 7: f */
171 "4/4 \" %08x\"" "\"\n\"", /* 8: H, X */
172 "8/2 \" %04x\"" "\"\n\"", /* 9: h, x */
173 "4/4 \" %11d\"" "\"\n\"", /* 10: i */
174 "4/4 \" %011o\"" "\"\n\"", /* 11: O */
175 "8/2 \" %6d\"" "\"\n\"", /* 12: s */
176 /* -I,L,l: depend on word width of the arch (what is "long"?) */
177 #if ULONG_MAX > 0xffffffff
178 "2/8 \" %20lld\"" "\"\n\"", /* 13: I, L, l */
181 /* 32-bit arch: -I,L,l are the same as -i */
186 static const char od_opts
[] ALIGN1
= "aBbcDdeFfHhIiLlOoXxsv";
188 static const char od_o2si
[] ALIGN1
= {
189 0, 1, 2, 3, 5, /* aBbcD */
190 4, 6, 6, 7, 8, /* deFfH */
191 9, L_
, 10, L_
, L_
, /* hIiLl */
192 11, 1, 8, 9, 12 /* OoXxs */
195 int od_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
196 int od_main(int argc
, char **argv
)
201 dumper_t
*dumper
= alloc_dumper();
203 while ((ch
= getopt(argc
, argv
, od_opts
)) > 0) {
205 dumper
->dump_vflag
= ALL
;
206 } else if (((p
= strchr(od_opts
, ch
)) != NULL
) && (*p
!= '\0')) {
209 bb_dump_add(dumper
, "\"%07.7_Ao\n\"");
210 bb_dump_add(dumper
, "\"%07.7_ao\"");
212 bb_dump_add(dumper
, "\" \"");
214 bb_dump_add(dumper
, add_strings
[(int)od_o2si
[(p
- od_opts
)]]);
215 } else { /* P, p, w, or other unhandled */
219 if (!dumper
->fshead
) {
220 bb_dump_add(dumper
, "\"%07.7_Ao\n\"");
221 bb_dump_add(dumper
, "\"%07.7_ao\"");
222 bb_dump_add(dumper
, add_strings
[1]); /* -o format is default */
224 dumper
->od_eofstring
= "\n";
229 odoffset(dumper
, argc
, &argv
);
231 return bb_dump_dump(dumper
, argv
);
233 #endif /* !ENABLE_DESKTOP */
236 * Copyright (c) 1990 The Regents of the University of California.
237 * All rights reserved.
239 * Redistribution and use in source and binary forms, with or without
240 * modification, are permitted provided that the following conditions
242 * 1. Redistributions of source code must retain the above copyright
243 * notice, this list of conditions and the following disclaimer.
244 * 2. Redistributions in binary form must reproduce the above copyright
245 * notice, this list of conditions and the following disclaimer in the
246 * documentation and/or other materials provided with the distribution.
247 * 3. Neither the name of the University nor the names of its contributors
248 * may be used to endorse or promote products derived from this software
249 * without specific prior written permission.
251 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
252 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
253 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
255 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
256 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
257 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
258 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
260 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF