]>
git.saurik.com Git - apple/shell_cmds.git/blob - hexdump/odsyntax.c
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid
[] = "@(#)odsyntax.c 8.2 (Berkeley) 5/4/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/usr.bin/hexdump/odsyntax.c,v 1.16 2002/09/04 23:29:01 dwmalone Exp $");
42 #include <sys/types.h>
59 static void odadd(const char *);
60 static void odformat(const char *);
61 static const char *odformatfp(char, const char *);
62 static const char *odformatint(char, const char *);
63 static void odoffset(int, char ***);
64 static void odusage(void);
67 oldsyntax(int argc
, char ***argvp
)
69 static char _n
[] = "%_n", padding
[] = PADDING
;
73 /* Add initial (default) address format. -A may change it later. */
75 add("\"%07.7_Ao\n\"");
80 while ((ch
= getopt(argc
, argv
, "A:aBbcDdeFfHhIij:LlN:Oost:vXx")) != -1)
84 case 'd': case 'o': case 'x':
85 fshead
->nextfu
->fmt
[TYPE_OFFSET
] = *optarg
;
86 fshead
->nextfs
->nextfu
->fmt
[TYPE_OFFSET
] =
90 fshead
->nextfu
->fmt
= _n
; /* mimic regular output */
91 fshead
->nextfs
->nextfu
->fmt
= padding
;
94 errx(1, "%s: invalid address base", optarg
);
116 case 'e': /* undocumented in od */
141 skip
= strtoll(optarg
, &end
, 0);
144 else if (*end
== 'k')
146 else if (*end
== 'm')
148 else if (*end
== 'g')
150 else if (*end
!= '\0')
152 if (errno
!= 0 || skip
< 0 || strlen(end
) > 1)
153 errx(1, "%s: invalid skip amount", optarg
);
156 if ((length
= atoi(optarg
)) <= 0)
157 errx(1, "%s: invalid length", optarg
);
176 if (fshead
->nextfs
->nextfs
== NULL
)
183 odoffset(argc
, argvp
);
191 "usage: od [-aBbcDdeFfHhIiLlOosvXx] [-A base] [-j skip] [-N length] [-t type]\n");
193 " [[+]offset[.][Bb]] [file ...]\n");
198 odoffset(int argc
, char ***argvp
)
200 unsigned char *p
, *q
, *num
, *end
;
204 * The offset syntax of od(1) was genuinely bizarre. First, if
205 * it started with a plus it had to be an offset. Otherwise, if
206 * there were at least two arguments, a number or lower-case 'x'
207 * followed by a number makes it an offset. By default it was
208 * octal; if it started with 'x' or '0x' it was hex. If it ended
209 * in a '.', it was decimal. If a 'b' or 'B' was appended, it
210 * multiplied the number by 512 or 1024 byte units. There was
211 * no way to assign a block count to a hex offset.
213 * We assume it's a file if the offset is bad.
215 p
= (unsigned char *)(argc
== 1 ? (*argvp
)[0] : (*argvp
)[1]);
217 if (*p
!= '+' && (argc
< 2 ||
218 (!isdigit(p
[0]) && (p
[0] != 'x' || !isxdigit(p
[1])))))
223 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
228 if (p
[0] == 'x' && isxdigit(p
[1])) {
231 } else if (p
[0] == '0' && p
[1] == 'x') {
236 /* skip over the number */
238 for (num
= p
; isxdigit(*p
); ++p
);
240 for (num
= p
; isdigit(*p
); ++p
);
242 /* check for no number */
246 /* if terminates with a '.', base is decimal */
254 skip
= strtoll((const char *)num
, (char **)&end
, base
? base
: 8);
256 /* if end isn't the same as p, we got a non-octal digit */
266 } else if (*q
== 'b') {
278 * If the offset uses a non-octal base, the base of the offset
279 * is changed as well. This isn't pretty, but it's easy.
282 fshead
->nextfu
->fmt
[TYPE_OFFSET
] = 'x';
283 fshead
->nextfs
->nextfu
->fmt
[TYPE_OFFSET
] = 'x';
284 } else if (base
== 10) {
285 fshead
->nextfu
->fmt
[TYPE_OFFSET
] = 'd';
286 fshead
->nextfs
->nextfu
->fmt
[TYPE_OFFSET
] = 'd';
289 /* Terminate file list. */
294 odformat(const char *fmt
)
298 while (*fmt
!= '\0') {
299 switch ((fchar
= *fmt
++)) {
301 odadd("16/1 \"%3_u \" \"\\n\"");
304 odadd("16/1 \"%3_c \" \"\\n\"");
306 case 'o': case 'u': case 'd': case 'x':
307 fmt
= odformatint(fchar
, fmt
);
310 fmt
= odformatfp(fchar
, fmt
);
313 errx(1, "%c: unrecognised format character", fchar
);
319 odformatfp(char fchar __unused
, const char *fmt
)
325 isize
= sizeof(double);
328 isize
= sizeof(float);
332 isize
= sizeof(double);
336 isize
= sizeof(long double);
340 if (isdigit((unsigned char)*fmt
)) {
342 isize
= (size_t)strtoul(fmt
, &end
, 10);
343 if (errno
!= 0 || isize
== 0)
344 errx(1, "%s: invalid size", fmt
);
345 fmt
= (const char *)end
;
356 if (isize
== sizeof(long double))
359 errx(1, "unsupported floating point size %lu",
363 asprintf(&hdfmt
, "%lu/%lu \" %%%d.%de \" \"\\n\"",
364 16UL / (u_long
)isize
, (u_long
)isize
, digits
+ 8, digits
);
374 odformatint(char fchar
, const char *fmt
)
376 unsigned long long n
;
384 isize
= sizeof(char);
392 isize
= sizeof(long);
396 isize
= sizeof(short);
400 if (isdigit((unsigned char)*fmt
)) {
402 isize
= (size_t)strtoul(fmt
, &end
, 10);
403 if (errno
!= 0 || isize
== 0)
404 errx(1, "%s: invalid size", fmt
);
405 if (isize
!= sizeof(char) && isize
!= sizeof(short) &&
406 isize
!= sizeof(int) && isize
!= sizeof(long))
407 errx(1, "unsupported int size %lu",
409 fmt
= (const char *)end
;
414 * Calculate the maximum number of digits we need to
415 * fit the number. Overestimate for decimal with log
416 * base 8. We need one extra space for signed numbers
419 n
= (1ULL << (8 * isize
)) - 1;
423 n
>>= (fchar
== 'x') ? 4 : 3;
427 asprintf(&hdfmt
, "%lu/%lu \"%*s%%%s%d%c\" \"\\n\"",
428 16UL / (u_long
)isize
, (u_long
)isize
, (int)(4 * isize
- digits
),
429 "", (fchar
== 'd' || fchar
== 'u') ? "" : "0", digits
, fchar
);
439 odadd(const char *fmt
)
444 add("\""PADDING
"\"");