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