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