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 #include <sys/param.h>
33327240c7SConrad Meyer #include <sys/capsicum.h>
34df7b0169SKyle Evans #include <sys/conf.h>
35df7b0169SKyle Evans #include <sys/ioctl.h>
369b50d902SRodney W. Grimes #include <sys/stat.h>
379b50d902SRodney W. Grimes
38327240c7SConrad Meyer #include <capsicum_helpers.h>
399b50d902SRodney W. Grimes #include <ctype.h>
4010731702SPhilippe Charnier #include <err.h>
41327240c7SConrad Meyer #include <errno.h>
429b50d902SRodney W. Grimes #include <stdio.h>
439b50d902SRodney W. Grimes #include <stdlib.h>
449b50d902SRodney W. Grimes #include <string.h>
45821df508SXin LI #include <unistd.h>
469b50d902SRodney W. Grimes #include "hexdump.h"
479b50d902SRodney W. Grimes
489b50d902SRodney W. Grimes enum _vflag vflag = FIRST;
499b50d902SRodney W. Grimes
509b50d902SRodney W. Grimes static off_t address; /* address/offset in stream */
519b50d902SRodney W. Grimes static off_t eaddress; /* end address */
529b50d902SRodney W. Grimes
53e95e2344SAlexander Kabaev static void print(PR *, u_char *);
54df7b0169SKyle Evans static void noseek(void);
559b50d902SRodney W. Grimes
569b50d902SRodney W. Grimes void
display(void)57f4ac32deSDavid Malone display(void)
589b50d902SRodney W. Grimes {
59f4ac32deSDavid Malone FS *fs;
60f4ac32deSDavid Malone FU *fu;
61f4ac32deSDavid Malone PR *pr;
62f4ac32deSDavid Malone int cnt;
63f4ac32deSDavid Malone u_char *bp;
649b50d902SRodney W. Grimes off_t saveaddress;
659b50d902SRodney W. Grimes u_char savech, *savebp;
669b50d902SRodney W. Grimes
67aae01d24SJohan Karlsson savech = 0;
68c38cc743SMark Murray while ((bp = get()))
699b50d902SRodney W. Grimes for (fs = fshead, savebp = bp, saveaddress = address; fs;
709b50d902SRodney W. Grimes fs = fs->nextfs, bp = savebp, address = saveaddress)
719b50d902SRodney W. Grimes for (fu = fs->nextfu; fu; fu = fu->nextfu) {
729b50d902SRodney W. Grimes if (fu->flags&F_IGNORE)
739b50d902SRodney W. Grimes break;
749b50d902SRodney W. Grimes for (cnt = fu->reps; cnt; --cnt)
759b50d902SRodney W. Grimes for (pr = fu->nextpr; pr; address += pr->bcnt,
769b50d902SRodney W. Grimes bp += pr->bcnt, pr = pr->nextpr) {
779b50d902SRodney W. Grimes if (eaddress && address >= eaddress &&
789b50d902SRodney W. Grimes !(pr->flags & (F_TEXT|F_BPAD)))
799b50d902SRodney W. Grimes bpad(pr);
809b50d902SRodney W. Grimes if (cnt == 1 && pr->nospace) {
819b50d902SRodney W. Grimes savech = *pr->nospace;
829b50d902SRodney W. Grimes *pr->nospace = '\0';
839b50d902SRodney W. Grimes }
849b50d902SRodney W. Grimes print(pr, bp);
859b50d902SRodney W. Grimes if (cnt == 1 && pr->nospace)
869b50d902SRodney W. Grimes *pr->nospace = savech;
879b50d902SRodney W. Grimes }
889b50d902SRodney W. Grimes }
899b50d902SRodney W. Grimes if (endfu) {
909b50d902SRodney W. Grimes /*
919b50d902SRodney W. Grimes * If eaddress not set, error or file size was multiple of
929b50d902SRodney W. Grimes * blocksize, and no partial block ever found.
939b50d902SRodney W. Grimes */
949b50d902SRodney W. Grimes if (!eaddress) {
959b50d902SRodney W. Grimes if (!address)
969b50d902SRodney W. Grimes return;
979b50d902SRodney W. Grimes eaddress = address;
989b50d902SRodney W. Grimes }
999b50d902SRodney W. Grimes for (pr = endfu->nextpr; pr; pr = pr->nextpr)
1009b50d902SRodney W. Grimes switch(pr->flags) {
1019b50d902SRodney W. Grimes case F_ADDRESS:
1029b50d902SRodney W. Grimes (void)printf(pr->fmt, (quad_t)eaddress);
1039b50d902SRodney W. Grimes break;
1049b50d902SRodney W. Grimes case F_TEXT:
10532d9afb6SKris Kennaway (void)printf("%s", pr->fmt);
1069b50d902SRodney W. Grimes break;
1079b50d902SRodney W. Grimes }
1089b50d902SRodney W. Grimes }
1099b50d902SRodney W. Grimes }
1109b50d902SRodney W. Grimes
111e95e2344SAlexander Kabaev static void
print(PR * pr,u_char * bp)112f4ac32deSDavid Malone print(PR *pr, u_char *bp)
1139b50d902SRodney W. Grimes {
11482866376STim J. Robbins long double ldbl;
1159b50d902SRodney W. Grimes double f8;
1169b50d902SRodney W. Grimes float f4;
1179b50d902SRodney W. Grimes int16_t s2;
1189b50d902SRodney W. Grimes int32_t s4;
11937e3f5b2SBen Harris int64_t s8;
1209b50d902SRodney W. Grimes u_int16_t u2;
1219b50d902SRodney W. Grimes u_int32_t u4;
1229b50d902SRodney W. Grimes u_int64_t u8;
1239b50d902SRodney W. Grimes
1249b50d902SRodney W. Grimes switch(pr->flags) {
1259b50d902SRodney W. Grimes case F_ADDRESS:
1269b50d902SRodney W. Grimes (void)printf(pr->fmt, (quad_t)address);
1279b50d902SRodney W. Grimes break;
1289b50d902SRodney W. Grimes case F_BPAD:
1299b50d902SRodney W. Grimes (void)printf(pr->fmt, "");
1309b50d902SRodney W. Grimes break;
1319b50d902SRodney W. Grimes case F_C:
13240ccfb31STim J. Robbins conv_c(pr, bp, eaddress ? eaddress - address :
13340ccfb31STim J. Robbins blocksize - address % blocksize);
1349b50d902SRodney W. Grimes break;
1359b50d902SRodney W. Grimes case F_CHAR:
1369b50d902SRodney W. Grimes (void)printf(pr->fmt, *bp);
1379b50d902SRodney W. Grimes break;
1389b50d902SRodney W. Grimes case F_DBL:
1399b50d902SRodney W. Grimes switch(pr->bcnt) {
1409b50d902SRodney W. Grimes case 4:
1419b50d902SRodney W. Grimes bcopy(bp, &f4, sizeof(f4));
1429b50d902SRodney W. Grimes (void)printf(pr->fmt, f4);
1439b50d902SRodney W. Grimes break;
1449b50d902SRodney W. Grimes case 8:
1459b50d902SRodney W. Grimes bcopy(bp, &f8, sizeof(f8));
1469b50d902SRodney W. Grimes (void)printf(pr->fmt, f8);
1479b50d902SRodney W. Grimes break;
148cf021af2STim J. Robbins default:
149cf021af2STim J. Robbins if (pr->bcnt == sizeof(long double)) {
15082866376STim J. Robbins bcopy(bp, &ldbl, sizeof(ldbl));
15182866376STim J. Robbins (void)printf(pr->fmt, ldbl);
152cf021af2STim J. Robbins }
15382866376STim J. Robbins break;
1549b50d902SRodney W. Grimes }
1559b50d902SRodney W. Grimes break;
1569b50d902SRodney W. Grimes case F_INT:
1579b50d902SRodney W. Grimes switch(pr->bcnt) {
1589b50d902SRodney W. Grimes case 1:
159cf45dcc8STim J. Robbins (void)printf(pr->fmt, (quad_t)(signed char)*bp);
1609b50d902SRodney W. Grimes break;
1619b50d902SRodney W. Grimes case 2:
1629b50d902SRodney W. Grimes bcopy(bp, &s2, sizeof(s2));
1639b50d902SRodney W. Grimes (void)printf(pr->fmt, (quad_t)s2);
1649b50d902SRodney W. Grimes break;
1659b50d902SRodney W. Grimes case 4:
1669b50d902SRodney W. Grimes bcopy(bp, &s4, sizeof(s4));
1679b50d902SRodney W. Grimes (void)printf(pr->fmt, (quad_t)s4);
1689b50d902SRodney W. Grimes break;
1699b50d902SRodney W. Grimes case 8:
1709b50d902SRodney W. Grimes bcopy(bp, &s8, sizeof(s8));
1719b50d902SRodney W. Grimes (void)printf(pr->fmt, s8);
1729b50d902SRodney W. Grimes break;
1739b50d902SRodney W. Grimes }
1749b50d902SRodney W. Grimes break;
1759b50d902SRodney W. Grimes case F_P:
1769b50d902SRodney W. Grimes (void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
1779b50d902SRodney W. Grimes break;
1789b50d902SRodney W. Grimes case F_STR:
1799b50d902SRodney W. Grimes (void)printf(pr->fmt, (char *)bp);
1809b50d902SRodney W. Grimes break;
1819b50d902SRodney W. Grimes case F_TEXT:
1823d3f014fSKris Kennaway (void)printf("%s", pr->fmt);
1839b50d902SRodney W. Grimes break;
1849b50d902SRodney W. Grimes case F_U:
1859b50d902SRodney W. Grimes conv_u(pr, bp);
1869b50d902SRodney W. Grimes break;
1879b50d902SRodney W. Grimes case F_UINT:
1889b50d902SRodney W. Grimes switch(pr->bcnt) {
1899b50d902SRodney W. Grimes case 1:
1909b50d902SRodney W. Grimes (void)printf(pr->fmt, (u_quad_t)*bp);
1919b50d902SRodney W. Grimes break;
1929b50d902SRodney W. Grimes case 2:
1939b50d902SRodney W. Grimes bcopy(bp, &u2, sizeof(u2));
1949b50d902SRodney W. Grimes (void)printf(pr->fmt, (u_quad_t)u2);
1959b50d902SRodney W. Grimes break;
1969b50d902SRodney W. Grimes case 4:
1979b50d902SRodney W. Grimes bcopy(bp, &u4, sizeof(u4));
1989b50d902SRodney W. Grimes (void)printf(pr->fmt, (u_quad_t)u4);
1999b50d902SRodney W. Grimes break;
2009b50d902SRodney W. Grimes case 8:
2019b50d902SRodney W. Grimes bcopy(bp, &u8, sizeof(u8));
2029b50d902SRodney W. Grimes (void)printf(pr->fmt, u8);
2039b50d902SRodney W. Grimes break;
2049b50d902SRodney W. Grimes }
2059b50d902SRodney W. Grimes break;
2069b50d902SRodney W. Grimes }
2079b50d902SRodney W. Grimes }
2089b50d902SRodney W. Grimes
2099b50d902SRodney W. Grimes void
bpad(PR * pr)210f4ac32deSDavid Malone bpad(PR *pr)
2119b50d902SRodney W. Grimes {
21266da3588SDavid E. O'Brien static char const *spec = " -0+#";
213f4ac32deSDavid Malone char *p1, *p2;
2149b50d902SRodney W. Grimes
2159b50d902SRodney W. Grimes /*
2169b50d902SRodney W. Grimes * Remove all conversion flags; '-' is the only one valid
2179b50d902SRodney W. Grimes * with %s, and it's not useful here.
2189b50d902SRodney W. Grimes */
2199b50d902SRodney W. Grimes pr->flags = F_BPAD;
2209b50d902SRodney W. Grimes pr->cchar[0] = 's';
2219b50d902SRodney W. Grimes pr->cchar[1] = '\0';
2229b50d902SRodney W. Grimes for (p1 = pr->fmt; *p1 != '%'; ++p1);
223b3608ae1SEd Schouten for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1);
224c38cc743SMark Murray while ((*p2++ = *p1++));
2259b50d902SRodney W. Grimes }
2269b50d902SRodney W. Grimes
2279b50d902SRodney W. Grimes static char **_argv;
2289b50d902SRodney W. Grimes
2299b50d902SRodney W. Grimes u_char *
get(void)230f4ac32deSDavid Malone get(void)
2319b50d902SRodney W. Grimes {
2329b50d902SRodney W. Grimes static int ateof = 1;
2339b50d902SRodney W. Grimes static u_char *curp, *savp;
234f4ac32deSDavid Malone 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))) {
2581232c788SXin LI if (odmode && skip > 0)
259cf021af2STim J. Robbins errx(1, "cannot skip past end of input");
2609b50d902SRodney W. Grimes if (need == blocksize)
2619b50d902SRodney W. Grimes return((u_char *)NULL);
26240ccfb31STim J. Robbins /*
26340ccfb31STim J. Robbins * XXX bcmp() is not quite right in the presence
26440ccfb31STim J. Robbins * of multibyte characters.
26540ccfb31STim J. Robbins */
266d1016568SXin LI if (need == 0 && vflag != ALL &&
267b001517fSMatthew Dillon valid_save &&
268b001517fSMatthew Dillon bcmp(curp, savp, nread) == 0) {
269272144abSPoul-Henning Kamp if (vflag != DUP) {
2709b50d902SRodney W. Grimes (void)printf("*\n");
271272144abSPoul-Henning Kamp (void)fflush(stdout);
272272144abSPoul-Henning Kamp }
2739b50d902SRodney W. Grimes return((u_char *)NULL);
2749b50d902SRodney W. Grimes }
2759b50d902SRodney W. Grimes bzero((char *)curp + nread, need);
2769b50d902SRodney W. Grimes eaddress = address + nread;
2779b50d902SRodney W. Grimes return(curp);
2789b50d902SRodney W. Grimes }
2799b50d902SRodney W. Grimes n = fread((char *)curp + nread, sizeof(u_char),
2809b50d902SRodney W. Grimes length == -1 ? need : MIN(length, need), stdin);
2819b50d902SRodney W. Grimes if (!n) {
2829b50d902SRodney W. Grimes if (ferror(stdin))
28310731702SPhilippe Charnier warn("%s", _argv[-1]);
2849b50d902SRodney W. Grimes ateof = 1;
2859b50d902SRodney W. Grimes continue;
2869b50d902SRodney W. Grimes }
2879b50d902SRodney W. Grimes ateof = 0;
2889b50d902SRodney W. Grimes if (length != -1)
2899b50d902SRodney W. Grimes length -= n;
2909b50d902SRodney W. Grimes if (!(need -= n)) {
29140ccfb31STim J. Robbins /*
29240ccfb31STim J. Robbins * XXX bcmp() is not quite right in the presence
29340ccfb31STim J. Robbins * of multibyte characters.
29440ccfb31STim J. Robbins */
2959b50d902SRodney W. Grimes if (vflag == ALL || vflag == FIRST ||
296b001517fSMatthew Dillon valid_save == 0 ||
297b001517fSMatthew Dillon bcmp(curp, savp, blocksize) != 0) {
2989b50d902SRodney W. Grimes if (vflag == DUP || vflag == FIRST)
2999b50d902SRodney W. Grimes vflag = WAIT;
3009b50d902SRodney W. Grimes return(curp);
3019b50d902SRodney W. Grimes }
302272144abSPoul-Henning Kamp if (vflag == WAIT) {
3039b50d902SRodney W. Grimes (void)printf("*\n");
304272144abSPoul-Henning Kamp (void)fflush(stdout);
305272144abSPoul-Henning Kamp }
3069b50d902SRodney W. Grimes vflag = DUP;
3079b50d902SRodney W. Grimes address += blocksize;
3089b50d902SRodney W. Grimes need = blocksize;
3099b50d902SRodney W. Grimes nread = 0;
3109b50d902SRodney W. Grimes }
3119b50d902SRodney W. Grimes else
3129b50d902SRodney W. Grimes nread += n;
3139b50d902SRodney W. Grimes }
3149b50d902SRodney W. Grimes }
3159b50d902SRodney W. Grimes
31640ccfb31STim J. Robbins size_t
peek(u_char * buf,size_t nbytes)31740ccfb31STim J. Robbins peek(u_char *buf, size_t nbytes)
31840ccfb31STim J. Robbins {
31940ccfb31STim J. Robbins size_t n, nread;
32040ccfb31STim J. Robbins int c;
32140ccfb31STim J. Robbins
322aae01d24SJohan Karlsson if (length != -1 && nbytes > (unsigned int)length)
32340ccfb31STim J. Robbins nbytes = length;
32440ccfb31STim J. Robbins nread = 0;
32540ccfb31STim J. Robbins while (nread < nbytes && (c = getchar()) != EOF) {
32640ccfb31STim J. Robbins *buf++ = c;
32740ccfb31STim J. Robbins nread++;
32840ccfb31STim J. Robbins }
32940ccfb31STim J. Robbins n = nread;
33040ccfb31STim J. Robbins while (n-- > 0) {
33140ccfb31STim J. Robbins c = *--buf;
33240ccfb31STim J. Robbins ungetc(c, stdin);
33340ccfb31STim J. Robbins }
33440ccfb31STim J. Robbins return (nread);
33540ccfb31STim J. Robbins }
33640ccfb31STim J. Robbins
3379b50d902SRodney W. Grimes int
next(char ** argv)338f4ac32deSDavid Malone next(char **argv)
3399b50d902SRodney W. Grimes {
3409b50d902SRodney W. Grimes static int done;
3419b50d902SRodney W. Grimes int statok;
3429b50d902SRodney W. Grimes
3439b50d902SRodney W. Grimes if (argv) {
3449b50d902SRodney W. Grimes _argv = argv;
3459b50d902SRodney W. Grimes return(1);
3469b50d902SRodney W. Grimes }
3479b50d902SRodney W. Grimes for (;;) {
3489b50d902SRodney W. Grimes if (*_argv) {
3499a1e2d06STim J. Robbins done = 1;
3509b50d902SRodney W. Grimes if (!(freopen(*_argv, "r", stdin))) {
35110731702SPhilippe Charnier warn("%s", *_argv);
3529b50d902SRodney W. Grimes exitval = 1;
3539b50d902SRodney W. Grimes ++_argv;
3549b50d902SRodney W. Grimes continue;
3559b50d902SRodney W. Grimes }
3569a1e2d06STim J. Robbins statok = 1;
3579b50d902SRodney W. Grimes } else {
3589b50d902SRodney W. Grimes if (done++)
3599b50d902SRodney W. Grimes return(0);
3609b50d902SRodney W. Grimes statok = 0;
3619b50d902SRodney W. Grimes }
362327240c7SConrad Meyer
363327240c7SConrad Meyer if (caph_limit_stream(fileno(stdin), CAPH_READ) < 0)
364327240c7SConrad Meyer err(1, "unable to restrict %s",
365e47ea033SEd Maste statok ? *_argv : "stdin");
366327240c7SConrad Meyer
367327240c7SConrad Meyer /*
368327240c7SConrad Meyer * We've opened our last input file; enter capsicum sandbox.
369327240c7SConrad Meyer */
370e47ea033SEd Maste if (statok == 0 || *(_argv + 1) == NULL) {
3717672a014SMariusz Zaborski if (caph_enter() < 0)
372327240c7SConrad Meyer err(1, "unable to enter capability mode");
373327240c7SConrad Meyer }
374327240c7SConrad Meyer
3759b50d902SRodney W. Grimes if (skip)
3769b50d902SRodney W. Grimes doskip(statok ? *_argv : "stdin", statok);
3779b50d902SRodney W. Grimes if (*_argv)
3789b50d902SRodney W. Grimes ++_argv;
3799b50d902SRodney W. Grimes if (!skip)
3809b50d902SRodney W. Grimes return(1);
3819b50d902SRodney W. Grimes }
3829b50d902SRodney W. Grimes /* NOTREACHED */
3839b50d902SRodney W. Grimes }
3849b50d902SRodney W. Grimes
3859b50d902SRodney W. Grimes void
doskip(const char * fname,int statok)386f4ac32deSDavid Malone doskip(const char *fname, int statok)
3879b50d902SRodney W. Grimes {
388df7b0169SKyle Evans int type;
3899b50d902SRodney W. Grimes struct stat sb;
3909b50d902SRodney W. Grimes
3919b50d902SRodney W. Grimes if (statok) {
3929b50d902SRodney W. Grimes if (fstat(fileno(stdin), &sb))
39310731702SPhilippe Charnier err(1, "%s", fname);
394*e23954bdSRicardo Branco if (S_ISREG(sb.st_mode) && skip > sb.st_size && sb.st_size > 0) {
3959b50d902SRodney W. Grimes address += sb.st_size;
3969b50d902SRodney W. Grimes skip -= sb.st_size;
3979b50d902SRodney W. Grimes return;
3989b50d902SRodney W. Grimes }
3999b50d902SRodney W. Grimes }
400*e23954bdSRicardo Branco if (!statok || S_ISFIFO(sb.st_mode) || S_ISSOCK(sb.st_mode) || \
401*e23954bdSRicardo Branco (S_ISREG(sb.st_mode) && sb.st_size == 0)) {
402df7b0169SKyle Evans noseek();
403df7b0169SKyle Evans return;
404df7b0169SKyle Evans }
405df7b0169SKyle Evans if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
406df7b0169SKyle Evans if (ioctl(fileno(stdin), FIODTYPE, &type))
40710731702SPhilippe Charnier err(1, "%s", fname);
408df7b0169SKyle Evans /*
409df7b0169SKyle Evans * Most tape drives don't support seeking,
410df7b0169SKyle Evans * yet fseek() would succeed.
411df7b0169SKyle Evans */
412df7b0169SKyle Evans if (type & D_TAPE) {
413df7b0169SKyle Evans noseek();
414df7b0169SKyle Evans return;
415df7b0169SKyle Evans }
416df7b0169SKyle Evans }
417df7b0169SKyle Evans if (fseeko(stdin, skip, SEEK_SET)) {
418df7b0169SKyle Evans noseek();
419df7b0169SKyle Evans return;
420df7b0169SKyle Evans }
4219b50d902SRodney W. Grimes address += skip;
4229b50d902SRodney W. Grimes skip = 0;
423df7b0169SKyle Evans }
424df7b0169SKyle Evans
425df7b0169SKyle Evans static void
noseek(void)426df7b0169SKyle Evans noseek(void)
427df7b0169SKyle Evans {
428df7b0169SKyle Evans int count;
429df7b0169SKyle Evans for (count = 0; count < skip; ++count)
4309b50d902SRodney W. Grimes if (getchar() == EOF)
4319b50d902SRodney W. Grimes break;
432df7b0169SKyle Evans address += count;
433df7b0169SKyle Evans skip -= count;
4349b50d902SRodney W. Grimes }
435