xref: /original-bsd/usr.bin/hexdump/hexdump.c (revision 02e832b2)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)hexdump.c	5.6 (Berkeley) 07/14/92";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include "hexdump.h"
25 
26 FS *fshead;				/* head of format strings */
27 int blocksize;				/* data block size */
28 int exitval;				/* final exit value */
29 int length = -1;			/* max bytes to read */
30 
31 int
32 main(argc, argv)
33 	int argc;
34 	char *argv[];
35 {
36 	register FS *tfs;
37 	char *p;
38 
39 	if (!(p = rindex(argv[0], 'o')) || strcmp(p, "od"))
40 		newsyntax(argc, &argv);
41 	else
42 		oldsyntax(argc, &argv);
43 
44 	/* figure out the data block size */
45 	for (blocksize = 0, tfs = fshead; tfs; tfs = tfs->nextfs) {
46 		tfs->bcnt = size(tfs);
47 		if (blocksize < tfs->bcnt)
48 			blocksize = tfs->bcnt;
49 	}
50 	/* rewrite the rules, do syntax checking */
51 	for (tfs = fshead; tfs; tfs = tfs->nextfs)
52 		rewrite(tfs);
53 
54 	(void)next(argv);
55 	display();
56 	exit(exitval);
57 }
58 
59 #if __STDC__
60 #include <stdarg.h>
61 #else
62 #include <varargs.h>
63 #endif
64 
65 void
66 #if __STDC__
67 err(const char *fmt, ...)
68 #else
69 err(fmt, va_alist)
70 	char *fmt;
71         va_dcl
72 #endif
73 {
74 	va_list ap;
75 #if __STDC__
76 	va_start(ap, fmt);
77 #else
78 	va_start(ap);
79 #endif
80 	(void)fprintf(stderr, "hexdump: ");
81 	(void)vfprintf(stderr, fmt, ap);
82 	va_end(ap);
83 	(void)fprintf(stderr, "\n");
84 	exit(1);
85 	/* NOTREACHED */
86 }
87