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
32 #include <sys/param.h>
33 #include <sys/capsicum.h>
35 #include <sys/ioctl.h>
38 #include <capsicum_helpers.h>
48 enum _vflag vflag = FIRST;
50 static off_t address; /* address/offset in stream */
51 static off_t eaddress; /* end address */
53 static void print(PR *, u_char *);
54 static void noseek(void);
65 u_char savech, *savebp;
69 for (fs = fshead, savebp = bp, saveaddress = address; fs;
70 fs = fs->nextfs, bp = savebp, address = saveaddress)
71 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
72 if (fu->flags&F_IGNORE)
74 for (cnt = fu->reps; cnt; --cnt)
75 for (pr = fu->nextpr; pr; address += pr->bcnt,
76 bp += pr->bcnt, pr = pr->nextpr) {
77 if (eaddress && address >= eaddress &&
78 !(pr->flags & (F_TEXT|F_BPAD)))
80 if (cnt == 1 && pr->nospace) {
81 savech = *pr->nospace;
85 if (cnt == 1 && pr->nospace)
86 *pr->nospace = savech;
91 * If eaddress not set, error or file size was multiple of
92 * blocksize, and no partial block ever found.
99 for (pr = endfu->nextpr; pr; pr = pr->nextpr)
102 (void)printf(pr->fmt, (quad_t)eaddress);
105 (void)printf("%s", pr->fmt);
112 print(PR *pr, u_char *bp)
126 (void)printf(pr->fmt, (quad_t)address);
129 (void)printf(pr->fmt, "");
132 conv_c(pr, bp, eaddress ? eaddress - address :
133 blocksize - address % blocksize);
136 (void)printf(pr->fmt, *bp);
141 bcopy(bp, &f4, sizeof(f4));
142 (void)printf(pr->fmt, f4);
145 bcopy(bp, &f8, sizeof(f8));
146 (void)printf(pr->fmt, f8);
149 if (pr->bcnt == sizeof(long double)) {
150 bcopy(bp, &ldbl, sizeof(ldbl));
151 (void)printf(pr->fmt, ldbl);
159 (void)printf(pr->fmt, (quad_t)(signed char)*bp);
162 bcopy(bp, &s2, sizeof(s2));
163 (void)printf(pr->fmt, (quad_t)s2);
166 bcopy(bp, &s4, sizeof(s4));
167 (void)printf(pr->fmt, (quad_t)s4);
170 bcopy(bp, &s8, sizeof(s8));
171 (void)printf(pr->fmt, s8);
176 (void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
179 (void)printf(pr->fmt, (char *)bp);
182 (void)printf("%s", pr->fmt);
190 (void)printf(pr->fmt, (u_quad_t)*bp);
193 bcopy(bp, &u2, sizeof(u2));
194 (void)printf(pr->fmt, (u_quad_t)u2);
197 bcopy(bp, &u4, sizeof(u4));
198 (void)printf(pr->fmt, (u_quad_t)u4);
201 bcopy(bp, &u8, sizeof(u8));
202 (void)printf(pr->fmt, u8);
212 static char const *spec = " -0+#";
216 * Remove all conversion flags; '-' is the only one valid
217 * with %s, and it's not useful here.
222 for (p1 = pr->fmt; *p1 != '%'; ++p1);
223 for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1);
224 while ((*p2++ = *p1++));
232 static int ateof = 1;
233 static u_char *curp, *savp;
240 if ((curp = calloc(1, blocksize)) == NULL)
242 if ((savp = calloc(1, blocksize)) == NULL)
248 address += blocksize;
251 for (need = blocksize, nread = 0;;) {
253 * if read the right number of bytes, or at EOF for one file,
254 * and no other files are available, zero-pad the rest of the
255 * block and set the end flag.
257 if (!length || (ateof && !next((char **)NULL))) {
258 if (odmode && skip > 0)
259 errx(1, "cannot skip past end of input");
260 if (need == blocksize)
261 return((u_char *)NULL);
263 * XXX bcmp() is not quite right in the presence
264 * of multibyte characters.
266 if (need == 0 && vflag != ALL &&
268 bcmp(curp, savp, nread) == 0) {
271 (void)fflush(stdout);
273 return((u_char *)NULL);
275 bzero((char *)curp + nread, need);
276 eaddress = address + nread;
279 n = fread((char *)curp + nread, sizeof(u_char),
280 length == -1 ? need : MIN(length, need), stdin);
283 warn("%s", _argv[-1]);
292 * XXX bcmp() is not quite right in the presence
293 * of multibyte characters.
295 if (vflag == ALL || vflag == FIRST ||
297 bcmp(curp, savp, blocksize) != 0) {
298 if (vflag == DUP || vflag == FIRST)
304 (void)fflush(stdout);
307 address += blocksize;
317 peek(u_char *buf, size_t nbytes)
322 if (length != -1 && nbytes > (unsigned int)length)
325 while (nread < nbytes && (c = getchar()) != EOF) {
350 if (!(freopen(*_argv, "r", stdin))) {
363 if (caph_limit_stream(fileno(stdin), CAPH_READ) < 0)
364 err(1, "unable to restrict %s",
365 statok ? *_argv : "stdin");
368 * We've opened our last input file; enter capsicum sandbox.
370 if (statok == 0 || *(_argv + 1) == NULL) {
371 if (caph_enter() < 0)
372 err(1, "unable to enter capability mode");
376 doskip(statok ? *_argv : "stdin", statok);
386 doskip(const char *fname, int statok)
392 if (fstat(fileno(stdin), &sb))
394 if (S_ISREG(sb.st_mode) && skip > sb.st_size) {
395 address += sb.st_size;
400 if (!statok || S_ISFIFO(sb.st_mode) || S_ISSOCK(sb.st_mode)) {
404 if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
405 if (ioctl(fileno(stdin), FIODTYPE, &type))
408 * Most tape drives don't support seeking,
409 * yet fseek() would succeed.
416 if (fseeko(stdin, skip, SEEK_SET)) {
428 for (count = 0; count < skip; ++count)
429 if (getchar() == EOF)