xref: /csrg-svn/usr.bin/hexdump/display.c (revision 38861)
138855Sbostic /*
238855Sbostic  * Copyright (c) 1989 The Regents of the University of California.
338855Sbostic  * All rights reserved.
438855Sbostic  *
538855Sbostic  * Redistribution and use in source and binary forms are permitted
638855Sbostic  * provided that the above copyright notice and this paragraph are
738855Sbostic  * duplicated in all such forms and that any documentation,
838855Sbostic  * advertising materials, and other materials related to such
938855Sbostic  * distribution and use acknowledge that the software was developed
1038855Sbostic  * by the University of California, Berkeley.  The name of the
1138855Sbostic  * University may not be used to endorse or promote products derived
1238855Sbostic  * from this software without specific prior written permission.
1338855Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1438855Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1538855Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1638855Sbostic  */
1738855Sbostic 
1838855Sbostic #ifndef lint
19*38861Sbostic static char sccsid[] = "@(#)display.c	5.3 (Berkeley) 08/29/89";
2038855Sbostic #endif /* not lint */
2138855Sbostic 
2238855Sbostic #include <sys/param.h>
2338855Sbostic #include <sys/stat.h>
2438855Sbostic #include <unistd.h>
2538855Sbostic #include <ctype.h>
2638855Sbostic #include <stdio.h>
2738855Sbostic #include <strings.h>
2838855Sbostic #include "hexdump.h"
2938855Sbostic 
3038855Sbostic static off_t address;			/* address/offset in stream */
3138855Sbostic static off_t eaddress;			/* end address */
3238855Sbostic static off_t savaddress;		/* saved address/offset in stream */
3338855Sbostic 
3438860Sbostic #define PRINT { \
3538860Sbostic 	switch(pr->flags) { \
3638860Sbostic 	case F_ADDRESS: \
3738860Sbostic 		(void)printf(pr->fmt, address); \
3838860Sbostic 		break; \
3938860Sbostic 	case F_BPAD: \
4038860Sbostic 		(void)printf(pr->fmt, ""); \
4138860Sbostic 		break; \
4238860Sbostic 	case F_C: \
4338860Sbostic 		conv_c(pr, bp); \
4438860Sbostic 		break; \
4538860Sbostic 	case F_CHAR: \
4638860Sbostic 		(void)printf(pr->fmt, *bp); \
4738860Sbostic 		break; \
4838860Sbostic 	case F_DBL: { \
4938860Sbostic 		double dval; \
5038860Sbostic 		float fval; \
5138860Sbostic 		switch(pr->bcnt) { \
5238860Sbostic 		case 4: \
5338860Sbostic 			bcopy((char *)bp, (char *)&fval, sizeof(fval)); \
5438860Sbostic 			(void)printf(pr->fmt, fval); \
5538860Sbostic 			break; \
5638860Sbostic 		case 8: \
5738860Sbostic 			bcopy((char *)bp, (char *)&dval, sizeof(dval)); \
5838860Sbostic 			(void)printf(pr->fmt, dval); \
5938860Sbostic 			break; \
6038860Sbostic 		} \
6138860Sbostic 		break; \
6238860Sbostic 	} \
6338860Sbostic 	case F_INT: { \
6438860Sbostic 		int ival; \
6538860Sbostic 		short sval; \
6638860Sbostic 		switch(pr->bcnt) { \
6738860Sbostic 		case 1: \
6838860Sbostic 			(void)printf(pr->fmt, (int)*bp); \
6938860Sbostic 			break; \
7038860Sbostic 		case 2: \
7138860Sbostic 			bcopy((char *)bp, (char *)&sval, sizeof(sval)); \
7238860Sbostic 			(void)printf(pr->fmt, (int)sval); \
7338860Sbostic 			break; \
7438860Sbostic 		case 4: \
7538860Sbostic 			bcopy((char *)bp, (char *)&ival, sizeof(ival)); \
7638860Sbostic 			(void)printf(pr->fmt, ival); \
7738860Sbostic 			break; \
7838860Sbostic 		} \
7938860Sbostic 		break; \
8038860Sbostic 	} \
8138860Sbostic 	case F_P: \
8238860Sbostic 		(void)printf(pr->fmt, isprint(*bp) ? *bp : '.'); \
8338860Sbostic 		break; \
8438860Sbostic 	case F_STR: \
8538860Sbostic 		(void)printf(pr->fmt, (char *)bp); \
8638860Sbostic 		break; \
8738860Sbostic 	case F_TEXT: \
8838860Sbostic 		(void)printf(pr->fmt); \
8938860Sbostic 		break; \
9038860Sbostic 	case F_U: \
9138860Sbostic 		conv_u(pr, bp); \
9238860Sbostic 		break; \
9338860Sbostic 	case F_UINT: { \
9438860Sbostic 		u_int ival; \
9538860Sbostic 		u_short sval; \
9638860Sbostic 		switch(pr->bcnt) { \
9738860Sbostic 		case 1: \
9838860Sbostic 			(void)printf(pr->fmt, (u_int)*bp); \
9938860Sbostic 			break; \
10038860Sbostic 		case 2: \
10138860Sbostic 			bcopy((char *)bp, (char *)&sval, sizeof(sval)); \
10238860Sbostic 			(void)printf(pr->fmt, (u_int)sval); \
10338860Sbostic 			break; \
10438860Sbostic 		case 4: \
10538860Sbostic 			bcopy((char *)bp, (char *)&ival, sizeof(ival)); \
10638860Sbostic 			(void)printf(pr->fmt, ival); \
10738860Sbostic 			break; \
10838860Sbostic 		} \
10938860Sbostic 		break; \
11038860Sbostic 	} \
11138860Sbostic 	} \
11238860Sbostic }
11338860Sbostic 
11438855Sbostic display()
11538855Sbostic {
11638855Sbostic 	extern FU *endfu;
11738855Sbostic 	register FS *fs;
11838855Sbostic 	register FU *fu;
11938855Sbostic 	register PR *pr;
12038855Sbostic 	register int cnt;
12138855Sbostic 	register u_char *bp;
12238855Sbostic 	off_t saveaddress;
12338855Sbostic 	u_char savech, *savebp, *get();
12438855Sbostic 
12538855Sbostic 	while (bp = get())
12638855Sbostic 	    for (fs = fshead, savebp = bp, saveaddress = address; fs;
12738860Sbostic 		fs = fs->nextfs, bp = savebp, address = saveaddress)
12838855Sbostic 		    for (fu = fs->nextfu; fu; fu = fu->nextfu) {
12938855Sbostic 			if (fu->flags&F_IGNORE)
13038855Sbostic 				break;
13138855Sbostic 			for (cnt = fu->reps; cnt; --cnt)
13238855Sbostic 			    for (pr = fu->nextpr; pr; address += pr->bcnt,
13338855Sbostic 				bp += pr->bcnt, pr = pr->nextpr) {
134*38861Sbostic 				    if (eaddress && address >= eaddress &&
135*38861Sbostic 					!(pr->flags&(F_TEXT|F_BPAD)))
136*38861Sbostic 					    bpad(pr);
13738855Sbostic 				    if (cnt == 1 && pr->nospace) {
13838855Sbostic 					savech = *pr->nospace;
13938855Sbostic 					*pr->nospace = '\0';
14038855Sbostic 				    }
14138860Sbostic 				    PRINT;
14238855Sbostic 				    if (cnt == 1 && pr->nospace)
14338855Sbostic 					*pr->nospace = savech;
14438855Sbostic 			    }
14538855Sbostic 		    }
14638855Sbostic 	if (endfu) {
14738855Sbostic 		/*
14838855Sbostic 		 * if eaddress not set, file size was multiple of blocksize,
14938855Sbostic 		 * and no partial block ever found.
15038855Sbostic 		 */
15138855Sbostic 		if (!eaddress)
15238855Sbostic 			eaddress = address + blocksize;
15338855Sbostic 		for (pr = endfu->nextpr; pr; pr = pr->nextpr)
15438855Sbostic 			switch(pr->flags) {
15538855Sbostic 			case F_ADDRESS:
15638855Sbostic 				(void)printf(pr->fmt, eaddress);
15738855Sbostic 				break;
15838855Sbostic 			case F_TEXT:
15938855Sbostic 				(void)printf(pr->fmt);
16038855Sbostic 				break;
16138855Sbostic 			}
16238855Sbostic 	}
16338855Sbostic }
16438855Sbostic 
165*38861Sbostic bpad(pr)
166*38861Sbostic 	PR *pr;
167*38861Sbostic {
168*38861Sbostic 	static char *spec = " -0+#";
169*38861Sbostic 	register char *p1, *p2;
170*38861Sbostic 
171*38861Sbostic 	/*
172*38861Sbostic 	 * remove all conversion flags; '-' is the only one valid
173*38861Sbostic 	 * with %s, and it's not useful here.
174*38861Sbostic 	 */
175*38861Sbostic 	pr->flags = F_BPAD;
176*38861Sbostic 	*pr->cchar = 's';
177*38861Sbostic 	for (p1 = pr->fmt; *p1 != '%'; ++p1);
178*38861Sbostic 	for (p2 = ++p1; *p1 && index(spec, *p1); ++p1);
179*38861Sbostic 	while (*p2++ = *p1++);
180*38861Sbostic }
181*38861Sbostic 
18238855Sbostic u_char *
18338855Sbostic get()
18438855Sbostic {
18538855Sbostic 	extern enum _vflag vflag;
18638855Sbostic 	extern int length;
18738855Sbostic 	static int ateof = 1;
18838855Sbostic 	static u_char *curp, *savp;
18938855Sbostic 	register int n;
19038855Sbostic 	int need, nread;
19138855Sbostic 	u_char *tmpp;
19238855Sbostic 
19338855Sbostic 	if (!curp) {
19438855Sbostic 		curp = (u_char *)emalloc(blocksize);
19538855Sbostic 		savp = (u_char *)emalloc(blocksize);
19638855Sbostic 	} else {
19738855Sbostic 		tmpp = curp;
19838855Sbostic 		curp = savp;
19938855Sbostic 		savp = tmpp;
20038855Sbostic 		address = savaddress += blocksize;
20138855Sbostic 	}
20238855Sbostic 	for (need = blocksize, nread = 0;;) {
20338855Sbostic 		/*
20438855Sbostic 		 * if read the right number of bytes, or at EOF for one file,
20538855Sbostic 		 * and no other files are available, zero-pad the rest of the
20638855Sbostic 		 * block and set the end flag.
20738855Sbostic 		 */
20838855Sbostic 		if (!length || ateof && !next((char **)NULL)) {
20938855Sbostic 			if (need == blocksize)
21038855Sbostic 				return((u_char *)NULL);
21138855Sbostic 			if (vflag != ALL && !bcmp(curp, savp, nread)) {
21238855Sbostic 				if (vflag != DUP)
21338855Sbostic 					(void)printf("*\n");
21438855Sbostic 				return((u_char *)NULL);
21538855Sbostic 			}
21638855Sbostic 			bzero((char *)curp + nread, need);
21738855Sbostic 			eaddress = address + nread;
21838855Sbostic 			return(curp);
21938855Sbostic 		}
22038855Sbostic 		n = fread((char *)curp + nread, sizeof(u_char),
22138855Sbostic 		    length == -1 ? need : MIN(length, need), stdin);
22238855Sbostic 		if (!n) {
22338855Sbostic 			ateof = 1;
22438855Sbostic 			continue;
22538855Sbostic 		}
22638855Sbostic 		ateof = 0;
22738855Sbostic 		if (length != -1)
22838855Sbostic 			length -= n;
22938855Sbostic 		if (!(need -= n)) {
23038855Sbostic 			if (vflag == ALL || bcmp(curp, savp, blocksize)) {
23138855Sbostic 				if (vflag == DUP)
23238855Sbostic 					vflag = WAIT;
23338855Sbostic 				return(curp);
23438855Sbostic 			}
23538855Sbostic 			if (vflag == WAIT)
23638855Sbostic 				(void)printf("*\n");
23738855Sbostic 			vflag = DUP;
23838855Sbostic 			address = savaddress += blocksize;
23938855Sbostic 			need = blocksize;
24038855Sbostic 			nread = 0;
24138855Sbostic 		}
24238855Sbostic 		else
24338855Sbostic 			nread += n;
24438855Sbostic 	}
24538855Sbostic }
24638855Sbostic 
24738855Sbostic extern off_t skip;			/* bytes to skip */
24838855Sbostic 
24938855Sbostic next(argv)
25038855Sbostic 	char **argv;
25138855Sbostic {
25238855Sbostic 	extern int errno, exitval;
25338855Sbostic 	static int done;
25438855Sbostic 	static char **_argv;
25538855Sbostic 	int statok;
25638855Sbostic 
25738855Sbostic 	if (argv) {
25838855Sbostic 		_argv = argv;
25938855Sbostic 		return(1);
26038855Sbostic 	}
26138855Sbostic 	for (;;) {
26238855Sbostic 		if (*_argv) {
26338855Sbostic 			if (!(freopen(*_argv, "r", stdin))) {
26438855Sbostic 				(void)fprintf(stderr, "hexdump: %s: %s\n",
26538855Sbostic 				    *_argv, strerror(errno));
26638855Sbostic 				exitval = 1;
26738855Sbostic 				++_argv;
26838855Sbostic 				continue;
26938855Sbostic 			}
27038855Sbostic 			statok = done = 1;
27138855Sbostic 		} else {
27238855Sbostic 			if (done++)
27338855Sbostic 				return(0);
27438855Sbostic 			statok = 0;
27538855Sbostic 		}
27638855Sbostic 		if (skip)
27738855Sbostic 			doskip(statok ? *_argv : "stdin", statok);
27838855Sbostic 		if (*_argv)
27938855Sbostic 			++_argv;
28038855Sbostic 		if (!skip)
28138855Sbostic 			return(1);
28238855Sbostic 	}
28338855Sbostic 	/* NOTREACHED */
28438855Sbostic }
28538855Sbostic 
28638855Sbostic doskip(fname, statok)
28738855Sbostic 	char *fname;
28838855Sbostic 	int statok;
28938855Sbostic {
29038855Sbostic 	extern int errno;
29138855Sbostic 	struct stat sbuf;
29238855Sbostic 
29338855Sbostic 	if (statok) {
29438855Sbostic 		if (fstat(fileno(stdin), &sbuf)) {
29538855Sbostic 			(void)fprintf(stderr, "hexdump: %s: %s.\n",
29638855Sbostic 			    fname, strerror(errno));
29738855Sbostic 			exit(1);
29838855Sbostic 		}
29938855Sbostic 		if (skip >= sbuf.st_size) {
30038855Sbostic 			skip -= sbuf.st_size;
30138855Sbostic 			address += sbuf.st_size;
30238855Sbostic 			return;
30338855Sbostic 		}
30438855Sbostic 	}
30538855Sbostic 	if (fseek(stdin, skip, SEEK_SET)) {
30638855Sbostic 		(void)fprintf(stderr, "hexdump: %s: %s.\n",
30738855Sbostic 		    fname, strerror(errno));
30838855Sbostic 		exit(1);
30938855Sbostic 	}
31038855Sbostic 	savaddress = address += skip;
31138855Sbostic 	skip = 0;
31238855Sbostic }
31338855Sbostic 
31438855Sbostic char *
31538855Sbostic emalloc(size)
31638855Sbostic 	int size;
31738855Sbostic {
31838855Sbostic 	char *p, *malloc();
31938855Sbostic 
32038855Sbostic 	if (!(p = malloc((u_int)size)))
32138855Sbostic 		nomem();
32238855Sbostic 	bzero(p, size);
32338855Sbostic 	return(p);
32438855Sbostic }
32538855Sbostic 
32638855Sbostic nomem()
32738855Sbostic {
32838855Sbostic 	(void)fprintf(stderr, "hexdump: out of memory.\n");
32938855Sbostic 	exit(1);
33038855Sbostic }
331