xref: /titanic_44/usr/src/cmd/file/magicutils.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
31*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <stdio.h>
36*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
37*7c478bd9Sstevel@tonic-gate #include <string.h>
38*7c478bd9Sstevel@tonic-gate #include <ctype.h>
39*7c478bd9Sstevel@tonic-gate #include <errno.h>
40*7c478bd9Sstevel@tonic-gate #include <limits.h>
41*7c478bd9Sstevel@tonic-gate #include <inttypes.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
43*7c478bd9Sstevel@tonic-gate #include <libintl.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate  *	Types
47*7c478bd9Sstevel@tonic-gate  */
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate #define	BYTE	1
50*7c478bd9Sstevel@tonic-gate #define	SHORT	2
51*7c478bd9Sstevel@tonic-gate #define	LONG	4
52*7c478bd9Sstevel@tonic-gate #define	LLONG	8
53*7c478bd9Sstevel@tonic-gate #define	UBYTE	16
54*7c478bd9Sstevel@tonic-gate #define	USHORT	32
55*7c478bd9Sstevel@tonic-gate #define	ULONG	64
56*7c478bd9Sstevel@tonic-gate #define	ULLONG	128
57*7c478bd9Sstevel@tonic-gate #define	STR	256
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  *	Opcodes
61*7c478bd9Sstevel@tonic-gate  */
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate #define	EQ	0
64*7c478bd9Sstevel@tonic-gate #define	GT	1
65*7c478bd9Sstevel@tonic-gate #define	LT	2
66*7c478bd9Sstevel@tonic-gate #define	STRC	3	/* string compare */
67*7c478bd9Sstevel@tonic-gate #define	ANY	4
68*7c478bd9Sstevel@tonic-gate #define	AND	5
69*7c478bd9Sstevel@tonic-gate #define	NSET	6	/* True if bit is not set */
70*7c478bd9Sstevel@tonic-gate #define	SUB	64	/* or'ed in, SUBstitution string, for example */
71*7c478bd9Sstevel@tonic-gate 			/* %ld, %s, %lo mask: with bit 6 on, used to locate */
72*7c478bd9Sstevel@tonic-gate 			/* print formats */
73*7c478bd9Sstevel@tonic-gate /*
74*7c478bd9Sstevel@tonic-gate  *	Misc
75*7c478bd9Sstevel@tonic-gate  */
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate #define	BSZ	128
78*7c478bd9Sstevel@tonic-gate #define	NENT	200
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate /*
81*7c478bd9Sstevel@tonic-gate  *	Structure of magic file entry
82*7c478bd9Sstevel@tonic-gate  */
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate struct	entry	{
85*7c478bd9Sstevel@tonic-gate 	char		e_level;	/* 0 or 1 */
86*7c478bd9Sstevel@tonic-gate 	off_t		e_off;		/* in bytes */
87*7c478bd9Sstevel@tonic-gate 	uint32_t	e_type;		/* BYTE, SHORT, STR, et al */
88*7c478bd9Sstevel@tonic-gate 	char		e_opcode;	/* EQ, GT, LT, ANY, AND, NSET */
89*7c478bd9Sstevel@tonic-gate 	uint64_t	e_mask;		/* if non-zero, mask value with this */
90*7c478bd9Sstevel@tonic-gate 	union	{
91*7c478bd9Sstevel@tonic-gate 		uint64_t	num;
92*7c478bd9Sstevel@tonic-gate 		char		*str;
93*7c478bd9Sstevel@tonic-gate 	}	e_value;
94*7c478bd9Sstevel@tonic-gate 	const char	*e_str;
95*7c478bd9Sstevel@tonic-gate };
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate typedef	struct entry	Entry;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate static Entry	*mtab1;	/* 1st magic table, applied before default tests */
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	/*
102*7c478bd9Sstevel@tonic-gate 	 * 2nd magic table, includes default tests and magic entries
103*7c478bd9Sstevel@tonic-gate 	 * to be applied after default position-sensitive tests
104*7c478bd9Sstevel@tonic-gate 	 */
105*7c478bd9Sstevel@tonic-gate static Entry	*mtab2;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate static Entry	*mend1;	/* one past last-allocated entry in mtab1 */
108*7c478bd9Sstevel@tonic-gate static Entry	*mend2;	/* one past last-allocated entry in mtab2 */
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate static Entry	*ep1;	/* current entry in mtab1 */
111*7c478bd9Sstevel@tonic-gate static Entry	*ep2;	/* current entry in mtab2 */
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate static char *
114*7c478bd9Sstevel@tonic-gate getstr(char *p)
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate 	char	*newstr;
117*7c478bd9Sstevel@tonic-gate 	char	*s;
118*7c478bd9Sstevel@tonic-gate 	long	val;
119*7c478bd9Sstevel@tonic-gate 	int	base;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	newstr = (char *)malloc((strlen(p) + 1) * sizeof (char));
122*7c478bd9Sstevel@tonic-gate 	if (newstr == NULL) {
123*7c478bd9Sstevel@tonic-gate 		perror(gettext("magic table string allocation"));
124*7c478bd9Sstevel@tonic-gate 		return (NULL);
125*7c478bd9Sstevel@tonic-gate 	}
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	s = newstr;
128*7c478bd9Sstevel@tonic-gate 	while (*p != '\0') {
129*7c478bd9Sstevel@tonic-gate 		if (*p != '\\') {
130*7c478bd9Sstevel@tonic-gate 			*s++ = *p++;
131*7c478bd9Sstevel@tonic-gate 			continue;
132*7c478bd9Sstevel@tonic-gate 		}
133*7c478bd9Sstevel@tonic-gate 		p++;
134*7c478bd9Sstevel@tonic-gate 		if (*p == '\0')
135*7c478bd9Sstevel@tonic-gate 			break;
136*7c478bd9Sstevel@tonic-gate 		if (isdigit(*p)) {
137*7c478bd9Sstevel@tonic-gate 			if (*p == '0' && (*(p+1) == 'x' || *(p+1) == 'X')) {
138*7c478bd9Sstevel@tonic-gate 				/* hex */
139*7c478bd9Sstevel@tonic-gate 				base = 16;
140*7c478bd9Sstevel@tonic-gate 			} else {
141*7c478bd9Sstevel@tonic-gate 				base = 8;
142*7c478bd9Sstevel@tonic-gate 			}
143*7c478bd9Sstevel@tonic-gate 			errno = 0;
144*7c478bd9Sstevel@tonic-gate 			val = strtol(p, &p, base);
145*7c478bd9Sstevel@tonic-gate 			if (val > UCHAR_MAX || val < 0 || errno != 0) {
146*7c478bd9Sstevel@tonic-gate 				perror(gettext("magic table invalid "
147*7c478bd9Sstevel@tonic-gate 				    "string value"));
148*7c478bd9Sstevel@tonic-gate 				return (NULL);
149*7c478bd9Sstevel@tonic-gate 			}
150*7c478bd9Sstevel@tonic-gate 			*s++ = (char)val;
151*7c478bd9Sstevel@tonic-gate 		} else {
152*7c478bd9Sstevel@tonic-gate 			/* escape the character */
153*7c478bd9Sstevel@tonic-gate 			switch (*p) {
154*7c478bd9Sstevel@tonic-gate 			case 'n':
155*7c478bd9Sstevel@tonic-gate 				*s = '\n';
156*7c478bd9Sstevel@tonic-gate 				break;
157*7c478bd9Sstevel@tonic-gate 			case 'r':
158*7c478bd9Sstevel@tonic-gate 				*s = '\r';
159*7c478bd9Sstevel@tonic-gate 				break;
160*7c478bd9Sstevel@tonic-gate 			case 'a':
161*7c478bd9Sstevel@tonic-gate 				*s = '\a';
162*7c478bd9Sstevel@tonic-gate 				break;
163*7c478bd9Sstevel@tonic-gate 			case 'b':
164*7c478bd9Sstevel@tonic-gate 				*s = '\b';
165*7c478bd9Sstevel@tonic-gate 				break;
166*7c478bd9Sstevel@tonic-gate 			case 'f':
167*7c478bd9Sstevel@tonic-gate 				*s = '\f';
168*7c478bd9Sstevel@tonic-gate 				break;
169*7c478bd9Sstevel@tonic-gate 			case 't':
170*7c478bd9Sstevel@tonic-gate 				*s = '\t';
171*7c478bd9Sstevel@tonic-gate 				break;
172*7c478bd9Sstevel@tonic-gate 			case 'v':
173*7c478bd9Sstevel@tonic-gate 				*s = '\v';
174*7c478bd9Sstevel@tonic-gate 				break;
175*7c478bd9Sstevel@tonic-gate 			default:
176*7c478bd9Sstevel@tonic-gate 				*s = *p;
177*7c478bd9Sstevel@tonic-gate 				break;
178*7c478bd9Sstevel@tonic-gate 			}
179*7c478bd9Sstevel@tonic-gate 			p++;
180*7c478bd9Sstevel@tonic-gate 			s++;
181*7c478bd9Sstevel@tonic-gate 		}
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 	*s = '\0';
184*7c478bd9Sstevel@tonic-gate 	return (newstr);
185*7c478bd9Sstevel@tonic-gate }
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate /*
188*7c478bd9Sstevel@tonic-gate  * f_mkmtab - fills mtab array of magic table entries with
189*7c478bd9Sstevel@tonic-gate  *	values from the file magfile.
190*7c478bd9Sstevel@tonic-gate  *	May be called more than once if multiple magic
191*7c478bd9Sstevel@tonic-gate  * 	files were specified.
192*7c478bd9Sstevel@tonic-gate  *	Stores entries sequentially in one of two magic
193*7c478bd9Sstevel@tonic-gate  *	tables: mtab1, if first = 1; mtab2 otherwise.
194*7c478bd9Sstevel@tonic-gate  *
195*7c478bd9Sstevel@tonic-gate  *	If -c option is specified, cflg is non-zero, and
196*7c478bd9Sstevel@tonic-gate  *	f_mkmtab() reports on errors in the magic file.
197*7c478bd9Sstevel@tonic-gate  *
198*7c478bd9Sstevel@tonic-gate  *	Two magic tables may need to be created.  The first
199*7c478bd9Sstevel@tonic-gate  *	one (mtab1) contains magic entries to be checked before
200*7c478bd9Sstevel@tonic-gate  *	the programmatic default position-sensitive tests in
201*7c478bd9Sstevel@tonic-gate  *	def_position_tests().
202*7c478bd9Sstevel@tonic-gate  *	The second one (mtab2) should start with the default
203*7c478bd9Sstevel@tonic-gate  *	/etc/magic file entries and is to be checked after
204*7c478bd9Sstevel@tonic-gate  *	the programmatic default position-sensitive tests in
205*7c478bd9Sstevel@tonic-gate  *	def_position_tests().  The parameter "first" would
206*7c478bd9Sstevel@tonic-gate  *	be 1 for the former set of tables, 0 for the latter
207*7c478bd9Sstevel@tonic-gate  *	set of magic tables.
208*7c478bd9Sstevel@tonic-gate  *	No mtab2 should be created if file will not be
209*7c478bd9Sstevel@tonic-gate  *	applying default tests; in that case, all magic table
210*7c478bd9Sstevel@tonic-gate  *	entries should be in mtab1.
211*7c478bd9Sstevel@tonic-gate  *
212*7c478bd9Sstevel@tonic-gate  *	f_mkmtab returns 0 on success, -1 on error.  The calling
213*7c478bd9Sstevel@tonic-gate  *	program is not expected to proceed after f_mkmtab()
214*7c478bd9Sstevel@tonic-gate  *	returns an error.
215*7c478bd9Sstevel@tonic-gate  */
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate int
218*7c478bd9Sstevel@tonic-gate f_mkmtab(char *magfile, int cflg, int first)
219*7c478bd9Sstevel@tonic-gate {
220*7c478bd9Sstevel@tonic-gate 	Entry	*mtab;	/* generic magic table pointer */
221*7c478bd9Sstevel@tonic-gate 	Entry	*ep;	/* current magic table entry */
222*7c478bd9Sstevel@tonic-gate 	Entry	*mend;	/* one past last-allocated entry of mtab */
223*7c478bd9Sstevel@tonic-gate 	FILE	*fp;
224*7c478bd9Sstevel@tonic-gate 	int	lcnt = 0;
225*7c478bd9Sstevel@tonic-gate 	char	buf[BSZ];
226*7c478bd9Sstevel@tonic-gate 	size_t	tbsize;
227*7c478bd9Sstevel@tonic-gate 	size_t	oldsize;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	if (first) {
230*7c478bd9Sstevel@tonic-gate 		mtab = mtab1;
231*7c478bd9Sstevel@tonic-gate 		mend = mend1;
232*7c478bd9Sstevel@tonic-gate 		ep = ep1;
233*7c478bd9Sstevel@tonic-gate 	} else {
234*7c478bd9Sstevel@tonic-gate 		mtab = mtab2;
235*7c478bd9Sstevel@tonic-gate 		mend = mend2;
236*7c478bd9Sstevel@tonic-gate 		ep = ep2;
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	/* mtab may have been allocated on a previous f_mkmtab call */
240*7c478bd9Sstevel@tonic-gate 	if (mtab == (Entry *)NULL) {
241*7c478bd9Sstevel@tonic-gate 		mtab = (Entry *) calloc(sizeof (Entry), NENT);
242*7c478bd9Sstevel@tonic-gate 		if (mtab == (Entry *)NULL) {
243*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
244*7c478bd9Sstevel@tonic-gate 			gettext("no memory for magic table\n"));
245*7c478bd9Sstevel@tonic-gate 			return (-1);
246*7c478bd9Sstevel@tonic-gate 		}
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 		ep = mtab;
249*7c478bd9Sstevel@tonic-gate 		mend = &mtab[NENT];
250*7c478bd9Sstevel@tonic-gate 	}
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	fp = fopen(magfile, "r");
253*7c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
254*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
255*7c478bd9Sstevel@tonic-gate 		gettext("cannot open magic file <%s>.\n"),
256*7c478bd9Sstevel@tonic-gate 		magfile);
257*7c478bd9Sstevel@tonic-gate 		return (-1);
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate 	while (fgets(buf, BSZ, fp) != NULL) {
260*7c478bd9Sstevel@tonic-gate 		char	*p = buf;
261*7c478bd9Sstevel@tonic-gate 		char	*p2;
262*7c478bd9Sstevel@tonic-gate 		char	*p3;
263*7c478bd9Sstevel@tonic-gate 		char	opc;
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 		/*
266*7c478bd9Sstevel@tonic-gate 		 * ensure we have one extra entry allocated
267*7c478bd9Sstevel@tonic-gate 		 * to mark end of the table, after the while loop
268*7c478bd9Sstevel@tonic-gate 		 */
269*7c478bd9Sstevel@tonic-gate 		if (ep >= (mend - 1)) {
270*7c478bd9Sstevel@tonic-gate 			oldsize = mend - mtab;
271*7c478bd9Sstevel@tonic-gate 			tbsize = (NENT + oldsize) * sizeof (Entry);
272*7c478bd9Sstevel@tonic-gate 			if ((mtab = realloc(mtab, tbsize)) == NULL) {
273*7c478bd9Sstevel@tonic-gate 				perror(gettext("magic table overflow"));
274*7c478bd9Sstevel@tonic-gate 				return (-1);
275*7c478bd9Sstevel@tonic-gate 			} else {
276*7c478bd9Sstevel@tonic-gate 				(void) memset(mtab + oldsize, 0,
277*7c478bd9Sstevel@tonic-gate 				    sizeof (Entry) * NENT);
278*7c478bd9Sstevel@tonic-gate 				mend = &mtab[tbsize / sizeof (Entry)];
279*7c478bd9Sstevel@tonic-gate 				ep = &mtab[oldsize-1];
280*7c478bd9Sstevel@tonic-gate 			}
281*7c478bd9Sstevel@tonic-gate 		}
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 		lcnt++;
284*7c478bd9Sstevel@tonic-gate 		if (*p == '\n' || *p == '#')
285*7c478bd9Sstevel@tonic-gate 			continue;
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 			/* LEVEL */
289*7c478bd9Sstevel@tonic-gate 		if (*p == '>') {
290*7c478bd9Sstevel@tonic-gate 			ep->e_level = 1;
291*7c478bd9Sstevel@tonic-gate 			p++;
292*7c478bd9Sstevel@tonic-gate 		}
293*7c478bd9Sstevel@tonic-gate 			/* OFFSET */
294*7c478bd9Sstevel@tonic-gate 		p2 = strchr(p, '\t');
295*7c478bd9Sstevel@tonic-gate 		if (p2 == NULL) {
296*7c478bd9Sstevel@tonic-gate 			if (cflg)
297*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
298*7c478bd9Sstevel@tonic-gate 				    gettext("fmt error, no tab after %s on "
299*7c478bd9Sstevel@tonic-gate 				    "line %d of %s\n"), p, lcnt, magfile);
300*7c478bd9Sstevel@tonic-gate 			continue;
301*7c478bd9Sstevel@tonic-gate 		}
302*7c478bd9Sstevel@tonic-gate 		*p2++ = NULL;
303*7c478bd9Sstevel@tonic-gate 		ep->e_off = strtol((const char *)p, (char **)NULL, 0);
304*7c478bd9Sstevel@tonic-gate 		while (*p2 == '\t')
305*7c478bd9Sstevel@tonic-gate 			p2++;
306*7c478bd9Sstevel@tonic-gate 			/* TYPE */
307*7c478bd9Sstevel@tonic-gate 		p = p2;
308*7c478bd9Sstevel@tonic-gate 		p2 = strchr(p, '\t');
309*7c478bd9Sstevel@tonic-gate 		if (p2 == NULL) {
310*7c478bd9Sstevel@tonic-gate 			if (cflg)
311*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
312*7c478bd9Sstevel@tonic-gate 				    gettext("fmt error, no tab after %s on "
313*7c478bd9Sstevel@tonic-gate 				    "line %d of %s\n"), p, lcnt, magfile);
314*7c478bd9Sstevel@tonic-gate 			continue;
315*7c478bd9Sstevel@tonic-gate 		}
316*7c478bd9Sstevel@tonic-gate 		*p2++ = NULL;
317*7c478bd9Sstevel@tonic-gate 		p3 = strchr(p, '&');
318*7c478bd9Sstevel@tonic-gate 		if (p3 != NULL) {
319*7c478bd9Sstevel@tonic-gate 			*p3++ = '\0';
320*7c478bd9Sstevel@tonic-gate 			ep->e_mask = strtoull((const char *)p3, (char **)NULL,
321*7c478bd9Sstevel@tonic-gate 			    0);	/* returns 0 or ULLONG_MAX on error */
322*7c478bd9Sstevel@tonic-gate 		} else {
323*7c478bd9Sstevel@tonic-gate 			ep->e_mask = 0ULL;
324*7c478bd9Sstevel@tonic-gate 		}
325*7c478bd9Sstevel@tonic-gate 		switch (*p) {
326*7c478bd9Sstevel@tonic-gate 			case 'd':
327*7c478bd9Sstevel@tonic-gate 				if (*(p+1) == NULL) {
328*7c478bd9Sstevel@tonic-gate 					/* d */
329*7c478bd9Sstevel@tonic-gate 					ep->e_type = LONG;
330*7c478bd9Sstevel@tonic-gate 				} else if (*(p+2) == NULL) {	/* d? */
331*7c478bd9Sstevel@tonic-gate 					switch (*(p+1)) {
332*7c478bd9Sstevel@tonic-gate 						case 'C':
333*7c478bd9Sstevel@tonic-gate 						case '1':
334*7c478bd9Sstevel@tonic-gate 							/* dC, d1 */
335*7c478bd9Sstevel@tonic-gate 							ep->e_type = BYTE;
336*7c478bd9Sstevel@tonic-gate 							break;
337*7c478bd9Sstevel@tonic-gate 						case 'S':
338*7c478bd9Sstevel@tonic-gate 						case '2':
339*7c478bd9Sstevel@tonic-gate 							/* dS, d2 */
340*7c478bd9Sstevel@tonic-gate 							ep->e_type = SHORT;
341*7c478bd9Sstevel@tonic-gate 							break;
342*7c478bd9Sstevel@tonic-gate 						case 'I':
343*7c478bd9Sstevel@tonic-gate 						case 'L':
344*7c478bd9Sstevel@tonic-gate 						case '4':
345*7c478bd9Sstevel@tonic-gate 							/* dI, dL, d4 */
346*7c478bd9Sstevel@tonic-gate 							ep->e_type = LONG;
347*7c478bd9Sstevel@tonic-gate 							break;
348*7c478bd9Sstevel@tonic-gate 						case '8':
349*7c478bd9Sstevel@tonic-gate 							/* d8 */
350*7c478bd9Sstevel@tonic-gate 							ep->e_type = LLONG;
351*7c478bd9Sstevel@tonic-gate 							break;
352*7c478bd9Sstevel@tonic-gate 						default:
353*7c478bd9Sstevel@tonic-gate 							ep->e_type = LONG;
354*7c478bd9Sstevel@tonic-gate 							break;
355*7c478bd9Sstevel@tonic-gate 					}
356*7c478bd9Sstevel@tonic-gate 				}
357*7c478bd9Sstevel@tonic-gate 				break;
358*7c478bd9Sstevel@tonic-gate 			case 'l':
359*7c478bd9Sstevel@tonic-gate 				if (*(p+1) == 'l') {	/* llong */
360*7c478bd9Sstevel@tonic-gate 					ep->e_type = LLONG;
361*7c478bd9Sstevel@tonic-gate 				} else { 		/* long */
362*7c478bd9Sstevel@tonic-gate 					ep->e_type = LONG;
363*7c478bd9Sstevel@tonic-gate 				}
364*7c478bd9Sstevel@tonic-gate 				break;
365*7c478bd9Sstevel@tonic-gate 			case 's':
366*7c478bd9Sstevel@tonic-gate 				if (*(p+1) == 'h') {
367*7c478bd9Sstevel@tonic-gate 					/* short */
368*7c478bd9Sstevel@tonic-gate 					ep->e_type = SHORT;
369*7c478bd9Sstevel@tonic-gate 				} else {
370*7c478bd9Sstevel@tonic-gate 					/* s or string */
371*7c478bd9Sstevel@tonic-gate 					ep->e_type = STR;
372*7c478bd9Sstevel@tonic-gate 				}
373*7c478bd9Sstevel@tonic-gate 				break;
374*7c478bd9Sstevel@tonic-gate 			case 'u':
375*7c478bd9Sstevel@tonic-gate 				if (*(p+1) == NULL) {
376*7c478bd9Sstevel@tonic-gate 					/* u */
377*7c478bd9Sstevel@tonic-gate 					ep->e_type = ULONG;
378*7c478bd9Sstevel@tonic-gate 				} else if (*(p+2) == NULL) {	/* u? */
379*7c478bd9Sstevel@tonic-gate 					switch (*(p+1)) {
380*7c478bd9Sstevel@tonic-gate 						case 'C':
381*7c478bd9Sstevel@tonic-gate 						case '1':
382*7c478bd9Sstevel@tonic-gate 							/* uC, u1 */
383*7c478bd9Sstevel@tonic-gate 							ep->e_type = UBYTE;
384*7c478bd9Sstevel@tonic-gate 							break;
385*7c478bd9Sstevel@tonic-gate 						case 'S':
386*7c478bd9Sstevel@tonic-gate 						case '2':
387*7c478bd9Sstevel@tonic-gate 							/* uS, u2 */
388*7c478bd9Sstevel@tonic-gate 							ep->e_type = USHORT;
389*7c478bd9Sstevel@tonic-gate 							break;
390*7c478bd9Sstevel@tonic-gate 						case 'I':
391*7c478bd9Sstevel@tonic-gate 						case 'L':
392*7c478bd9Sstevel@tonic-gate 						case '4':
393*7c478bd9Sstevel@tonic-gate 							/* uI, uL, u4 */
394*7c478bd9Sstevel@tonic-gate 							ep->e_type = ULONG;
395*7c478bd9Sstevel@tonic-gate 							break;
396*7c478bd9Sstevel@tonic-gate 						case '8':
397*7c478bd9Sstevel@tonic-gate 							/* u8 */
398*7c478bd9Sstevel@tonic-gate 							ep->e_type = ULLONG;
399*7c478bd9Sstevel@tonic-gate 							break;
400*7c478bd9Sstevel@tonic-gate 						default:
401*7c478bd9Sstevel@tonic-gate 							ep->e_type = ULONG;
402*7c478bd9Sstevel@tonic-gate 							break;
403*7c478bd9Sstevel@tonic-gate 					}
404*7c478bd9Sstevel@tonic-gate 				} else { /* u?* */
405*7c478bd9Sstevel@tonic-gate 					switch (*(p+1)) {
406*7c478bd9Sstevel@tonic-gate 					case 'b':	/* ubyte */
407*7c478bd9Sstevel@tonic-gate 						ep->e_type = UBYTE;
408*7c478bd9Sstevel@tonic-gate 						break;
409*7c478bd9Sstevel@tonic-gate 					case 's':	/* ushort */
410*7c478bd9Sstevel@tonic-gate 						ep->e_type = USHORT;
411*7c478bd9Sstevel@tonic-gate 						break;
412*7c478bd9Sstevel@tonic-gate 					case 'l':
413*7c478bd9Sstevel@tonic-gate 						if (*(p+2) == 'l') {
414*7c478bd9Sstevel@tonic-gate 							/* ullong */
415*7c478bd9Sstevel@tonic-gate 							ep->e_type = ULLONG;
416*7c478bd9Sstevel@tonic-gate 						} else {
417*7c478bd9Sstevel@tonic-gate 							/* ulong */
418*7c478bd9Sstevel@tonic-gate 							ep->e_type = ULONG;
419*7c478bd9Sstevel@tonic-gate 						}
420*7c478bd9Sstevel@tonic-gate 						break;
421*7c478bd9Sstevel@tonic-gate 					default:
422*7c478bd9Sstevel@tonic-gate 						/* default, same as "u" */
423*7c478bd9Sstevel@tonic-gate 						ep->e_type = ULONG;
424*7c478bd9Sstevel@tonic-gate 						break;
425*7c478bd9Sstevel@tonic-gate 					}
426*7c478bd9Sstevel@tonic-gate 				}
427*7c478bd9Sstevel@tonic-gate 				break;
428*7c478bd9Sstevel@tonic-gate 			default:
429*7c478bd9Sstevel@tonic-gate 				/* retain (undocumented) default type */
430*7c478bd9Sstevel@tonic-gate 				ep->e_type = BYTE;
431*7c478bd9Sstevel@tonic-gate 				break;
432*7c478bd9Sstevel@tonic-gate 		}
433*7c478bd9Sstevel@tonic-gate 		if (ep->e_type == 0) {
434*7c478bd9Sstevel@tonic-gate 			ep->e_type = BYTE;	/* default */
435*7c478bd9Sstevel@tonic-gate 		}
436*7c478bd9Sstevel@tonic-gate 		while (*p2 == '\t')
437*7c478bd9Sstevel@tonic-gate 			p2++;
438*7c478bd9Sstevel@tonic-gate 			/* OP-VALUE */
439*7c478bd9Sstevel@tonic-gate 		p = p2;
440*7c478bd9Sstevel@tonic-gate 		p2 = strchr(p, '\t');
441*7c478bd9Sstevel@tonic-gate 		if (p2 == NULL) {
442*7c478bd9Sstevel@tonic-gate 			if (cflg)
443*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
444*7c478bd9Sstevel@tonic-gate 				    gettext("fmt error, no tab after %s on "
445*7c478bd9Sstevel@tonic-gate 				    "line %d of %s\n"), p, lcnt, magfile);
446*7c478bd9Sstevel@tonic-gate 			continue;
447*7c478bd9Sstevel@tonic-gate 		}
448*7c478bd9Sstevel@tonic-gate 		*p2++ = NULL;
449*7c478bd9Sstevel@tonic-gate 		if (ep->e_type != STR) {
450*7c478bd9Sstevel@tonic-gate 			opc = *p++;
451*7c478bd9Sstevel@tonic-gate 			switch (opc) {
452*7c478bd9Sstevel@tonic-gate 			case '=':
453*7c478bd9Sstevel@tonic-gate 				ep->e_opcode = EQ;
454*7c478bd9Sstevel@tonic-gate 				break;
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 			case '>':
457*7c478bd9Sstevel@tonic-gate 				ep->e_opcode = GT;
458*7c478bd9Sstevel@tonic-gate 				break;
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 			case '<':
461*7c478bd9Sstevel@tonic-gate 				ep->e_opcode = LT;
462*7c478bd9Sstevel@tonic-gate 				break;
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 			case 'x':
465*7c478bd9Sstevel@tonic-gate 				ep->e_opcode = ANY;
466*7c478bd9Sstevel@tonic-gate 				break;
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 			case '&':
469*7c478bd9Sstevel@tonic-gate 				ep->e_opcode = AND;
470*7c478bd9Sstevel@tonic-gate 				break;
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 			case '^':
473*7c478bd9Sstevel@tonic-gate 				ep->e_opcode = NSET;
474*7c478bd9Sstevel@tonic-gate 				break;
475*7c478bd9Sstevel@tonic-gate 			default:	/* EQ (i.e. 0) is default	*/
476*7c478bd9Sstevel@tonic-gate 				p--;	/* since global ep->e_opcode=0	*/
477*7c478bd9Sstevel@tonic-gate 			}
478*7c478bd9Sstevel@tonic-gate 		}
479*7c478bd9Sstevel@tonic-gate 		if (ep->e_opcode != ANY) {
480*7c478bd9Sstevel@tonic-gate 			if (ep->e_type != STR) {
481*7c478bd9Sstevel@tonic-gate 				ep->e_value.num = strtoull((const char *)p,
482*7c478bd9Sstevel@tonic-gate 				    (char **)NULL, 0);
483*7c478bd9Sstevel@tonic-gate 			} else if ((ep->e_value.str = getstr(p)) == NULL) {
484*7c478bd9Sstevel@tonic-gate 				return (-1);
485*7c478bd9Sstevel@tonic-gate 			}
486*7c478bd9Sstevel@tonic-gate 		}
487*7c478bd9Sstevel@tonic-gate 		p2 += strspn(p2, "\t");
488*7c478bd9Sstevel@tonic-gate 			/* STRING */
489*7c478bd9Sstevel@tonic-gate 		if ((ep->e_str = strdup(p2)) == NULL) {
490*7c478bd9Sstevel@tonic-gate 			perror(gettext("magic table message allocation"));
491*7c478bd9Sstevel@tonic-gate 			return (-1);
492*7c478bd9Sstevel@tonic-gate 		} else {
493*7c478bd9Sstevel@tonic-gate 			if ((p = strchr(ep->e_str, '\n')) != NULL)
494*7c478bd9Sstevel@tonic-gate 				*p = '\0';
495*7c478bd9Sstevel@tonic-gate 			if (strchr(ep->e_str, '%') != NULL)
496*7c478bd9Sstevel@tonic-gate 				ep->e_opcode |= SUB;
497*7c478bd9Sstevel@tonic-gate 		}
498*7c478bd9Sstevel@tonic-gate 		ep++;
499*7c478bd9Sstevel@tonic-gate 	}	/* end while (fgets) */
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 	ep->e_off = -1L;	/* mark end of table */
502*7c478bd9Sstevel@tonic-gate 	if (first) {
503*7c478bd9Sstevel@tonic-gate 		mtab1 = mtab;
504*7c478bd9Sstevel@tonic-gate 		mend1 = mend;
505*7c478bd9Sstevel@tonic-gate 		ep1 = ep;
506*7c478bd9Sstevel@tonic-gate 	} else {
507*7c478bd9Sstevel@tonic-gate 		mtab2 = mtab;
508*7c478bd9Sstevel@tonic-gate 		mend2 = mend;
509*7c478bd9Sstevel@tonic-gate 		ep2 = ep;
510*7c478bd9Sstevel@tonic-gate 	}
511*7c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
512*7c478bd9Sstevel@tonic-gate 		perror(magfile);
513*7c478bd9Sstevel@tonic-gate 		return (-1);
514*7c478bd9Sstevel@tonic-gate 	}
515*7c478bd9Sstevel@tonic-gate 	return (0);
516*7c478bd9Sstevel@tonic-gate }
517*7c478bd9Sstevel@tonic-gate 
518*7c478bd9Sstevel@tonic-gate /*
519*7c478bd9Sstevel@tonic-gate  * Check for Magic Table entries in the file.
520*7c478bd9Sstevel@tonic-gate  *
521*7c478bd9Sstevel@tonic-gate  * Since there may be two sets of magic tables, first = 1
522*7c478bd9Sstevel@tonic-gate  * for the first magic table (mtab1) and 0 for the second magic
523*7c478bd9Sstevel@tonic-gate  * table (mtab2).
524*7c478bd9Sstevel@tonic-gate  */
525*7c478bd9Sstevel@tonic-gate int
526*7c478bd9Sstevel@tonic-gate f_ckmtab(char *buf, int bufsize, int first)
527*7c478bd9Sstevel@tonic-gate {
528*7c478bd9Sstevel@tonic-gate 	int 		result;
529*7c478bd9Sstevel@tonic-gate 	Entry		*mtab;
530*7c478bd9Sstevel@tonic-gate 	Entry		*ep;
531*7c478bd9Sstevel@tonic-gate 	char		*p;
532*7c478bd9Sstevel@tonic-gate 	int		lev1 = 0;
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate 	uint16_t	u16_val;
535*7c478bd9Sstevel@tonic-gate 	uint32_t	u32_val;
536*7c478bd9Sstevel@tonic-gate 	uint64_t	u64_val;
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate 	if (first) {
539*7c478bd9Sstevel@tonic-gate 		mtab = mtab1;
540*7c478bd9Sstevel@tonic-gate 	} else {
541*7c478bd9Sstevel@tonic-gate 		mtab = mtab2;
542*7c478bd9Sstevel@tonic-gate 	}
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate 	if (mtab == (Entry *)NULL) {
545*7c478bd9Sstevel@tonic-gate 		return (0);	/* no magic file tests in this table */
546*7c478bd9Sstevel@tonic-gate 	}
547*7c478bd9Sstevel@tonic-gate 
548*7c478bd9Sstevel@tonic-gate 	for (ep = mtab; ep->e_off != -1L; ep++) {  /* -1 offset marks end of */
549*7c478bd9Sstevel@tonic-gate 		if (lev1) {			/* valid magic file entries */
550*7c478bd9Sstevel@tonic-gate 			if (ep->e_level != 1)
551*7c478bd9Sstevel@tonic-gate 				break;
552*7c478bd9Sstevel@tonic-gate 		} else if (ep->e_level == 1) {
553*7c478bd9Sstevel@tonic-gate 			continue;
554*7c478bd9Sstevel@tonic-gate 		}
555*7c478bd9Sstevel@tonic-gate 		if (ep->e_off > (off_t)bufsize)
556*7c478bd9Sstevel@tonic-gate 			continue;
557*7c478bd9Sstevel@tonic-gate 		p = &buf[ep->e_off];
558*7c478bd9Sstevel@tonic-gate 		switch (ep->e_type) {
559*7c478bd9Sstevel@tonic-gate 		case STR:
560*7c478bd9Sstevel@tonic-gate 		{
561*7c478bd9Sstevel@tonic-gate 			if (strncmp(p, ep->e_value.str,
562*7c478bd9Sstevel@tonic-gate 			    strlen(ep->e_value.str)))
563*7c478bd9Sstevel@tonic-gate 				continue;
564*7c478bd9Sstevel@tonic-gate 			if (lev1) {
565*7c478bd9Sstevel@tonic-gate 				(void) putchar(' ');
566*7c478bd9Sstevel@tonic-gate 			}
567*7c478bd9Sstevel@tonic-gate 			if (ep->e_opcode & SUB)
568*7c478bd9Sstevel@tonic-gate 				(void) printf(ep->e_str,
569*7c478bd9Sstevel@tonic-gate 				    ep->e_value.str);
570*7c478bd9Sstevel@tonic-gate 			else
571*7c478bd9Sstevel@tonic-gate 				(void) printf(ep->e_str);
572*7c478bd9Sstevel@tonic-gate 			lev1 = 1;
573*7c478bd9Sstevel@tonic-gate 			continue;
574*7c478bd9Sstevel@tonic-gate 			/*
575*7c478bd9Sstevel@tonic-gate 			 * We've matched the string and printed the message;
576*7c478bd9Sstevel@tonic-gate 			 * no STR processing occurs beyond this point.
577*7c478bd9Sstevel@tonic-gate 			 */
578*7c478bd9Sstevel@tonic-gate 		}
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 		case BYTE:
581*7c478bd9Sstevel@tonic-gate 		case UBYTE:
582*7c478bd9Sstevel@tonic-gate 			u64_val = (uint64_t)(uint8_t)(*p);
583*7c478bd9Sstevel@tonic-gate 			break;
584*7c478bd9Sstevel@tonic-gate 
585*7c478bd9Sstevel@tonic-gate 		case SHORT:
586*7c478bd9Sstevel@tonic-gate 		case USHORT:
587*7c478bd9Sstevel@tonic-gate 			(void) memcpy(&u16_val, p, sizeof (uint16_t));
588*7c478bd9Sstevel@tonic-gate 			u64_val = (uint64_t)u16_val;
589*7c478bd9Sstevel@tonic-gate 			break;
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate 		case LONG:
592*7c478bd9Sstevel@tonic-gate 		case ULONG:
593*7c478bd9Sstevel@tonic-gate 			(void) memcpy(&u32_val, p, sizeof (uint32_t));
594*7c478bd9Sstevel@tonic-gate 			u64_val = (uint64_t)u32_val;
595*7c478bd9Sstevel@tonic-gate 			break;
596*7c478bd9Sstevel@tonic-gate 
597*7c478bd9Sstevel@tonic-gate 		case LLONG:
598*7c478bd9Sstevel@tonic-gate 		case ULLONG:
599*7c478bd9Sstevel@tonic-gate 			(void) memcpy(&(u64_val), p, sizeof (uint64_t));
600*7c478bd9Sstevel@tonic-gate 			break;
601*7c478bd9Sstevel@tonic-gate 
602*7c478bd9Sstevel@tonic-gate 		}
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate 		if (ep->e_mask) {
605*7c478bd9Sstevel@tonic-gate 			u64_val &= ep->e_mask;
606*7c478bd9Sstevel@tonic-gate 		}
607*7c478bd9Sstevel@tonic-gate 
608*7c478bd9Sstevel@tonic-gate 		/*
609*7c478bd9Sstevel@tonic-gate 		 * Compare the values according to the size and sign
610*7c478bd9Sstevel@tonic-gate 		 * of the type.  For =, &, and ^ operators, the sign
611*7c478bd9Sstevel@tonic-gate 		 * does not have any effect, so these are always compared
612*7c478bd9Sstevel@tonic-gate 		 * unsigned.  Only for < and > operators is the
613*7c478bd9Sstevel@tonic-gate 		 * sign significant.
614*7c478bd9Sstevel@tonic-gate 		 * If the file value was masked, the compare should
615*7c478bd9Sstevel@tonic-gate 		 * be unsigned.
616*7c478bd9Sstevel@tonic-gate 		 */
617*7c478bd9Sstevel@tonic-gate 		switch (ep->e_opcode & ~SUB) {
618*7c478bd9Sstevel@tonic-gate 		case EQ:
619*7c478bd9Sstevel@tonic-gate 			switch (ep->e_type) {
620*7c478bd9Sstevel@tonic-gate 			case BYTE:
621*7c478bd9Sstevel@tonic-gate 			case UBYTE:
622*7c478bd9Sstevel@tonic-gate 				if ((uint8_t)u64_val !=
623*7c478bd9Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
624*7c478bd9Sstevel@tonic-gate 					continue;
625*7c478bd9Sstevel@tonic-gate 				break;
626*7c478bd9Sstevel@tonic-gate 			case SHORT:
627*7c478bd9Sstevel@tonic-gate 			case USHORT:
628*7c478bd9Sstevel@tonic-gate 				if ((uint16_t)u64_val !=
629*7c478bd9Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
630*7c478bd9Sstevel@tonic-gate 					continue;
631*7c478bd9Sstevel@tonic-gate 				break;
632*7c478bd9Sstevel@tonic-gate 			case LONG:
633*7c478bd9Sstevel@tonic-gate 			case ULONG:
634*7c478bd9Sstevel@tonic-gate 				if ((uint32_t)u64_val !=
635*7c478bd9Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
636*7c478bd9Sstevel@tonic-gate 					continue;
637*7c478bd9Sstevel@tonic-gate 				break;
638*7c478bd9Sstevel@tonic-gate 			case LLONG:
639*7c478bd9Sstevel@tonic-gate 			case ULLONG:
640*7c478bd9Sstevel@tonic-gate 				if (u64_val != ep->e_value.num)
641*7c478bd9Sstevel@tonic-gate 					continue;
642*7c478bd9Sstevel@tonic-gate 				break;
643*7c478bd9Sstevel@tonic-gate 			default:
644*7c478bd9Sstevel@tonic-gate 				continue;
645*7c478bd9Sstevel@tonic-gate 			}
646*7c478bd9Sstevel@tonic-gate 			break;
647*7c478bd9Sstevel@tonic-gate 		case GT:
648*7c478bd9Sstevel@tonic-gate 			switch (ep->e_type) {
649*7c478bd9Sstevel@tonic-gate 			case BYTE:
650*7c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
651*7c478bd9Sstevel@tonic-gate 					if ((int8_t)u64_val <=
652*7c478bd9Sstevel@tonic-gate 					    (int8_t)(ep->e_value.num))
653*7c478bd9Sstevel@tonic-gate 						continue;
654*7c478bd9Sstevel@tonic-gate 					break;
655*7c478bd9Sstevel@tonic-gate 				}
656*7c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
657*7c478bd9Sstevel@tonic-gate 			case UBYTE:
658*7c478bd9Sstevel@tonic-gate 				if ((uint8_t)u64_val <=
659*7c478bd9Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
660*7c478bd9Sstevel@tonic-gate 					continue;
661*7c478bd9Sstevel@tonic-gate 				break;
662*7c478bd9Sstevel@tonic-gate 			case SHORT:
663*7c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
664*7c478bd9Sstevel@tonic-gate 					if ((int16_t)u64_val <=
665*7c478bd9Sstevel@tonic-gate 					    (int16_t)(ep->e_value.num))
666*7c478bd9Sstevel@tonic-gate 						continue;
667*7c478bd9Sstevel@tonic-gate 					break;
668*7c478bd9Sstevel@tonic-gate 				}
669*7c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
670*7c478bd9Sstevel@tonic-gate 			case USHORT:
671*7c478bd9Sstevel@tonic-gate 				if ((uint16_t)u64_val <=
672*7c478bd9Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
673*7c478bd9Sstevel@tonic-gate 					continue;
674*7c478bd9Sstevel@tonic-gate 				break;
675*7c478bd9Sstevel@tonic-gate 			case LONG:
676*7c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
677*7c478bd9Sstevel@tonic-gate 					if ((int32_t)u64_val <=
678*7c478bd9Sstevel@tonic-gate 					    (int32_t)(ep->e_value.num))
679*7c478bd9Sstevel@tonic-gate 						continue;
680*7c478bd9Sstevel@tonic-gate 					break;
681*7c478bd9Sstevel@tonic-gate 				}
682*7c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
683*7c478bd9Sstevel@tonic-gate 			case ULONG:
684*7c478bd9Sstevel@tonic-gate 				if ((uint32_t)u64_val <=
685*7c478bd9Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
686*7c478bd9Sstevel@tonic-gate 					continue;
687*7c478bd9Sstevel@tonic-gate 				break;
688*7c478bd9Sstevel@tonic-gate 			case LLONG:
689*7c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
690*7c478bd9Sstevel@tonic-gate 					if ((int64_t)u64_val <=
691*7c478bd9Sstevel@tonic-gate 					    (int64_t)(ep->e_value.num))
692*7c478bd9Sstevel@tonic-gate 						continue;
693*7c478bd9Sstevel@tonic-gate 					break;
694*7c478bd9Sstevel@tonic-gate 				}
695*7c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
696*7c478bd9Sstevel@tonic-gate 			case ULLONG:
697*7c478bd9Sstevel@tonic-gate 				if (u64_val <= ep->e_value.num)
698*7c478bd9Sstevel@tonic-gate 					continue;
699*7c478bd9Sstevel@tonic-gate 				break;
700*7c478bd9Sstevel@tonic-gate 			default:
701*7c478bd9Sstevel@tonic-gate 				continue;
702*7c478bd9Sstevel@tonic-gate 			}
703*7c478bd9Sstevel@tonic-gate 			break;
704*7c478bd9Sstevel@tonic-gate 		case LT:
705*7c478bd9Sstevel@tonic-gate 			switch (ep->e_type) {
706*7c478bd9Sstevel@tonic-gate 			case BYTE:
707*7c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
708*7c478bd9Sstevel@tonic-gate 					if ((int8_t)u64_val >=
709*7c478bd9Sstevel@tonic-gate 					    (int8_t)(ep->e_value.num))
710*7c478bd9Sstevel@tonic-gate 						continue;
711*7c478bd9Sstevel@tonic-gate 					break;
712*7c478bd9Sstevel@tonic-gate 				}
713*7c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
714*7c478bd9Sstevel@tonic-gate 			case UBYTE:
715*7c478bd9Sstevel@tonic-gate 				if ((uint8_t)u64_val >=
716*7c478bd9Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
717*7c478bd9Sstevel@tonic-gate 					continue;
718*7c478bd9Sstevel@tonic-gate 				break;
719*7c478bd9Sstevel@tonic-gate 			case SHORT:
720*7c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
721*7c478bd9Sstevel@tonic-gate 					if ((int16_t)u64_val >=
722*7c478bd9Sstevel@tonic-gate 					    (int16_t)(ep->e_value.num))
723*7c478bd9Sstevel@tonic-gate 						continue;
724*7c478bd9Sstevel@tonic-gate 					break;
725*7c478bd9Sstevel@tonic-gate 				}
726*7c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
727*7c478bd9Sstevel@tonic-gate 			case USHORT:
728*7c478bd9Sstevel@tonic-gate 				if ((uint16_t)u64_val >=
729*7c478bd9Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
730*7c478bd9Sstevel@tonic-gate 					continue;
731*7c478bd9Sstevel@tonic-gate 				break;
732*7c478bd9Sstevel@tonic-gate 			case LONG:
733*7c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
734*7c478bd9Sstevel@tonic-gate 					if ((int32_t)u64_val >=
735*7c478bd9Sstevel@tonic-gate 					    (int32_t)(ep->e_value.num))
736*7c478bd9Sstevel@tonic-gate 						continue;
737*7c478bd9Sstevel@tonic-gate 					break;
738*7c478bd9Sstevel@tonic-gate 				}
739*7c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
740*7c478bd9Sstevel@tonic-gate 			case ULONG:
741*7c478bd9Sstevel@tonic-gate 				if ((uint32_t)u64_val >=
742*7c478bd9Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
743*7c478bd9Sstevel@tonic-gate 					continue;
744*7c478bd9Sstevel@tonic-gate 				break;
745*7c478bd9Sstevel@tonic-gate 			case LLONG:
746*7c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
747*7c478bd9Sstevel@tonic-gate 					if ((int64_t)u64_val >=
748*7c478bd9Sstevel@tonic-gate 					    (int64_t)(ep->e_value.num))
749*7c478bd9Sstevel@tonic-gate 						continue;
750*7c478bd9Sstevel@tonic-gate 					break;
751*7c478bd9Sstevel@tonic-gate 				}
752*7c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
753*7c478bd9Sstevel@tonic-gate 			case ULLONG:
754*7c478bd9Sstevel@tonic-gate 				if (u64_val >= ep->e_value.num)
755*7c478bd9Sstevel@tonic-gate 					continue;
756*7c478bd9Sstevel@tonic-gate 				break;
757*7c478bd9Sstevel@tonic-gate 			default:
758*7c478bd9Sstevel@tonic-gate 				continue;
759*7c478bd9Sstevel@tonic-gate 			}
760*7c478bd9Sstevel@tonic-gate 			break;
761*7c478bd9Sstevel@tonic-gate 		case AND:
762*7c478bd9Sstevel@tonic-gate 			switch (ep->e_type) {
763*7c478bd9Sstevel@tonic-gate 			case BYTE:
764*7c478bd9Sstevel@tonic-gate 			case UBYTE:
765*7c478bd9Sstevel@tonic-gate 				if (((uint8_t)u64_val &
766*7c478bd9Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num)) ==
767*7c478bd9Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
768*7c478bd9Sstevel@tonic-gate 					break;
769*7c478bd9Sstevel@tonic-gate 				continue;
770*7c478bd9Sstevel@tonic-gate 			case SHORT:
771*7c478bd9Sstevel@tonic-gate 			case USHORT:
772*7c478bd9Sstevel@tonic-gate 				if (((uint16_t)u64_val &
773*7c478bd9Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num)) ==
774*7c478bd9Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
775*7c478bd9Sstevel@tonic-gate 					break;
776*7c478bd9Sstevel@tonic-gate 				continue;
777*7c478bd9Sstevel@tonic-gate 			case LONG:
778*7c478bd9Sstevel@tonic-gate 			case ULONG:
779*7c478bd9Sstevel@tonic-gate 				if (((uint32_t)u64_val &
780*7c478bd9Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num)) ==
781*7c478bd9Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
782*7c478bd9Sstevel@tonic-gate 					break;
783*7c478bd9Sstevel@tonic-gate 				continue;
784*7c478bd9Sstevel@tonic-gate 			case LLONG:
785*7c478bd9Sstevel@tonic-gate 			case ULLONG:
786*7c478bd9Sstevel@tonic-gate 				if ((u64_val & ep->e_value.num) ==
787*7c478bd9Sstevel@tonic-gate 				    ep->e_value.num)
788*7c478bd9Sstevel@tonic-gate 					break;
789*7c478bd9Sstevel@tonic-gate 				continue;
790*7c478bd9Sstevel@tonic-gate 			default:
791*7c478bd9Sstevel@tonic-gate 				continue;
792*7c478bd9Sstevel@tonic-gate 			}
793*7c478bd9Sstevel@tonic-gate 			break;
794*7c478bd9Sstevel@tonic-gate 		case NSET:
795*7c478bd9Sstevel@tonic-gate 			switch (ep->e_type) {
796*7c478bd9Sstevel@tonic-gate 			case BYTE:
797*7c478bd9Sstevel@tonic-gate 			case UBYTE:
798*7c478bd9Sstevel@tonic-gate 				if (((uint8_t)u64_val &
799*7c478bd9Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num)) !=
800*7c478bd9Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
801*7c478bd9Sstevel@tonic-gate 					break;
802*7c478bd9Sstevel@tonic-gate 				continue;
803*7c478bd9Sstevel@tonic-gate 			case SHORT:
804*7c478bd9Sstevel@tonic-gate 			case USHORT:
805*7c478bd9Sstevel@tonic-gate 				if (((uint16_t)u64_val &
806*7c478bd9Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num)) !=
807*7c478bd9Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
808*7c478bd9Sstevel@tonic-gate 					break;
809*7c478bd9Sstevel@tonic-gate 				continue;
810*7c478bd9Sstevel@tonic-gate 			case LONG:
811*7c478bd9Sstevel@tonic-gate 			case ULONG:
812*7c478bd9Sstevel@tonic-gate 				if (((uint32_t)u64_val &
813*7c478bd9Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num)) !=
814*7c478bd9Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
815*7c478bd9Sstevel@tonic-gate 					break;
816*7c478bd9Sstevel@tonic-gate 				continue;
817*7c478bd9Sstevel@tonic-gate 			case LLONG:
818*7c478bd9Sstevel@tonic-gate 			case ULLONG:
819*7c478bd9Sstevel@tonic-gate 				if ((u64_val & ep->e_value.num) !=
820*7c478bd9Sstevel@tonic-gate 				    ep->e_value.num)
821*7c478bd9Sstevel@tonic-gate 					break;
822*7c478bd9Sstevel@tonic-gate 				continue;
823*7c478bd9Sstevel@tonic-gate 			default:
824*7c478bd9Sstevel@tonic-gate 				continue;
825*7c478bd9Sstevel@tonic-gate 			}
826*7c478bd9Sstevel@tonic-gate 			break;
827*7c478bd9Sstevel@tonic-gate 		case ANY:	/* matches anything */
828*7c478bd9Sstevel@tonic-gate 			break;
829*7c478bd9Sstevel@tonic-gate 		default:	/* shouldn't occur; ignore it */
830*7c478bd9Sstevel@tonic-gate 			continue;
831*7c478bd9Sstevel@tonic-gate 		}
832*7c478bd9Sstevel@tonic-gate 		if (lev1)
833*7c478bd9Sstevel@tonic-gate 			(void) putchar(' ');
834*7c478bd9Sstevel@tonic-gate 		if (ep->e_opcode & SUB) {
835*7c478bd9Sstevel@tonic-gate 			switch (ep->e_type) {
836*7c478bd9Sstevel@tonic-gate 			case LLONG:
837*7c478bd9Sstevel@tonic-gate #ifdef XPG4
838*7c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
839*7c478bd9Sstevel@tonic-gate 					(void) printf(ep->e_str,
840*7c478bd9Sstevel@tonic-gate 						(int64_t)u64_val);
841*7c478bd9Sstevel@tonic-gate 					break;
842*7c478bd9Sstevel@tonic-gate 				}
843*7c478bd9Sstevel@tonic-gate #endif	/* XPG4 */
844*7c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
845*7c478bd9Sstevel@tonic-gate 			case ULLONG:
846*7c478bd9Sstevel@tonic-gate 				(void) printf(ep->e_str, u64_val);
847*7c478bd9Sstevel@tonic-gate 				break;
848*7c478bd9Sstevel@tonic-gate 			case LONG:
849*7c478bd9Sstevel@tonic-gate #ifdef XPG4
850*7c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
851*7c478bd9Sstevel@tonic-gate 					(void) printf(ep->e_str,
852*7c478bd9Sstevel@tonic-gate 						(int32_t)u64_val);
853*7c478bd9Sstevel@tonic-gate 					break;
854*7c478bd9Sstevel@tonic-gate 				}
855*7c478bd9Sstevel@tonic-gate #endif	/* XPG4 */
856*7c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
857*7c478bd9Sstevel@tonic-gate 			case ULONG:
858*7c478bd9Sstevel@tonic-gate 				(void) printf(ep->e_str,
859*7c478bd9Sstevel@tonic-gate 				    (uint32_t)u64_val);
860*7c478bd9Sstevel@tonic-gate 				break;
861*7c478bd9Sstevel@tonic-gate 			case SHORT:
862*7c478bd9Sstevel@tonic-gate #ifdef XPG4
863*7c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
864*7c478bd9Sstevel@tonic-gate 					(void) printf(ep->e_str,
865*7c478bd9Sstevel@tonic-gate 						(int16_t)u64_val);
866*7c478bd9Sstevel@tonic-gate 					break;
867*7c478bd9Sstevel@tonic-gate 				}
868*7c478bd9Sstevel@tonic-gate #endif	/* XPG4 */
869*7c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
870*7c478bd9Sstevel@tonic-gate 			case USHORT:
871*7c478bd9Sstevel@tonic-gate 				(void) printf(ep->e_str,
872*7c478bd9Sstevel@tonic-gate 				    (uint16_t)u64_val);
873*7c478bd9Sstevel@tonic-gate 				break;
874*7c478bd9Sstevel@tonic-gate 			case BYTE:
875*7c478bd9Sstevel@tonic-gate #ifdef XPG4
876*7c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
877*7c478bd9Sstevel@tonic-gate 					(void) printf(ep->e_str,
878*7c478bd9Sstevel@tonic-gate 						(int8_t)u64_val);
879*7c478bd9Sstevel@tonic-gate 					break;
880*7c478bd9Sstevel@tonic-gate 				}
881*7c478bd9Sstevel@tonic-gate #endif	/* XPG4 */
882*7c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
883*7c478bd9Sstevel@tonic-gate 			case UBYTE:
884*7c478bd9Sstevel@tonic-gate 				(void) printf(ep->e_str,
885*7c478bd9Sstevel@tonic-gate 				    (uint8_t)u64_val);
886*7c478bd9Sstevel@tonic-gate 				break;
887*7c478bd9Sstevel@tonic-gate 			case STR:
888*7c478bd9Sstevel@tonic-gate 				/*
889*7c478bd9Sstevel@tonic-gate 				 * Note: Currently can't get type
890*7c478bd9Sstevel@tonic-gate 				 * STR here because we already
891*7c478bd9Sstevel@tonic-gate 				 * did a 'continue' out of the
892*7c478bd9Sstevel@tonic-gate 				 * loop earlier for case STR
893*7c478bd9Sstevel@tonic-gate 				 */
894*7c478bd9Sstevel@tonic-gate 				break;
895*7c478bd9Sstevel@tonic-gate 			}
896*7c478bd9Sstevel@tonic-gate 		} else
897*7c478bd9Sstevel@tonic-gate 			(void) printf(ep->e_str);
898*7c478bd9Sstevel@tonic-gate 		lev1 = 1;
899*7c478bd9Sstevel@tonic-gate 	}
900*7c478bd9Sstevel@tonic-gate 	result = lev1 ? (int)(1 + ep - mtab) : 0;
901*7c478bd9Sstevel@tonic-gate 
902*7c478bd9Sstevel@tonic-gate 	return (result);
903*7c478bd9Sstevel@tonic-gate }
904*7c478bd9Sstevel@tonic-gate 
905*7c478bd9Sstevel@tonic-gate static void
906*7c478bd9Sstevel@tonic-gate showstr(char *s, int width)
907*7c478bd9Sstevel@tonic-gate {
908*7c478bd9Sstevel@tonic-gate 	char c;
909*7c478bd9Sstevel@tonic-gate 
910*7c478bd9Sstevel@tonic-gate 	while ((c = *s++) != '\0')
911*7c478bd9Sstevel@tonic-gate 		if (c >= 040 && c < 0176) {
912*7c478bd9Sstevel@tonic-gate 			(void) putchar(c);
913*7c478bd9Sstevel@tonic-gate 			width--;
914*7c478bd9Sstevel@tonic-gate 		} else {
915*7c478bd9Sstevel@tonic-gate 			(void) putchar('\\');
916*7c478bd9Sstevel@tonic-gate 			switch (c) {
917*7c478bd9Sstevel@tonic-gate 
918*7c478bd9Sstevel@tonic-gate 			case '\n':
919*7c478bd9Sstevel@tonic-gate 				(void) putchar('n');
920*7c478bd9Sstevel@tonic-gate 				width -= 2;
921*7c478bd9Sstevel@tonic-gate 				break;
922*7c478bd9Sstevel@tonic-gate 
923*7c478bd9Sstevel@tonic-gate 			case '\r':
924*7c478bd9Sstevel@tonic-gate 				(void) putchar('r');
925*7c478bd9Sstevel@tonic-gate 				width -= 2;
926*7c478bd9Sstevel@tonic-gate 				break;
927*7c478bd9Sstevel@tonic-gate 
928*7c478bd9Sstevel@tonic-gate 			case '\a':
929*7c478bd9Sstevel@tonic-gate 				(void) putchar('a');
930*7c478bd9Sstevel@tonic-gate 				width -= 2;
931*7c478bd9Sstevel@tonic-gate 				break;
932*7c478bd9Sstevel@tonic-gate 
933*7c478bd9Sstevel@tonic-gate 			case '\b':
934*7c478bd9Sstevel@tonic-gate 				(void) putchar('b');
935*7c478bd9Sstevel@tonic-gate 				width -= 2;
936*7c478bd9Sstevel@tonic-gate 				break;
937*7c478bd9Sstevel@tonic-gate 
938*7c478bd9Sstevel@tonic-gate 			case '\t':
939*7c478bd9Sstevel@tonic-gate 				(void) putchar('t');
940*7c478bd9Sstevel@tonic-gate 				width -= 2;
941*7c478bd9Sstevel@tonic-gate 				break;
942*7c478bd9Sstevel@tonic-gate 
943*7c478bd9Sstevel@tonic-gate 			case '\f':
944*7c478bd9Sstevel@tonic-gate 				(void) putchar('f');
945*7c478bd9Sstevel@tonic-gate 				width -= 2;
946*7c478bd9Sstevel@tonic-gate 				break;
947*7c478bd9Sstevel@tonic-gate 
948*7c478bd9Sstevel@tonic-gate 			case '\v':
949*7c478bd9Sstevel@tonic-gate 				(void) putchar('v');
950*7c478bd9Sstevel@tonic-gate 				width -= 2;
951*7c478bd9Sstevel@tonic-gate 				break;
952*7c478bd9Sstevel@tonic-gate 
953*7c478bd9Sstevel@tonic-gate 			default:
954*7c478bd9Sstevel@tonic-gate 				(void) printf("%.3o", c & 0377);
955*7c478bd9Sstevel@tonic-gate 				width -= 4;
956*7c478bd9Sstevel@tonic-gate 				break;
957*7c478bd9Sstevel@tonic-gate 			}
958*7c478bd9Sstevel@tonic-gate 		}
959*7c478bd9Sstevel@tonic-gate 	while (width >= 0) {
960*7c478bd9Sstevel@tonic-gate 		(void) putchar(' ');
961*7c478bd9Sstevel@tonic-gate 		width--;
962*7c478bd9Sstevel@tonic-gate 	};
963*7c478bd9Sstevel@tonic-gate }
964*7c478bd9Sstevel@tonic-gate 
965*7c478bd9Sstevel@tonic-gate static char *
966*7c478bd9Sstevel@tonic-gate type_to_name(Entry *ep)
967*7c478bd9Sstevel@tonic-gate {
968*7c478bd9Sstevel@tonic-gate 	static char buf[20];
969*7c478bd9Sstevel@tonic-gate 	char	*s;
970*7c478bd9Sstevel@tonic-gate 
971*7c478bd9Sstevel@tonic-gate 	switch (ep->e_type) {
972*7c478bd9Sstevel@tonic-gate 	case BYTE:
973*7c478bd9Sstevel@tonic-gate 		s = "byte";
974*7c478bd9Sstevel@tonic-gate 		break;
975*7c478bd9Sstevel@tonic-gate 	case SHORT:
976*7c478bd9Sstevel@tonic-gate 		s = "short";
977*7c478bd9Sstevel@tonic-gate 		break;
978*7c478bd9Sstevel@tonic-gate 	case LONG:
979*7c478bd9Sstevel@tonic-gate 		s = "long";
980*7c478bd9Sstevel@tonic-gate 		break;
981*7c478bd9Sstevel@tonic-gate 	case LLONG:
982*7c478bd9Sstevel@tonic-gate 		s = "llong";
983*7c478bd9Sstevel@tonic-gate 		break;
984*7c478bd9Sstevel@tonic-gate 	case UBYTE:
985*7c478bd9Sstevel@tonic-gate 		s = "ubyte";
986*7c478bd9Sstevel@tonic-gate 		break;
987*7c478bd9Sstevel@tonic-gate 	case USHORT:
988*7c478bd9Sstevel@tonic-gate 		s = "ushort";
989*7c478bd9Sstevel@tonic-gate 		break;
990*7c478bd9Sstevel@tonic-gate 	case ULONG:
991*7c478bd9Sstevel@tonic-gate 		s = "ulong";
992*7c478bd9Sstevel@tonic-gate 		break;
993*7c478bd9Sstevel@tonic-gate 	case ULLONG:
994*7c478bd9Sstevel@tonic-gate 		s = "ullong";
995*7c478bd9Sstevel@tonic-gate 		break;
996*7c478bd9Sstevel@tonic-gate 	case STR:
997*7c478bd9Sstevel@tonic-gate 		return ("string");
998*7c478bd9Sstevel@tonic-gate 	default:
999*7c478bd9Sstevel@tonic-gate 		/* more of an emergency measure .. */
1000*7c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "%d", ep->e_type);
1001*7c478bd9Sstevel@tonic-gate 		return (buf);
1002*7c478bd9Sstevel@tonic-gate 	}
1003*7c478bd9Sstevel@tonic-gate 	if (ep->e_mask) {
1004*7c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s&0x%llx", s, ep->e_mask);
1005*7c478bd9Sstevel@tonic-gate 		return (buf);
1006*7c478bd9Sstevel@tonic-gate 	} else
1007*7c478bd9Sstevel@tonic-gate 		return (s);
1008*7c478bd9Sstevel@tonic-gate }
1009*7c478bd9Sstevel@tonic-gate 
1010*7c478bd9Sstevel@tonic-gate static char
1011*7c478bd9Sstevel@tonic-gate op_to_name(char op)
1012*7c478bd9Sstevel@tonic-gate {
1013*7c478bd9Sstevel@tonic-gate 	char c;
1014*7c478bd9Sstevel@tonic-gate 
1015*7c478bd9Sstevel@tonic-gate 	switch (op & ~SUB) {
1016*7c478bd9Sstevel@tonic-gate 
1017*7c478bd9Sstevel@tonic-gate 	case EQ:
1018*7c478bd9Sstevel@tonic-gate 	case STRC:
1019*7c478bd9Sstevel@tonic-gate 		c = '=';
1020*7c478bd9Sstevel@tonic-gate 		break;
1021*7c478bd9Sstevel@tonic-gate 
1022*7c478bd9Sstevel@tonic-gate 	case GT:
1023*7c478bd9Sstevel@tonic-gate 		c = '>';
1024*7c478bd9Sstevel@tonic-gate 		break;
1025*7c478bd9Sstevel@tonic-gate 
1026*7c478bd9Sstevel@tonic-gate 	case LT:
1027*7c478bd9Sstevel@tonic-gate 		c = '<';
1028*7c478bd9Sstevel@tonic-gate 		break;
1029*7c478bd9Sstevel@tonic-gate 
1030*7c478bd9Sstevel@tonic-gate 	case ANY:
1031*7c478bd9Sstevel@tonic-gate 		c = 'x';
1032*7c478bd9Sstevel@tonic-gate 		break;
1033*7c478bd9Sstevel@tonic-gate 
1034*7c478bd9Sstevel@tonic-gate 	case AND:
1035*7c478bd9Sstevel@tonic-gate 		c = '&';
1036*7c478bd9Sstevel@tonic-gate 		break;
1037*7c478bd9Sstevel@tonic-gate 
1038*7c478bd9Sstevel@tonic-gate 	case NSET:
1039*7c478bd9Sstevel@tonic-gate 		c = '^';
1040*7c478bd9Sstevel@tonic-gate 		break;
1041*7c478bd9Sstevel@tonic-gate 
1042*7c478bd9Sstevel@tonic-gate 	default:
1043*7c478bd9Sstevel@tonic-gate 		c = '?';
1044*7c478bd9Sstevel@tonic-gate 		break;
1045*7c478bd9Sstevel@tonic-gate 	}
1046*7c478bd9Sstevel@tonic-gate 
1047*7c478bd9Sstevel@tonic-gate 	return (c);
1048*7c478bd9Sstevel@tonic-gate }
1049*7c478bd9Sstevel@tonic-gate 
1050*7c478bd9Sstevel@tonic-gate /*
1051*7c478bd9Sstevel@tonic-gate  * f_prtmtab - Prints out a header, then entries from both magic
1052*7c478bd9Sstevel@tonic-gate  *	tables, mtab1 and mtab2, if any exist.
1053*7c478bd9Sstevel@tonic-gate  */
1054*7c478bd9Sstevel@tonic-gate void
1055*7c478bd9Sstevel@tonic-gate f_prtmtab(void)
1056*7c478bd9Sstevel@tonic-gate {
1057*7c478bd9Sstevel@tonic-gate 	Entry	*mtab;
1058*7c478bd9Sstevel@tonic-gate 	Entry	*ep;
1059*7c478bd9Sstevel@tonic-gate 	int	count;
1060*7c478bd9Sstevel@tonic-gate 
1061*7c478bd9Sstevel@tonic-gate 	(void) printf("%-7s %-7s %-10s %-7s %-11s %s\n",
1062*7c478bd9Sstevel@tonic-gate 		"level", "off", "type", "opcode", "value", "string");
1063*7c478bd9Sstevel@tonic-gate 	for (mtab = mtab1, count = 1; count <= 2; count++, mtab = mtab2) {
1064*7c478bd9Sstevel@tonic-gate 		if (mtab == (Entry *)NULL) {
1065*7c478bd9Sstevel@tonic-gate 			continue;
1066*7c478bd9Sstevel@tonic-gate 		}
1067*7c478bd9Sstevel@tonic-gate 		for (ep = mtab; ep->e_off != -1L; ep++) {
1068*7c478bd9Sstevel@tonic-gate 			(void) printf("%-7d %-7ld %-10s %-7c ",
1069*7c478bd9Sstevel@tonic-gate 			    ep->e_level,
1070*7c478bd9Sstevel@tonic-gate 			    ep->e_off, type_to_name(ep),
1071*7c478bd9Sstevel@tonic-gate 			    op_to_name(ep->e_opcode));
1072*7c478bd9Sstevel@tonic-gate 			if (ep->e_type == STR) {
1073*7c478bd9Sstevel@tonic-gate 				showstr(ep->e_value.str, 10);
1074*7c478bd9Sstevel@tonic-gate 			} else {	/* numeric */
1075*7c478bd9Sstevel@tonic-gate 				(void) printf("%-#11llo", ep->e_value.num);
1076*7c478bd9Sstevel@tonic-gate 			}
1077*7c478bd9Sstevel@tonic-gate 			(void) printf(" %s", ep->e_str);
1078*7c478bd9Sstevel@tonic-gate 			if (ep->e_opcode & SUB)
1079*7c478bd9Sstevel@tonic-gate 				(void) printf("\tsubst");
1080*7c478bd9Sstevel@tonic-gate 			(void) printf("\n");
1081*7c478bd9Sstevel@tonic-gate 		}
1082*7c478bd9Sstevel@tonic-gate 	}
1083*7c478bd9Sstevel@tonic-gate }
1084*7c478bd9Sstevel@tonic-gate 
1085*7c478bd9Sstevel@tonic-gate intmax_t
1086*7c478bd9Sstevel@tonic-gate f_getmaxoffset(int first)
1087*7c478bd9Sstevel@tonic-gate {
1088*7c478bd9Sstevel@tonic-gate 	Entry *mtab;
1089*7c478bd9Sstevel@tonic-gate 	Entry *ep;
1090*7c478bd9Sstevel@tonic-gate 	intmax_t cur;
1091*7c478bd9Sstevel@tonic-gate 	intmax_t max = 0;
1092*7c478bd9Sstevel@tonic-gate 
1093*7c478bd9Sstevel@tonic-gate 	if (first) {
1094*7c478bd9Sstevel@tonic-gate 		mtab = mtab1;
1095*7c478bd9Sstevel@tonic-gate 	} else {
1096*7c478bd9Sstevel@tonic-gate 		mtab = mtab2;
1097*7c478bd9Sstevel@tonic-gate 	}
1098*7c478bd9Sstevel@tonic-gate 	if (mtab == (Entry *)NULL) {
1099*7c478bd9Sstevel@tonic-gate 		return (0);
1100*7c478bd9Sstevel@tonic-gate 	}
1101*7c478bd9Sstevel@tonic-gate 	for (ep = mtab; ep->e_off != -1L; ep++) {
1102*7c478bd9Sstevel@tonic-gate 		cur = ep->e_off;
1103*7c478bd9Sstevel@tonic-gate 		switch (ep->e_type) {
1104*7c478bd9Sstevel@tonic-gate 		case STR:
1105*7c478bd9Sstevel@tonic-gate 			cur += strlen(ep->e_value.str);
1106*7c478bd9Sstevel@tonic-gate 			break;
1107*7c478bd9Sstevel@tonic-gate 		case BYTE:
1108*7c478bd9Sstevel@tonic-gate 		case UBYTE:
1109*7c478bd9Sstevel@tonic-gate 			cur += sizeof (uchar_t);
1110*7c478bd9Sstevel@tonic-gate 			break;
1111*7c478bd9Sstevel@tonic-gate 		case SHORT:
1112*7c478bd9Sstevel@tonic-gate 		case USHORT:
1113*7c478bd9Sstevel@tonic-gate 			cur += sizeof (uint16_t);
1114*7c478bd9Sstevel@tonic-gate 			break;
1115*7c478bd9Sstevel@tonic-gate 		case LONG:
1116*7c478bd9Sstevel@tonic-gate 		case ULONG:
1117*7c478bd9Sstevel@tonic-gate 			cur += sizeof (uint32_t);
1118*7c478bd9Sstevel@tonic-gate 			break;
1119*7c478bd9Sstevel@tonic-gate 		case LLONG:
1120*7c478bd9Sstevel@tonic-gate 		case ULLONG:
1121*7c478bd9Sstevel@tonic-gate 			cur += sizeof (uint64_t);
1122*7c478bd9Sstevel@tonic-gate 			break;
1123*7c478bd9Sstevel@tonic-gate 		}
1124*7c478bd9Sstevel@tonic-gate 		if (cur <= INT_MAX && cur > max) {
1125*7c478bd9Sstevel@tonic-gate 			max = cur;
1126*7c478bd9Sstevel@tonic-gate 		}
1127*7c478bd9Sstevel@tonic-gate 	}
1128*7c478bd9Sstevel@tonic-gate 
1129*7c478bd9Sstevel@tonic-gate 	return (max);
1130*7c478bd9Sstevel@tonic-gate }
1131