2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)odsyntax.c 8.2 (Berkeley) 5/4/95
30 * $FreeBSD: src/usr.bin/hexdump/odsyntax.c,v 1.8.2.1 2002/07/23 14:27:06 tjr Exp $
31 * $DragonFly: src/usr.bin/hexdump/odsyntax.c,v 1.3 2003/10/04 20:36:45 hmp Exp $
34 #include <sys/types.h>
51 static void odadd(const char *);
52 static void odformat(const char *);
53 static const char *odformatfp(char, const char *);
54 static const char *odformatint(char, const char *);
55 static void odoffset(int, char ***);
56 static void odusage(void);
59 oldsyntax(int argc, char ***argvp)
61 static char empty[] = "", padding[] = PADDING;
65 /* Add initial (default) address format. -A may change it later. */
67 add("\"%07.7_Ao\n\"");
72 while ((ch = getopt(argc, argv, "A:aBbcDdeFfHhIij:LlN:Oost:vXx")) != -1)
76 case 'd': case 'o': case 'x':
77 fshead->nextfu->fmt[TYPE_OFFSET] = *optarg;
78 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] =
82 fshead->nextfu->fmt = empty;
83 fshead->nextfs->nextfu->fmt = padding;
86 errx(1, "%s: invalid address base", optarg);
108 case 'e': /* undocumented in od */
133 skip = strtoll(optarg, &end, 0);
136 else if (*end == 'k')
138 else if (*end == 'm')
140 if (errno != 0 || skip < 0 || strlen(end) > 1)
141 errx(1, "%s: invalid skip amount", optarg);
144 if ((length = atoi(optarg)) <= 0)
145 errx(1, "%s: invalid length", optarg);
164 if (fshead->nextfs->nextfs == NULL)
171 odoffset(argc, argvp);
179 "usage: od [-aBbcDdeFfHhIiLlOosvXx] [-A base] [-j skip] [-N length] [-t type]\n");
181 " [[+]offset[.][Bb]] [file ...]\n");
186 odoffset(int argc, char ***argvp)
188 unsigned char *p, *num, *end;
192 * The offset syntax of od(1) was genuinely bizarre. First, if
193 * it started with a plus it had to be an offset. Otherwise, if
194 * there were at least two arguments, a number or lower-case 'x'
195 * followed by a number makes it an offset. By default it was
196 * octal; if it started with 'x' or '0x' it was hex. If it ended
197 * in a '.', it was decimal. If a 'b' or 'B' was appended, it
198 * multiplied the number by 512 or 1024 byte units. There was
199 * no way to assign a block count to a hex offset.
201 * We assume it's a file if the offset is bad.
203 p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
205 if (*p != '+' && (argc < 2 ||
206 (!isdigit(p[0]) && (p[0] != 'x' || !isxdigit(p[1])))))
211 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
216 if (p[0] == 'x' && isxdigit(p[1])) {
219 } else if (p[0] == '0' && p[1] == 'x') {
224 /* skip over the number */
226 for (num = p; isxdigit(*p); ++p);
228 for (num = p; isdigit(*p); ++p);
230 /* check for no number */
234 /* if terminates with a '.', base is decimal */
241 skip = strtoll(num, (char **)&end, base ? base : 8);
243 /* if end isn't the same as p, we got a non-octal digit */
253 } else if (*p == 'b') {
265 * If the offset uses a non-octal base, the base of the offset
266 * is changed as well. This isn't pretty, but it's easy.
269 fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
270 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
271 } else if (base == 10) {
272 fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
273 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
276 /* Terminate file list. */
281 odformat(const char *fmt)
285 while (*fmt != '\0') {
286 switch ((fchar = *fmt++)) {
288 odadd("16/1 \"%3_u \" \"\\n\"");
291 odadd("16/1 \"%3_c \" \"\\n\"");
293 case 'o': case 'u': case 'd': case 'x':
294 fmt = odformatint(fchar, fmt);
297 fmt = odformatfp(fchar, fmt);
300 errx(1, "%c: unrecognised format character", fchar);
306 odformatfp(char fchar __unused, const char *fmt)
312 isize = sizeof(double);
315 isize = sizeof(float);
319 isize = sizeof(double);
323 isize = sizeof(long double);
327 if (isdigit((unsigned char)*fmt)) {
329 isize = (size_t)strtoul(fmt, &end, 10);
330 if (errno != 0 || isize == 0)
331 errx(1, "%s: invalid size", fmt);
332 fmt = (const char *)end;
343 if (isize == sizeof(long double))
346 errx(1, "unsupported floating point size %lu",
350 asprintf(&hdfmt, "%lu/%lu \" %%%d.%de \" \"\\n\"",
351 16UL / (u_long)isize, (u_long)isize, digits + 8, digits);
361 odformatint(char fchar, const char *fmt)
363 unsigned long long n;
371 isize = sizeof(char);
379 isize = sizeof(long);
383 isize = sizeof(short);
387 if (isdigit((unsigned char)*fmt)) {
389 isize = (size_t)strtoul(fmt, &end, 10);
390 if (errno != 0 || isize == 0)
391 errx(1, "%s: invalid size", fmt);
392 if (isize != sizeof(char) && isize != sizeof(short) &&
393 isize != sizeof(int) && isize != sizeof(long))
394 errx(1, "unsupported int size %lu",
396 fmt = (const char *)end;
401 * Calculate the maximum number of digits we need to
402 * fit the number. Overestimate for decimal with log
403 * base 8. We need one extra space for signed numbers
406 n = (1ULL << (8 * isize)) - 1;
410 n >>= (fchar == 'x') ? 4 : 3;
414 asprintf(&hdfmt, "%lu/%lu \"%*s%%%s%d%c\" \"\\n\"",
415 16UL / (u_long)isize, (u_long)isize, (int)(4 * isize - digits),
416 "", (fchar == 'd' || fchar == 'u') ? "" : "0", digits, fchar);
426 odadd(const char *fmt)
431 add("\""PADDING"\"");