util-linux/lsusb.c: print manufacturer/product strings if available
[busybox-git.git] / coreutils / od.c
bloba7b1ba444cbe1bdfab2a03c47f9033ee0e6f7062
1 /* vi: set sw=4 ts=4: */
2 /*
3 * od implementation for busybox
4 * Based on code from util-linux v 2.11l
6 * Copyright (c) 1990
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.
13 //config:config OD
14 //config: bool "od (11 kb)"
15 //config: default y
16 //config: help
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"
30 //usage:#endif
32 #include "libbb.h"
33 #if ENABLE_DESKTOP
34 /* This one provides -t (busybox's own build script needs it) */
35 #include "od_bloaty.c"
36 #else
38 #include "dump.h"
40 static void
41 odoffset(dumper_t *dumper, int argc, char ***argvp)
43 char *num, *p;
44 int base;
45 char *end;
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.
59 p = **argvp;
61 if (!p) {
62 /* hey someone is probably piping to us ... */
63 return;
66 if ((*p != '+')
67 && (argc < 2
68 || (!isdigit(p[0])
69 && ((p[0] != 'x') || !isxdigit(p[1])))))
70 return;
72 base = 0;
74 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
75 * set base.
77 if (p[0] == '+')
78 ++p;
79 if (p[0] == 'x' && isxdigit(p[1])) {
80 ++p;
81 base = 16;
82 } else if (p[0] == '0' && p[1] == 'x') {
83 p += 2;
84 base = 16;
87 /* skip over the number */
88 if (base == 16)
89 for (num = p; isxdigit(*p); ++p)
90 continue;
91 else
92 for (num = p; isdigit(*p); ++p)
93 continue;
95 /* check for no number */
96 if (num == p)
97 return;
99 /* if terminates with a '.', base is decimal */
100 if (*p == '.') {
101 if (base)
102 return;
103 base = 10;
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 */
109 if (end != p)
110 dumper->dump_skip = 0;
111 else {
112 if (*p) {
113 if (*p == 'b') {
114 dumper->dump_skip *= 512;
115 ++p;
116 } else if (*p == 'B') {
117 dumper->dump_skip *= 1024;
118 ++p;
121 if (*p)
122 dumper->dump_skip = 0;
123 else {
124 ++*argvp;
126 * If the offset uses a non-octal base, the base of
127 * the offset is changed as well. This isn't pretty,
128 * but it's easy.
130 #define TYPE_OFFSET 7
132 char x_or_d;
133 if (base == 16) {
134 x_or_d = 'x';
135 goto DO_X_OR_D;
137 if (base == 10) {
138 x_or_d = 'd';
139 DO_X_OR_D:
140 dumper->fshead->nextfu->fmt[TYPE_OFFSET]
141 = dumper->fshead->nextfs->nextfu->fmt[TYPE_OFFSET]
142 = x_or_d;
149 // bb_dump_add():
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,
152 // and a format.
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 */
179 #define L_ 13
180 #else
181 /* 32-bit arch: -I,L,l are the same as -i */
182 #define L_ 10
183 #endif
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)
198 int ch;
199 int first = 1;
200 char *p;
201 dumper_t *dumper = alloc_dumper();
203 while ((ch = getopt(argc, argv, od_opts)) > 0) {
204 if (ch == 'v') {
205 dumper->dump_vflag = ALL;
206 } else if (((p = strchr(od_opts, ch)) != NULL) && (*p != '\0')) {
207 if (first) {
208 first = 0;
209 bb_dump_add(dumper, "\"%07.7_Ao\n\"");
210 bb_dump_add(dumper, "\"%07.7_ao\"");
211 } else {
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 */
216 bb_show_usage();
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";
226 argc -= optind;
227 argv += optind;
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
241 * are met:
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
261 * SUCH DAMAGE.