xref: /original-bsd/usr.bin/hexdump/hexdump.c (revision 8c1e77f1)
141e118a2Sbostic /*
2*8c1e77f1Sbostic  * Copyright (c) 1989, 1993
3*8c1e77f1Sbostic  *	The Regents of the University of California.  All rights reserved.
441e118a2Sbostic  *
5b4e93e05Sbostic  * %sccs.include.redist.c%
641e118a2Sbostic  */
741e118a2Sbostic 
841e118a2Sbostic #ifndef lint
9*8c1e77f1Sbostic static char copyright[] =
10*8c1e77f1Sbostic "@(#) Copyright (c) 1989, 1993\n\
11*8c1e77f1Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1241e118a2Sbostic #endif /* not lint */
1341e118a2Sbostic 
1441e118a2Sbostic #ifndef lint
15*8c1e77f1Sbostic static char sccsid[] = "@(#)hexdump.c	8.1 (Berkeley) 06/06/93";
1641e118a2Sbostic #endif /* not lint */
1741e118a2Sbostic 
1841e118a2Sbostic #include <sys/types.h>
1942e6d3ddSbostic 
2042e6d3ddSbostic #include <errno.h>
2142e6d3ddSbostic #include <stdlib.h>
2241e118a2Sbostic #include <stdio.h>
2342e6d3ddSbostic #include <string.h>
2441e118a2Sbostic #include "hexdump.h"
2541e118a2Sbostic 
2641e118a2Sbostic FS *fshead;				/* head of format strings */
2741e118a2Sbostic int blocksize;				/* data block size */
2841e118a2Sbostic int exitval;				/* final exit value */
29f949950fSbostic int length = -1;			/* max bytes to read */
3041e118a2Sbostic 
3142e6d3ddSbostic int
main(argc,argv)3241e118a2Sbostic main(argc, argv)
3341e118a2Sbostic 	int argc;
3442e6d3ddSbostic 	char *argv[];
3541e118a2Sbostic {
3641e118a2Sbostic 	register FS *tfs;
3742e6d3ddSbostic 	char *p;
3841e118a2Sbostic 
39f949950fSbostic 	if (!(p = rindex(argv[0], 'o')) || strcmp(p, "od"))
40f949950fSbostic 		newsyntax(argc, &argv);
41f949950fSbostic 	else
42f949950fSbostic 		oldsyntax(argc, &argv);
4341e118a2Sbostic 
4441e118a2Sbostic 	/* figure out the data block size */
4541e118a2Sbostic 	for (blocksize = 0, tfs = fshead; tfs; tfs = tfs->nextfs) {
4641e118a2Sbostic 		tfs->bcnt = size(tfs);
4741e118a2Sbostic 		if (blocksize < tfs->bcnt)
4841e118a2Sbostic 			blocksize = tfs->bcnt;
4941e118a2Sbostic 	}
5041e118a2Sbostic 	/* rewrite the rules, do syntax checking */
5141e118a2Sbostic 	for (tfs = fshead; tfs; tfs = tfs->nextfs)
5241e118a2Sbostic 		rewrite(tfs);
5341e118a2Sbostic 
5441e118a2Sbostic 	(void)next(argv);
5541e118a2Sbostic 	display();
5641e118a2Sbostic 	exit(exitval);
5741e118a2Sbostic }
5842e6d3ddSbostic 
5942e6d3ddSbostic #if __STDC__
6042e6d3ddSbostic #include <stdarg.h>
6142e6d3ddSbostic #else
6242e6d3ddSbostic #include <varargs.h>
6342e6d3ddSbostic #endif
6442e6d3ddSbostic 
6542e6d3ddSbostic void
6642e6d3ddSbostic #if __STDC__
err(const char * fmt,...)6742e6d3ddSbostic err(const char *fmt, ...)
6842e6d3ddSbostic #else
6942e6d3ddSbostic err(fmt, va_alist)
7042e6d3ddSbostic 	char *fmt;
7142e6d3ddSbostic         va_dcl
7242e6d3ddSbostic #endif
7342e6d3ddSbostic {
7442e6d3ddSbostic 	va_list ap;
7542e6d3ddSbostic #if __STDC__
7642e6d3ddSbostic 	va_start(ap, fmt);
7742e6d3ddSbostic #else
7842e6d3ddSbostic 	va_start(ap);
7942e6d3ddSbostic #endif
8042e6d3ddSbostic 	(void)fprintf(stderr, "hexdump: ");
8142e6d3ddSbostic 	(void)vfprintf(stderr, fmt, ap);
8242e6d3ddSbostic 	va_end(ap);
8342e6d3ddSbostic 	(void)fprintf(stderr, "\n");
8442e6d3ddSbostic 	exit(1);
8542e6d3ddSbostic 	/* NOTREACHED */
8642e6d3ddSbostic }
87