18a16b7a1SPedro F. Giffuni /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 49b50d902SRodney W. Grimes * Copyright (c) 1989, 1993 59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 69b50d902SRodney W. Grimes * 79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 99b50d902SRodney W. Grimes * are met: 109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 179b50d902SRodney W. Grimes * without specific prior written permission. 189b50d902SRodney W. Grimes * 199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 299b50d902SRodney W. Grimes * SUCH DAMAGE. 309b50d902SRodney W. Grimes */ 319b50d902SRodney W. Grimes 329b50d902SRodney W. Grimes #ifndef lint 3310731702SPhilippe Charnier #if 0 349b50d902SRodney W. Grimes static char sccsid[] = "@(#)display.c 8.1 (Berkeley) 6/6/93"; 3510731702SPhilippe Charnier #endif 369b50d902SRodney W. Grimes #endif /* not lint */ 37e026a48cSDavid E. O'Brien #include <sys/cdefs.h> 38e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$"); 399b50d902SRodney W. Grimes 409b50d902SRodney W. Grimes #include <sys/param.h> 41327240c7SConrad Meyer #include <sys/capsicum.h> 42df7b0169SKyle Evans #include <sys/conf.h> 43df7b0169SKyle Evans #include <sys/ioctl.h> 449b50d902SRodney W. Grimes #include <sys/stat.h> 459b50d902SRodney W. Grimes 46327240c7SConrad Meyer #include <capsicum_helpers.h> 479b50d902SRodney W. Grimes #include <ctype.h> 4810731702SPhilippe Charnier #include <err.h> 49327240c7SConrad Meyer #include <errno.h> 509b50d902SRodney W. Grimes #include <stdio.h> 519b50d902SRodney W. Grimes #include <stdlib.h> 529b50d902SRodney W. Grimes #include <string.h> 53821df508SXin LI #include <unistd.h> 549b50d902SRodney W. Grimes #include "hexdump.h" 559b50d902SRodney W. Grimes 569b50d902SRodney W. Grimes enum _vflag vflag = FIRST; 579b50d902SRodney W. Grimes 589b50d902SRodney W. Grimes static off_t address; /* address/offset in stream */ 599b50d902SRodney W. Grimes static off_t eaddress; /* end address */ 609b50d902SRodney W. Grimes 61e95e2344SAlexander Kabaev static void print(PR *, u_char *); 62df7b0169SKyle Evans static void noseek(void); 639b50d902SRodney W. Grimes 649b50d902SRodney W. Grimes void 65f4ac32deSDavid Malone display(void) 669b50d902SRodney W. Grimes { 67f4ac32deSDavid Malone FS *fs; 68f4ac32deSDavid Malone FU *fu; 69f4ac32deSDavid Malone PR *pr; 70f4ac32deSDavid Malone int cnt; 71f4ac32deSDavid Malone u_char *bp; 729b50d902SRodney W. Grimes off_t saveaddress; 739b50d902SRodney W. Grimes u_char savech, *savebp; 749b50d902SRodney W. Grimes 75aae01d24SJohan Karlsson savech = 0; 76c38cc743SMark Murray while ((bp = get())) 779b50d902SRodney W. Grimes for (fs = fshead, savebp = bp, saveaddress = address; fs; 789b50d902SRodney W. Grimes fs = fs->nextfs, bp = savebp, address = saveaddress) 799b50d902SRodney W. Grimes for (fu = fs->nextfu; fu; fu = fu->nextfu) { 809b50d902SRodney W. Grimes if (fu->flags&F_IGNORE) 819b50d902SRodney W. Grimes break; 829b50d902SRodney W. Grimes for (cnt = fu->reps; cnt; --cnt) 839b50d902SRodney W. Grimes for (pr = fu->nextpr; pr; address += pr->bcnt, 849b50d902SRodney W. Grimes bp += pr->bcnt, pr = pr->nextpr) { 859b50d902SRodney W. Grimes if (eaddress && address >= eaddress && 869b50d902SRodney W. Grimes !(pr->flags & (F_TEXT|F_BPAD))) 879b50d902SRodney W. Grimes bpad(pr); 889b50d902SRodney W. Grimes if (cnt == 1 && pr->nospace) { 899b50d902SRodney W. Grimes savech = *pr->nospace; 909b50d902SRodney W. Grimes *pr->nospace = '\0'; 919b50d902SRodney W. Grimes } 929b50d902SRodney W. Grimes print(pr, bp); 939b50d902SRodney W. Grimes if (cnt == 1 && pr->nospace) 949b50d902SRodney W. Grimes *pr->nospace = savech; 959b50d902SRodney W. Grimes } 969b50d902SRodney W. Grimes } 979b50d902SRodney W. Grimes if (endfu) { 989b50d902SRodney W. Grimes /* 999b50d902SRodney W. Grimes * If eaddress not set, error or file size was multiple of 1009b50d902SRodney W. Grimes * blocksize, and no partial block ever found. 1019b50d902SRodney W. Grimes */ 1029b50d902SRodney W. Grimes if (!eaddress) { 1039b50d902SRodney W. Grimes if (!address) 1049b50d902SRodney W. Grimes return; 1059b50d902SRodney W. Grimes eaddress = address; 1069b50d902SRodney W. Grimes } 1079b50d902SRodney W. Grimes for (pr = endfu->nextpr; pr; pr = pr->nextpr) 1089b50d902SRodney W. Grimes switch(pr->flags) { 1099b50d902SRodney W. Grimes case F_ADDRESS: 1109b50d902SRodney W. Grimes (void)printf(pr->fmt, (quad_t)eaddress); 1119b50d902SRodney W. Grimes break; 1129b50d902SRodney W. Grimes case F_TEXT: 11332d9afb6SKris Kennaway (void)printf("%s", pr->fmt); 1149b50d902SRodney W. Grimes break; 1159b50d902SRodney W. Grimes } 1169b50d902SRodney W. Grimes } 1179b50d902SRodney W. Grimes } 1189b50d902SRodney W. Grimes 119e95e2344SAlexander Kabaev static void 120f4ac32deSDavid Malone print(PR *pr, u_char *bp) 1219b50d902SRodney W. Grimes { 12282866376STim J. Robbins long double ldbl; 1239b50d902SRodney W. Grimes double f8; 1249b50d902SRodney W. Grimes float f4; 1259b50d902SRodney W. Grimes int16_t s2; 1269b50d902SRodney W. Grimes int8_t s8; 1279b50d902SRodney W. Grimes int32_t s4; 1289b50d902SRodney W. Grimes u_int16_t u2; 1299b50d902SRodney W. Grimes u_int32_t u4; 1309b50d902SRodney W. Grimes u_int64_t u8; 1319b50d902SRodney W. Grimes 1329b50d902SRodney W. Grimes switch(pr->flags) { 1339b50d902SRodney W. Grimes case F_ADDRESS: 1349b50d902SRodney W. Grimes (void)printf(pr->fmt, (quad_t)address); 1359b50d902SRodney W. Grimes break; 1369b50d902SRodney W. Grimes case F_BPAD: 1379b50d902SRodney W. Grimes (void)printf(pr->fmt, ""); 1389b50d902SRodney W. Grimes break; 1399b50d902SRodney W. Grimes case F_C: 14040ccfb31STim J. Robbins conv_c(pr, bp, eaddress ? eaddress - address : 14140ccfb31STim J. Robbins blocksize - address % blocksize); 1429b50d902SRodney W. Grimes break; 1439b50d902SRodney W. Grimes case F_CHAR: 1449b50d902SRodney W. Grimes (void)printf(pr->fmt, *bp); 1459b50d902SRodney W. Grimes break; 1469b50d902SRodney W. Grimes case F_DBL: 1479b50d902SRodney W. Grimes switch(pr->bcnt) { 1489b50d902SRodney W. Grimes case 4: 1499b50d902SRodney W. Grimes bcopy(bp, &f4, sizeof(f4)); 1509b50d902SRodney W. Grimes (void)printf(pr->fmt, f4); 1519b50d902SRodney W. Grimes break; 1529b50d902SRodney W. Grimes case 8: 1539b50d902SRodney W. Grimes bcopy(bp, &f8, sizeof(f8)); 1549b50d902SRodney W. Grimes (void)printf(pr->fmt, f8); 1559b50d902SRodney W. Grimes break; 156cf021af2STim J. Robbins default: 157cf021af2STim J. Robbins if (pr->bcnt == sizeof(long double)) { 15882866376STim J. Robbins bcopy(bp, &ldbl, sizeof(ldbl)); 15982866376STim J. Robbins (void)printf(pr->fmt, ldbl); 160cf021af2STim J. Robbins } 16182866376STim J. Robbins break; 1629b50d902SRodney W. Grimes } 1639b50d902SRodney W. Grimes break; 1649b50d902SRodney W. Grimes case F_INT: 1659b50d902SRodney W. Grimes switch(pr->bcnt) { 1669b50d902SRodney W. Grimes case 1: 167cf45dcc8STim J. Robbins (void)printf(pr->fmt, (quad_t)(signed char)*bp); 1689b50d902SRodney W. Grimes break; 1699b50d902SRodney W. Grimes case 2: 1709b50d902SRodney W. Grimes bcopy(bp, &s2, sizeof(s2)); 1719b50d902SRodney W. Grimes (void)printf(pr->fmt, (quad_t)s2); 1729b50d902SRodney W. Grimes break; 1739b50d902SRodney W. Grimes case 4: 1749b50d902SRodney W. Grimes bcopy(bp, &s4, sizeof(s4)); 1759b50d902SRodney W. Grimes (void)printf(pr->fmt, (quad_t)s4); 1769b50d902SRodney W. Grimes break; 1779b50d902SRodney W. Grimes case 8: 1789b50d902SRodney W. Grimes bcopy(bp, &s8, sizeof(s8)); 1799b50d902SRodney W. Grimes (void)printf(pr->fmt, s8); 1809b50d902SRodney W. Grimes break; 1819b50d902SRodney W. Grimes } 1829b50d902SRodney W. Grimes break; 1839b50d902SRodney W. Grimes case F_P: 1849b50d902SRodney W. Grimes (void)printf(pr->fmt, isprint(*bp) ? *bp : '.'); 1859b50d902SRodney W. Grimes break; 1869b50d902SRodney W. Grimes case F_STR: 1879b50d902SRodney W. Grimes (void)printf(pr->fmt, (char *)bp); 1889b50d902SRodney W. Grimes break; 1899b50d902SRodney W. Grimes case F_TEXT: 1903d3f014fSKris Kennaway (void)printf("%s", pr->fmt); 1919b50d902SRodney W. Grimes break; 1929b50d902SRodney W. Grimes case F_U: 1939b50d902SRodney W. Grimes conv_u(pr, bp); 1949b50d902SRodney W. Grimes break; 1959b50d902SRodney W. Grimes case F_UINT: 1969b50d902SRodney W. Grimes switch(pr->bcnt) { 1979b50d902SRodney W. Grimes case 1: 1989b50d902SRodney W. Grimes (void)printf(pr->fmt, (u_quad_t)*bp); 1999b50d902SRodney W. Grimes break; 2009b50d902SRodney W. Grimes case 2: 2019b50d902SRodney W. Grimes bcopy(bp, &u2, sizeof(u2)); 2029b50d902SRodney W. Grimes (void)printf(pr->fmt, (u_quad_t)u2); 2039b50d902SRodney W. Grimes break; 2049b50d902SRodney W. Grimes case 4: 2059b50d902SRodney W. Grimes bcopy(bp, &u4, sizeof(u4)); 2069b50d902SRodney W. Grimes (void)printf(pr->fmt, (u_quad_t)u4); 2079b50d902SRodney W. Grimes break; 2089b50d902SRodney W. Grimes case 8: 2099b50d902SRodney W. Grimes bcopy(bp, &u8, sizeof(u8)); 2109b50d902SRodney W. Grimes (void)printf(pr->fmt, u8); 2119b50d902SRodney W. Grimes break; 2129b50d902SRodney W. Grimes } 2139b50d902SRodney W. Grimes break; 2149b50d902SRodney W. Grimes } 2159b50d902SRodney W. Grimes } 2169b50d902SRodney W. Grimes 2179b50d902SRodney W. Grimes void 218f4ac32deSDavid Malone bpad(PR *pr) 2199b50d902SRodney W. Grimes { 22066da3588SDavid E. O'Brien static char const *spec = " -0+#"; 221f4ac32deSDavid Malone char *p1, *p2; 2229b50d902SRodney W. Grimes 2239b50d902SRodney W. Grimes /* 2249b50d902SRodney W. Grimes * Remove all conversion flags; '-' is the only one valid 2259b50d902SRodney W. Grimes * with %s, and it's not useful here. 2269b50d902SRodney W. Grimes */ 2279b50d902SRodney W. Grimes pr->flags = F_BPAD; 2289b50d902SRodney W. Grimes pr->cchar[0] = 's'; 2299b50d902SRodney W. Grimes pr->cchar[1] = '\0'; 2309b50d902SRodney W. Grimes for (p1 = pr->fmt; *p1 != '%'; ++p1); 231b3608ae1SEd Schouten for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1); 232c38cc743SMark Murray while ((*p2++ = *p1++)); 2339b50d902SRodney W. Grimes } 2349b50d902SRodney W. Grimes 2359b50d902SRodney W. Grimes static char **_argv; 2369b50d902SRodney W. Grimes 2379b50d902SRodney W. Grimes u_char * 238f4ac32deSDavid Malone get(void) 2399b50d902SRodney W. Grimes { 2409b50d902SRodney W. Grimes static int ateof = 1; 2419b50d902SRodney W. Grimes static u_char *curp, *savp; 242f4ac32deSDavid Malone int n; 2439b50d902SRodney W. Grimes int need, nread; 244b001517fSMatthew Dillon int valid_save = 0; 2459b50d902SRodney W. Grimes u_char *tmpp; 2469b50d902SRodney W. Grimes 2479b50d902SRodney W. Grimes if (!curp) { 248ac3c230cSDavid E. O'Brien if ((curp = calloc(1, blocksize)) == NULL) 249ac3c230cSDavid E. O'Brien err(1, NULL); 250ac3c230cSDavid E. O'Brien if ((savp = calloc(1, blocksize)) == NULL) 251ac3c230cSDavid E. O'Brien err(1, NULL); 2529b50d902SRodney W. Grimes } else { 2539b50d902SRodney W. Grimes tmpp = curp; 2549b50d902SRodney W. Grimes curp = savp; 2559b50d902SRodney W. Grimes savp = tmpp; 2569b50d902SRodney W. Grimes address += blocksize; 257b001517fSMatthew Dillon valid_save = 1; 2589b50d902SRodney W. Grimes } 2599b50d902SRodney W. Grimes for (need = blocksize, nread = 0;;) { 2609b50d902SRodney W. Grimes /* 2619b50d902SRodney W. Grimes * if read the right number of bytes, or at EOF for one file, 2629b50d902SRodney W. Grimes * and no other files are available, zero-pad the rest of the 2639b50d902SRodney W. Grimes * block and set the end flag. 2649b50d902SRodney W. Grimes */ 265c38cc743SMark Murray if (!length || (ateof && !next((char **)NULL))) { 266cf021af2STim J. Robbins if (odmode && address < skip) 267cf021af2STim J. Robbins errx(1, "cannot skip past end of input"); 2689b50d902SRodney W. Grimes if (need == blocksize) 2699b50d902SRodney W. Grimes return((u_char *)NULL); 27040ccfb31STim J. Robbins /* 27140ccfb31STim J. Robbins * XXX bcmp() is not quite right in the presence 27240ccfb31STim J. Robbins * of multibyte characters. 27340ccfb31STim J. Robbins */ 274b001517fSMatthew Dillon if (vflag != ALL && 275b001517fSMatthew Dillon valid_save && 276b001517fSMatthew Dillon bcmp(curp, savp, nread) == 0) { 277*272144abSPoul-Henning Kamp if (vflag != DUP) { 2789b50d902SRodney W. Grimes (void)printf("*\n"); 279*272144abSPoul-Henning Kamp (void)fflush(stdout); 280*272144abSPoul-Henning Kamp } 2819b50d902SRodney W. Grimes return((u_char *)NULL); 2829b50d902SRodney W. Grimes } 2839b50d902SRodney W. Grimes bzero((char *)curp + nread, need); 2849b50d902SRodney W. Grimes eaddress = address + nread; 2859b50d902SRodney W. Grimes return(curp); 2869b50d902SRodney W. Grimes } 2879b50d902SRodney W. Grimes n = fread((char *)curp + nread, sizeof(u_char), 2889b50d902SRodney W. Grimes length == -1 ? need : MIN(length, need), stdin); 2899b50d902SRodney W. Grimes if (!n) { 2909b50d902SRodney W. Grimes if (ferror(stdin)) 29110731702SPhilippe Charnier warn("%s", _argv[-1]); 2929b50d902SRodney W. Grimes ateof = 1; 2939b50d902SRodney W. Grimes continue; 2949b50d902SRodney W. Grimes } 2959b50d902SRodney W. Grimes ateof = 0; 2969b50d902SRodney W. Grimes if (length != -1) 2979b50d902SRodney W. Grimes length -= n; 2989b50d902SRodney W. Grimes if (!(need -= n)) { 29940ccfb31STim J. Robbins /* 30040ccfb31STim J. Robbins * XXX bcmp() is not quite right in the presence 30140ccfb31STim J. Robbins * of multibyte characters. 30240ccfb31STim J. Robbins */ 3039b50d902SRodney W. Grimes if (vflag == ALL || vflag == FIRST || 304b001517fSMatthew Dillon valid_save == 0 || 305b001517fSMatthew Dillon bcmp(curp, savp, blocksize) != 0) { 3069b50d902SRodney W. Grimes if (vflag == DUP || vflag == FIRST) 3079b50d902SRodney W. Grimes vflag = WAIT; 3089b50d902SRodney W. Grimes return(curp); 3099b50d902SRodney W. Grimes } 310*272144abSPoul-Henning Kamp if (vflag == WAIT) { 3119b50d902SRodney W. Grimes (void)printf("*\n"); 312*272144abSPoul-Henning Kamp (void)fflush(stdout); 313*272144abSPoul-Henning Kamp } 3149b50d902SRodney W. Grimes vflag = DUP; 3159b50d902SRodney W. Grimes address += blocksize; 3169b50d902SRodney W. Grimes need = blocksize; 3179b50d902SRodney W. Grimes nread = 0; 3189b50d902SRodney W. Grimes } 3199b50d902SRodney W. Grimes else 3209b50d902SRodney W. Grimes nread += n; 3219b50d902SRodney W. Grimes } 3229b50d902SRodney W. Grimes } 3239b50d902SRodney W. Grimes 32440ccfb31STim J. Robbins size_t 32540ccfb31STim J. Robbins peek(u_char *buf, size_t nbytes) 32640ccfb31STim J. Robbins { 32740ccfb31STim J. Robbins size_t n, nread; 32840ccfb31STim J. Robbins int c; 32940ccfb31STim J. Robbins 330aae01d24SJohan Karlsson if (length != -1 && nbytes > (unsigned int)length) 33140ccfb31STim J. Robbins nbytes = length; 33240ccfb31STim J. Robbins nread = 0; 33340ccfb31STim J. Robbins while (nread < nbytes && (c = getchar()) != EOF) { 33440ccfb31STim J. Robbins *buf++ = c; 33540ccfb31STim J. Robbins nread++; 33640ccfb31STim J. Robbins } 33740ccfb31STim J. Robbins n = nread; 33840ccfb31STim J. Robbins while (n-- > 0) { 33940ccfb31STim J. Robbins c = *--buf; 34040ccfb31STim J. Robbins ungetc(c, stdin); 34140ccfb31STim J. Robbins } 34240ccfb31STim J. Robbins return (nread); 34340ccfb31STim J. Robbins } 34440ccfb31STim J. Robbins 3459b50d902SRodney W. Grimes int 346f4ac32deSDavid Malone next(char **argv) 3479b50d902SRodney W. Grimes { 3489b50d902SRodney W. Grimes static int done; 3499b50d902SRodney W. Grimes int statok; 3509b50d902SRodney W. Grimes 3519b50d902SRodney W. Grimes if (argv) { 3529b50d902SRodney W. Grimes _argv = argv; 3539b50d902SRodney W. Grimes return(1); 3549b50d902SRodney W. Grimes } 3559b50d902SRodney W. Grimes for (;;) { 3569b50d902SRodney W. Grimes if (*_argv) { 3579a1e2d06STim J. Robbins done = 1; 3589b50d902SRodney W. Grimes if (!(freopen(*_argv, "r", stdin))) { 35910731702SPhilippe Charnier warn("%s", *_argv); 3609b50d902SRodney W. Grimes exitval = 1; 3619b50d902SRodney W. Grimes ++_argv; 3629b50d902SRodney W. Grimes continue; 3639b50d902SRodney W. Grimes } 3649a1e2d06STim J. Robbins statok = 1; 3659b50d902SRodney W. Grimes } else { 3669b50d902SRodney W. Grimes if (done++) 3679b50d902SRodney W. Grimes return(0); 3689b50d902SRodney W. Grimes statok = 0; 3699b50d902SRodney W. Grimes } 370327240c7SConrad Meyer 371327240c7SConrad Meyer if (caph_limit_stream(fileno(stdin), CAPH_READ) < 0) 372327240c7SConrad Meyer err(1, "unable to restrict %s", 373e47ea033SEd Maste statok ? *_argv : "stdin"); 374327240c7SConrad Meyer 375327240c7SConrad Meyer /* 376327240c7SConrad Meyer * We've opened our last input file; enter capsicum sandbox. 377327240c7SConrad Meyer */ 378e47ea033SEd Maste if (statok == 0 || *(_argv + 1) == NULL) { 3797672a014SMariusz Zaborski if (caph_enter() < 0) 380327240c7SConrad Meyer err(1, "unable to enter capability mode"); 381327240c7SConrad Meyer } 382327240c7SConrad Meyer 3839b50d902SRodney W. Grimes if (skip) 3849b50d902SRodney W. Grimes doskip(statok ? *_argv : "stdin", statok); 3859b50d902SRodney W. Grimes if (*_argv) 3869b50d902SRodney W. Grimes ++_argv; 3879b50d902SRodney W. Grimes if (!skip) 3889b50d902SRodney W. Grimes return(1); 3899b50d902SRodney W. Grimes } 3909b50d902SRodney W. Grimes /* NOTREACHED */ 3919b50d902SRodney W. Grimes } 3929b50d902SRodney W. Grimes 3939b50d902SRodney W. Grimes void 394f4ac32deSDavid Malone doskip(const char *fname, int statok) 3959b50d902SRodney W. Grimes { 396df7b0169SKyle Evans int type; 3979b50d902SRodney W. Grimes struct stat sb; 3989b50d902SRodney W. Grimes 3999b50d902SRodney W. Grimes if (statok) { 4009b50d902SRodney W. Grimes if (fstat(fileno(stdin), &sb)) 40110731702SPhilippe Charnier err(1, "%s", fname); 402daa1a379SKevin Lo if (S_ISREG(sb.st_mode) && skip > sb.st_size) { 4039b50d902SRodney W. Grimes address += sb.st_size; 4049b50d902SRodney W. Grimes skip -= sb.st_size; 4059b50d902SRodney W. Grimes return; 4069b50d902SRodney W. Grimes } 4079b50d902SRodney W. Grimes } 408df7b0169SKyle Evans if (!statok || S_ISFIFO(sb.st_mode) || S_ISSOCK(sb.st_mode)) { 409df7b0169SKyle Evans noseek(); 410df7b0169SKyle Evans return; 411df7b0169SKyle Evans } 412df7b0169SKyle Evans if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) { 413df7b0169SKyle Evans if (ioctl(fileno(stdin), FIODTYPE, &type)) 41410731702SPhilippe Charnier err(1, "%s", fname); 415df7b0169SKyle Evans /* 416df7b0169SKyle Evans * Most tape drives don't support seeking, 417df7b0169SKyle Evans * yet fseek() would succeed. 418df7b0169SKyle Evans */ 419df7b0169SKyle Evans if (type & D_TAPE) { 420df7b0169SKyle Evans noseek(); 421df7b0169SKyle Evans return; 422df7b0169SKyle Evans } 423df7b0169SKyle Evans } 424df7b0169SKyle Evans if (fseeko(stdin, skip, SEEK_SET)) { 425df7b0169SKyle Evans noseek(); 426df7b0169SKyle Evans return; 427df7b0169SKyle Evans } 4289b50d902SRodney W. Grimes address += skip; 4299b50d902SRodney W. Grimes skip = 0; 430df7b0169SKyle Evans } 431df7b0169SKyle Evans 432df7b0169SKyle Evans static void 433df7b0169SKyle Evans noseek(void) 434df7b0169SKyle Evans { 435df7b0169SKyle Evans int count; 436df7b0169SKyle Evans for (count = 0; count < skip; ++count) 4379b50d902SRodney W. Grimes if (getchar() == EOF) 4389b50d902SRodney W. Grimes break; 439df7b0169SKyle Evans address += count; 440df7b0169SKyle Evans skip -= count; 4419b50d902SRodney W. Grimes } 442