comparison toys/posix/od.c @ 653:2986aa63a021

Move commands into "posix", "lsb", and "other" menus/directories.
author Rob Landley <rob@landley.net>
date Sat, 25 Aug 2012 14:25:22 -0500
parents toys/od.c@93ef2516f15a
children 6df4ccc0acbe
comparison
equal deleted inserted replaced
652:2d7c56913fda 653:2986aa63a021
1 /* vi: set sw=4 ts=4:
2 *
3 * od.c - Provide octal/hex dumps of data
4 *
5 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
6 * Copyright 2012 Rob Landley <rob@landley.net>
7 *
8 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/od.html
9
10 USE_OD(NEWTOY(od, "j#vN#xsodcbA:t*", TOYFLAG_USR|TOYFLAG_BIN))
11
12 config OD
13 bool "od"
14 default y
15 help
16 usage: od [-bdosxv] [-j #] [-N #] [-A doxn] [-t arg]
17
18 -A Address base (decimal, octal, hexdecimal, none)
19 -t output type(s) a (ascii) c (char) d (decimal) foux
20 */
21
22 #include "toys.h"
23
24 #define FLAG_t (1 << 0)
25 #define FLAG_A (1 << 1)
26 #define FLAG_b (1 << 2)
27 #define FLAG_c (1 << 3)
28 #define FLAG_d (1 << 4)
29 #define FLAG_o (1 << 5)
30 #define FLAG_s (1 << 6)
31 #define FLAG_x (1 << 7)
32 #define FLAG_N (1 << 8)
33 #define FLAG_v (1 << 9)
34
35 DEFINE_GLOBALS(
36 struct arg_list *output_base;
37 char *address_base;
38 long max_count;
39 long jump_bytes;
40
41 unsigned types, leftover, star, address_idx;
42 char *buf;
43 uint64_t bufs[4]; // force 64-bit alignment
44 off_t pos;
45 )
46
47 #define TT this.od
48
49 static char *ascii = "nulsohstxetxeotenqackbel bs ht nl vt ff cr so si"
50 "dledc1dc2dc3dc4naksynetbcan emsubesc fs gs rs us sp";
51
52 struct odtype {
53 int type;
54 int size;
55 };
56
57 static void od_outline(void)
58 {
59 unsigned flags = toys.optflags;
60 char *abases[] = {"", "%07d", "%07o", "%06x"};
61 struct odtype *types = (struct odtype *)toybuf, *t;
62 int i, len;
63
64 if (TT.leftover<16) memset(TT.buf+TT.leftover, 0, 16-TT.leftover);
65
66 // Handle duplciate lines as *
67 if (!(flags&FLAG_v) && TT.jump_bytes != TT.pos && TT.leftover
68 && !memcmp(TT.bufs, TT.bufs + 2, 16))
69 {
70 if (!TT.star) {
71 xputs("*");
72 TT.star++;
73 }
74
75 // Print line position
76 } else {
77 TT.star = 0;
78
79 xprintf(abases[TT.address_idx], TT.pos);
80 if (!TT.leftover) {
81 if (TT.address_idx) xputc('\n');
82 return;
83 }
84 }
85
86 TT.pos += len = TT.leftover;
87 TT.leftover = 0;
88 if (TT.star) return;
89
90 // For each output type, print one line
91
92 for (i=0; i<TT.types; i++) {
93 int j = 0, pad = i ? 8 : 0;
94 char buf[128];
95
96 t = types+i;
97 while (j<len) {
98 unsigned k;
99 int throw = 0;
100
101 // Handle ascii
102 if (t->type < 2) {
103 char c = TT.buf[j++];
104 pad += 4;
105
106 if (!t->type) {
107 c &= 127;
108 if (c<=32) sprintf(buf, "%.3s", ascii+(3*c));
109 else if (c==127) strcpy(buf, "del");
110 else sprintf(buf, "%c", c);
111 } else {
112 char *bfnrtav = "\b\f\n\r\t\a\v", *s = strchr(bfnrtav, c);
113 if (s) sprintf(buf, "\\%c", "bfnrtav0"[s-bfnrtav]);
114 else if (c < 32 || c >= 127) sprintf(buf, "%03o", c);
115 else {
116 // TODO: this should be UTF8 aware.
117 sprintf(buf, "%c", c);
118 }
119 }
120 } else if (CFG_TOYBOX_FLOAT && t->type == 6) {
121 long double ld;
122 union {float f; double d; long double ld;} fdl;
123
124 memcpy(&fdl, TT.buf+j, t->size);
125 j += t->size;
126 if (sizeof(float) == t->size) {
127 ld = fdl.f;
128 pad += (throw = 8)+7;
129 } else if (sizeof(double) == t->size) {
130 ld = fdl.d;
131 pad += (throw = 17)+8;
132 } else if (sizeof(long double) == t->size) {
133 ld = fdl.ld;
134 pad += (throw = 21)+9;
135 } else error_exit("bad -tf '%d'", t->size);
136
137 sprintf(buf, "%.*Le", throw, ld);
138 // Integer types
139 } else {
140 unsigned long long ll = 0, or;
141 char *c[] = {"%*lld", "%*llu", "%0*llo", "%0*llx"},
142 *class = c[t->type-2];
143
144 // Work out width of field
145 if (t->size == 8) {
146 or = -1LL;
147 if (t->type == 2) or >>= 1;
148 } else or = (1LL<<(8*t->size))-1;
149 throw = sprintf(buf, class, 0, or);
150
151 // Accumulate integer based on size argument
152 for (k=0; k < t->size; k++) {
153 or = TT.buf[j++];
154 ll |= or << (8*(IS_BIG_ENDIAN ? t->size-k-1 : k));
155 }
156
157 // Handle negative values
158 if (t->type == 2) {
159 or = sizeof(or) - t->size;
160 throw++;
161 if (or && (ll & (1l<<((8*t->size)-1))))
162 ll |= ((or<<(8*or))-1) << (8*t->size);
163 }
164
165 sprintf(buf, class, throw, ll);
166 pad += throw+1;
167 }
168 xprintf("%*s", pad, buf);
169 pad = 0;
170 }
171 xputc('\n');
172 }
173
174 // buffer toggle for "same as last time" check.
175 TT.buf = (char *)((TT.buf == (char *)TT.bufs) ? TT.bufs+2 : TT.bufs);
176 }
177
178 static void do_od(int fd, char *name)
179 {
180 // Skip input, possibly more than one entire file.
181 if (TT.jump_bytes < TT.pos) {
182 off_t off = lskip(fd, TT.jump_bytes);
183 if (off > 0) TT.pos += off;
184 if (TT.jump_bytes < TT.pos) return;
185 }
186
187 for(;;) {
188 char *buf = TT.buf + TT.leftover;
189 int len = 16 - TT.leftover;
190
191 if (toys.optflags & FLAG_N) {
192 if (!TT.max_count) break;
193 if (TT.max_count < len) len = TT.max_count;
194 }
195
196 len = readall(fd, buf, len);
197 if (len < 0) {
198 perror_msg("%s", name);
199 break;
200 }
201 if (TT.max_count) TT.max_count -= len;
202 TT.leftover += len;
203 if (TT.leftover < 16) break;
204
205 od_outline();
206 }
207 }
208
209 static void append_base(char *base)
210 {
211 char *s = base;
212 struct odtype *types = (struct odtype *)toybuf;
213 int type;
214
215 for (;;) {
216 int size = 1;
217
218 if (!*s) return;
219 if (TT.types >= sizeof(toybuf)/sizeof(struct odtype)) break;
220 if (-1 == (type = stridx("acduox"USE_TOYBOX_FLOAT("f"), *(s++)))) break;
221
222 if (isdigit(*s)) {
223 size = strtol(s, &s, 10);
224 if (type < 2 && size != 1) break;
225 if (CFG_TOYBOX_FLOAT && type == 6 && size == sizeof(long double));
226 else if (size < 0 || size > 8) break;
227 } else if (CFG_TOYBOX_FLOAT && type == 6) {
228 int sizes[] = {sizeof(float), sizeof(double), sizeof(long double)};
229 if (-1 == (size = stridx("FDL", *s))) size = sizeof(double);
230 else {
231 s++;
232 size = sizes[size];
233 }
234 } else if (type > 1) {
235 if (-1 == (size = stridx("CSIL", *s))) size = 4;
236 else {
237 s++;
238 size = 1 << size;
239 }
240 }
241
242 types[TT.types].type = type;
243 types[TT.types].size = size;
244 TT.types++;
245 }
246
247 error_exit("bad -t %s", base);
248 }
249
250 void od_main(void)
251 {
252 struct arg_list *arg;
253
254 TT.buf = (char *)TT.bufs;
255
256 if (!TT.address_base) TT.address_idx = 2;
257 else if (0>(TT.address_idx = stridx("ndox", *TT.address_base)))
258 error_exit("bad -A '%c'", *TT.address_base);
259
260 // Collect -t entries
261
262 for (arg = TT.output_base; arg; arg = arg->next) append_base(arg->arg);
263 if (toys.optflags & FLAG_b) append_base("o1");
264 if (toys.optflags & FLAG_d) append_base("u2");
265 if (toys.optflags & FLAG_o) append_base("o2");
266 if (toys.optflags & FLAG_s) append_base("d2");
267 if (toys.optflags & FLAG_x) append_base("x2");
268 if (!TT.output_base) append_base("o2");
269
270 loopfiles(toys.optargs, do_od);
271
272 if (TT.leftover) od_outline();
273 od_outline();
274 }