Commit | Line | Data |
---|---|---|
5880e327 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 | |
19 | static char sccsid[] = "@(#)display.c 5.1 (Berkeley) %G%"; | |
20 | #endif /* not lint */ | |
21 | ||
22 | #include <sys/param.h> | |
23 | #include <sys/stat.h> | |
24 | #include <unistd.h> | |
25 | #include <ctype.h> | |
26 | #include <stdio.h> | |
27 | #include <strings.h> | |
28 | #include "hexdump.h" | |
29 | ||
30 | static off_t address; /* address/offset in stream */ | |
31 | static off_t eaddress; /* end address */ | |
32 | static off_t savaddress; /* saved address/offset in stream */ | |
33 | ||
34 | display() | |
35 | { | |
36 | extern FU *endfu; | |
37 | register FS *fs; | |
38 | register FU *fu; | |
39 | register PR *pr; | |
40 | register int cnt; | |
41 | register u_char *bp; | |
42 | off_t saveaddress; | |
43 | u_char savech, *savebp, *get(); | |
44 | ||
45 | while (bp = get()) | |
46 | for (fs = fshead, savebp = bp, saveaddress = address; fs; | |
47 | fs = fs->nextfs, bp = savebp, address = saveaddress) { | |
48 | for (fu = fs->nextfu; fu; fu = fu->nextfu) { | |
49 | if (fu->flags&F_IGNORE) | |
50 | break; | |
51 | for (cnt = fu->reps; cnt; --cnt) | |
52 | for (pr = fu->nextpr; pr; address += pr->bcnt, | |
53 | bp += pr->bcnt, pr = pr->nextpr) { | |
54 | if (eaddress && address >= eaddress) { | |
55 | bpad(fu, pr, bp); | |
56 | goto nextfs; | |
57 | } | |
58 | if (cnt == 1 && pr->nospace) { | |
59 | savech = *pr->nospace; | |
60 | *pr->nospace = '\0'; | |
61 | } | |
62 | print(pr, bp); | |
63 | if (cnt == 1 && pr->nospace) | |
64 | *pr->nospace = savech; | |
65 | } | |
66 | } | |
67 | nextfs: ; | |
68 | } | |
69 | if (endfu) { | |
70 | /* | |
71 | * if eaddress not set, file size was multiple of blocksize, | |
72 | * and no partial block ever found. | |
73 | */ | |
74 | if (!eaddress) | |
75 | eaddress = address + blocksize; | |
76 | for (pr = endfu->nextpr; pr; pr = pr->nextpr) | |
77 | switch(pr->flags) { | |
78 | case F_ADDRESS: | |
79 | (void)printf(pr->fmt, eaddress); | |
80 | break; | |
81 | case F_TEXT: | |
82 | (void)printf(pr->fmt); | |
83 | break; | |
84 | } | |
85 | } | |
86 | } | |
87 | ||
88 | print(pr, bp) | |
89 | PR *pr; | |
90 | u_char *bp; | |
91 | { | |
92 | switch(pr->flags) { | |
93 | case F_ADDRESS: | |
94 | (void)printf(pr->fmt, address); | |
95 | break; | |
96 | case F_C: | |
97 | conv_c(pr, bp); | |
98 | break; | |
99 | case F_CHAR: | |
100 | (void)printf(pr->fmt, *bp); | |
101 | break; | |
102 | case F_DBL: { | |
103 | double dval; | |
104 | float fval; | |
105 | ||
106 | switch(pr->bcnt) { | |
107 | case 4: | |
108 | bcopy((char *)bp, (char *)&fval, sizeof(fval)); | |
109 | (void)printf(pr->fmt, fval); | |
110 | break; | |
111 | case 8: | |
112 | bcopy((char *)bp, (char *)&dval, sizeof(dval)); | |
113 | (void)printf(pr->fmt, dval); | |
114 | break; | |
115 | } | |
116 | break; | |
117 | } | |
118 | case F_INT: { | |
119 | int ival; | |
120 | short sval; | |
121 | ||
122 | switch(pr->bcnt) { | |
123 | case 1: | |
124 | (void)printf(pr->fmt, (int)*bp); | |
125 | break; | |
126 | case 2: | |
127 | bcopy((char *)bp, (char *)&sval, sizeof(sval)); | |
128 | (void)printf(pr->fmt, (int)sval); | |
129 | break; | |
130 | case 4: | |
131 | bcopy((char *)bp, (char *)&ival, sizeof(ival)); | |
132 | (void)printf(pr->fmt, ival); | |
133 | break; | |
134 | } | |
135 | break; | |
136 | } | |
137 | case F_P: | |
138 | (void)printf(pr->fmt, isprint(*bp) ? *bp : '.'); | |
139 | break; | |
140 | case F_STR: | |
141 | (void)printf(pr->fmt, (char *)bp); | |
142 | break; | |
143 | case F_TEXT: | |
144 | (void)printf(pr->fmt); | |
145 | break; | |
146 | case F_U: | |
147 | conv_u(pr, bp); | |
148 | break; | |
149 | case F_UINT: { | |
150 | u_int ival; | |
151 | u_short sval; | |
152 | ||
153 | switch(pr->bcnt) { | |
154 | case 1: | |
155 | (void)printf(pr->fmt, (u_int)*bp); | |
156 | break; | |
157 | case 2: | |
158 | bcopy((char *)bp, (char *)&sval, sizeof(sval)); | |
159 | (void)printf(pr->fmt, (u_int)sval); | |
160 | break; | |
161 | case 4: | |
162 | bcopy((char *)bp, (char *)&ival, sizeof(ival)); | |
163 | (void)printf(pr->fmt, ival); | |
164 | break; | |
165 | } | |
166 | break; | |
167 | } | |
168 | } | |
169 | } | |
170 | ||
171 | u_char * | |
172 | get() | |
173 | { | |
174 | extern enum _vflag vflag; | |
175 | extern int length; | |
176 | static int ateof = 1; | |
177 | static u_char *curp, *savp; | |
178 | register int n; | |
179 | int need, nread; | |
180 | u_char *tmpp; | |
181 | ||
182 | if (!curp) { | |
183 | curp = (u_char *)emalloc(blocksize); | |
184 | savp = (u_char *)emalloc(blocksize); | |
185 | } else { | |
186 | tmpp = curp; | |
187 | curp = savp; | |
188 | savp = tmpp; | |
189 | address = savaddress += blocksize; | |
190 | } | |
191 | for (need = blocksize, nread = 0;;) { | |
192 | /* | |
193 | * if read the right number of bytes, or at EOF for one file, | |
194 | * and no other files are available, zero-pad the rest of the | |
195 | * block and set the end flag. | |
196 | */ | |
197 | if (!length || ateof && !next((char **)NULL)) { | |
198 | if (need == blocksize) | |
199 | return((u_char *)NULL); | |
200 | if (vflag != ALL && !bcmp(curp, savp, nread)) { | |
201 | if (vflag != DUP) | |
202 | (void)printf("*\n"); | |
203 | return((u_char *)NULL); | |
204 | } | |
205 | bzero((char *)curp + nread, need); | |
206 | eaddress = address + nread; | |
207 | return(curp); | |
208 | } | |
209 | n = fread((char *)curp + nread, sizeof(u_char), | |
210 | length == -1 ? need : MIN(length, need), stdin); | |
211 | if (!n) { | |
212 | ateof = 1; | |
213 | continue; | |
214 | } | |
215 | ateof = 0; | |
216 | if (length != -1) | |
217 | length -= n; | |
218 | if (!(need -= n)) { | |
219 | if (vflag == ALL || bcmp(curp, savp, blocksize)) { | |
220 | if (vflag == DUP) | |
221 | vflag = WAIT; | |
222 | return(curp); | |
223 | } | |
224 | if (vflag == WAIT) | |
225 | (void)printf("*\n"); | |
226 | vflag = DUP; | |
227 | address = savaddress += blocksize; | |
228 | need = blocksize; | |
229 | nread = 0; | |
230 | } | |
231 | else | |
232 | nread += n; | |
233 | } | |
234 | } | |
235 | ||
236 | extern off_t skip; /* bytes to skip */ | |
237 | ||
238 | next(argv) | |
239 | char **argv; | |
240 | { | |
241 | extern int errno, exitval; | |
242 | static int done; | |
243 | static char **_argv; | |
244 | int statok; | |
245 | ||
246 | if (argv) { | |
247 | _argv = argv; | |
248 | return(1); | |
249 | } | |
250 | for (;;) { | |
251 | if (*_argv) { | |
252 | if (!(freopen(*_argv, "r", stdin))) { | |
253 | (void)fprintf(stderr, "hexdump: %s: %s\n", | |
254 | *_argv, strerror(errno)); | |
255 | exitval = 1; | |
256 | ++_argv; | |
257 | continue; | |
258 | } | |
259 | statok = done = 1; | |
260 | } else { | |
261 | if (done++) | |
262 | return(0); | |
263 | statok = 0; | |
264 | } | |
265 | if (skip) | |
266 | doskip(statok ? *_argv : "stdin", statok); | |
267 | if (*_argv) | |
268 | ++_argv; | |
269 | if (!skip) | |
270 | return(1); | |
271 | } | |
272 | /* NOTREACHED */ | |
273 | } | |
274 | ||
275 | doskip(fname, statok) | |
276 | char *fname; | |
277 | int statok; | |
278 | { | |
279 | extern int errno; | |
280 | struct stat sbuf; | |
281 | ||
282 | if (statok) { | |
283 | if (fstat(fileno(stdin), &sbuf)) { | |
284 | (void)fprintf(stderr, "hexdump: %s: %s.\n", | |
285 | fname, strerror(errno)); | |
286 | exit(1); | |
287 | } | |
288 | if (skip >= sbuf.st_size) { | |
289 | skip -= sbuf.st_size; | |
290 | address += sbuf.st_size; | |
291 | return; | |
292 | } | |
293 | } | |
294 | if (fseek(stdin, skip, SEEK_SET)) { | |
295 | (void)fprintf(stderr, "hexdump: %s: %s.\n", | |
296 | fname, strerror(errno)); | |
297 | exit(1); | |
298 | } | |
299 | savaddress = address += skip; | |
300 | skip = 0; | |
301 | } | |
302 | ||
303 | char * | |
304 | emalloc(size) | |
305 | int size; | |
306 | { | |
307 | char *p, *malloc(); | |
308 | ||
309 | if (!(p = malloc((u_int)size))) | |
310 | nomem(); | |
311 | bzero(p, size); | |
312 | return(p); | |
313 | } | |
314 | ||
315 | nomem() | |
316 | { | |
317 | (void)fprintf(stderr, "hexdump: out of memory.\n"); | |
318 | exit(1); | |
319 | } |