_c should zero-pad the hexnumbers, which should be octal
[unix-history] / usr / src / usr.bin / hexdump / hexdump.c
CommitLineData
a53581a1
KB
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18#ifndef lint
19char copyright[] =
20"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
21 All rights reserved.\n";
22#endif /* not lint */
23
24#ifndef lint
25static char sccsid[] = "@(#)hexdump.c 5.1 (Berkeley) %G%";
26#endif /* not lint */
27
28#include <sys/types.h>
29#include <stdio.h>
30#include "hexdump.h"
31
32enum _vflag vflag = WAIT; /* display duplicate lines */
33FS *fshead; /* head of format strings */
34off_t skip; /* bytes to skip */
35int blocksize; /* data block size */
36int exitval; /* final exit value */
37int length; /* max bytes to read */
38
39main(argc, argv)
40 int argc;
41 char **argv;
42{
43 extern int errno, optind;
44 extern char *optarg;
45 register FS *tfs;
46 int ch;
47 char *p, *rindex();
48
49 length = -1;
50 while ((ch = getopt(argc, argv, "bcde:f:n:os:v")) != EOF)
51 switch (ch) {
52 case 'b':
53 add("\"%07.7_Ax\n\"");
54 add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
55 break;
56 case 'c':
57 add("\"%07.7_Ax\n\"");
58 add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
59 break;
60 case 'd':
61 add("\"%07.7_Ax\n\"");
62 add("\"%07.7_ax \" 8/2 \"%05u \" \"\\n\"");
63 break;
64 case 'e':
65 add(optarg);
66 break;
67 case 'f':
68 addfile(optarg);
69 break;
70 case 'n':
71 if ((length = atoi(optarg)) < 0) {
72 (void)fprintf(stderr,
73 "hexdump: bad length value.\n");
74 exit(1);
75 }
76 break;
77 case 'o':
78 add("\"%07.7_Ax\n\"");
79 add("\"%07.7_ax \" 8/2 \"%06o \" \"\\n\"");
80 break;
81 case 's':
82 if ((skip = strtol(optarg, &p, 0)) < 0) {
83 (void)fprintf(stderr,
84 "hexdump: bad skip value.\n");
85 exit(1);
86 }
87 switch(*p) {
88 case 'b':
89 skip *= 512;
90 break;
91 case 'k':
92 skip *= 1024;
93 break;
94 case 'm':
95 skip *= 1048576;
96 break;
97 }
98 break;
99 case 'v':
100 vflag = ALL;
101 break;
102 case '?':
103 usage();
104 exit(1);
105 }
106
107 if (!fshead) {
108 p = rindex(argv[0], 'o');
109 if (p && !strcmp(p, "od")) {
110 add("\"%07.7_Ao\n\"");
111 add("\"%07.7_ao \" 8/2 \"%06o \" \"\\n\"");
112 } else {
113 add("\"%07.7_Ax\n\"");
114 add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
115 }
116 }
117
118 argv += optind;
119 argc -= optind;
120
121 /* figure out the data block size */
122 for (blocksize = 0, tfs = fshead; tfs; tfs = tfs->nextfs) {
123 tfs->bcnt = size(tfs);
124 if (blocksize < tfs->bcnt)
125 blocksize = tfs->bcnt;
126 }
127 /* rewrite the rules, do syntax checking */
128 for (tfs = fshead; tfs; tfs = tfs->nextfs)
129 rewrite(tfs);
130
131 (void)next(argv);
132 display();
133 exit(exitval);
134}
135
136usage()
137{
138 (void)fprintf(stderr,
139"hexdump: [bcdov] [-e format] [-f file] [-n length] [-s skip] [file ...]\n");
140 exit(1);
141}