]> git.saurik.com Git - apple/shell_cmds.git/blame - hexdump/display.c
shell_cmds-170.tar.gz
[apple/shell_cmds.git] / hexdump / display.c
CommitLineData
1e9ba8f2
A
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
36static 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
54enum _vflag vflag = FIRST;
55
56static off_t address; /* address/offset in stream */
57static off_t eaddress; /* end address */
58
59static __inline void print(PR *, u_char *);
60
61void
62display(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
118static __inline void
119print(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
216void
217bpad(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
234static char **_argv;
235
236u_char *
237get(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 if (vflag != ALL &&
274 valid_save &&
275 bcmp(curp, savp, nread) == 0) {
276 if (vflag != DUP)
277 (void)printf("*\n");
278 return((u_char *)NULL);
279 }
280 bzero((char *)curp + nread, need);
281 eaddress = address + nread;
282 if (length == 0)
283 lseek(STDIN_FILENO, ftell(stdin), SEEK_SET); /* rewind stdin for next process */
284 return(curp);
285 }
286 n = fread((char *)curp + nread, sizeof(u_char),
287 length == -1 ? need : MIN(length, need), stdin);
288 if (!n) {
289 if (ferror(stdin))
290 warn("%s", _argv[-1]);
291 ateof = 1;
292 continue;
293 }
294 ateof = 0;
295 if (length != -1)
296 length -= n;
297 if (!(need -= n)) {
298 /*
299 * XXX bcmp() is not quite right in the presence
300 * of multibyte characters.
301 */
302 if (vflag == ALL || vflag == FIRST ||
303 valid_save == 0 ||
304 bcmp(curp, savp, blocksize) != 0) {
305 if (vflag == DUP || vflag == FIRST)
306 vflag = WAIT;
307 return(curp);
308 }
309 if (vflag == WAIT)
310 (void)printf("*\n");
311 vflag = DUP;
312 address += blocksize;
313 need = blocksize;
314 nread = 0;
315 }
316 else
317 nread += n;
318 }
319}
320
321size_t
322peek(u_char *buf, size_t nbytes)
323{
324 size_t n, nread;
325 int c;
326
327 if (length != -1 && nbytes > length)
328 nbytes = length;
329 nread = 0;
330 while (nread < nbytes && (c = getchar()) != EOF) {
331 *buf++ = c;
332 nread++;
333 }
334 n = nread;
335 while (n-- > 0) {
336 c = *--buf;
337 ungetc(c, stdin);
338 }
339 return (nread);
340}
341
342int
343next(char **argv)
344{
345 static int done;
346 int statok;
347
348 if (argv) {
349 _argv = argv;
350 return(1);
351 }
352 for (;;) {
353 if (*_argv) {
354 if (!(freopen(*_argv, "r", stdin))) {
355 warn("%s", *_argv);
356 exitval = 1;
357 ++_argv;
358 continue;
359 }
360 statok = done = 1;
361 } else {
362 if (done++)
363 return(0);
364 statok = 0;
365 }
366 if (skip)
367 doskip(statok ? *_argv : "stdin", statok);
368 if (*_argv)
369 ++_argv;
370 if (!skip)
371 return(1);
372 }
373 /* NOTREACHED */
374}
375
376void
377doskip(const char *fname, int statok)
378{
379 int cnt;
380 struct stat sb;
381
382 if (statok) {
383 if (fstat(fileno(stdin), &sb))
384 err(1, "%s", fname);
385 if (S_ISREG(sb.st_mode) && skip >= sb.st_size) {
386 address += sb.st_size;
387 skip -= sb.st_size;
388 return;
389 }
390 }
391#ifdef __APPLE__
392 /* try to seek first; fall back on ESPIPE */
393 if (fseeko(stdin, skip, SEEK_SET) == 0) {
394#else /* !__APPLE__ */
395 if (S_ISREG(sb.st_mode)) {
396 if (fseeko(stdin, skip, SEEK_SET))
397 err(1, "%s", fname);
398#endif /* __APPLE__ */
399 address += skip;
400 skip = 0;
401 } else {
402#ifdef __APPLE__
403 if (errno != ESPIPE)
404 err(1, "%s", fname);
405#endif /* __APPLE__ */
406 for (cnt = 0; cnt < skip; ++cnt)
407 if (getchar() == EOF)
408 break;
409 address += cnt;
410 skip -= cnt;
411 }
412}