19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1989, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 3510731702SPhilippe Charnier #if 0 369b50d902SRodney W. Grimes static char sccsid[] = "@(#)display.c 8.1 (Berkeley) 6/6/93"; 3710731702SPhilippe Charnier #endif 3810731702SPhilippe Charnier static const char rcsid[] = 39c3aac50fSPeter Wemm "$FreeBSD$"; 409b50d902SRodney W. Grimes #endif /* not lint */ 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes #include <sys/param.h> 439b50d902SRodney W. Grimes #include <sys/stat.h> 449b50d902SRodney W. Grimes 459b50d902SRodney W. Grimes #include <ctype.h> 4610731702SPhilippe Charnier #include <err.h> 479b50d902SRodney W. Grimes #include <stdio.h> 489b50d902SRodney W. Grimes #include <stdlib.h> 499b50d902SRodney W. Grimes #include <string.h> 5010731702SPhilippe Charnier #include <unistd.h> 519b50d902SRodney W. Grimes #include "hexdump.h" 529b50d902SRodney W. Grimes 539b50d902SRodney W. Grimes enum _vflag vflag = FIRST; 549b50d902SRodney W. Grimes 559b50d902SRodney W. Grimes static off_t address; /* address/offset in stream */ 569b50d902SRodney W. Grimes static off_t eaddress; /* end address */ 579b50d902SRodney W. Grimes 589b50d902SRodney W. Grimes static inline void print __P((PR *, u_char *)); 599b50d902SRodney W. Grimes 609b50d902SRodney W. Grimes void 619b50d902SRodney W. Grimes display() 629b50d902SRodney W. Grimes { 639b50d902SRodney W. Grimes extern FU *endfu; 649b50d902SRodney W. Grimes register FS *fs; 659b50d902SRodney W. Grimes register FU *fu; 669b50d902SRodney W. Grimes register PR *pr; 679b50d902SRodney W. Grimes register int cnt; 689b50d902SRodney W. Grimes register u_char *bp; 699b50d902SRodney W. Grimes off_t saveaddress; 709b50d902SRodney W. Grimes u_char savech, *savebp; 719b50d902SRodney W. Grimes 72c38cc743SMark Murray while ((bp = get())) 739b50d902SRodney W. Grimes for (fs = fshead, savebp = bp, saveaddress = address; fs; 749b50d902SRodney W. Grimes fs = fs->nextfs, bp = savebp, address = saveaddress) 759b50d902SRodney W. Grimes for (fu = fs->nextfu; fu; fu = fu->nextfu) { 769b50d902SRodney W. Grimes if (fu->flags&F_IGNORE) 779b50d902SRodney W. Grimes break; 789b50d902SRodney W. Grimes for (cnt = fu->reps; cnt; --cnt) 799b50d902SRodney W. Grimes for (pr = fu->nextpr; pr; address += pr->bcnt, 809b50d902SRodney W. Grimes bp += pr->bcnt, pr = pr->nextpr) { 819b50d902SRodney W. Grimes if (eaddress && address >= eaddress && 829b50d902SRodney W. Grimes !(pr->flags & (F_TEXT|F_BPAD))) 839b50d902SRodney W. Grimes bpad(pr); 849b50d902SRodney W. Grimes if (cnt == 1 && pr->nospace) { 859b50d902SRodney W. Grimes savech = *pr->nospace; 869b50d902SRodney W. Grimes *pr->nospace = '\0'; 879b50d902SRodney W. Grimes } 889b50d902SRodney W. Grimes print(pr, bp); 899b50d902SRodney W. Grimes if (cnt == 1 && pr->nospace) 909b50d902SRodney W. Grimes *pr->nospace = savech; 919b50d902SRodney W. Grimes } 929b50d902SRodney W. Grimes } 939b50d902SRodney W. Grimes if (endfu) { 949b50d902SRodney W. Grimes /* 959b50d902SRodney W. Grimes * If eaddress not set, error or file size was multiple of 969b50d902SRodney W. Grimes * blocksize, and no partial block ever found. 979b50d902SRodney W. Grimes */ 989b50d902SRodney W. Grimes if (!eaddress) { 999b50d902SRodney W. Grimes if (!address) 1009b50d902SRodney W. Grimes return; 1019b50d902SRodney W. Grimes eaddress = address; 1029b50d902SRodney W. Grimes } 1039b50d902SRodney W. Grimes for (pr = endfu->nextpr; pr; pr = pr->nextpr) 1049b50d902SRodney W. Grimes switch(pr->flags) { 1059b50d902SRodney W. Grimes case F_ADDRESS: 1069b50d902SRodney W. Grimes (void)printf(pr->fmt, (quad_t)eaddress); 1079b50d902SRodney W. Grimes break; 1089b50d902SRodney W. Grimes case F_TEXT: 10932d9afb6SKris Kennaway (void)printf("%s", pr->fmt); 1109b50d902SRodney W. Grimes break; 1119b50d902SRodney W. Grimes } 1129b50d902SRodney W. Grimes } 1139b50d902SRodney W. Grimes } 1149b50d902SRodney W. Grimes 1159b50d902SRodney W. Grimes static inline void 1169b50d902SRodney W. Grimes print(pr, bp) 1179b50d902SRodney W. Grimes PR *pr; 1189b50d902SRodney W. Grimes u_char *bp; 1199b50d902SRodney W. Grimes { 1209b50d902SRodney W. Grimes double f8; 1219b50d902SRodney W. Grimes float f4; 1229b50d902SRodney W. Grimes int16_t s2; 1239b50d902SRodney W. Grimes int8_t s8; 1249b50d902SRodney W. Grimes int32_t s4; 1259b50d902SRodney W. Grimes u_int16_t u2; 1269b50d902SRodney W. Grimes u_int32_t u4; 1279b50d902SRodney W. Grimes u_int64_t u8; 1289b50d902SRodney W. Grimes 1299b50d902SRodney W. Grimes switch(pr->flags) { 1309b50d902SRodney W. Grimes case F_ADDRESS: 1319b50d902SRodney W. Grimes (void)printf(pr->fmt, (quad_t)address); 1329b50d902SRodney W. Grimes break; 1339b50d902SRodney W. Grimes case F_BPAD: 1349b50d902SRodney W. Grimes (void)printf(pr->fmt, ""); 1359b50d902SRodney W. Grimes break; 1369b50d902SRodney W. Grimes case F_C: 1379b50d902SRodney W. Grimes conv_c(pr, bp); 1389b50d902SRodney W. Grimes break; 1399b50d902SRodney W. Grimes case F_CHAR: 1409b50d902SRodney W. Grimes (void)printf(pr->fmt, *bp); 1419b50d902SRodney W. Grimes break; 1429b50d902SRodney W. Grimes case F_DBL: 1439b50d902SRodney W. Grimes switch(pr->bcnt) { 1449b50d902SRodney W. Grimes case 4: 1459b50d902SRodney W. Grimes bcopy(bp, &f4, sizeof(f4)); 1469b50d902SRodney W. Grimes (void)printf(pr->fmt, f4); 1479b50d902SRodney W. Grimes break; 1489b50d902SRodney W. Grimes case 8: 1499b50d902SRodney W. Grimes bcopy(bp, &f8, sizeof(f8)); 1509b50d902SRodney W. Grimes (void)printf(pr->fmt, f8); 1519b50d902SRodney W. Grimes break; 1529b50d902SRodney W. Grimes } 1539b50d902SRodney W. Grimes break; 1549b50d902SRodney W. Grimes case F_INT: 1559b50d902SRodney W. Grimes switch(pr->bcnt) { 1569b50d902SRodney W. Grimes case 1: 1579b50d902SRodney W. Grimes (void)printf(pr->fmt, (quad_t)*bp); 1589b50d902SRodney W. Grimes break; 1599b50d902SRodney W. Grimes case 2: 1609b50d902SRodney W. Grimes bcopy(bp, &s2, sizeof(s2)); 1619b50d902SRodney W. Grimes (void)printf(pr->fmt, (quad_t)s2); 1629b50d902SRodney W. Grimes break; 1639b50d902SRodney W. Grimes case 4: 1649b50d902SRodney W. Grimes bcopy(bp, &s4, sizeof(s4)); 1659b50d902SRodney W. Grimes (void)printf(pr->fmt, (quad_t)s4); 1669b50d902SRodney W. Grimes break; 1679b50d902SRodney W. Grimes case 8: 1689b50d902SRodney W. Grimes bcopy(bp, &s8, sizeof(s8)); 1699b50d902SRodney W. Grimes (void)printf(pr->fmt, s8); 1709b50d902SRodney W. Grimes break; 1719b50d902SRodney W. Grimes } 1729b50d902SRodney W. Grimes break; 1739b50d902SRodney W. Grimes case F_P: 1749b50d902SRodney W. Grimes (void)printf(pr->fmt, isprint(*bp) ? *bp : '.'); 1759b50d902SRodney W. Grimes break; 1769b50d902SRodney W. Grimes case F_STR: 1779b50d902SRodney W. Grimes (void)printf(pr->fmt, (char *)bp); 1789b50d902SRodney W. Grimes break; 1799b50d902SRodney W. Grimes case F_TEXT: 1803d3f014fSKris Kennaway (void)printf("%s", pr->fmt); 1819b50d902SRodney W. Grimes break; 1829b50d902SRodney W. Grimes case F_U: 1839b50d902SRodney W. Grimes conv_u(pr, bp); 1849b50d902SRodney W. Grimes break; 1859b50d902SRodney W. Grimes case F_UINT: 1869b50d902SRodney W. Grimes switch(pr->bcnt) { 1879b50d902SRodney W. Grimes case 1: 1889b50d902SRodney W. Grimes (void)printf(pr->fmt, (u_quad_t)*bp); 1899b50d902SRodney W. Grimes break; 1909b50d902SRodney W. Grimes case 2: 1919b50d902SRodney W. Grimes bcopy(bp, &u2, sizeof(u2)); 1929b50d902SRodney W. Grimes (void)printf(pr->fmt, (u_quad_t)u2); 1939b50d902SRodney W. Grimes break; 1949b50d902SRodney W. Grimes case 4: 1959b50d902SRodney W. Grimes bcopy(bp, &u4, sizeof(u4)); 1969b50d902SRodney W. Grimes (void)printf(pr->fmt, (u_quad_t)u4); 1979b50d902SRodney W. Grimes break; 1989b50d902SRodney W. Grimes case 8: 1999b50d902SRodney W. Grimes bcopy(bp, &u8, sizeof(u8)); 2009b50d902SRodney W. Grimes (void)printf(pr->fmt, u8); 2019b50d902SRodney W. Grimes break; 2029b50d902SRodney W. Grimes } 2039b50d902SRodney W. Grimes break; 2049b50d902SRodney W. Grimes } 2059b50d902SRodney W. Grimes } 2069b50d902SRodney W. Grimes 2079b50d902SRodney W. Grimes void 2089b50d902SRodney W. Grimes bpad(pr) 2099b50d902SRodney W. Grimes PR *pr; 2109b50d902SRodney W. Grimes { 211c38cc743SMark Murray static char spec[] = " -0+#"; 2129b50d902SRodney W. Grimes register char *p1, *p2; 2139b50d902SRodney W. Grimes 2149b50d902SRodney W. Grimes /* 2159b50d902SRodney W. Grimes * Remove all conversion flags; '-' is the only one valid 2169b50d902SRodney W. Grimes * with %s, and it's not useful here. 2179b50d902SRodney W. Grimes */ 2189b50d902SRodney W. Grimes pr->flags = F_BPAD; 2199b50d902SRodney W. Grimes pr->cchar[0] = 's'; 2209b50d902SRodney W. Grimes pr->cchar[1] = '\0'; 2219b50d902SRodney W. Grimes for (p1 = pr->fmt; *p1 != '%'; ++p1); 2229b50d902SRodney W. Grimes for (p2 = ++p1; *p1 && index(spec, *p1); ++p1); 223c38cc743SMark Murray while ((*p2++ = *p1++)); 2249b50d902SRodney W. Grimes } 2259b50d902SRodney W. Grimes 2269b50d902SRodney W. Grimes static char **_argv; 2279b50d902SRodney W. Grimes 2289b50d902SRodney W. Grimes u_char * 2299b50d902SRodney W. Grimes get() 2309b50d902SRodney W. Grimes { 2319b50d902SRodney W. Grimes extern int length; 2329b50d902SRodney W. Grimes static int ateof = 1; 2339b50d902SRodney W. Grimes static u_char *curp, *savp; 2349b50d902SRodney W. Grimes register int n; 2359b50d902SRodney W. Grimes int need, nread; 236b001517fSMatthew Dillon int valid_save = 0; 2379b50d902SRodney W. Grimes u_char *tmpp; 2389b50d902SRodney W. Grimes 2399b50d902SRodney W. Grimes if (!curp) { 240ac3c230cSDavid E. O'Brien if ((curp = calloc(1, blocksize)) == NULL) 241ac3c230cSDavid E. O'Brien err(1, NULL); 242ac3c230cSDavid E. O'Brien if ((savp = calloc(1, blocksize)) == NULL) 243ac3c230cSDavid E. O'Brien err(1, NULL); 2449b50d902SRodney W. Grimes } else { 2459b50d902SRodney W. Grimes tmpp = curp; 2469b50d902SRodney W. Grimes curp = savp; 2479b50d902SRodney W. Grimes savp = tmpp; 2489b50d902SRodney W. Grimes address += blocksize; 249b001517fSMatthew Dillon valid_save = 1; 2509b50d902SRodney W. Grimes } 2519b50d902SRodney W. Grimes for (need = blocksize, nread = 0;;) { 2529b50d902SRodney W. Grimes /* 2539b50d902SRodney W. Grimes * if read the right number of bytes, or at EOF for one file, 2549b50d902SRodney W. Grimes * and no other files are available, zero-pad the rest of the 2559b50d902SRodney W. Grimes * block and set the end flag. 2569b50d902SRodney W. Grimes */ 257c38cc743SMark Murray if (!length || (ateof && !next((char **)NULL))) { 2589b50d902SRodney W. Grimes if (need == blocksize) 2599b50d902SRodney W. Grimes return((u_char *)NULL); 260b001517fSMatthew Dillon if (vflag != ALL && 261b001517fSMatthew Dillon valid_save && 262b001517fSMatthew Dillon bcmp(curp, savp, nread) == 0) { 2639b50d902SRodney W. Grimes if (vflag != DUP) 2649b50d902SRodney W. Grimes (void)printf("*\n"); 2659b50d902SRodney W. Grimes return((u_char *)NULL); 2669b50d902SRodney W. Grimes } 2679b50d902SRodney W. Grimes bzero((char *)curp + nread, need); 2689b50d902SRodney W. Grimes eaddress = address + nread; 2699b50d902SRodney W. Grimes return(curp); 2709b50d902SRodney W. Grimes } 2719b50d902SRodney W. Grimes n = fread((char *)curp + nread, sizeof(u_char), 2729b50d902SRodney W. Grimes length == -1 ? need : MIN(length, need), stdin); 2739b50d902SRodney W. Grimes if (!n) { 2749b50d902SRodney W. Grimes if (ferror(stdin)) 27510731702SPhilippe Charnier warn("%s", _argv[-1]); 2769b50d902SRodney W. Grimes ateof = 1; 2779b50d902SRodney W. Grimes continue; 2789b50d902SRodney W. Grimes } 2799b50d902SRodney W. Grimes ateof = 0; 2809b50d902SRodney W. Grimes if (length != -1) 2819b50d902SRodney W. Grimes length -= n; 2829b50d902SRodney W. Grimes if (!(need -= n)) { 2839b50d902SRodney W. Grimes if (vflag == ALL || vflag == FIRST || 284b001517fSMatthew Dillon valid_save == 0 || 285b001517fSMatthew Dillon bcmp(curp, savp, blocksize) != 0) { 2869b50d902SRodney W. Grimes if (vflag == DUP || vflag == FIRST) 2879b50d902SRodney W. Grimes vflag = WAIT; 2889b50d902SRodney W. Grimes return(curp); 2899b50d902SRodney W. Grimes } 2909b50d902SRodney W. Grimes if (vflag == WAIT) 2919b50d902SRodney W. Grimes (void)printf("*\n"); 2929b50d902SRodney W. Grimes vflag = DUP; 2939b50d902SRodney W. Grimes address += blocksize; 2949b50d902SRodney W. Grimes need = blocksize; 2959b50d902SRodney W. Grimes nread = 0; 2969b50d902SRodney W. Grimes } 2979b50d902SRodney W. Grimes else 2989b50d902SRodney W. Grimes nread += n; 2999b50d902SRodney W. Grimes } 3009b50d902SRodney W. Grimes } 3019b50d902SRodney W. Grimes 3029b50d902SRodney W. Grimes extern off_t skip; /* bytes to skip */ 3039b50d902SRodney W. Grimes 3049b50d902SRodney W. Grimes int 3059b50d902SRodney W. Grimes next(argv) 3069b50d902SRodney W. Grimes char **argv; 3079b50d902SRodney W. Grimes { 3089b50d902SRodney W. Grimes extern int exitval; 3099b50d902SRodney W. Grimes static int done; 3109b50d902SRodney W. Grimes int statok; 3119b50d902SRodney W. Grimes 3129b50d902SRodney W. Grimes if (argv) { 3139b50d902SRodney W. Grimes _argv = argv; 3149b50d902SRodney W. Grimes return(1); 3159b50d902SRodney W. Grimes } 3169b50d902SRodney W. Grimes for (;;) { 3179b50d902SRodney W. Grimes if (*_argv) { 3189b50d902SRodney W. Grimes if (!(freopen(*_argv, "r", stdin))) { 31910731702SPhilippe Charnier warn("%s", *_argv); 3209b50d902SRodney W. Grimes exitval = 1; 3219b50d902SRodney W. Grimes ++_argv; 3229b50d902SRodney W. Grimes continue; 3239b50d902SRodney W. Grimes } 3249b50d902SRodney W. Grimes statok = done = 1; 3259b50d902SRodney W. Grimes } else { 3269b50d902SRodney W. Grimes if (done++) 3279b50d902SRodney W. Grimes return(0); 3289b50d902SRodney W. Grimes statok = 0; 3299b50d902SRodney W. Grimes } 3309b50d902SRodney W. Grimes if (skip) 3319b50d902SRodney W. Grimes doskip(statok ? *_argv : "stdin", statok); 3329b50d902SRodney W. Grimes if (*_argv) 3339b50d902SRodney W. Grimes ++_argv; 3349b50d902SRodney W. Grimes if (!skip) 3359b50d902SRodney W. Grimes return(1); 3369b50d902SRodney W. Grimes } 3379b50d902SRodney W. Grimes /* NOTREACHED */ 3389b50d902SRodney W. Grimes } 3399b50d902SRodney W. Grimes 3409b50d902SRodney W. Grimes void 3419b50d902SRodney W. Grimes doskip(fname, statok) 342c38cc743SMark Murray const char *fname; 3439b50d902SRodney W. Grimes int statok; 3449b50d902SRodney W. Grimes { 3459b50d902SRodney W. Grimes register int cnt; 3469b50d902SRodney W. Grimes struct stat sb; 3479b50d902SRodney W. Grimes 3489b50d902SRodney W. Grimes if (statok) { 3499b50d902SRodney W. Grimes if (fstat(fileno(stdin), &sb)) 35010731702SPhilippe Charnier err(1, "%s", fname); 3519b50d902SRodney W. Grimes if (S_ISREG(sb.st_mode) && skip >= sb.st_size) { 3529b50d902SRodney W. Grimes address += sb.st_size; 3539b50d902SRodney W. Grimes skip -= sb.st_size; 3549b50d902SRodney W. Grimes return; 3559b50d902SRodney W. Grimes } 3569b50d902SRodney W. Grimes } 3579b50d902SRodney W. Grimes if (S_ISREG(sb.st_mode)) { 3587a27e657SAndrey A. Chernov if (fseeko(stdin, skip, SEEK_SET)) 35910731702SPhilippe Charnier err(1, "%s", fname); 3609b50d902SRodney W. Grimes address += skip; 3619b50d902SRodney W. Grimes skip = 0; 3629b50d902SRodney W. Grimes } else { 3639b50d902SRodney W. Grimes for (cnt = 0; cnt < skip; ++cnt) 3649b50d902SRodney W. Grimes if (getchar() == EOF) 3659b50d902SRodney W. Grimes break; 3669b50d902SRodney W. Grimes address += cnt; 3679b50d902SRodney W. Grimes skip -= cnt; 3689b50d902SRodney W. Grimes } 3699b50d902SRodney W. Grimes } 370