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 * 3. 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
29 * @(#)display.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.bin/hexdump/display.c,v 1.4.2.2 2002/07/23 14:27:06 tjr Exp $
33 #include <sys/param.h>
44 enum _vflag vflag
= FIRST
;
46 static off_t address
; /* address/offset in stream */
47 static off_t eaddress
; /* end address */
49 static __inline
void print(PR
*, u_char
*);
52 print(PR
*pr
, u_char
*bp
)
66 (void)printf(pr
->fmt
, (quad_t
)address
);
69 (void)printf(pr
->fmt
, "");
75 (void)printf(pr
->fmt
, *bp
);
80 bcopy(bp
, &f4
, sizeof(f4
));
81 (void)printf(pr
->fmt
, f4
);
84 bcopy(bp
, &f8
, sizeof(f8
));
85 (void)printf(pr
->fmt
, f8
);
88 if (pr
->bcnt
== sizeof(long double)) {
89 bcopy(bp
, &ldbl
, sizeof(ldbl
));
90 (void)printf(pr
->fmt
, ldbl
);
98 (void)printf(pr
->fmt
, (quad_t
)(signed char)*bp
);
101 bcopy(bp
, &s2
, sizeof(s2
));
102 (void)printf(pr
->fmt
, (quad_t
)s2
);
105 bcopy(bp
, &s4
, sizeof(s4
));
106 (void)printf(pr
->fmt
, (quad_t
)s4
);
109 bcopy(bp
, &s8
, sizeof(s8
));
110 (void)printf(pr
->fmt
, s8
);
115 (void)printf(pr
->fmt
, isprint(*bp
) ? *bp
: '.');
118 (void)printf(pr
->fmt
, (char *)bp
);
121 (void)printf("%s", pr
->fmt
);
129 (void)printf(pr
->fmt
, (u_quad_t
)*bp
);
132 bcopy(bp
, &u2
, sizeof(u2
));
133 (void)printf(pr
->fmt
, (u_quad_t
)u2
);
136 bcopy(bp
, &u4
, sizeof(u4
));
137 (void)printf(pr
->fmt
, (u_quad_t
)u4
);
140 bcopy(bp
, &u8
, sizeof(u8
));
141 (void)printf(pr
->fmt
, u8
);
158 u_char savech
= '\0', *savebp
;
161 for (fs
= fshead
, savebp
= bp
, saveaddress
= address
; fs
;
162 fs
= fs
->nextfs
, bp
= savebp
, address
= saveaddress
)
163 for (fu
= fs
->nextfu
; fu
; fu
= fu
->nextfu
) {
164 if (fu
->flags
&F_IGNORE
)
166 for (cnt
= fu
->reps
; cnt
; --cnt
)
167 for (pr
= fu
->nextpr
; pr
; address
+= pr
->bcnt
,
168 bp
+= pr
->bcnt
, pr
= pr
->nextpr
) {
169 if (eaddress
&& address
>= eaddress
&&
170 !(pr
->flags
& (F_TEXT
|F_BPAD
)))
172 if (cnt
== 1 && pr
->nospace
) {
173 savech
= *pr
->nospace
;
177 if (cnt
== 1 && pr
->nospace
)
178 *pr
->nospace
= savech
;
183 * If eaddress not set, error or file size was multiple of
184 * blocksize, and no partial block ever found.
191 for (pr
= endfu
->nextpr
; pr
; pr
= pr
->nextpr
)
194 (void)printf(pr
->fmt
, (quad_t
)eaddress
);
197 (void)printf("%s", pr
->fmt
);
206 static char const *spec
= " -0+#";
210 * Remove all conversion flags; '-' is the only one valid
211 * with %s, and it's not useful here.
216 for (p1
= pr
->fmt
; *p1
!= '%'; ++p1
);
217 for (p2
= ++p1
; *p1
&& strchr(spec
, *p1
); ++p1
);
218 while ((*p2
++ = *p1
++));
226 static int ateof
= 1;
227 static u_char
*curp
, *savp
;
234 if ((curp
= calloc(1, blocksize
)) == NULL
)
236 if ((savp
= calloc(1, blocksize
)) == NULL
)
242 address
+= blocksize
;
245 for (need
= blocksize
, nread
= 0;;) {
247 * if read the right number of bytes, or at EOF for one file,
248 * and no other files are available, zero-pad the rest of the
249 * block and set the end flag.
251 if (!length
|| (ateof
&& !next(NULL
))) {
252 if (odmode
&& address
< skip
)
253 errx(1, "cannot skip past end of input");
254 if (need
== blocksize
)
258 bcmp(curp
, savp
, nread
) == 0) {
263 bzero((char *)curp
+ nread
, need
);
264 eaddress
= address
+ nread
;
267 n
= fread((char *)curp
+ nread
, sizeof(u_char
),
268 length
== -1 ? need
: MIN(length
, need
), stdin
);
271 warn("%s", _argv
[-1]);
279 if (vflag
== ALL
|| vflag
== FIRST
||
281 bcmp(curp
, savp
, blocksize
) != 0) {
282 if (vflag
== DUP
|| vflag
== FIRST
)
289 address
+= blocksize
;
309 if (!(freopen(*_argv
, "r", stdin
))) {
321 doskip(*_argv
? *_argv
: "stdin");
331 doskip(const char *fname
)
336 if (fstat(fileno(stdin
), &sb
))
339 /* Pseudo-filesystems advertize a zero size. */
340 if (S_ISREG(sb
.st_mode
) && sb
.st_size
> 0) {
341 if (skip
>= sb
.st_size
) {
342 address
+= sb
.st_size
;
345 if (fseeko(stdin
, skip
, SEEK_SET
))
351 for (cnt
= 0; cnt
< skip
; ++cnt
)
352 if (getchar() == EOF
)