2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)display.c 8.1 (Berkeley) 6/6/93";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
40 #include <sys/ioctl.h>
51 enum _vflag vflag = FIRST;
53 static off_t address; /* address/offset in stream */
54 static off_t eaddress; /* end address */
56 static void print(PR *, u_char *);
57 static void noseek(void);
68 u_char savech, *savebp;
72 for (fs = fshead, savebp = bp, saveaddress = address; fs;
73 fs = fs->nextfs, bp = savebp, address = saveaddress)
74 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
75 if (fu->flags&F_IGNORE)
77 for (cnt = fu->reps; cnt; --cnt)
78 for (pr = fu->nextpr; pr; address += pr->bcnt,
79 bp += pr->bcnt, pr = pr->nextpr) {
80 if (eaddress && address >= eaddress &&
81 !(pr->flags & (F_TEXT|F_BPAD)))
83 if (cnt == 1 && pr->nospace) {
84 savech = *pr->nospace;
88 if (cnt == 1 && pr->nospace)
89 *pr->nospace = savech;
94 * If eaddress not set, error or file size was multiple of
95 * blocksize, and no partial block ever found.
102 for (pr = endfu->nextpr; pr; pr = pr->nextpr)
105 (void)printf(pr->fmt, (quad_t)eaddress);
108 (void)printf("%s", pr->fmt);
115 print(PR *pr, u_char *bp)
129 (void)printf(pr->fmt, (quad_t)address);
132 (void)printf(pr->fmt, "");
135 conv_c(pr, bp, eaddress ? eaddress - address :
136 blocksize - address % blocksize);
139 (void)printf(pr->fmt, *bp);
144 bcopy(bp, &f4, sizeof(f4));
145 (void)printf(pr->fmt, f4);
148 bcopy(bp, &f8, sizeof(f8));
149 (void)printf(pr->fmt, f8);
152 if (pr->bcnt == sizeof(long double)) {
153 bcopy(bp, &ldbl, sizeof(ldbl));
154 (void)printf(pr->fmt, ldbl);
162 (void)printf(pr->fmt, (quad_t)(signed char)*bp);
165 bcopy(bp, &s2, sizeof(s2));
166 (void)printf(pr->fmt, (quad_t)s2);
169 bcopy(bp, &s4, sizeof(s4));
170 (void)printf(pr->fmt, (quad_t)s4);
173 bcopy(bp, &s8, sizeof(s8));
174 (void)printf(pr->fmt, s8);
179 (void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
182 (void)printf(pr->fmt, (char *)bp);
185 (void)printf("%s", pr->fmt);
193 (void)printf(pr->fmt, (u_quad_t)*bp);
196 bcopy(bp, &u2, sizeof(u2));
197 (void)printf(pr->fmt, (u_quad_t)u2);
200 bcopy(bp, &u4, sizeof(u4));
201 (void)printf(pr->fmt, (u_quad_t)u4);
204 bcopy(bp, &u8, sizeof(u8));
205 (void)printf(pr->fmt, u8);
215 static char const *spec = " -0+#";
219 * Remove all conversion flags; '-' is the only one valid
220 * with %s, and it's not useful here.
225 for (p1 = pr->fmt; *p1 != '%'; ++p1);
226 for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1);
227 while ((*p2++ = *p1++));
235 static int ateof = 1;
236 static u_char *curp, *savp;
243 if ((curp = calloc(1, blocksize)) == NULL)
245 if ((savp = calloc(1, blocksize)) == NULL)
251 address += blocksize;
254 for (need = blocksize, nread = 0;;) {
256 * if read the right number of bytes, or at EOF for one file,
257 * and no other files are available, zero-pad the rest of the
258 * block and set the end flag.
260 if (!length || (ateof && !next((char **)NULL))) {
261 if (odmode && address < skip)
262 errx(1, "cannot skip past end of input");
263 if (need == blocksize)
264 return((u_char *)NULL);
266 * XXX bcmp() is not quite right in the presence
267 * of multibyte characters.
271 bcmp(curp, savp, nread) == 0) {
274 return((u_char *)NULL);
276 bzero((char *)curp + nread, need);
277 eaddress = address + nread;
280 n = fread((char *)curp + nread, sizeof(u_char),
281 length == -1 ? need : MIN(length, need), stdin);
284 warn("%s", _argv[-1]);
293 * XXX bcmp() is not quite right in the presence
294 * of multibyte characters.
296 if (vflag == ALL || vflag == FIRST ||
298 bcmp(curp, savp, blocksize) != 0) {
299 if (vflag == DUP || vflag == FIRST)
306 address += blocksize;
316 peek(u_char *buf, size_t nbytes)
321 if (length != -1 && nbytes > (unsigned int)length)
324 while (nread < nbytes && (c = getchar()) != EOF) {
349 if (!(freopen(*_argv, "r", stdin))) {
362 doskip(statok ? *_argv : "stdin", statok);
372 doskip(const char *fname, int statok)
378 if (fstat(fileno(stdin), &sb))
380 if (S_ISREG(sb.st_mode) && skip > sb.st_size) {
381 address += sb.st_size;
386 if (!statok || S_ISFIFO(sb.st_mode) || S_ISSOCK(sb.st_mode)) {
390 if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
391 if (ioctl(fileno(stdin), FIODTYPE, &type))
394 * Most tape drives don't support seeking,
395 * yet fseek() would succeed.
402 if (fseeko(stdin, skip, SEEK_SET)) {
414 for (count = 0; count < skip; ++count)
415 if (getchar() == EOF)