Annotation of src/usr.bin/hexdump/hexsyntax.c, Revision 1.9
1.9 ! miod 1: /* $OpenBSD: hexsyntax.c,v 1.8 2003/06/12 20:58:09 deraadt Exp $ */
1.6 pvalchev 2: /* $NetBSD: hexsyntax.c,v 1.8 1998/04/08 23:48:57 jeremy Exp $ */
1.2 deraadt 3:
1.1 deraadt 4: /*-
1.6 pvalchev 5: * Copyright (c) 1990, 1993
6: * The Regents of the University of California. All rights reserved.
1.1 deraadt 7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.7 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
34: /*static char sccsid[] = "from: @(#)hexsyntax.c 5.2 (Berkeley) 5/8/90";*/
1.9 ! miod 35: static char rcsid[] = "$OpenBSD: hexsyntax.c,v 1.8 2003/06/12 20:58:09 deraadt Exp $";
1.1 deraadt 36: #endif /* not lint */
37:
38: #include <sys/types.h>
1.6 pvalchev 39:
40: #include <err.h>
1.9 ! miod 41: #include <errno.h>
1.1 deraadt 42: #include <stdio.h>
1.4 deraadt 43: #include <stdlib.h>
1.6 pvalchev 44: #include <string.h>
1.4 deraadt 45: #include <unistd.h>
1.6 pvalchev 46:
1.1 deraadt 47: #include "hexdump.h"
48:
49: off_t skip; /* bytes to skip */
50:
1.4 deraadt 51: void
1.8 deraadt 52: newsyntax(int argc, char ***argvp)
1.1 deraadt 53: {
54: int ch;
55: char *p, **argv;
56:
57: argv = *argvp;
1.6 pvalchev 58: while ((ch = getopt(argc, argv, "bcCde:f:n:os:vx")) != -1)
1.1 deraadt 59: switch (ch) {
60: case 'b':
61: add("\"%07.7_Ax\n\"");
62: add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
63: break;
64: case 'c':
65: add("\"%07.7_Ax\n\"");
66: add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
67: break;
1.6 pvalchev 68: case 'C':
69: add("\"%08.8_Ax\n\"");
70: add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
71: add("\" |\" 16/1 \"%_p\" \"|\\n\"");
72: break;
1.1 deraadt 73: case 'd':
74: add("\"%07.7_Ax\n\"");
75: add("\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"");
76: break;
77: case 'e':
78: add(optarg);
79: break;
80: case 'f':
81: addfile(optarg);
82: break;
83: case 'n':
1.9 ! miod 84: errno = 0;
! 85: if ((length = strtol(optarg, NULL, 0)) < 0 ||
! 86: errno != 0)
1.6 pvalchev 87: errx(1, "%s: bad length value", optarg);
1.1 deraadt 88: break;
89: case 'o':
90: add("\"%07.7_Ax\n\"");
91: add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
92: break;
93: case 's':
1.9 ! miod 94: errno = 0;
! 95: if ((skip = (off_t)strtoll(optarg, &p, 0)) < 0 ||
! 96: errno != 0)
1.6 pvalchev 97: errx(1, "%s: bad skip value", optarg);
1.1 deraadt 98: switch(*p) {
99: case 'b':
100: skip *= 512;
101: break;
102: case 'k':
103: skip *= 1024;
104: break;
105: case 'm':
106: skip *= 1048576;
107: break;
108: }
109: break;
110: case 'v':
111: vflag = ALL;
112: break;
113: case 'x':
114: add("\"%07.7_Ax\n\"");
115: add("\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"");
116: break;
117: case '?':
118: usage();
119: }
120:
121: if (!fshead) {
122: add("\"%07.7_Ax\n\"");
123: add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
124: }
125:
126: *argvp += optind;
127: }
128:
1.4 deraadt 129: void
1.8 deraadt 130: usage(void)
1.1 deraadt 131: {
1.5 mickey 132: extern char *__progname;
1.6 pvalchev 133: fprintf(stderr, "usage: %s [-bcCdovx] [-e fmt] [-f fmt_file] "
134: "[-n length] [-s skip] [file ...]\n", __progname);
1.1 deraadt 135: exit(1);
136: }