Lines 43-58
Link Here
|
43 |
|
43 |
|
44 |
#include <ctype.h> |
44 |
#include <ctype.h> |
45 |
#include <err.h> |
45 |
#include <err.h> |
|
|
46 |
#include <errno.h> |
46 |
#include <stdio.h> |
47 |
#include <stdio.h> |
47 |
#include <stdlib.h> |
48 |
#include <stdlib.h> |
48 |
#include <unistd.h> |
49 |
#include <unistd.h> |
49 |
|
50 |
|
50 |
#include "hexdump.h" |
51 |
#include "hexdump.h" |
51 |
|
52 |
|
52 |
int deprecated; |
53 |
#define PADDING " " |
53 |
|
54 |
|
|
|
55 |
int odmode; |
56 |
|
57 |
static void odadd(const char *); |
58 |
static void odformat(const char *); |
54 |
static void odoffset(int, char ***); |
59 |
static void odoffset(int, char ***); |
55 |
static void odprecede(void); |
|
|
56 |
|
60 |
|
57 |
void |
61 |
void |
58 |
oldsyntax(argc, argvp) |
62 |
oldsyntax(argc, argvp) |
Lines 62-152
Link Here
|
62 |
extern enum _vflag vflag; |
66 |
extern enum _vflag vflag; |
63 |
extern FS *fshead; |
67 |
extern FS *fshead; |
64 |
int ch; |
68 |
int ch; |
65 |
char **argv; |
69 |
char **argv, *end; |
|
|
70 |
|
71 |
/* Add initial (default) address format. -A may change it later. */ |
72 |
#define TYPE_OFFSET 7 |
73 |
add("\"%07.7_Ao\n\""); |
74 |
add("\"%07.7_ao \""); |
66 |
|
75 |
|
67 |
deprecated = 1; |
76 |
odmode = 1; |
68 |
argv = *argvp; |
77 |
argv = *argvp; |
69 |
while ((ch = getopt(argc, argv, "aBbcDdeFfHhIiLlOoPpswvXx")) != -1) |
78 |
while ((ch = getopt(argc, argv, "A:aBbcDdeFfHhIij:LlN:Oost:vXx")) != -1) |
70 |
switch (ch) { |
79 |
switch (ch) { |
|
|
80 |
case 'A': |
81 |
switch (*optarg) { |
82 |
case 'd': |
83 |
case 'o': |
84 |
case 'x': |
85 |
fshead->nextfu->fmt[TYPE_OFFSET] = *optarg; |
86 |
fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = *optarg; |
87 |
break; |
88 |
case 'n': |
89 |
fshead->nextfu->fmt = ""; |
90 |
fshead->nextfs->nextfu->fmt = PADDING; |
91 |
break; |
92 |
default: |
93 |
errx(1, "%s: invalid address base", optarg); |
94 |
} |
95 |
break; |
71 |
case 'a': |
96 |
case 'a': |
72 |
odprecede(); |
97 |
odformat("a"); |
73 |
add("16/1 \"%3_u \" \"\\n\""); |
|
|
74 |
break; |
98 |
break; |
75 |
case 'B': |
99 |
case 'B': |
76 |
case 'o': |
100 |
case 'o': |
77 |
odprecede(); |
101 |
odformat("o2"); |
78 |
add("8/2 \" %06o \" \"\\n\""); |
|
|
79 |
break; |
102 |
break; |
80 |
case 'b': |
103 |
case 'b': |
81 |
odprecede(); |
104 |
odformat("o1"); |
82 |
add("16/1 \"%03o \" \"\\n\""); |
|
|
83 |
break; |
105 |
break; |
84 |
case 'c': |
106 |
case 'c': |
85 |
odprecede(); |
107 |
odformat("c"); |
86 |
add("16/1 \"%3_c \" \"\\n\""); |
|
|
87 |
break; |
108 |
break; |
88 |
case 'd': |
109 |
case 'd': |
89 |
odprecede(); |
110 |
odformat("u2"); |
90 |
add("8/2 \" %05u \" \"\\n\""); |
|
|
91 |
break; |
111 |
break; |
92 |
case 'D': |
112 |
case 'D': |
93 |
odprecede(); |
113 |
odformat("u4"); |
94 |
add("4/4 \" %010u \" \"\\n\""); |
|
|
95 |
break; |
114 |
break; |
96 |
case 'e': /* undocumented in od */ |
115 |
case 'e': /* undocumented in od */ |
97 |
case 'F': |
116 |
case 'F': |
98 |
odprecede(); |
117 |
odformat("fD"); |
99 |
add("2/8 \" %21.14e \" \"\\n\""); |
|
|
100 |
break; |
118 |
break; |
101 |
|
|
|
102 |
case 'f': |
119 |
case 'f': |
103 |
odprecede(); |
120 |
odformat("fF"); |
104 |
add("4/4 \" %14.7e \" \"\\n\""); |
|
|
105 |
break; |
121 |
break; |
106 |
case 'H': |
122 |
case 'H': |
107 |
case 'X': |
123 |
case 'X': |
108 |
odprecede(); |
124 |
odformat("x4"); |
109 |
add("4/4 \" %08x \" \"\\n\""); |
|
|
110 |
break; |
125 |
break; |
111 |
case 'h': |
126 |
case 'h': |
112 |
case 'x': |
127 |
case 'x': |
113 |
odprecede(); |
128 |
odformat("x2"); |
114 |
add("8/2 \" %04x \" \"\\n\""); |
|
|
115 |
break; |
129 |
break; |
116 |
case 'I': |
130 |
case 'I': |
117 |
case 'L': |
131 |
case 'L': |
118 |
case 'l': |
132 |
case 'l': |
119 |
odprecede(); |
133 |
odformat("dL"); |
120 |
add("4/4 \" %11d \" \"\\n\""); |
|
|
121 |
break; |
134 |
break; |
122 |
case 'i': |
135 |
case 'i': |
123 |
odprecede(); |
136 |
odformat("dI"); |
124 |
add("8/2 \" %6d \" \"\\n\""); |
137 |
break; |
|
|
138 |
case 'j': |
139 |
errno = 0; |
140 |
skip = (off_t)strtoul(optarg, &end, 0); |
141 |
if (*end == 'b') |
142 |
skip *= 512; |
143 |
else if (*end == 'k') |
144 |
skip *= 1024; |
145 |
else if (*end == 'm') |
146 |
skip *= 1048576L; |
147 |
if (errno != 0 || strlen(end) > 1) |
148 |
errx(1, "%s: invalid skip amount", optarg); |
149 |
break; |
150 |
case 'N': |
151 |
if ((length = atoi(optarg)) <= 0) |
152 |
errx(1, "%s: invalid length", optarg); |
125 |
break; |
153 |
break; |
126 |
case 'O': |
154 |
case 'O': |
127 |
odprecede(); |
155 |
odformat("o4"); |
128 |
add("4/4 \" %011o \" \"\\n\""); |
156 |
break; |
|
|
157 |
case 's': |
158 |
odformat("d2"); |
129 |
break; |
159 |
break; |
|
|
160 |
case 't': |
161 |
odformat(optarg); |
162 |
break; |
130 |
case 'v': |
163 |
case 'v': |
131 |
vflag = ALL; |
164 |
vflag = ALL; |
132 |
break; |
165 |
break; |
133 |
case 'P': |
|
|
134 |
case 'p': |
135 |
case 's': |
136 |
case 'w': |
137 |
case '?': |
166 |
case '?': |
138 |
default: |
167 |
default: |
139 |
warnx("od(1) has been deprecated for hexdump(1)"); |
|
|
140 |
if (ch != '?') |
141 |
warnx("hexdump(1) compatibility doesn't support the -%c option%s", |
142 |
ch, ch == 's' ? "; see strings(1)" : ""); |
143 |
usage(); |
168 |
usage(); |
144 |
} |
169 |
} |
145 |
|
170 |
|
146 |
if (!fshead) { |
171 |
if (fshead->nextfs->nextfs == NULL) |
147 |
add("\"%07.7_Ao\n\""); |
172 |
odformat("oS"); |
148 |
add("\"%07.7_ao \" 8/2 \"%06o \" \"\\n\""); |
|
|
149 |
} |
150 |
|
173 |
|
151 |
argc -= optind; |
174 |
argc -= optind; |
152 |
*argvp += optind; |
175 |
*argvp += optind; |
Lines 160-166
Link Here
|
160 |
int argc; |
183 |
int argc; |
161 |
char ***argvp; |
184 |
char ***argvp; |
162 |
{ |
185 |
{ |
163 |
extern off_t skip; |
|
|
164 |
unsigned char *p, *num, *end; |
186 |
unsigned char *p, *num, *end; |
165 |
int base; |
187 |
int base; |
166 |
|
188 |
|
Lines 241-247
Link Here
|
241 |
* If the offset uses a non-octal base, the base of the offset |
263 |
* If the offset uses a non-octal base, the base of the offset |
242 |
* is changed as well. This isn't pretty, but it's easy. |
264 |
* is changed as well. This isn't pretty, but it's easy. |
243 |
*/ |
265 |
*/ |
244 |
#define TYPE_OFFSET 7 |
|
|
245 |
if (base == 16) { |
266 |
if (base == 16) { |
246 |
fshead->nextfu->fmt[TYPE_OFFSET] = 'x'; |
267 |
fshead->nextfu->fmt[TYPE_OFFSET] = 'x'; |
247 |
fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x'; |
268 |
fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x'; |
Lines 254-268
Link Here
|
254 |
(*argvp)[1] = NULL; |
275 |
(*argvp)[1] = NULL; |
255 |
} |
276 |
} |
256 |
|
277 |
|
|
|
278 |
static void |
279 |
odformat(const char *fmt) |
280 |
{ |
281 |
size_t size; |
282 |
unsigned long long n; |
283 |
int digits; |
284 |
char *hdfmt, fchar, *end; |
285 |
|
286 |
while (*fmt != '\0') { |
287 |
switch ((fchar = *fmt++)) { |
288 |
case 'a': |
289 |
odadd("16/1 \"%3_u \" \"\\n\""); |
290 |
break; |
291 |
case 'c': |
292 |
odadd("16/1 \"%3_c \" \"\\n\""); |
293 |
break; |
294 |
case 'o': |
295 |
case 'u': |
296 |
case 'd': |
297 |
case 'x': |
298 |
size = sizeof(int); |
299 |
switch (*fmt) { |
300 |
case 'C': |
301 |
size = sizeof(char); |
302 |
fmt++; |
303 |
break; |
304 |
case 'I': |
305 |
size = sizeof(int); |
306 |
fmt++; |
307 |
break; |
308 |
case 'L': |
309 |
size = sizeof(long); |
310 |
fmt++; |
311 |
break; |
312 |
case 'S': |
313 |
size = sizeof(short); |
314 |
fmt++; |
315 |
break; |
316 |
default: |
317 |
if (isdigit(*fmt)) { |
318 |
errno = 0; |
319 |
size = strtol(fmt, &end, 10); |
320 |
if (errno != 0 || size <= 0) |
321 |
errx(1, "bad size"); |
322 |
if (size != sizeof(char) && |
323 |
size != sizeof(short) && |
324 |
size != sizeof(int) && |
325 |
size != sizeof(long)) |
326 |
errx(1, |
327 |
"unsupported int size %ld", |
328 |
size); |
329 |
fmt = (const char *)end; |
330 |
} |
331 |
} |
332 |
n = (1ULL << (8 * size)) - 1; |
333 |
digits = 0; |
334 |
while (n != 0) { |
335 |
digits++; |
336 |
n >>= (fchar == 'o' || fchar == 'd') ? 3 : 4; |
337 |
} |
338 |
asprintf(&hdfmt, "%d/%d \"%%%s%d%c \" \"\\n\"", |
339 |
16 / size, size, fchar == 'd' ? "" : "0", |
340 |
digits, fchar); |
341 |
if (hdfmt == NULL) |
342 |
err(1, NULL); |
343 |
odadd(hdfmt); |
344 |
free(hdfmt); |
345 |
break; |
346 |
case 'f': |
347 |
size = sizeof(double); |
348 |
switch (*fmt) { |
349 |
case 'F': |
350 |
size = sizeof(float); |
351 |
fmt++; |
352 |
break; |
353 |
case 'D': |
354 |
size = sizeof(double); |
355 |
fmt++; |
356 |
break; |
357 |
case 'L': |
358 |
size = sizeof(long double); |
359 |
fmt++; |
360 |
break; |
361 |
default: |
362 |
if (isdigit(*fmt)) { |
363 |
errno = 0; |
364 |
size = strtol(fmt, &end, 10); |
365 |
if (errno != 0 || size <= 0) |
366 |
errx(1, "bad size"); |
367 |
fmt = (const char *)end; |
368 |
} |
369 |
} |
370 |
switch (size) { |
371 |
case sizeof(float): |
372 |
odadd("4/4 \" %14.7e \" \"\\n\""); |
373 |
break; |
374 |
case sizeof(double): |
375 |
odadd("2/8 \" %21.14e \" \"\\n\""); |
376 |
break; |
377 |
case sizeof(long double): |
378 |
#ifdef notyet |
379 |
odadd("1/16 \" %21.14Le \" \"\\n\""); |
380 |
break; |
381 |
#endif |
382 |
default: |
383 |
errx(1, "unsupported FP size %ld", size); |
384 |
} |
385 |
break; |
386 |
default: |
387 |
errx(1, "%s: bad format", fmt); |
388 |
} |
389 |
} |
390 |
} |
391 |
|
257 |
static void |
392 |
static void |
258 |
odprecede() |
393 |
odadd(const char *fmt) |
259 |
{ |
394 |
{ |
260 |
static int first = 1; |
395 |
static int needpad; |
261 |
|
396 |
|
262 |
if (first) { |
397 |
if (needpad) |
263 |
first = 0; |
398 |
add("\""PADDING"\""); |
264 |
add("\"%07.7_Ao\n\""); |
399 |
add(fmt); |
265 |
add("\"%07.7_ao \""); |
400 |
needpad = 1; |
266 |
} else |
|
|
267 |
add("\" \""); |
268 |
} |
401 |
} |