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