minor fixes for safecopy & safemap tests
[minix.git] / commands / hexdump / display.c
blob54832f89bc51c0aa1c94820fd849ebf63f4c3412
1 /* $NetBSD: display.c,v 1.21 2009/01/18 21:34:32 apb Exp $ */
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
36 #include <sys/cdefs.h>
37 #if 0
38 #if !defined(lint)
39 #if 0
40 static char sccsid[] = "@(#)display.c 8.1 (Berkeley) 6/6/93";
41 #else
42 __RCSID("$NetBSD: display.c,v 1.21 2009/01/18 21:34:32 apb Exp $");
43 #endif
44 #endif /* not lint */
45 #endif
47 #include <sys/param.h>
48 #include <sys/stat.h>
50 #include <ctype.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <inttypes.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <util.h>
60 #include "hexdump.h"
62 enum _vflag vflag = FIRST;
64 static off_t address; /* address/offset in stream */
65 static off_t eaddress; /* end address */
67 static inline void print(PR *, u_char *);
69 void
70 display(void)
72 FS *fs;
73 FU *fu;
74 PR *pr;
75 int cnt;
76 u_char *bp;
77 off_t saveaddress;
78 u_char savech, *savebp;
80 savech = 0;
81 while ((bp = get()) != NULL)
82 for (fs = fshead, savebp = bp, saveaddress = address; fs;
83 fs = fs->nextfs, bp = savebp, address = saveaddress)
84 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
85 if (fu->flags&F_IGNORE)
86 break;
87 for (cnt = fu->reps; cnt; --cnt)
88 for (pr = fu->nextpr; pr; address += pr->bcnt,
89 bp += pr->bcnt, pr = pr->nextpr) {
90 if (eaddress && address >= eaddress &&
91 !(pr->flags & (F_TEXT|F_BPAD)))
92 bpad(pr);
93 if (cnt == 1 && pr->nospace) {
94 savech = *pr->nospace;
95 *pr->nospace = '\0';
97 print(pr, bp);
98 if (cnt == 1 && pr->nospace)
99 *pr->nospace = savech;
102 if (endfu) {
104 * If eaddress not set, error or file size was multiple of
105 * blocksize, and no partial block ever found.
107 if (!eaddress) {
108 if (!address)
109 return;
110 eaddress = address;
112 for (pr = endfu->nextpr; pr; pr = pr->nextpr)
113 switch(pr->flags) {
114 case F_ADDRESS:
115 (void)printf(pr->fmt, (int64_t)eaddress);
116 break;
117 case F_TEXT:
118 (void)printf("%s", pr->fmt);
119 break;
124 static inline void
125 print(PR *pr, u_char *bp)
127 double f8;
128 float f4;
129 int16_t s2;
130 int32_t s4;
131 int64_t s8;
132 uint16_t u2;
133 uint32_t u4;
134 uint64_t u8;
136 switch(pr->flags) {
137 case F_ADDRESS:
138 (void)printf(pr->fmt, (int64_t)address);
139 break;
140 case F_BPAD:
141 (void)printf(pr->fmt, "");
142 break;
143 case F_C:
144 conv_c(pr, bp);
145 break;
146 case F_CHAR:
147 (void)printf(pr->fmt, *bp);
148 break;
149 case F_DBL:
150 switch(pr->bcnt) {
151 case 4:
152 memmove(&f4, bp, sizeof(f4));
153 (void)printf(pr->fmt, f4);
154 break;
155 case 8:
156 memmove(&f8, bp, sizeof(f8));
157 (void)printf(pr->fmt, f8);
158 break;
160 break;
161 case F_INT:
162 switch(pr->bcnt) {
163 case 1:
164 (void)printf(pr->fmt, (int64_t)*bp);
165 break;
166 case 2:
167 memmove(&s2, bp, sizeof(s2));
168 (void)printf(pr->fmt, (int64_t)s2);
169 break;
170 case 4:
171 memmove(&s4, bp, sizeof(s4));
172 (void)printf(pr->fmt, (int64_t)s4);
173 break;
174 case 8:
175 memmove(&s8, bp, sizeof(s8));
176 (void)printf(pr->fmt, (int64_t)s8);
177 break;
179 break;
180 case F_P:
181 (void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
182 break;
183 case F_STR:
184 (void)printf(pr->fmt, (char *)bp);
185 break;
186 case F_TEXT:
187 (void)printf("%s", pr->fmt);
188 break;
189 case F_U:
190 conv_u(pr, bp);
191 break;
192 case F_UINT:
193 switch(pr->bcnt) {
194 case 1:
195 (void)printf(pr->fmt, (uint64_t)*bp);
196 break;
197 case 2:
198 memmove(&u2, bp, sizeof(u2));
199 (void)printf(pr->fmt, (uint64_t)u2);
200 break;
201 case 4:
202 memmove(&u4, bp, sizeof(u4));
203 (void)printf(pr->fmt, (uint64_t)u4);
204 break;
205 case 8:
206 memmove(&u8, bp, sizeof(u8));
207 (void)printf(pr->fmt, (uint64_t)u8);
208 break;
210 break;
214 void
215 bpad(PR *pr)
217 static const char *spec = " -0+#";
218 char *p1, *p2;
221 * Remove all conversion flags; '-' is the only one valid
222 * with %s, and it's not useful here.
224 pr->flags = F_BPAD;
225 pr->cchar[0] = 's';
226 pr->cchar[1] = '\0';
227 for (p1 = pr->fmt; *p1 != '%'; ++p1);
228 for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1);
229 while ((*p2++ = *p1++) != '\0');
232 static char **_argv;
234 u_char *
235 get(void)
237 static int ateof = 1;
238 static u_char *curp, *savp;
239 int n;
240 int need, nread;
241 u_char *tmpp;
243 if (!curp) {
244 curp = ecalloc(blocksize, 1);
245 savp = ecalloc(blocksize, 1);
246 } else {
247 tmpp = curp;
248 curp = savp;
249 savp = tmpp;
250 address += blocksize;
252 for (need = blocksize, nread = 0;;) {
254 * if read the right number of bytes, or at EOF for one file,
255 * and no other files are available, zero-pad the rest of the
256 * block and set the end flag.
258 if (!length || (ateof && !next(NULL))) {
259 if (need == blocksize)
260 return(NULL);
261 if (!need && vflag != ALL &&
262 !memcmp(curp, savp, nread)) {
263 if (vflag != DUP)
264 (void)printf("*\n");
265 return(NULL);
267 memset((char *)curp + nread, 0, need);
268 eaddress = address + nread;
269 return(curp);
271 n = fread((char *)curp + nread, sizeof(u_char),
272 length == -1 ? need : MIN(length, need), stdin);
273 if (!n) {
274 if (ferror(stdin))
275 warn("%s", _argv[-1]);
276 ateof = 1;
277 continue;
279 ateof = 0;
280 if (length != -1)
281 length -= n;
282 if (!(need -= n)) {
283 if (vflag == ALL || vflag == FIRST ||
284 memcmp(curp, savp, blocksize)) {
285 if (vflag == DUP || vflag == FIRST)
286 vflag = WAIT;
287 return(curp);
289 if (vflag == WAIT)
290 (void)printf("*\n");
291 vflag = DUP;
292 address += blocksize;
293 need = blocksize;
294 nread = 0;
296 else
297 nread += n;
302 next(char **argv)
304 static int done;
305 int statok;
307 if (argv) {
308 _argv = argv;
309 return(1);
311 for (;;) {
312 if (*_argv) {
313 if (!(freopen(*_argv, "r", stdin))) {
314 warn("%s", *_argv);
315 exitval = 1;
316 ++_argv;
317 continue;
319 statok = done = 1;
320 } else {
321 if (done++)
322 return(0);
323 statok = 0;
325 if (skip)
326 doskip(statok ? *_argv : "stdin", statok);
327 if (*_argv)
328 ++_argv;
329 if (!skip)
330 return(1);
332 /* NOTREACHED */
335 void
336 doskip(const char *fname, int statok)
338 int cnt;
339 struct stat sb;
341 if (statok) {
342 if (fstat(fileno(stdin), &sb))
343 err(1, "fstat %s", fname);
344 if (S_ISREG(sb.st_mode) && skip >= sb.st_size) {
345 address += sb.st_size;
346 skip -= sb.st_size;
347 return;
350 if (S_ISREG(sb.st_mode)) {
351 if (fseek(stdin, skip, SEEK_SET))
352 err(1, "fseek %s", fname);
353 address += skip;
354 skip = 0;
355 } else {
356 for (cnt = 0; cnt < skip; ++cnt)
357 if (getchar() == EOF)
358 break;
359 address += cnt;
360 skip -= cnt;