Home | History | Annotate | Line # | Download | only in hexdump
display.c revision 1.1
      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 #ifndef lint
     35 static char sccsid[] = "@(#)display.c	5.11 (Berkeley) 3/9/91";
     36 #endif /* not lint */
     37 
     38 #include <sys/param.h>
     39 #include <sys/stat.h>
     40 #include <unistd.h>
     41 #include <errno.h>
     42 #include <ctype.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include "hexdump.h"
     47 
     48 enum _vflag vflag = FIRST;
     49 
     50 static off_t address;			/* address/offset in stream */
     51 static off_t eaddress;			/* end address */
     52 static off_t savaddress;		/* saved address/offset in stream */
     53 
     54 #define PRINT { \
     55 	switch(pr->flags) { \
     56 	case F_ADDRESS: \
     57 		(void)printf(pr->fmt, address); \
     58 		break; \
     59 	case F_BPAD: \
     60 		(void)printf(pr->fmt, ""); \
     61 		break; \
     62 	case F_C: \
     63 		conv_c(pr, bp); \
     64 		break; \
     65 	case F_CHAR: \
     66 		(void)printf(pr->fmt, *bp); \
     67 		break; \
     68 	case F_DBL: { \
     69 		double dval; \
     70 		float fval; \
     71 		switch(pr->bcnt) { \
     72 		case 4: \
     73 			bcopy((char *)bp, (char *)&fval, sizeof(fval)); \
     74 			(void)printf(pr->fmt, fval); \
     75 			break; \
     76 		case 8: \
     77 			bcopy((char *)bp, (char *)&dval, sizeof(dval)); \
     78 			(void)printf(pr->fmt, dval); \
     79 			break; \
     80 		} \
     81 		break; \
     82 	} \
     83 	case F_INT: { \
     84 		int ival; \
     85 		short sval; \
     86 		switch(pr->bcnt) { \
     87 		case 1: \
     88 			(void)printf(pr->fmt, (int)*bp); \
     89 			break; \
     90 		case 2: \
     91 			bcopy((char *)bp, (char *)&sval, sizeof(sval)); \
     92 			(void)printf(pr->fmt, (int)sval); \
     93 			break; \
     94 		case 4: \
     95 			bcopy((char *)bp, (char *)&ival, sizeof(ival)); \
     96 			(void)printf(pr->fmt, ival); \
     97 			break; \
     98 		} \
     99 		break; \
    100 	} \
    101 	case F_P: \
    102 		(void)printf(pr->fmt, isprint(*bp) ? *bp : '.'); \
    103 		break; \
    104 	case F_STR: \
    105 		(void)printf(pr->fmt, (char *)bp); \
    106 		break; \
    107 	case F_TEXT: \
    108 		(void)printf(pr->fmt); \
    109 		break; \
    110 	case F_U: \
    111 		conv_u(pr, bp); \
    112 		break; \
    113 	case F_UINT: { \
    114 		u_int ival; \
    115 		u_short sval; \
    116 		switch(pr->bcnt) { \
    117 		case 1: \
    118 			(void)printf(pr->fmt, (u_int)*bp); \
    119 			break; \
    120 		case 2: \
    121 			bcopy((char *)bp, (char *)&sval, sizeof(sval)); \
    122 			(void)printf(pr->fmt, (u_int)sval); \
    123 			break; \
    124 		case 4: \
    125 			bcopy((char *)bp, (char *)&ival, sizeof(ival)); \
    126 			(void)printf(pr->fmt, ival); \
    127 			break; \
    128 		} \
    129 		break; \
    130 	} \
    131 	} \
    132 }
    133 
    134 display()
    135 {
    136 	extern FU *endfu;
    137 	register FS *fs;
    138 	register FU *fu;
    139 	register PR *pr;
    140 	register int cnt;
    141 	register u_char *bp;
    142 	off_t saveaddress;
    143 	u_char savech, *savebp, *get();
    144 
    145 	while (bp = get())
    146 	    for (fs = fshead, savebp = bp, saveaddress = address; fs;
    147 		fs = fs->nextfs, bp = savebp, address = saveaddress)
    148 		    for (fu = fs->nextfu; fu; fu = fu->nextfu) {
    149 			if (fu->flags&F_IGNORE)
    150 				break;
    151 			for (cnt = fu->reps; cnt; --cnt)
    152 			    for (pr = fu->nextpr; pr; address += pr->bcnt,
    153 				bp += pr->bcnt, pr = pr->nextpr) {
    154 				    if (eaddress && address >= eaddress &&
    155 					!(pr->flags&(F_TEXT|F_BPAD)))
    156 					    bpad(pr);
    157 				    if (cnt == 1 && pr->nospace) {
    158 					savech = *pr->nospace;
    159 					*pr->nospace = '\0';
    160 				    }
    161 				    PRINT;
    162 				    if (cnt == 1 && pr->nospace)
    163 					*pr->nospace = savech;
    164 			    }
    165 		    }
    166 	if (endfu) {
    167 		/*
    168 		 * if eaddress not set, error or file size was multiple of
    169 		 * blocksize, and no partial block ever found.
    170 		 */
    171 		if (!eaddress) {
    172 			if (!address)
    173 				return;
    174 			eaddress = address;
    175 		}
    176 		for (pr = endfu->nextpr; pr; pr = pr->nextpr)
    177 			switch(pr->flags) {
    178 			case F_ADDRESS:
    179 				(void)printf(pr->fmt, eaddress);
    180 				break;
    181 			case F_TEXT:
    182 				(void)printf(pr->fmt);
    183 				break;
    184 			}
    185 	}
    186 }
    187 
    188 bpad(pr)
    189 	PR *pr;
    190 {
    191 	static char *spec = " -0+#";
    192 	register char *p1, *p2;
    193 
    194 	/*
    195 	 * remove all conversion flags; '-' is the only one valid
    196 	 * with %s, and it's not useful here.
    197 	 */
    198 	pr->flags = F_BPAD;
    199 	*pr->cchar = 's';
    200 	for (p1 = pr->fmt; *p1 != '%'; ++p1);
    201 	for (p2 = ++p1; *p1 && index(spec, *p1); ++p1);
    202 	while (*p2++ = *p1++);
    203 }
    204 
    205 static char **_argv;
    206 
    207 u_char *
    208 get()
    209 {
    210 	extern enum _vflag vflag;
    211 	extern int length;
    212 	static int ateof = 1;
    213 	static u_char *curp, *savp;
    214 	register int n;
    215 	int need, nread;
    216 	u_char *tmpp;
    217 
    218 	if (!curp) {
    219 		curp = (u_char *)emalloc(blocksize);
    220 		savp = (u_char *)emalloc(blocksize);
    221 	} else {
    222 		tmpp = curp;
    223 		curp = savp;
    224 		savp = tmpp;
    225 		address = savaddress += blocksize;
    226 	}
    227 	for (need = blocksize, nread = 0;;) {
    228 		/*
    229 		 * if read the right number of bytes, or at EOF for one file,
    230 		 * and no other files are available, zero-pad the rest of the
    231 		 * block and set the end flag.
    232 		 */
    233 		if (!length || ateof && !next((char **)NULL)) {
    234 			if (need == blocksize)
    235 				return((u_char *)NULL);
    236 			if (vflag != ALL && !bcmp(curp, savp, nread)) {
    237 				if (vflag != DUP)
    238 					(void)printf("*\n");
    239 				return((u_char *)NULL);
    240 			}
    241 			bzero((char *)curp + nread, need);
    242 			eaddress = address + nread;
    243 			return(curp);
    244 		}
    245 		n = fread((char *)curp + nread, sizeof(u_char),
    246 		    length == -1 ? need : MIN(length, need), stdin);
    247 		if (!n) {
    248 			if (ferror(stdin))
    249 				(void)fprintf(stderr, "hexdump: %s: %s\n",
    250 				    _argv[-1], strerror(errno));
    251 			ateof = 1;
    252 			continue;
    253 		}
    254 		ateof = 0;
    255 		if (length != -1)
    256 			length -= n;
    257 		if (!(need -= n)) {
    258 			if (vflag == ALL || vflag == FIRST ||
    259 			    bcmp(curp, savp, blocksize)) {
    260 				if (vflag == DUP || vflag == FIRST)
    261 					vflag = WAIT;
    262 				return(curp);
    263 			}
    264 			if (vflag == WAIT)
    265 				(void)printf("*\n");
    266 			vflag = DUP;
    267 			address = savaddress += blocksize;
    268 			need = blocksize;
    269 			nread = 0;
    270 		}
    271 		else
    272 			nread += n;
    273 	}
    274 }
    275 
    276 extern off_t skip;			/* bytes to skip */
    277 
    278 next(argv)
    279 	char **argv;
    280 {
    281 	extern int errno, exitval;
    282 	static int done;
    283 	int statok;
    284 
    285 	if (argv) {
    286 		_argv = argv;
    287 		return(1);
    288 	}
    289 	for (;;) {
    290 		if (*_argv) {
    291 			if (!(freopen(*_argv, "r", stdin))) {
    292 				(void)fprintf(stderr, "hexdump: %s: %s\n",
    293 				    *_argv, strerror(errno));
    294 				exitval = 1;
    295 				++_argv;
    296 				continue;
    297 			}
    298 			statok = done = 1;
    299 		} else {
    300 			if (done++)
    301 				return(0);
    302 			statok = 0;
    303 		}
    304 		if (skip)
    305 			doskip(statok ? *_argv : "stdin", statok);
    306 		if (*_argv)
    307 			++_argv;
    308 		if (!skip)
    309 			return(1);
    310 	}
    311 	/* NOTREACHED */
    312 }
    313 
    314 doskip(fname, statok)
    315 	char *fname;
    316 	int statok;
    317 {
    318 	extern int errno;
    319 	struct stat sbuf;
    320 
    321 	if (statok) {
    322 		if (fstat(fileno(stdin), &sbuf)) {
    323 			(void)fprintf(stderr, "hexdump: %s: %s.\n",
    324 			    fname, strerror(errno));
    325 			exit(1);
    326 		}
    327 		if (skip >= sbuf.st_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(size)
    344 	int size;
    345 {
    346 	char *p;
    347 
    348 	if (!(p = malloc((u_int)size)))
    349 		nomem();
    350 	bzero(p, size);
    351 	return(p);
    352 }
    353 
    354 nomem()
    355 {
    356 	extern int errno;
    357 
    358 	(void)fprintf(stderr, "hexdump: %s.\n", strerror(errno));
    359 	exit(1);
    360 }
    361