2 * SPDX-License-Identifier: BSD-3-Clause
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
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
34 static char sccsid[] = "@(#)display.c 8.1 (Berkeley) 6/6/93";
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
41 #include <sys/ioctl.h>
44 #include <capsicum_helpers.h>
54 enum _vflag vflag = FIRST;
56 static off_t address; /* address/offset in stream */
57 static off_t eaddress; /* end address */
59 static void print(PR *, u_char *);
60 static void noseek(void);
71 u_char savech, *savebp;
75 for (fs = fshead, savebp = bp, saveaddress = address; fs;
76 fs = fs->nextfs, bp = savebp, address = saveaddress)
77 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
78 if (fu->flags&F_IGNORE)
80 for (cnt = fu->reps; cnt; --cnt)
81 for (pr = fu->nextpr; pr; address += pr->bcnt,
82 bp += pr->bcnt, pr = pr->nextpr) {
83 if (eaddress && address >= eaddress &&
84 !(pr->flags & (F_TEXT|F_BPAD)))
86 if (cnt == 1 && pr->nospace) {
87 savech = *pr->nospace;
91 if (cnt == 1 && pr->nospace)
92 *pr->nospace = savech;
97 * If eaddress not set, error or file size was multiple of
98 * blocksize, and no partial block ever found.
105 for (pr = endfu->nextpr; pr; pr = pr->nextpr)
108 (void)printf(pr->fmt, (quad_t)eaddress);
111 (void)printf("%s", pr->fmt);
118 print(PR *pr, u_char *bp)
132 (void)printf(pr->fmt, (quad_t)address);
135 (void)printf(pr->fmt, "");
138 conv_c(pr, bp, eaddress ? eaddress - address :
139 blocksize - address % blocksize);
142 (void)printf(pr->fmt, *bp);
147 bcopy(bp, &f4, sizeof(f4));
148 (void)printf(pr->fmt, f4);
151 bcopy(bp, &f8, sizeof(f8));
152 (void)printf(pr->fmt, f8);
155 if (pr->bcnt == sizeof(long double)) {
156 bcopy(bp, &ldbl, sizeof(ldbl));
157 (void)printf(pr->fmt, ldbl);
165 (void)printf(pr->fmt, (quad_t)(signed char)*bp);
168 bcopy(bp, &s2, sizeof(s2));
169 (void)printf(pr->fmt, (quad_t)s2);
172 bcopy(bp, &s4, sizeof(s4));
173 (void)printf(pr->fmt, (quad_t)s4);
176 bcopy(bp, &s8, sizeof(s8));
177 (void)printf(pr->fmt, s8);
182 (void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
185 (void)printf(pr->fmt, (char *)bp);
188 (void)printf("%s", pr->fmt);
196 (void)printf(pr->fmt, (u_quad_t)*bp);
199 bcopy(bp, &u2, sizeof(u2));
200 (void)printf(pr->fmt, (u_quad_t)u2);
203 bcopy(bp, &u4, sizeof(u4));
204 (void)printf(pr->fmt, (u_quad_t)u4);
207 bcopy(bp, &u8, sizeof(u8));
208 (void)printf(pr->fmt, u8);
218 static char const *spec = " -0+#";
222 * Remove all conversion flags; '-' is the only one valid
223 * with %s, and it's not useful here.
228 for (p1 = pr->fmt; *p1 != '%'; ++p1);
229 for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1);
230 while ((*p2++ = *p1++));
238 static int ateof = 1;
239 static u_char *curp, *savp;
246 if ((curp = calloc(1, blocksize)) == NULL)
248 if ((savp = calloc(1, blocksize)) == NULL)
254 address += blocksize;
257 for (need = blocksize, nread = 0;;) {
259 * if read the right number of bytes, or at EOF for one file,
260 * and no other files are available, zero-pad the rest of the
261 * block and set the end flag.
263 if (!length || (ateof && !next((char **)NULL))) {
264 if (odmode && skip > 0)
265 errx(1, "cannot skip past end of input");
266 if (need == blocksize)
267 return((u_char *)NULL);
269 * XXX bcmp() is not quite right in the presence
270 * of multibyte characters.
272 if (need == 0 && vflag != ALL &&
274 bcmp(curp, savp, nread) == 0) {
277 (void)fflush(stdout);
279 return((u_char *)NULL);
281 bzero((char *)curp + nread, need);
282 eaddress = address + nread;
285 n = fread((char *)curp + nread, sizeof(u_char),
286 length == -1 ? need : MIN(length, need), stdin);
289 warn("%s", _argv[-1]);
298 * XXX bcmp() is not quite right in the presence
299 * of multibyte characters.
301 if (vflag == ALL || vflag == FIRST ||
303 bcmp(curp, savp, blocksize) != 0) {
304 if (vflag == DUP || vflag == FIRST)
310 (void)fflush(stdout);
313 address += blocksize;
323 peek(u_char *buf, size_t nbytes)
328 if (length != -1 && nbytes > (unsigned int)length)
331 while (nread < nbytes && (c = getchar()) != EOF) {
356 if (!(freopen(*_argv, "r", stdin))) {
369 if (caph_limit_stream(fileno(stdin), CAPH_READ) < 0)
370 err(1, "unable to restrict %s",
371 statok ? *_argv : "stdin");
374 * We've opened our last input file; enter capsicum sandbox.
376 if (statok == 0 || *(_argv + 1) == NULL) {
377 if (caph_enter() < 0)
378 err(1, "unable to enter capability mode");
382 doskip(statok ? *_argv : "stdin", statok);
392 doskip(const char *fname, int statok)
398 if (fstat(fileno(stdin), &sb))
400 if (S_ISREG(sb.st_mode) && skip > sb.st_size) {
401 address += sb.st_size;
406 if (!statok || S_ISFIFO(sb.st_mode) || S_ISSOCK(sb.st_mode)) {
410 if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
411 if (ioctl(fileno(stdin), FIODTYPE, &type))
414 * Most tape drives don't support seeking,
415 * yet fseek() would succeed.
422 if (fseeko(stdin, skip, SEEK_SET)) {
434 for (count = 0; count < skip; ++count)
435 if (getchar() == EOF)