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
32 #include <sys/types.h>
49 static void odadd(const char *);
50 static void odformat(const char *);
51 static const char *odformatfp(char, const char *);
52 static const char *odformatint(char, const char *);
53 static void odoffset(int, char ***);
54 static void odusage(void);
57 oldsyntax(int argc, char ***argvp)
59 static char empty[] = "", padding[] = PADDING;
63 /* Add initial (default) address format. -A may change it later. */
65 add("\"%07.7_Ao\n\"");
70 while ((ch = getopt(argc, argv, "A:aBbcDdeFfHhIij:LlN:Oost:vXx")) != -1)
74 case 'd': case 'o': case 'x':
75 fshead->nextfu->fmt[TYPE_OFFSET] = *optarg;
76 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] =
80 fshead->nextfu->fmt = empty;
81 fshead->nextfs->nextfu->fmt = padding;
84 errx(1, "%s: invalid address base", optarg);
106 case 'e': /* undocumented in od */
131 skip = strtoll(optarg, &end, 0);
134 else if (*end == 'k')
136 else if (*end == 'm')
138 if (errno != 0 || skip < 0 || strlen(end) > 1)
139 errx(1, "%s: invalid skip amount", optarg);
142 if ((length = atoi(optarg)) <= 0)
143 errx(1, "%s: invalid length", optarg);
162 if (fshead->nextfs->nextfs == NULL)
169 odoffset(argc, argvp);
177 "usage: od [-aBbcDdeFfHhIiLlOosvXx] [-A base] [-j skip] [-N length] [-t type]\n");
179 " [[+]offset[.][Bb]] [file ...]\n");
184 odoffset(int argc, char ***argvp)
190 * The offset syntax of od(1) was genuinely bizarre. First, if
191 * it started with a plus it had to be an offset. Otherwise, if
192 * there were at least two arguments, a number or lower-case 'x'
193 * followed by a number makes it an offset. By default it was
194 * octal; if it started with 'x' or '0x' it was hex. If it ended
195 * in a '.', it was decimal. If a 'b' or 'B' was appended, it
196 * multiplied the number by 512 or 1024 byte units. There was
197 * no way to assign a block count to a hex offset.
199 * We assume it's a file if the offset is bad.
201 p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
203 if (*p != '+' && (argc < 2 ||
204 (!isdigit(p[0]) && (p[0] != 'x' || !isxdigit(p[1])))))
209 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
214 if (p[0] == 'x' && isxdigit(p[1])) {
217 } else if (p[0] == '0' && p[1] == 'x') {
222 /* skip over the number */
224 for (num = p; isxdigit(*p); ++p);
226 for (num = p; isdigit(*p); ++p);
228 /* check for no number */
232 /* if terminates with a '.', base is decimal */
239 skip = strtoll(num, &end, base ? base : 8);
241 /* if end isn't the same as p, we got a non-octal digit */
251 } else if (*p == 'b') {
263 * If the offset uses a non-octal base, the base of the offset
264 * is changed as well. This isn't pretty, but it's easy.
267 fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
268 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
269 } else if (base == 10) {
270 fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
271 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
274 /* Terminate file list. */
279 odformat(const char *fmt)
283 while (*fmt != '\0') {
284 switch ((fchar = *fmt++)) {
286 odadd("16/1 \"%3_u \" \"\\n\"");
289 odadd("16/1 \"%3_c \" \"\\n\"");
291 case 'o': case 'u': case 'd': case 'x':
292 fmt = odformatint(fchar, fmt);
295 fmt = odformatfp(fchar, fmt);
298 errx(1, "%c: unrecognised format character", fchar);
304 odformatfp(char fchar __unused, const char *fmt)
310 isize = sizeof(double);
313 isize = sizeof(float);
317 isize = sizeof(double);
321 isize = sizeof(long double);
325 if (isdigit((unsigned char)*fmt)) {
327 isize = (size_t)strtoul(fmt, &end, 10);
328 if (errno != 0 || isize == 0)
329 errx(1, "%s: invalid size", fmt);
330 fmt = (const char *)end;
341 if (isize == sizeof(long double))
344 errx(1, "unsupported floating point size %lu",
348 asprintf(&hdfmt, "%lu/%lu \" %%%d.%de \" \"\\n\"",
349 16UL / (u_long)isize, (u_long)isize, digits + 8, digits);
359 odformatint(char fchar, const char *fmt)
361 unsigned long long n;
369 isize = sizeof(char);
377 isize = sizeof(long);
381 isize = sizeof(short);
385 if (isdigit((unsigned char)*fmt)) {
387 isize = (size_t)strtoul(fmt, &end, 10);
388 if (errno != 0 || isize == 0)
389 errx(1, "%s: invalid size", fmt);
390 if (isize != sizeof(char) && isize != sizeof(short) &&
391 isize != sizeof(int) && isize != sizeof(long))
392 errx(1, "unsupported int size %lu",
394 fmt = (const char *)end;
399 * Calculate the maximum number of digits we need to
400 * fit the number. Overestimate for decimal with log
401 * base 8. We need one extra space for signed numbers
404 n = (1ULL << (8 * isize)) - 1;
408 n >>= (fchar == 'x') ? 4 : 3;
412 asprintf(&hdfmt, "%lu/%lu \"%*s%%%s%d%c\" \"\\n\"",
413 16UL / (u_long)isize, (u_long)isize, (int)(4 * isize - digits),
414 "", (fchar == 'd' || fchar == 'u') ? "" : "0", digits, fchar);
424 odadd(const char *fmt)
429 add("\""PADDING"\"");