]> git.saurik.com Git - apple/shell_cmds.git/blob - hexdump/display.c
shell_cmds-207.11.1.tar.gz
[apple/shell_cmds.git] / hexdump / display.c
1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)display.c 8.1 (Berkeley) 6/6/93";
37 #endif
38 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/usr.bin/hexdump/display.c,v 1.19 2004/07/11 01:11:12 tjr Exp $");
41
42 #include <sys/param.h>
43 #include <sys/stat.h>
44
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include "hexdump.h"
53
54 enum _vflag vflag = FIRST;
55
56 static off_t address; /* address/offset in stream */
57 static off_t eaddress; /* end address */
58
59 static __inline void print(PR *, u_char *);
60
61 void
62 display(void)
63 {
64 FS *fs;
65 FU *fu;
66 PR *pr;
67 int cnt;
68 u_char *bp;
69 off_t saveaddress;
70 u_char savech=0, *savebp;
71
72 while ((bp = get()))
73 for (fs = fshead, savebp = bp, saveaddress = address; fs;
74 fs = fs->nextfs, bp = savebp, address = saveaddress)
75 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
76 if (fu->flags&F_IGNORE)
77 break;
78 for (cnt = fu->reps; cnt; --cnt)
79 for (pr = fu->nextpr; pr; address += pr->bcnt,
80 bp += pr->bcnt, pr = pr->nextpr) {
81 if (eaddress && address >= eaddress &&
82 !(pr->flags & (F_TEXT|F_BPAD)))
83 bpad(pr);
84 if (cnt == 1 && pr->nospace) {
85 savech = *pr->nospace;
86 *pr->nospace = '\0';
87 }
88 print(pr, bp);
89 if (cnt == 1 && pr->nospace)
90 *pr->nospace = savech;
91 }
92 }
93 if (endfu) {
94 /*
95 * If eaddress not set, error or file size was multiple of
96 * blocksize, and no partial block ever found.
97 */
98 if (!eaddress) {
99 if (!address) {
100 return;
101 }
102 eaddress = address;
103 }
104 for (pr = endfu->nextpr; pr; pr = pr->nextpr)
105 switch(pr->flags) {
106 case F_ADDRESS:
107 (void)printf(pr->fmt, (quad_t)eaddress);
108 break;
109 case F_TEXT:
110 (void)printf("%s", pr->fmt);
111 break;
112 default:
113 break;
114 }
115 }
116 }
117
118 static __inline void
119 print(PR *pr, u_char *bp)
120 {
121 long double ldbl;
122 double f8;
123 float f4;
124 int16_t s2;
125 int8_t s8;
126 int32_t s4;
127 u_int16_t u2;
128 u_int32_t u4;
129 u_int64_t u8;
130
131 switch(pr->flags) {
132 case F_ADDRESS:
133 (void)printf(pr->fmt, (quad_t)address);
134 break;
135 case F_BPAD:
136 (void)printf(pr->fmt, "");
137 break;
138 case F_C:
139 conv_c(pr, bp, eaddress ? eaddress - address :
140 blocksize - address % blocksize);
141 break;
142 case F_CHAR:
143 (void)printf(pr->fmt, *bp);
144 break;
145 case F_DBL:
146 switch(pr->bcnt) {
147 case 4:
148 bcopy(bp, &f4, sizeof(f4));
149 (void)printf(pr->fmt, f4);
150 break;
151 case 8:
152 bcopy(bp, &f8, sizeof(f8));
153 (void)printf(pr->fmt, f8);
154 break;
155 default:
156 if (pr->bcnt == sizeof(long double)) {
157 bcopy(bp, &ldbl, sizeof(ldbl));
158 (void)printf(pr->fmt, ldbl);
159 }
160 break;
161 }
162 break;
163 case F_INT:
164 switch(pr->bcnt) {
165 case 1:
166 (void)printf(pr->fmt, (quad_t)(signed char)*bp);
167 break;
168 case 2:
169 bcopy(bp, &s2, sizeof(s2));
170 (void)printf(pr->fmt, (quad_t)s2);
171 break;
172 case 4:
173 bcopy(bp, &s4, sizeof(s4));
174 (void)printf(pr->fmt, (quad_t)s4);
175 break;
176 case 8:
177 bcopy(bp, &s8, sizeof(s8));
178 (void)printf(pr->fmt, s8);
179 break;
180 }
181 break;
182 case F_P:
183 (void)printf(pr->fmt, isprint(*bp) && isascii(*bp) ? *bp : '.');
184 break;
185 case F_STR:
186 (void)printf(pr->fmt, (char *)bp);
187 break;
188 case F_TEXT:
189 (void)printf("%s", pr->fmt);
190 break;
191 case F_U:
192 conv_u(pr, bp);
193 break;
194 case F_UINT:
195 switch(pr->bcnt) {
196 case 1:
197 (void)printf(pr->fmt, (u_quad_t)*bp);
198 break;
199 case 2:
200 bcopy(bp, &u2, sizeof(u2));
201 (void)printf(pr->fmt, (u_quad_t)u2);
202 break;
203 case 4:
204 bcopy(bp, &u4, sizeof(u4));
205 (void)printf(pr->fmt, (u_quad_t)u4);
206 break;
207 case 8:
208 bcopy(bp, &u8, sizeof(u8));
209 (void)printf(pr->fmt, u8);
210 break;
211 }
212 break;
213 }
214 }
215
216 void
217 bpad(PR *pr)
218 {
219 static char const *spec = " -0+#";
220 char *p1, *p2;
221
222 /*
223 * Remove all conversion flags; '-' is the only one valid
224 * with %s, and it's not useful here.
225 */
226 pr->flags = F_BPAD;
227 pr->cchar[0] = 's';
228 pr->cchar[1] = '\0';
229 for (p1 = pr->fmt; *p1 != '%'; ++p1);
230 for (p2 = ++p1; *p1 && index(spec, *p1); ++p1);
231 while ((*p2++ = *p1++));
232 }
233
234 static char **_argv;
235
236 u_char *
237 get(void)
238 {
239 static int ateof = 1;
240 static u_char *curp, *savp;
241 int n;
242 int need, nread;
243 int valid_save = 0;
244 u_char *tmpp;
245
246 if (!curp) {
247 if ((curp = calloc(1, blocksize)) == NULL)
248 err(1, NULL);
249 if ((savp = calloc(1, blocksize)) == NULL)
250 err(1, NULL);
251 } else {
252 tmpp = curp;
253 curp = savp;
254 savp = tmpp;
255 address += blocksize;
256 valid_save = 1;
257 }
258 for (need = blocksize, nread = 0;;) {
259 /*
260 * if read the right number of bytes, or at EOF for one file,
261 * and no other files are available, zero-pad the rest of the
262 * block and set the end flag.
263 */
264 if (!length || (ateof && !next((char **)NULL))) {
265 if (odmode && address < skip)
266 errx(1, "cannot skip past end of input");
267 if (need == blocksize)
268 return((u_char *)NULL);
269 /*
270 * XXX bcmp() is not quite right in the presence
271 * of multibyte characters.
272 */
273 #ifdef __APPLE__
274 /* 5650060 */
275 if (!need && vflag != ALL &&
276 #else
277 if (vflag != ALL &&
278 #endif
279 valid_save &&
280 bcmp(curp, savp, nread) == 0) {
281 if (vflag != DUP)
282 (void)printf("*\n");
283 return((u_char *)NULL);
284 }
285 bzero((char *)curp + nread, need);
286 eaddress = address + nread;
287 if (length == 0)
288 lseek(STDIN_FILENO, ftell(stdin), SEEK_SET); /* rewind stdin for next process */
289 return(curp);
290 }
291 n = fread((char *)curp + nread, sizeof(u_char),
292 length == -1 ? need : MIN(length, need), stdin);
293 if (!n) {
294 if (ferror(stdin))
295 warn("%s", _argv[-1]);
296 ateof = 1;
297 continue;
298 }
299 ateof = 0;
300 if (length != -1)
301 length -= n;
302 if (!(need -= n)) {
303 /*
304 * XXX bcmp() is not quite right in the presence
305 * of multibyte characters.
306 */
307 if (vflag == ALL || vflag == FIRST ||
308 valid_save == 0 ||
309 bcmp(curp, savp, blocksize) != 0) {
310 if (vflag == DUP || vflag == FIRST)
311 vflag = WAIT;
312 return(curp);
313 }
314 if (vflag == WAIT)
315 (void)printf("*\n");
316 vflag = DUP;
317 address += blocksize;
318 need = blocksize;
319 nread = 0;
320 }
321 else
322 nread += n;
323 }
324 }
325
326 size_t
327 peek(u_char *buf, size_t nbytes)
328 {
329 size_t n, nread;
330 int c;
331
332 if (length != -1 && nbytes > length)
333 nbytes = length;
334 nread = 0;
335 while (nread < nbytes && (c = getchar()) != EOF) {
336 *buf++ = c;
337 nread++;
338 }
339 n = nread;
340 while (n-- > 0) {
341 c = *--buf;
342 ungetc(c, stdin);
343 }
344 return (nread);
345 }
346
347 int
348 next(char **argv)
349 {
350 static int done;
351 int statok;
352
353 if (argv) {
354 _argv = argv;
355 return(1);
356 }
357 for (;;) {
358 if (*_argv) {
359 if (!(freopen(*_argv, "r", stdin))) {
360 warn("%s", *_argv);
361 exitval = 1;
362 ++_argv;
363 continue;
364 }
365 statok = done = 1;
366 } else {
367 if (done++)
368 return(0);
369 statok = 0;
370 }
371 if (skip)
372 doskip(statok ? *_argv : "stdin", statok);
373 if (*_argv)
374 ++_argv;
375 if (!skip)
376 return(1);
377 }
378 /* NOTREACHED */
379 }
380
381 void
382 doskip(const char *fname, int statok)
383 {
384 int cnt;
385 struct stat sb;
386
387 if (statok) {
388 if (fstat(fileno(stdin), &sb))
389 err(1, "%s", fname);
390 if (S_ISREG(sb.st_mode) && skip >= sb.st_size) {
391 address += sb.st_size;
392 skip -= sb.st_size;
393 return;
394 }
395 }
396 #ifdef __APPLE__
397 /* try to seek first; fall back on ESPIPE */
398 if (fseeko(stdin, skip, SEEK_SET) == 0) {
399 #else /* !__APPLE__ */
400 if (S_ISREG(sb.st_mode)) {
401 if (fseeko(stdin, skip, SEEK_SET))
402 err(1, "%s", fname);
403 #endif /* __APPLE__ */
404 address += skip;
405 skip = 0;
406 } else {
407 #ifdef __APPLE__
408 if (errno != ESPIPE)
409 err(1, "%s", fname);
410 #endif /* __APPLE__ */
411 for (cnt = 0; cnt < skip; ++cnt)
412 if (getchar() == EOF)
413 break;
414 address += cnt;
415 skip -= cnt;
416 }
417 }