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