2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)odsyntax.c 8.2 (Berkeley) 5/4/95";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/types.h>
57 static void odadd(const char *);
58 static void odformat(const char *);
59 static const char *odformatfp(char, const char *);
60 static const char *odformatint(char, const char *);
61 static void odoffset(int, char ***);
62 static void odusage(void);
65 oldsyntax(int argc, char ***argvp)
67 static char empty[] = "", padding[] = PADDING;
71 /* Add initial (default) address format. -A may change it later. */
73 add("\"%07.7_Ao\n\"");
78 while ((ch = getopt(argc, argv, "A:aBbcDdeFfHhIij:LlN:Oost:vXx")) != -1)
82 case 'd': case 'o': case 'x':
83 fshead->nextfu->fmt[TYPE_OFFSET] = *optarg;
84 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] =
88 fshead->nextfu->fmt = empty;
89 fshead->nextfs->nextfu->fmt = padding;
92 errx(1, "%s: invalid address base", optarg);
114 case 'e': /* undocumented in od */
139 skip = strtoll(optarg, &end, 0);
142 else if (*end == 'k')
144 else if (*end == 'm')
146 if (errno != 0 || skip < 0 || strlen(end) > 1)
147 errx(1, "%s: invalid skip amount", optarg);
150 if ((length = atoi(optarg)) <= 0)
151 errx(1, "%s: invalid length", optarg);
170 if (fshead->nextfs->nextfs == NULL)
177 odoffset(argc, argvp);
185 "usage: od [-aBbcDdeFfHhIiLlOosvXx] [-A base] [-j skip] [-N length] [-t type]\n");
187 " [[+]offset[.][Bb]] [file ...]\n");
192 odoffset(int argc, char ***argvp)
198 * The offset syntax of od(1) was genuinely bizarre. First, if
199 * it started with a plus it had to be an offset. Otherwise, if
200 * there were at least two arguments, a number or lower-case 'x'
201 * followed by a number makes it an offset. By default it was
202 * octal; if it started with 'x' or '0x' it was hex. If it ended
203 * in a '.', it was decimal. If a 'b' or 'B' was appended, it
204 * multiplied the number by 512 or 1024 byte units. There was
205 * no way to assign a block count to a hex offset.
207 * We assume it's a file if the offset is bad.
209 p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
211 if (*p != '+' && (argc < 2 ||
212 (!isdigit(p[0]) && (p[0] != 'x' || !isxdigit(p[1])))))
217 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
222 if (p[0] == 'x' && isxdigit(p[1])) {
225 } else if (p[0] == '0' && p[1] == 'x') {
230 /* skip over the number */
232 for (num = p; isxdigit(*p); ++p);
234 for (num = p; isdigit(*p); ++p);
236 /* check for no number */
240 /* if terminates with a '.', base is decimal */
247 skip = strtoll(num, &end, base ? base : 8);
249 /* if end isn't the same as p, we got a non-octal digit */
259 } else if (*p == 'b') {
271 * If the offset uses a non-octal base, the base of the offset
272 * is changed as well. This isn't pretty, but it's easy.
275 fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
276 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
277 } else if (base == 10) {
278 fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
279 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
282 /* Terminate file list. */
287 odformat(const char *fmt)
291 while (*fmt != '\0') {
292 switch ((fchar = *fmt++)) {
294 odadd("16/1 \"%3_u \" \"\\n\"");
297 odadd("16/1 \"%3_c \" \"\\n\"");
299 case 'o': case 'u': case 'd': case 'x':
300 fmt = odformatint(fchar, fmt);
303 fmt = odformatfp(fchar, fmt);
306 errx(1, "%c: unrecognised format character", fchar);
312 odformatfp(char fchar __unused, const char *fmt)
318 isize = sizeof(double);
321 isize = sizeof(float);
325 isize = sizeof(double);
329 isize = sizeof(long double);
333 if (isdigit((unsigned char)*fmt)) {
335 isize = (size_t)strtoul(fmt, &end, 10);
336 if (errno != 0 || isize == 0)
337 errx(1, "%s: invalid size", fmt);
338 fmt = (const char *)end;
349 if (isize == sizeof(long double))
352 errx(1, "unsupported floating point size %lu",
356 asprintf(&hdfmt, "%lu/%lu \" %%%d.%de \" \"\\n\"",
357 16UL / (u_long)isize, (u_long)isize, digits + 8, digits);
367 odformatint(char fchar, const char *fmt)
369 unsigned long long n;
377 isize = sizeof(char);
385 isize = sizeof(long);
389 isize = sizeof(short);
393 if (isdigit((unsigned char)*fmt)) {
395 isize = (size_t)strtoul(fmt, &end, 10);
396 if (errno != 0 || isize == 0)
397 errx(1, "%s: invalid size", fmt);
398 if (isize != sizeof(char) && isize != sizeof(short) &&
399 isize != sizeof(int) && isize != sizeof(long))
400 errx(1, "unsupported int size %lu",
402 fmt = (const char *)end;
407 * Calculate the maximum number of digits we need to
408 * fit the number. Overestimate for decimal with log
409 * base 8. We need one extra space for signed numbers
412 n = (1ULL << (8 * isize)) - 1;
416 n >>= (fchar == 'x') ? 4 : 3;
420 asprintf(&hdfmt, "%lu/%lu \"%*s%%%s%d%c\" \"\\n\"",
421 16UL / (u_long)isize, (u_long)isize, (int)(4 * isize - digits),
422 "", (fchar == 'd' || fchar == 'u') ? "" : "0", digits, fchar);
432 odadd(const char *fmt)
437 add("\""PADDING"\"");