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