]> git.ipfire.org Git - thirdparty/util-linux.git/blob - text-utils/display.c
Imported from util-linux-2.10s tarball.
[thirdparty/util-linux.git] / text-utils / display.c
1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <ctype.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include "hexdump.h"
43
44 static void doskip(char *, int);
45 static u_char *get(void);
46
47 #ifndef MIN
48 #define MIN(a,b) ((a)<(b)?(a):(b))
49 #endif
50
51 enum _vflag vflag = FIRST;
52
53 static off_t address; /* address/offset in stream */
54 static off_t eaddress; /* end address */
55 static off_t savaddress; /* saved address/offset in stream */
56
57 #define PRINT { \
58 switch(pr->flags) { \
59 case F_ADDRESS: \
60 (void)printf(pr->fmt, address); \
61 break; \
62 case F_BPAD: \
63 (void)printf(pr->fmt, ""); \
64 break; \
65 case F_C: \
66 conv_c(pr, bp); \
67 break; \
68 case F_CHAR: \
69 (void)printf(pr->fmt, *bp); \
70 break; \
71 case F_DBL: { \
72 double dval; \
73 float fval; \
74 switch(pr->bcnt) { \
75 case 4: \
76 bcopy((char *)bp, (char *)&fval, sizeof(fval)); \
77 (void)printf(pr->fmt, fval); \
78 break; \
79 case 8: \
80 bcopy((char *)bp, (char *)&dval, sizeof(dval)); \
81 (void)printf(pr->fmt, dval); \
82 break; \
83 } \
84 break; \
85 } \
86 case F_INT: { \
87 int ival; \
88 short sval; \
89 switch(pr->bcnt) { \
90 case 1: \
91 (void)printf(pr->fmt, (int)*bp); \
92 break; \
93 case 2: \
94 bcopy((char *)bp, (char *)&sval, sizeof(sval)); \
95 (void)printf(pr->fmt, (int)sval); \
96 break; \
97 case 4: \
98 bcopy((char *)bp, (char *)&ival, sizeof(ival)); \
99 (void)printf(pr->fmt, ival); \
100 break; \
101 } \
102 break; \
103 } \
104 case F_P: \
105 (void)printf(pr->fmt, isprint(*bp) ? *bp : '.'); \
106 break; \
107 case F_STR: \
108 (void)printf(pr->fmt, (char *)bp); \
109 break; \
110 case F_TEXT: \
111 (void)printf(pr->fmt); \
112 break; \
113 case F_U: \
114 conv_u(pr, bp); \
115 break; \
116 case F_UINT: { \
117 u_int ival; \
118 u_short sval; \
119 switch(pr->bcnt) { \
120 case 1: \
121 (void)printf(pr->fmt, (u_int)*bp); \
122 break; \
123 case 2: \
124 bcopy((char *)bp, (char *)&sval, sizeof(sval)); \
125 (void)printf(pr->fmt, (u_int)sval); \
126 break; \
127 case 4: \
128 bcopy((char *)bp, (char *)&ival, sizeof(ival)); \
129 (void)printf(pr->fmt, ival); \
130 break; \
131 } \
132 break; \
133 } \
134 } \
135 }
136
137 static void bpad(PR *pr)
138 {
139 static char *spec = " -0+#";
140 register char *p1, *p2;
141
142 /*
143 * remove all conversion flags; '-' is the only one valid
144 * with %s, and it's not useful here.
145 */
146 pr->flags = F_BPAD;
147 *pr->cchar = 's';
148 for (p1 = pr->fmt; *p1 != '%'; ++p1);
149 for (p2 = ++p1; *p1 && index(spec, *p1); ++p1);
150 while ((*p2++ = *p1++) != 0) ;
151 }
152
153 void display(void)
154 {
155 extern FU *endfu;
156 register FS *fs;
157 register FU *fu;
158 register PR *pr;
159 register int cnt;
160 register u_char *bp;
161 off_t saveaddress;
162 u_char savech = 0, *savebp;
163
164 while ((bp = get()) != NULL)
165 for (fs = fshead, savebp = bp, saveaddress = address; fs;
166 fs = fs->nextfs, bp = savebp, address = saveaddress)
167 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
168 if (fu->flags&F_IGNORE)
169 break;
170 for (cnt = fu->reps; cnt; --cnt)
171 for (pr = fu->nextpr; pr; address += pr->bcnt,
172 bp += pr->bcnt, pr = pr->nextpr) {
173 if (eaddress && address >= eaddress &&
174 !(pr->flags&(F_TEXT|F_BPAD)))
175 bpad(pr);
176 if (cnt == 1 && pr->nospace) {
177 savech = *pr->nospace;
178 *pr->nospace = '\0';
179 }
180 PRINT;
181 if (cnt == 1 && pr->nospace)
182 *pr->nospace = savech;
183 }
184 }
185 if (endfu) {
186 /*
187 * if eaddress not set, error or file size was multiple of
188 * blocksize, and no partial block ever found.
189 */
190 if (!eaddress) {
191 if (!address)
192 return;
193 eaddress = address;
194 }
195 for (pr = endfu->nextpr; pr; pr = pr->nextpr)
196 switch(pr->flags) {
197 case F_ADDRESS:
198 (void)printf(pr->fmt, eaddress);
199 break;
200 case F_TEXT:
201 (void)printf(pr->fmt);
202 break;
203 }
204 }
205 }
206
207 static char **_argv;
208
209 static u_char *
210 get(void)
211 {
212 extern int length;
213 static int ateof = 1;
214 static u_char *curp, *savp;
215 register int n;
216 int need, nread;
217 u_char *tmpp;
218
219 if (!curp) {
220 curp = (u_char *)emalloc(blocksize);
221 savp = (u_char *)emalloc(blocksize);
222 } else {
223 tmpp = curp;
224 curp = savp;
225 savp = tmpp;
226 address = savaddress += blocksize;
227 }
228 for (need = blocksize, nread = 0;;) {
229 /*
230 * if read the right number of bytes, or at EOF for one file,
231 * and no other files are available, zero-pad the rest of the
232 * block and set the end flag.
233 */
234 if (!length || (ateof && !next((char **)NULL))) {
235 if (need == blocksize)
236 return((u_char *)NULL);
237 if (vflag != ALL && !bcmp(curp, savp, nread)) {
238 if (vflag != DUP)
239 (void)printf("*\n");
240 return((u_char *)NULL);
241 }
242 bzero((char *)curp + nread, need);
243 eaddress = address + nread;
244 return(curp);
245 }
246 n = fread((char *)curp + nread, sizeof(u_char),
247 length == -1 ? need : MIN(length, need), stdin);
248 if (!n) {
249 if (ferror(stdin))
250 (void)fprintf(stderr, "hexdump: %s: %s\n",
251 _argv[-1], strerror(errno));
252 ateof = 1;
253 continue;
254 }
255 ateof = 0;
256 if (length != -1)
257 length -= n;
258 if (!(need -= n)) {
259 if (vflag == ALL || vflag == FIRST ||
260 bcmp(curp, savp, blocksize)) {
261 if (vflag == DUP || vflag == FIRST)
262 vflag = WAIT;
263 return(curp);
264 }
265 if (vflag == WAIT)
266 (void)printf("*\n");
267 vflag = DUP;
268 address = savaddress += blocksize;
269 need = blocksize;
270 nread = 0;
271 }
272 else
273 nread += n;
274 }
275 }
276
277 int next(char **argv)
278 {
279 extern int exitval;
280 static int done;
281 int statok;
282
283 if (argv) {
284 _argv = argv;
285 return(1);
286 }
287 for (;;) {
288 if (*_argv) {
289 if (!(freopen(*_argv, "r", stdin))) {
290 (void)fprintf(stderr, "hexdump: %s: %s\n",
291 *_argv, strerror(errno));
292 exitval = 1;
293 ++_argv;
294 continue;
295 }
296 statok = done = 1;
297 } else {
298 if (done++)
299 return(0);
300 statok = 0;
301 }
302 if (skip)
303 doskip(statok ? *_argv : "stdin", statok);
304 if (*_argv)
305 ++_argv;
306 if (!skip)
307 return(1);
308 }
309 /* NOTREACHED */
310 }
311
312 static void
313 doskip(char *fname, int statok)
314 {
315 struct stat sbuf;
316
317 if (statok) {
318 if (fstat(fileno(stdin), &sbuf)) {
319 (void)fprintf(stderr, "hexdump: %s: %s.\n",
320 fname, strerror(errno));
321 exit(1);
322 }
323 if ( ( ! (S_ISCHR(sbuf.st_mode) ||
324 S_ISBLK(sbuf.st_mode) ||
325 S_ISFIFO(sbuf.st_mode)) ) &&
326 skip >= sbuf.st_size) {
327 /* If size valid and skip >= size */
328 skip -= sbuf.st_size;
329 address += sbuf.st_size;
330 return;
331 }
332 }
333 if (fseek(stdin, skip, SEEK_SET)) {
334 (void)fprintf(stderr, "hexdump: %s: %s.\n",
335 fname, strerror(errno));
336 exit(1);
337 }
338 savaddress = address += skip;
339 skip = 0;
340 }
341
342 char *
343 emalloc(int sz) {
344 char *p;
345
346 if (!(p = malloc((u_int)sz)))
347 nomem();
348 bzero(p, sz);
349 return(p);
350 }
351
352 void nomem() {
353 (void)fprintf(stderr, "hexdump: %s.\n", strerror(errno));
354 exit(1);
355 }