1 /* od.c - Provide octal/hex dumps of data
3 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
4 * Copyright 2012 Rob Landley <rob@landley.net>
6 * See http://opengroup.org/onlinepubs/9699919799/utilities/od.html
8 USE_OD(NEWTOY(od, "j#vw#<1=16N#xsodcbA:t*", TOYFLAG_USR|TOYFLAG_BIN))
14 usage: od [-bcdosxv] [-j #] [-N #] [-w #] [-A doxn] [-t acdfoux[#]]
16 -A Address base (decimal, octal, hexdecimal, none)
17 -j Skip this many bytes of input
18 -N Stop dumping after this many bytes
19 -t Output type a(scii) c(har) d(ecimal) f(loat) o(ctal) u(nsigned) (he)x
20 plus optional size in bytes
21 aliases: -b=-t o1, -c=-t c, -d=-t u2, -o=-t o2, -s=-t d2, -x=-t x2
22 -v Don't collapse repeated lines together
23 -w Total line width in bytes (default 16).
30 struct arg_list *output_base;
37 unsigned types, leftover, star;
38 char *buf; // Points to buffers[0] or buffers[1].
39 char *bufs[2]; // Used to detect duplicate lines.
43 static char *ascii = "nulsohstxetxeotenqackbel bs ht nl vt ff cr so si"
44 "dledc1dc2dc3dc4naksynetbcan emsubesc fs gs rs us sp";
51 static int od_out_t(struct odtype *t, char *buf, int *offset)
54 int throw = 0, pad = 0;
58 char c = TT.buf[(*offset)++];
63 if (c<=32) sprintf(buf, "%.3s", ascii+(3*c));
64 else if (c==127) strcpy(buf, "del");
65 else sprintf(buf, "%c", c);
67 char *bfnrtav = "\b\f\n\r\t\a\v", *s = strchr(bfnrtav, c);
68 if (s) sprintf(buf, "\\%c", "bfnrtav0"[s-bfnrtav]);
69 else if (c < 32 || c >= 127) sprintf(buf, "%03o", c);
71 // TODO: this should be UTF8 aware.
72 sprintf(buf, "%c", c);
75 } else if (CFG_TOYBOX_FLOAT && t->type == 6) {
77 union {float f; double d; long double ld;} fdl;
79 memcpy(&fdl, TT.buf+*offset, t->size);
81 if (sizeof(float) == t->size) {
84 } else if (sizeof(double) == t->size) {
86 pad += (throw = 17)+8;
87 } else if (sizeof(long double) == t->size) {
89 pad += (throw = 21)+9;
90 } else error_exit("bad -tf '%d'", t->size);
92 sprintf(buf, "%.*Le", throw, ld);
95 unsigned long long ll = 0, or;
96 char *c[] = {"%*lld", "%*llu", "%0*llo", "%0*llx"},
97 *class = c[t->type-2];
99 // Work out width of field
102 if (t->type == 2) or >>= 1;
103 } else or = (1LL<<(8*t->size))-1;
104 throw = sprintf(buf, class, 0, or);
106 // Accumulate integer based on size argument
107 for (k=0; k < t->size; k++) {
108 or = TT.buf[(*offset)++];
109 ll |= or << (8*(IS_BIG_ENDIAN ? t->size-k-1 : k));
112 // Handle negative values
114 or = sizeof(or) - t->size;
116 if (or && (ll & (1l<<((8*t->size)-1))))
117 ll |= ((or<<(8*or))-1) << (8*t->size);
120 sprintf(buf, class, throw, ll);
127 static void od_outline(void)
129 unsigned flags = toys.optflags;
130 char buf[128], *abases[] = {"", "%07lld", "%07llo", "%06llx"};
131 struct odtype *types = (struct odtype *)toybuf;
134 if (TT.leftover<TT.width) memset(TT.buf+TT.leftover, 0, TT.width-TT.leftover);
136 // Handle duplciate lines as *
137 if (!(flags&FLAG_v) && TT.jump_bytes != TT.pos && TT.leftover
138 && !memcmp(TT.bufs[0], TT.bufs[1], TT.width))
145 // Print line position
149 // off_t varies so expand it to largest possible size
150 xprintf(abases[TT.address_idx], (long long)TT.pos);
152 if (TT.address_idx) xputc('\n');
157 TT.pos += len = TT.leftover;
161 // Find largest "pad" of the output types.
162 for (i = pad = 0; i<TT.types; i++) {
165 // If more than one byte of input consumed, average rounding up.
166 j = od_out_t(types+i, buf, &bytes);
167 j = (j+bytes-1)/bytes;
169 if (j > pad) pad = j;
172 // For each output type, print one line
173 for (i=0; i<TT.types; i++) {
174 for (j = 0; j<len;) {
177 // pad for as many bytes as were consumed, and indent non-numbered lines
178 od_out_t(types+i, buf, &bytes);
179 xprintf("%*s", pad*(bytes-j) + 7*(!!i)*!j, buf);
185 // Toggle buffer for "same as last time" check.
186 TT.buf = (TT.buf == TT.bufs[0]) ? TT.bufs[1] : TT.bufs[0];
189 // Loop through input files
190 static void do_od(int fd, char *name)
192 // Skip input, possibly more than one entire file.
193 if (TT.jump_bytes > TT.pos) {
194 off_t pos = TT.jump_bytes-TT.pos, off = lskip(fd, pos);
196 if (off >= 0) TT.pos += pos-off;
197 if (TT.jump_bytes > TT.pos) return;
201 char *buf = TT.buf + TT.leftover;
202 int len = TT.width - TT.leftover;
204 if (toys.optflags & FLAG_N) {
205 if (!TT.max_count) break;
206 if (TT.max_count < len) len = TT.max_count;
209 len = readall(fd, buf, len);
211 perror_msg_raw(name);
214 if (TT.max_count) TT.max_count -= len;
216 if (TT.leftover < TT.width) break;
222 // Handle one -t argument (including implicit ones)
223 static void append_base(char *base)
226 struct odtype *types = (struct odtype *)toybuf;
233 if (TT.types >= sizeof(toybuf)/sizeof(struct odtype)) break;
234 if (-1 == (type = stridx("acduox"USE_TOYBOX_FLOAT("f"), *(s++)))) break;
237 size = strtol(s, &s, 10);
238 if (type < 2 && size != 1) break;
239 if (CFG_TOYBOX_FLOAT && type == 6 && size == sizeof(long double));
240 else if (size < 1 || size > 8) break;
241 } else if (CFG_TOYBOX_FLOAT && type == 6) {
242 int sizes[] = {sizeof(float), sizeof(double), sizeof(long double)};
243 if (-1 == (size = stridx("FDL", *s))) size = sizeof(double);
248 } else if (type > 1) {
249 if (-1 == (size = stridx("CSIL", *s))) size = 4;
256 types[TT.types].type = type;
257 types[TT.types].size = size;
261 error_exit("bad -t %s", base);
266 struct arg_list *arg;
268 TT.bufs[0] = xzalloc(TT.width);
269 TT.bufs[1] = xzalloc(TT.width);
272 if (!TT.address_base) TT.address_idx = 2;
273 else if (0>(TT.address_idx = stridx("ndox", *TT.address_base)))
274 error_exit("bad -A '%c'", *TT.address_base);
276 // Collect -t entries
278 for (arg = TT.output_base; arg; arg = arg->next) append_base(arg->arg);
279 if (toys.optflags & FLAG_b) append_base("o1");
280 if (toys.optflags & FLAG_c) append_base("c");
281 if (toys.optflags & FLAG_d) append_base("u2");
282 if (toys.optflags & FLAG_o) append_base("o2");
283 if (toys.optflags & FLAG_s) append_base("d2");
284 if (toys.optflags & FLAG_x) append_base("x2");
285 if (!TT.types) append_base("o2");
287 loopfiles(toys.optargs, do_od);
289 if (TT.leftover) od_outline();
292 if (CFG_TOYBOX_FREE) {