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 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/capsicum.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 *);
70 u_char savech, *savebp;
74 for (fs = fshead, savebp = bp, saveaddress = address; fs;
75 fs = fs->nextfs, bp = savebp, address = saveaddress)
76 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
77 if (fu->flags&F_IGNORE)
79 for (cnt = fu->reps; cnt; --cnt)
80 for (pr = fu->nextpr; pr; address += pr->bcnt,
81 bp += pr->bcnt, pr = pr->nextpr) {
82 if (eaddress && address >= eaddress &&
83 !(pr->flags & (F_TEXT|F_BPAD)))
85 if (cnt == 1 && pr->nospace) {
86 savech = *pr->nospace;
90 if (cnt == 1 && pr->nospace)
91 *pr->nospace = savech;
96 * If eaddress not set, error or file size was multiple of
97 * blocksize, and no partial block ever found.
104 for (pr = endfu->nextpr; pr; pr = pr->nextpr)
107 (void)printf(pr->fmt, (quad_t)eaddress);
110 (void)printf("%s", pr->fmt);
117 print(PR *pr, u_char *bp)
131 (void)printf(pr->fmt, (quad_t)address);
134 (void)printf(pr->fmt, "");
137 conv_c(pr, bp, eaddress ? eaddress - address :
138 blocksize - address % blocksize);
141 (void)printf(pr->fmt, *bp);
146 bcopy(bp, &f4, sizeof(f4));
147 (void)printf(pr->fmt, f4);
150 bcopy(bp, &f8, sizeof(f8));
151 (void)printf(pr->fmt, f8);
154 if (pr->bcnt == sizeof(long double)) {
155 bcopy(bp, &ldbl, sizeof(ldbl));
156 (void)printf(pr->fmt, ldbl);
164 (void)printf(pr->fmt, (quad_t)(signed char)*bp);
167 bcopy(bp, &s2, sizeof(s2));
168 (void)printf(pr->fmt, (quad_t)s2);
171 bcopy(bp, &s4, sizeof(s4));
172 (void)printf(pr->fmt, (quad_t)s4);
175 bcopy(bp, &s8, sizeof(s8));
176 (void)printf(pr->fmt, s8);
181 (void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
184 (void)printf(pr->fmt, (char *)bp);
187 (void)printf("%s", pr->fmt);
195 (void)printf(pr->fmt, (u_quad_t)*bp);
198 bcopy(bp, &u2, sizeof(u2));
199 (void)printf(pr->fmt, (u_quad_t)u2);
202 bcopy(bp, &u4, sizeof(u4));
203 (void)printf(pr->fmt, (u_quad_t)u4);
206 bcopy(bp, &u8, sizeof(u8));
207 (void)printf(pr->fmt, u8);
217 static char const *spec = " -0+#";
221 * Remove all conversion flags; '-' is the only one valid
222 * with %s, and it's not useful here.
227 for (p1 = pr->fmt; *p1 != '%'; ++p1);
228 for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1);
229 while ((*p2++ = *p1++));
237 static int ateof = 1;
238 static u_char *curp, *savp;
245 if ((curp = calloc(1, blocksize)) == NULL)
247 if ((savp = calloc(1, blocksize)) == NULL)
253 address += blocksize;
256 for (need = blocksize, nread = 0;;) {
258 * if read the right number of bytes, or at EOF for one file,
259 * and no other files are available, zero-pad the rest of the
260 * block and set the end flag.
262 if (!length || (ateof && !next((char **)NULL))) {
263 if (odmode && address < skip)
264 errx(1, "cannot skip past end of input");
265 if (need == blocksize)
266 return((u_char *)NULL);
268 * XXX bcmp() is not quite right in the presence
269 * of multibyte characters.
273 bcmp(curp, savp, nread) == 0) {
276 return((u_char *)NULL);
278 bzero((char *)curp + nread, need);
279 eaddress = address + nread;
282 n = fread((char *)curp + nread, sizeof(u_char),
283 length == -1 ? need : MIN(length, need), stdin);
286 warn("%s", _argv[-1]);
295 * XXX bcmp() is not quite right in the presence
296 * of multibyte characters.
298 if (vflag == ALL || vflag == FIRST ||
300 bcmp(curp, savp, blocksize) != 0) {
301 if (vflag == DUP || vflag == FIRST)
308 address += blocksize;
318 peek(u_char *buf, size_t nbytes)
323 if (length != -1 && nbytes > (unsigned int)length)
326 while (nread < nbytes && (c = getchar()) != EOF) {
351 if (!(freopen(*_argv, "r", stdin))) {
364 if (caph_limit_stream(fileno(stdin), CAPH_READ) < 0)
365 err(1, "unable to restrict %s",
366 statok ? *_argv : "stdin");
369 * We've opened our last input file; enter capsicum sandbox.
371 if (statok == 0 || *(_argv + 1) == NULL) {
372 if (cap_enter() < 0 && errno != ENOSYS)
373 err(1, "unable to enter capability mode");
377 doskip(statok ? *_argv : "stdin", statok);
387 doskip(const char *fname, int statok)
393 if (fstat(fileno(stdin), &sb))
395 if (S_ISREG(sb.st_mode) && skip > sb.st_size) {
396 address += sb.st_size;
401 if (statok && S_ISREG(sb.st_mode)) {
402 if (fseeko(stdin, skip, SEEK_SET))
407 for (cnt = 0; cnt < skip; ++cnt)
408 if (getchar() == EOF)