xref: /titanic_52/usr/src/cmd/file/magicutils.c (revision 9a411307f0d1eedbc81618ec290e0685284d8a2b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9a411307Srie  * Common Development and Distribution License (the "License").
6*9a411307Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*9a411307Srie  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
307c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <limits.h>
407c478bd9Sstevel@tonic-gate #include <inttypes.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <libintl.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  *	Types
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	BYTE	1
497c478bd9Sstevel@tonic-gate #define	SHORT	2
507c478bd9Sstevel@tonic-gate #define	LONG	4
517c478bd9Sstevel@tonic-gate #define	LLONG	8
527c478bd9Sstevel@tonic-gate #define	UBYTE	16
537c478bd9Sstevel@tonic-gate #define	USHORT	32
547c478bd9Sstevel@tonic-gate #define	ULONG	64
557c478bd9Sstevel@tonic-gate #define	ULLONG	128
567c478bd9Sstevel@tonic-gate #define	STR	256
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  *	Opcodes
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	EQ	0
637c478bd9Sstevel@tonic-gate #define	GT	1
647c478bd9Sstevel@tonic-gate #define	LT	2
657c478bd9Sstevel@tonic-gate #define	STRC	3	/* string compare */
667c478bd9Sstevel@tonic-gate #define	ANY	4
677c478bd9Sstevel@tonic-gate #define	AND	5
687c478bd9Sstevel@tonic-gate #define	NSET	6	/* True if bit is not set */
697c478bd9Sstevel@tonic-gate #define	SUB	64	/* or'ed in, SUBstitution string, for example */
707c478bd9Sstevel@tonic-gate 			/* %ld, %s, %lo mask: with bit 6 on, used to locate */
717c478bd9Sstevel@tonic-gate 			/* print formats */
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  *	Misc
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	BSZ	128
777c478bd9Sstevel@tonic-gate #define	NENT	200
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  *	Structure of magic file entry
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate struct	entry	{
847c478bd9Sstevel@tonic-gate 	char		e_level;	/* 0 or 1 */
857c478bd9Sstevel@tonic-gate 	off_t		e_off;		/* in bytes */
867c478bd9Sstevel@tonic-gate 	uint32_t	e_type;		/* BYTE, SHORT, STR, et al */
877c478bd9Sstevel@tonic-gate 	char		e_opcode;	/* EQ, GT, LT, ANY, AND, NSET */
887c478bd9Sstevel@tonic-gate 	uint64_t	e_mask;		/* if non-zero, mask value with this */
897c478bd9Sstevel@tonic-gate 	union	{
907c478bd9Sstevel@tonic-gate 		uint64_t	num;
917c478bd9Sstevel@tonic-gate 		char		*str;
927c478bd9Sstevel@tonic-gate 	}	e_value;
937c478bd9Sstevel@tonic-gate 	const char	*e_str;
947c478bd9Sstevel@tonic-gate };
957c478bd9Sstevel@tonic-gate 
96*9a411307Srie /* Non-localized string giving name of command.  Defined in file.c */
97*9a411307Srie extern const char	*File;
98*9a411307Srie 
997c478bd9Sstevel@tonic-gate typedef	struct entry	Entry;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static Entry	*mtab1;	/* 1st magic table, applied before default tests */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	/*
1047c478bd9Sstevel@tonic-gate 	 * 2nd magic table, includes default tests and magic entries
1057c478bd9Sstevel@tonic-gate 	 * to be applied after default position-sensitive tests
1067c478bd9Sstevel@tonic-gate 	 */
1077c478bd9Sstevel@tonic-gate static Entry	*mtab2;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static Entry	*mend1;	/* one past last-allocated entry in mtab1 */
1107c478bd9Sstevel@tonic-gate static Entry	*mend2;	/* one past last-allocated entry in mtab2 */
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static Entry	*ep1;	/* current entry in mtab1 */
1137c478bd9Sstevel@tonic-gate static Entry	*ep2;	/* current entry in mtab2 */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static char *
116*9a411307Srie getstr(char *p, char *file)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	char	*newstr;
1197c478bd9Sstevel@tonic-gate 	char	*s;
1207c478bd9Sstevel@tonic-gate 	long	val;
1217c478bd9Sstevel@tonic-gate 	int	base;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	newstr = (char *)malloc((strlen(p) + 1) * sizeof (char));
1247c478bd9Sstevel@tonic-gate 	if (newstr == NULL) {
125*9a411307Srie 		int err = errno;
126*9a411307Srie 		(void) fprintf(stderr, gettext("%s: malloc failed: %s\n"),
127*9a411307Srie 		    File, strerror(err));
1287c478bd9Sstevel@tonic-gate 		return (NULL);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	s = newstr;
1327c478bd9Sstevel@tonic-gate 	while (*p != '\0') {
1337c478bd9Sstevel@tonic-gate 		if (*p != '\\') {
1347c478bd9Sstevel@tonic-gate 			*s++ = *p++;
1357c478bd9Sstevel@tonic-gate 			continue;
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 		p++;
1387c478bd9Sstevel@tonic-gate 		if (*p == '\0')
1397c478bd9Sstevel@tonic-gate 			break;
1407c478bd9Sstevel@tonic-gate 		if (isdigit(*p)) {
1417c478bd9Sstevel@tonic-gate 			if (*p == '0' && (*(p+1) == 'x' || *(p+1) == 'X')) {
1427c478bd9Sstevel@tonic-gate 				/* hex */
1437c478bd9Sstevel@tonic-gate 				base = 16;
1447c478bd9Sstevel@tonic-gate 			} else {
1457c478bd9Sstevel@tonic-gate 				base = 8;
1467c478bd9Sstevel@tonic-gate 			}
1477c478bd9Sstevel@tonic-gate 			errno = 0;
1487c478bd9Sstevel@tonic-gate 			val = strtol(p, &p, base);
1497c478bd9Sstevel@tonic-gate 			if (val > UCHAR_MAX || val < 0 || errno != 0) {
150*9a411307Srie 				(void) fprintf(stderr, gettext("%s: %s: magic "
151*9a411307Srie 				    "table invalid string value\n"), File,
152*9a411307Srie 				    file);
1537c478bd9Sstevel@tonic-gate 				return (NULL);
1547c478bd9Sstevel@tonic-gate 			}
1557c478bd9Sstevel@tonic-gate 			*s++ = (char)val;
1567c478bd9Sstevel@tonic-gate 		} else {
1577c478bd9Sstevel@tonic-gate 			/* escape the character */
1587c478bd9Sstevel@tonic-gate 			switch (*p) {
1597c478bd9Sstevel@tonic-gate 			case 'n':
1607c478bd9Sstevel@tonic-gate 				*s = '\n';
1617c478bd9Sstevel@tonic-gate 				break;
1627c478bd9Sstevel@tonic-gate 			case 'r':
1637c478bd9Sstevel@tonic-gate 				*s = '\r';
1647c478bd9Sstevel@tonic-gate 				break;
1657c478bd9Sstevel@tonic-gate 			case 'a':
1667c478bd9Sstevel@tonic-gate 				*s = '\a';
1677c478bd9Sstevel@tonic-gate 				break;
1687c478bd9Sstevel@tonic-gate 			case 'b':
1697c478bd9Sstevel@tonic-gate 				*s = '\b';
1707c478bd9Sstevel@tonic-gate 				break;
1717c478bd9Sstevel@tonic-gate 			case 'f':
1727c478bd9Sstevel@tonic-gate 				*s = '\f';
1737c478bd9Sstevel@tonic-gate 				break;
1747c478bd9Sstevel@tonic-gate 			case 't':
1757c478bd9Sstevel@tonic-gate 				*s = '\t';
1767c478bd9Sstevel@tonic-gate 				break;
1777c478bd9Sstevel@tonic-gate 			case 'v':
1787c478bd9Sstevel@tonic-gate 				*s = '\v';
1797c478bd9Sstevel@tonic-gate 				break;
1807c478bd9Sstevel@tonic-gate 			default:
1817c478bd9Sstevel@tonic-gate 				*s = *p;
1827c478bd9Sstevel@tonic-gate 				break;
1837c478bd9Sstevel@tonic-gate 			}
1847c478bd9Sstevel@tonic-gate 			p++;
1857c478bd9Sstevel@tonic-gate 			s++;
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	*s = '\0';
1897c478bd9Sstevel@tonic-gate 	return (newstr);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate  * f_mkmtab - fills mtab array of magic table entries with
1947c478bd9Sstevel@tonic-gate  *	values from the file magfile.
1957c478bd9Sstevel@tonic-gate  *	May be called more than once if multiple magic
1967c478bd9Sstevel@tonic-gate  * 	files were specified.
1977c478bd9Sstevel@tonic-gate  *	Stores entries sequentially in one of two magic
1987c478bd9Sstevel@tonic-gate  *	tables: mtab1, if first = 1; mtab2 otherwise.
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  *	If -c option is specified, cflg is non-zero, and
2017c478bd9Sstevel@tonic-gate  *	f_mkmtab() reports on errors in the magic file.
2027c478bd9Sstevel@tonic-gate  *
2037c478bd9Sstevel@tonic-gate  *	Two magic tables may need to be created.  The first
2047c478bd9Sstevel@tonic-gate  *	one (mtab1) contains magic entries to be checked before
2057c478bd9Sstevel@tonic-gate  *	the programmatic default position-sensitive tests in
2067c478bd9Sstevel@tonic-gate  *	def_position_tests().
2077c478bd9Sstevel@tonic-gate  *	The second one (mtab2) should start with the default
2087c478bd9Sstevel@tonic-gate  *	/etc/magic file entries and is to be checked after
2097c478bd9Sstevel@tonic-gate  *	the programmatic default position-sensitive tests in
2107c478bd9Sstevel@tonic-gate  *	def_position_tests().  The parameter "first" would
2117c478bd9Sstevel@tonic-gate  *	be 1 for the former set of tables, 0 for the latter
2127c478bd9Sstevel@tonic-gate  *	set of magic tables.
2137c478bd9Sstevel@tonic-gate  *	No mtab2 should be created if file will not be
2147c478bd9Sstevel@tonic-gate  *	applying default tests; in that case, all magic table
2157c478bd9Sstevel@tonic-gate  *	entries should be in mtab1.
2167c478bd9Sstevel@tonic-gate  *
2177c478bd9Sstevel@tonic-gate  *	f_mkmtab returns 0 on success, -1 on error.  The calling
2187c478bd9Sstevel@tonic-gate  *	program is not expected to proceed after f_mkmtab()
2197c478bd9Sstevel@tonic-gate  *	returns an error.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate int
2237c478bd9Sstevel@tonic-gate f_mkmtab(char *magfile, int cflg, int first)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	Entry	*mtab;	/* generic magic table pointer */
2267c478bd9Sstevel@tonic-gate 	Entry	*ep;	/* current magic table entry */
2277c478bd9Sstevel@tonic-gate 	Entry	*mend;	/* one past last-allocated entry of mtab */
2287c478bd9Sstevel@tonic-gate 	FILE	*fp;
2297c478bd9Sstevel@tonic-gate 	int	lcnt = 0;
2307c478bd9Sstevel@tonic-gate 	char	buf[BSZ];
2317c478bd9Sstevel@tonic-gate 	size_t	tbsize;
2327c478bd9Sstevel@tonic-gate 	size_t	oldsize;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (first) {
2357c478bd9Sstevel@tonic-gate 		mtab = mtab1;
2367c478bd9Sstevel@tonic-gate 		mend = mend1;
2377c478bd9Sstevel@tonic-gate 		ep = ep1;
2387c478bd9Sstevel@tonic-gate 	} else {
2397c478bd9Sstevel@tonic-gate 		mtab = mtab2;
2407c478bd9Sstevel@tonic-gate 		mend = mend2;
2417c478bd9Sstevel@tonic-gate 		ep = ep2;
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/* mtab may have been allocated on a previous f_mkmtab call */
2457c478bd9Sstevel@tonic-gate 	if (mtab == (Entry *)NULL) {
246*9a411307Srie 		if ((mtab = calloc(sizeof (Entry), NENT)) == NULL) {
247*9a411307Srie 			int err = errno;
248*9a411307Srie 			(void) fprintf(stderr, gettext("%s: malloc "
249*9a411307Srie 			    "failed: %s\n"), File, strerror(err));
2507c478bd9Sstevel@tonic-gate 			return (-1);
2517c478bd9Sstevel@tonic-gate 		}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 		ep = mtab;
2547c478bd9Sstevel@tonic-gate 		mend = &mtab[NENT];
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
257*9a411307Srie 	errno = 0;
258*9a411307Srie 	if ((fp = fopen(magfile, "r")) == NULL) {
259*9a411307Srie 		int err = errno;
260*9a411307Srie 		(void) fprintf(stderr, gettext("%s: %s: cannot open magic "
261*9a411307Srie 		    "file: %s\n"), File, magfile, err ? strerror(err) : "");
2627c478bd9Sstevel@tonic-gate 		return (-1);
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 	while (fgets(buf, BSZ, fp) != NULL) {
2657c478bd9Sstevel@tonic-gate 		char	*p = buf;
2667c478bd9Sstevel@tonic-gate 		char	*p2;
2677c478bd9Sstevel@tonic-gate 		char	*p3;
2687c478bd9Sstevel@tonic-gate 		char	opc;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		/*
2717c478bd9Sstevel@tonic-gate 		 * ensure we have one extra entry allocated
2727c478bd9Sstevel@tonic-gate 		 * to mark end of the table, after the while loop
2737c478bd9Sstevel@tonic-gate 		 */
2747c478bd9Sstevel@tonic-gate 		if (ep >= (mend - 1)) {
2757c478bd9Sstevel@tonic-gate 			oldsize = mend - mtab;
2767c478bd9Sstevel@tonic-gate 			tbsize = (NENT + oldsize) * sizeof (Entry);
2777c478bd9Sstevel@tonic-gate 			if ((mtab = realloc(mtab, tbsize)) == NULL) {
278*9a411307Srie 				int err = errno;
279*9a411307Srie 				(void) fprintf(stderr, gettext("%s: malloc "
280*9a411307Srie 				    "failed: %s\n"), File, strerror(err));
2817c478bd9Sstevel@tonic-gate 				return (-1);
2827c478bd9Sstevel@tonic-gate 			} else {
2837c478bd9Sstevel@tonic-gate 				(void) memset(mtab + oldsize, 0,
2847c478bd9Sstevel@tonic-gate 				    sizeof (Entry) * NENT);
2857c478bd9Sstevel@tonic-gate 				mend = &mtab[tbsize / sizeof (Entry)];
2867c478bd9Sstevel@tonic-gate 				ep = &mtab[oldsize-1];
2877c478bd9Sstevel@tonic-gate 			}
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		lcnt++;
2917c478bd9Sstevel@tonic-gate 		if (*p == '\n' || *p == '#')
2927c478bd9Sstevel@tonic-gate 			continue;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 			/* LEVEL */
2967c478bd9Sstevel@tonic-gate 		if (*p == '>') {
2977c478bd9Sstevel@tonic-gate 			ep->e_level = 1;
2987c478bd9Sstevel@tonic-gate 			p++;
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 			/* OFFSET */
3017c478bd9Sstevel@tonic-gate 		p2 = strchr(p, '\t');
3027c478bd9Sstevel@tonic-gate 		if (p2 == NULL) {
3037c478bd9Sstevel@tonic-gate 			if (cflg)
304*9a411307Srie 				(void) fprintf(stderr, gettext("%s: %s: format "
305*9a411307Srie 				    "error, no tab after %s on line %d\n"),
306*9a411307Srie 				    File, magfile, p, lcnt);
3077c478bd9Sstevel@tonic-gate 			continue;
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 		*p2++ = NULL;
3107c478bd9Sstevel@tonic-gate 		ep->e_off = strtol((const char *)p, (char **)NULL, 0);
3117c478bd9Sstevel@tonic-gate 		while (*p2 == '\t')
3127c478bd9Sstevel@tonic-gate 			p2++;
3137c478bd9Sstevel@tonic-gate 			/* TYPE */
3147c478bd9Sstevel@tonic-gate 		p = p2;
3157c478bd9Sstevel@tonic-gate 		p2 = strchr(p, '\t');
3167c478bd9Sstevel@tonic-gate 		if (p2 == NULL) {
3177c478bd9Sstevel@tonic-gate 			if (cflg)
318*9a411307Srie 				(void) fprintf(stderr, gettext("%s: %s: format "
319*9a411307Srie 				    "error, no tab after %s on line %d\n"),
320*9a411307Srie 				    File, magfile, p, lcnt);
3217c478bd9Sstevel@tonic-gate 			continue;
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 		*p2++ = NULL;
3247c478bd9Sstevel@tonic-gate 		p3 = strchr(p, '&');
3257c478bd9Sstevel@tonic-gate 		if (p3 != NULL) {
3267c478bd9Sstevel@tonic-gate 			*p3++ = '\0';
3277c478bd9Sstevel@tonic-gate 			ep->e_mask = strtoull((const char *)p3, (char **)NULL,
3287c478bd9Sstevel@tonic-gate 			    0);	/* returns 0 or ULLONG_MAX on error */
3297c478bd9Sstevel@tonic-gate 		} else {
3307c478bd9Sstevel@tonic-gate 			ep->e_mask = 0ULL;
3317c478bd9Sstevel@tonic-gate 		}
3327c478bd9Sstevel@tonic-gate 		switch (*p) {
3337c478bd9Sstevel@tonic-gate 			case 'd':
3347c478bd9Sstevel@tonic-gate 				if (*(p+1) == NULL) {
3357c478bd9Sstevel@tonic-gate 					/* d */
3367c478bd9Sstevel@tonic-gate 					ep->e_type = LONG;
3377c478bd9Sstevel@tonic-gate 				} else if (*(p+2) == NULL) {	/* d? */
3387c478bd9Sstevel@tonic-gate 					switch (*(p+1)) {
3397c478bd9Sstevel@tonic-gate 						case 'C':
3407c478bd9Sstevel@tonic-gate 						case '1':
3417c478bd9Sstevel@tonic-gate 							/* dC, d1 */
3427c478bd9Sstevel@tonic-gate 							ep->e_type = BYTE;
3437c478bd9Sstevel@tonic-gate 							break;
3447c478bd9Sstevel@tonic-gate 						case 'S':
3457c478bd9Sstevel@tonic-gate 						case '2':
3467c478bd9Sstevel@tonic-gate 							/* dS, d2 */
3477c478bd9Sstevel@tonic-gate 							ep->e_type = SHORT;
3487c478bd9Sstevel@tonic-gate 							break;
3497c478bd9Sstevel@tonic-gate 						case 'I':
3507c478bd9Sstevel@tonic-gate 						case 'L':
3517c478bd9Sstevel@tonic-gate 						case '4':
3527c478bd9Sstevel@tonic-gate 							/* dI, dL, d4 */
3537c478bd9Sstevel@tonic-gate 							ep->e_type = LONG;
3547c478bd9Sstevel@tonic-gate 							break;
3557c478bd9Sstevel@tonic-gate 						case '8':
3567c478bd9Sstevel@tonic-gate 							/* d8 */
3577c478bd9Sstevel@tonic-gate 							ep->e_type = LLONG;
3587c478bd9Sstevel@tonic-gate 							break;
3597c478bd9Sstevel@tonic-gate 						default:
3607c478bd9Sstevel@tonic-gate 							ep->e_type = LONG;
3617c478bd9Sstevel@tonic-gate 							break;
3627c478bd9Sstevel@tonic-gate 					}
3637c478bd9Sstevel@tonic-gate 				}
3647c478bd9Sstevel@tonic-gate 				break;
3657c478bd9Sstevel@tonic-gate 			case 'l':
3667c478bd9Sstevel@tonic-gate 				if (*(p+1) == 'l') {	/* llong */
3677c478bd9Sstevel@tonic-gate 					ep->e_type = LLONG;
3687c478bd9Sstevel@tonic-gate 				} else { 		/* long */
3697c478bd9Sstevel@tonic-gate 					ep->e_type = LONG;
3707c478bd9Sstevel@tonic-gate 				}
3717c478bd9Sstevel@tonic-gate 				break;
3727c478bd9Sstevel@tonic-gate 			case 's':
3737c478bd9Sstevel@tonic-gate 				if (*(p+1) == 'h') {
3747c478bd9Sstevel@tonic-gate 					/* short */
3757c478bd9Sstevel@tonic-gate 					ep->e_type = SHORT;
3767c478bd9Sstevel@tonic-gate 				} else {
3777c478bd9Sstevel@tonic-gate 					/* s or string */
3787c478bd9Sstevel@tonic-gate 					ep->e_type = STR;
3797c478bd9Sstevel@tonic-gate 				}
3807c478bd9Sstevel@tonic-gate 				break;
3817c478bd9Sstevel@tonic-gate 			case 'u':
3827c478bd9Sstevel@tonic-gate 				if (*(p+1) == NULL) {
3837c478bd9Sstevel@tonic-gate 					/* u */
3847c478bd9Sstevel@tonic-gate 					ep->e_type = ULONG;
3857c478bd9Sstevel@tonic-gate 				} else if (*(p+2) == NULL) {	/* u? */
3867c478bd9Sstevel@tonic-gate 					switch (*(p+1)) {
3877c478bd9Sstevel@tonic-gate 						case 'C':
3887c478bd9Sstevel@tonic-gate 						case '1':
3897c478bd9Sstevel@tonic-gate 							/* uC, u1 */
3907c478bd9Sstevel@tonic-gate 							ep->e_type = UBYTE;
3917c478bd9Sstevel@tonic-gate 							break;
3927c478bd9Sstevel@tonic-gate 						case 'S':
3937c478bd9Sstevel@tonic-gate 						case '2':
3947c478bd9Sstevel@tonic-gate 							/* uS, u2 */
3957c478bd9Sstevel@tonic-gate 							ep->e_type = USHORT;
3967c478bd9Sstevel@tonic-gate 							break;
3977c478bd9Sstevel@tonic-gate 						case 'I':
3987c478bd9Sstevel@tonic-gate 						case 'L':
3997c478bd9Sstevel@tonic-gate 						case '4':
4007c478bd9Sstevel@tonic-gate 							/* uI, uL, u4 */
4017c478bd9Sstevel@tonic-gate 							ep->e_type = ULONG;
4027c478bd9Sstevel@tonic-gate 							break;
4037c478bd9Sstevel@tonic-gate 						case '8':
4047c478bd9Sstevel@tonic-gate 							/* u8 */
4057c478bd9Sstevel@tonic-gate 							ep->e_type = ULLONG;
4067c478bd9Sstevel@tonic-gate 							break;
4077c478bd9Sstevel@tonic-gate 						default:
4087c478bd9Sstevel@tonic-gate 							ep->e_type = ULONG;
4097c478bd9Sstevel@tonic-gate 							break;
4107c478bd9Sstevel@tonic-gate 					}
4117c478bd9Sstevel@tonic-gate 				} else { /* u?* */
4127c478bd9Sstevel@tonic-gate 					switch (*(p+1)) {
4137c478bd9Sstevel@tonic-gate 					case 'b':	/* ubyte */
4147c478bd9Sstevel@tonic-gate 						ep->e_type = UBYTE;
4157c478bd9Sstevel@tonic-gate 						break;
4167c478bd9Sstevel@tonic-gate 					case 's':	/* ushort */
4177c478bd9Sstevel@tonic-gate 						ep->e_type = USHORT;
4187c478bd9Sstevel@tonic-gate 						break;
4197c478bd9Sstevel@tonic-gate 					case 'l':
4207c478bd9Sstevel@tonic-gate 						if (*(p+2) == 'l') {
4217c478bd9Sstevel@tonic-gate 							/* ullong */
4227c478bd9Sstevel@tonic-gate 							ep->e_type = ULLONG;
4237c478bd9Sstevel@tonic-gate 						} else {
4247c478bd9Sstevel@tonic-gate 							/* ulong */
4257c478bd9Sstevel@tonic-gate 							ep->e_type = ULONG;
4267c478bd9Sstevel@tonic-gate 						}
4277c478bd9Sstevel@tonic-gate 						break;
4287c478bd9Sstevel@tonic-gate 					default:
4297c478bd9Sstevel@tonic-gate 						/* default, same as "u" */
4307c478bd9Sstevel@tonic-gate 						ep->e_type = ULONG;
4317c478bd9Sstevel@tonic-gate 						break;
4327c478bd9Sstevel@tonic-gate 					}
4337c478bd9Sstevel@tonic-gate 				}
4347c478bd9Sstevel@tonic-gate 				break;
4357c478bd9Sstevel@tonic-gate 			default:
4367c478bd9Sstevel@tonic-gate 				/* retain (undocumented) default type */
4377c478bd9Sstevel@tonic-gate 				ep->e_type = BYTE;
4387c478bd9Sstevel@tonic-gate 				break;
4397c478bd9Sstevel@tonic-gate 		}
4407c478bd9Sstevel@tonic-gate 		if (ep->e_type == 0) {
4417c478bd9Sstevel@tonic-gate 			ep->e_type = BYTE;	/* default */
4427c478bd9Sstevel@tonic-gate 		}
4437c478bd9Sstevel@tonic-gate 		while (*p2 == '\t')
4447c478bd9Sstevel@tonic-gate 			p2++;
4457c478bd9Sstevel@tonic-gate 			/* OP-VALUE */
4467c478bd9Sstevel@tonic-gate 		p = p2;
4477c478bd9Sstevel@tonic-gate 		p2 = strchr(p, '\t');
4487c478bd9Sstevel@tonic-gate 		if (p2 == NULL) {
4497c478bd9Sstevel@tonic-gate 			if (cflg)
450*9a411307Srie 				(void) fprintf(stderr, gettext("%s: %s: format "
451*9a411307Srie 				    "error, no tab after %s on line %d\n"),
452*9a411307Srie 				    File, magfile, p, lcnt);
4537c478bd9Sstevel@tonic-gate 			continue;
4547c478bd9Sstevel@tonic-gate 		}
4557c478bd9Sstevel@tonic-gate 		*p2++ = NULL;
4567c478bd9Sstevel@tonic-gate 		if (ep->e_type != STR) {
4577c478bd9Sstevel@tonic-gate 			opc = *p++;
4587c478bd9Sstevel@tonic-gate 			switch (opc) {
4597c478bd9Sstevel@tonic-gate 			case '=':
4607c478bd9Sstevel@tonic-gate 				ep->e_opcode = EQ;
4617c478bd9Sstevel@tonic-gate 				break;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 			case '>':
4647c478bd9Sstevel@tonic-gate 				ep->e_opcode = GT;
4657c478bd9Sstevel@tonic-gate 				break;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 			case '<':
4687c478bd9Sstevel@tonic-gate 				ep->e_opcode = LT;
4697c478bd9Sstevel@tonic-gate 				break;
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 			case 'x':
4727c478bd9Sstevel@tonic-gate 				ep->e_opcode = ANY;
4737c478bd9Sstevel@tonic-gate 				break;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 			case '&':
4767c478bd9Sstevel@tonic-gate 				ep->e_opcode = AND;
4777c478bd9Sstevel@tonic-gate 				break;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 			case '^':
4807c478bd9Sstevel@tonic-gate 				ep->e_opcode = NSET;
4817c478bd9Sstevel@tonic-gate 				break;
4827c478bd9Sstevel@tonic-gate 			default:	/* EQ (i.e. 0) is default	*/
4837c478bd9Sstevel@tonic-gate 				p--;	/* since global ep->e_opcode=0	*/
4847c478bd9Sstevel@tonic-gate 			}
4857c478bd9Sstevel@tonic-gate 		}
4867c478bd9Sstevel@tonic-gate 		if (ep->e_opcode != ANY) {
4877c478bd9Sstevel@tonic-gate 			if (ep->e_type != STR) {
4887c478bd9Sstevel@tonic-gate 				ep->e_value.num = strtoull((const char *)p,
4897c478bd9Sstevel@tonic-gate 				    (char **)NULL, 0);
490*9a411307Srie 			} else if ((ep->e_value.str =
491*9a411307Srie 			    getstr(p, magfile)) == NULL) {
4927c478bd9Sstevel@tonic-gate 				return (-1);
4937c478bd9Sstevel@tonic-gate 			}
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 		p2 += strspn(p2, "\t");
4967c478bd9Sstevel@tonic-gate 			/* STRING */
4977c478bd9Sstevel@tonic-gate 		if ((ep->e_str = strdup(p2)) == NULL) {
498*9a411307Srie 			int err = errno;
499*9a411307Srie 			(void) fprintf(stderr, gettext("%s: malloc "
500*9a411307Srie 			    "failed: %s\n"), File, strerror(err));
5017c478bd9Sstevel@tonic-gate 			return (-1);
5027c478bd9Sstevel@tonic-gate 		} else {
5037c478bd9Sstevel@tonic-gate 			if ((p = strchr(ep->e_str, '\n')) != NULL)
5047c478bd9Sstevel@tonic-gate 				*p = '\0';
5057c478bd9Sstevel@tonic-gate 			if (strchr(ep->e_str, '%') != NULL)
5067c478bd9Sstevel@tonic-gate 				ep->e_opcode |= SUB;
5077c478bd9Sstevel@tonic-gate 		}
5087c478bd9Sstevel@tonic-gate 		ep++;
5097c478bd9Sstevel@tonic-gate 	}	/* end while (fgets) */
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	ep->e_off = -1L;	/* mark end of table */
5127c478bd9Sstevel@tonic-gate 	if (first) {
5137c478bd9Sstevel@tonic-gate 		mtab1 = mtab;
5147c478bd9Sstevel@tonic-gate 		mend1 = mend;
5157c478bd9Sstevel@tonic-gate 		ep1 = ep;
5167c478bd9Sstevel@tonic-gate 	} else {
5177c478bd9Sstevel@tonic-gate 		mtab2 = mtab;
5187c478bd9Sstevel@tonic-gate 		mend2 = mend;
5197c478bd9Sstevel@tonic-gate 		ep2 = ep;
5207c478bd9Sstevel@tonic-gate 	}
521*9a411307Srie 	if (fclose(fp) != 0) {
522*9a411307Srie 		int err = errno;
523*9a411307Srie 		(void) fprintf(stderr, gettext("%s: fclose failed: %s\n"),
524*9a411307Srie 		    File, strerror(err));
5257c478bd9Sstevel@tonic-gate 		return (-1);
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 	return (0);
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate /*
5317c478bd9Sstevel@tonic-gate  * Check for Magic Table entries in the file.
5327c478bd9Sstevel@tonic-gate  *
5337c478bd9Sstevel@tonic-gate  * Since there may be two sets of magic tables, first = 1
5347c478bd9Sstevel@tonic-gate  * for the first magic table (mtab1) and 0 for the second magic
5357c478bd9Sstevel@tonic-gate  * table (mtab2).
5367c478bd9Sstevel@tonic-gate  */
5377c478bd9Sstevel@tonic-gate int
5387c478bd9Sstevel@tonic-gate f_ckmtab(char *buf, int bufsize, int first)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	int 		result;
5417c478bd9Sstevel@tonic-gate 	Entry		*mtab;
5427c478bd9Sstevel@tonic-gate 	Entry		*ep;
5437c478bd9Sstevel@tonic-gate 	char		*p;
5447c478bd9Sstevel@tonic-gate 	int		lev1 = 0;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	uint16_t	u16_val;
5477c478bd9Sstevel@tonic-gate 	uint32_t	u32_val;
5487c478bd9Sstevel@tonic-gate 	uint64_t	u64_val;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if (first) {
5517c478bd9Sstevel@tonic-gate 		mtab = mtab1;
5527c478bd9Sstevel@tonic-gate 	} else {
5537c478bd9Sstevel@tonic-gate 		mtab = mtab2;
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	if (mtab == (Entry *)NULL) {
5577c478bd9Sstevel@tonic-gate 		return (0);	/* no magic file tests in this table */
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	for (ep = mtab; ep->e_off != -1L; ep++) {  /* -1 offset marks end of */
5617c478bd9Sstevel@tonic-gate 		if (lev1) {			/* valid magic file entries */
5627c478bd9Sstevel@tonic-gate 			if (ep->e_level != 1)
5637c478bd9Sstevel@tonic-gate 				break;
5647c478bd9Sstevel@tonic-gate 		} else if (ep->e_level == 1) {
5657c478bd9Sstevel@tonic-gate 			continue;
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 		if (ep->e_off > (off_t)bufsize)
5687c478bd9Sstevel@tonic-gate 			continue;
5697c478bd9Sstevel@tonic-gate 		p = &buf[ep->e_off];
5707c478bd9Sstevel@tonic-gate 		switch (ep->e_type) {
5717c478bd9Sstevel@tonic-gate 		case STR:
5727c478bd9Sstevel@tonic-gate 		{
5737c478bd9Sstevel@tonic-gate 			if (strncmp(p, ep->e_value.str,
5747c478bd9Sstevel@tonic-gate 			    strlen(ep->e_value.str)))
5757c478bd9Sstevel@tonic-gate 				continue;
5767c478bd9Sstevel@tonic-gate 			if (lev1) {
5777c478bd9Sstevel@tonic-gate 				(void) putchar(' ');
5787c478bd9Sstevel@tonic-gate 			}
5797c478bd9Sstevel@tonic-gate 			if (ep->e_opcode & SUB)
5807c478bd9Sstevel@tonic-gate 				(void) printf(ep->e_str,
5817c478bd9Sstevel@tonic-gate 				    ep->e_value.str);
5827c478bd9Sstevel@tonic-gate 			else
5837c478bd9Sstevel@tonic-gate 				(void) printf(ep->e_str);
5847c478bd9Sstevel@tonic-gate 			lev1 = 1;
5857c478bd9Sstevel@tonic-gate 			continue;
5867c478bd9Sstevel@tonic-gate 			/*
5877c478bd9Sstevel@tonic-gate 			 * We've matched the string and printed the message;
5887c478bd9Sstevel@tonic-gate 			 * no STR processing occurs beyond this point.
5897c478bd9Sstevel@tonic-gate 			 */
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 		case BYTE:
5937c478bd9Sstevel@tonic-gate 		case UBYTE:
5947c478bd9Sstevel@tonic-gate 			u64_val = (uint64_t)(uint8_t)(*p);
5957c478bd9Sstevel@tonic-gate 			break;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 		case SHORT:
5987c478bd9Sstevel@tonic-gate 		case USHORT:
5997c478bd9Sstevel@tonic-gate 			(void) memcpy(&u16_val, p, sizeof (uint16_t));
6007c478bd9Sstevel@tonic-gate 			u64_val = (uint64_t)u16_val;
6017c478bd9Sstevel@tonic-gate 			break;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		case LONG:
6047c478bd9Sstevel@tonic-gate 		case ULONG:
6057c478bd9Sstevel@tonic-gate 			(void) memcpy(&u32_val, p, sizeof (uint32_t));
6067c478bd9Sstevel@tonic-gate 			u64_val = (uint64_t)u32_val;
6077c478bd9Sstevel@tonic-gate 			break;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 		case LLONG:
6107c478bd9Sstevel@tonic-gate 		case ULLONG:
6117c478bd9Sstevel@tonic-gate 			(void) memcpy(&(u64_val), p, sizeof (uint64_t));
6127c478bd9Sstevel@tonic-gate 			break;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 		}
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 		if (ep->e_mask) {
6177c478bd9Sstevel@tonic-gate 			u64_val &= ep->e_mask;
6187c478bd9Sstevel@tonic-gate 		}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 		/*
6217c478bd9Sstevel@tonic-gate 		 * Compare the values according to the size and sign
6227c478bd9Sstevel@tonic-gate 		 * of the type.  For =, &, and ^ operators, the sign
6237c478bd9Sstevel@tonic-gate 		 * does not have any effect, so these are always compared
6247c478bd9Sstevel@tonic-gate 		 * unsigned.  Only for < and > operators is the
6257c478bd9Sstevel@tonic-gate 		 * sign significant.
6267c478bd9Sstevel@tonic-gate 		 * If the file value was masked, the compare should
6277c478bd9Sstevel@tonic-gate 		 * be unsigned.
6287c478bd9Sstevel@tonic-gate 		 */
6297c478bd9Sstevel@tonic-gate 		switch (ep->e_opcode & ~SUB) {
6307c478bd9Sstevel@tonic-gate 		case EQ:
6317c478bd9Sstevel@tonic-gate 			switch (ep->e_type) {
6327c478bd9Sstevel@tonic-gate 			case BYTE:
6337c478bd9Sstevel@tonic-gate 			case UBYTE:
6347c478bd9Sstevel@tonic-gate 				if ((uint8_t)u64_val !=
6357c478bd9Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
6367c478bd9Sstevel@tonic-gate 					continue;
6377c478bd9Sstevel@tonic-gate 				break;
6387c478bd9Sstevel@tonic-gate 			case SHORT:
6397c478bd9Sstevel@tonic-gate 			case USHORT:
6407c478bd9Sstevel@tonic-gate 				if ((uint16_t)u64_val !=
6417c478bd9Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
6427c478bd9Sstevel@tonic-gate 					continue;
6437c478bd9Sstevel@tonic-gate 				break;
6447c478bd9Sstevel@tonic-gate 			case LONG:
6457c478bd9Sstevel@tonic-gate 			case ULONG:
6467c478bd9Sstevel@tonic-gate 				if ((uint32_t)u64_val !=
6477c478bd9Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
6487c478bd9Sstevel@tonic-gate 					continue;
6497c478bd9Sstevel@tonic-gate 				break;
6507c478bd9Sstevel@tonic-gate 			case LLONG:
6517c478bd9Sstevel@tonic-gate 			case ULLONG:
6527c478bd9Sstevel@tonic-gate 				if (u64_val != ep->e_value.num)
6537c478bd9Sstevel@tonic-gate 					continue;
6547c478bd9Sstevel@tonic-gate 				break;
6557c478bd9Sstevel@tonic-gate 			default:
6567c478bd9Sstevel@tonic-gate 				continue;
6577c478bd9Sstevel@tonic-gate 			}
6587c478bd9Sstevel@tonic-gate 			break;
6597c478bd9Sstevel@tonic-gate 		case GT:
6607c478bd9Sstevel@tonic-gate 			switch (ep->e_type) {
6617c478bd9Sstevel@tonic-gate 			case BYTE:
6627c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
6637c478bd9Sstevel@tonic-gate 					if ((int8_t)u64_val <=
6647c478bd9Sstevel@tonic-gate 					    (int8_t)(ep->e_value.num))
6657c478bd9Sstevel@tonic-gate 						continue;
6667c478bd9Sstevel@tonic-gate 					break;
6677c478bd9Sstevel@tonic-gate 				}
6687c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
6697c478bd9Sstevel@tonic-gate 			case UBYTE:
6707c478bd9Sstevel@tonic-gate 				if ((uint8_t)u64_val <=
6717c478bd9Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
6727c478bd9Sstevel@tonic-gate 					continue;
6737c478bd9Sstevel@tonic-gate 				break;
6747c478bd9Sstevel@tonic-gate 			case SHORT:
6757c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
6767c478bd9Sstevel@tonic-gate 					if ((int16_t)u64_val <=
6777c478bd9Sstevel@tonic-gate 					    (int16_t)(ep->e_value.num))
6787c478bd9Sstevel@tonic-gate 						continue;
6797c478bd9Sstevel@tonic-gate 					break;
6807c478bd9Sstevel@tonic-gate 				}
6817c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
6827c478bd9Sstevel@tonic-gate 			case USHORT:
6837c478bd9Sstevel@tonic-gate 				if ((uint16_t)u64_val <=
6847c478bd9Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
6857c478bd9Sstevel@tonic-gate 					continue;
6867c478bd9Sstevel@tonic-gate 				break;
6877c478bd9Sstevel@tonic-gate 			case LONG:
6887c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
6897c478bd9Sstevel@tonic-gate 					if ((int32_t)u64_val <=
6907c478bd9Sstevel@tonic-gate 					    (int32_t)(ep->e_value.num))
6917c478bd9Sstevel@tonic-gate 						continue;
6927c478bd9Sstevel@tonic-gate 					break;
6937c478bd9Sstevel@tonic-gate 				}
6947c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
6957c478bd9Sstevel@tonic-gate 			case ULONG:
6967c478bd9Sstevel@tonic-gate 				if ((uint32_t)u64_val <=
6977c478bd9Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
6987c478bd9Sstevel@tonic-gate 					continue;
6997c478bd9Sstevel@tonic-gate 				break;
7007c478bd9Sstevel@tonic-gate 			case LLONG:
7017c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
7027c478bd9Sstevel@tonic-gate 					if ((int64_t)u64_val <=
7037c478bd9Sstevel@tonic-gate 					    (int64_t)(ep->e_value.num))
7047c478bd9Sstevel@tonic-gate 						continue;
7057c478bd9Sstevel@tonic-gate 					break;
7067c478bd9Sstevel@tonic-gate 				}
7077c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
7087c478bd9Sstevel@tonic-gate 			case ULLONG:
7097c478bd9Sstevel@tonic-gate 				if (u64_val <= ep->e_value.num)
7107c478bd9Sstevel@tonic-gate 					continue;
7117c478bd9Sstevel@tonic-gate 				break;
7127c478bd9Sstevel@tonic-gate 			default:
7137c478bd9Sstevel@tonic-gate 				continue;
7147c478bd9Sstevel@tonic-gate 			}
7157c478bd9Sstevel@tonic-gate 			break;
7167c478bd9Sstevel@tonic-gate 		case LT:
7177c478bd9Sstevel@tonic-gate 			switch (ep->e_type) {
7187c478bd9Sstevel@tonic-gate 			case BYTE:
7197c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
7207c478bd9Sstevel@tonic-gate 					if ((int8_t)u64_val >=
7217c478bd9Sstevel@tonic-gate 					    (int8_t)(ep->e_value.num))
7227c478bd9Sstevel@tonic-gate 						continue;
7237c478bd9Sstevel@tonic-gate 					break;
7247c478bd9Sstevel@tonic-gate 				}
7257c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
7267c478bd9Sstevel@tonic-gate 			case UBYTE:
7277c478bd9Sstevel@tonic-gate 				if ((uint8_t)u64_val >=
7287c478bd9Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
7297c478bd9Sstevel@tonic-gate 					continue;
7307c478bd9Sstevel@tonic-gate 				break;
7317c478bd9Sstevel@tonic-gate 			case SHORT:
7327c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
7337c478bd9Sstevel@tonic-gate 					if ((int16_t)u64_val >=
7347c478bd9Sstevel@tonic-gate 					    (int16_t)(ep->e_value.num))
7357c478bd9Sstevel@tonic-gate 						continue;
7367c478bd9Sstevel@tonic-gate 					break;
7377c478bd9Sstevel@tonic-gate 				}
7387c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
7397c478bd9Sstevel@tonic-gate 			case USHORT:
7407c478bd9Sstevel@tonic-gate 				if ((uint16_t)u64_val >=
7417c478bd9Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
7427c478bd9Sstevel@tonic-gate 					continue;
7437c478bd9Sstevel@tonic-gate 				break;
7447c478bd9Sstevel@tonic-gate 			case LONG:
7457c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
7467c478bd9Sstevel@tonic-gate 					if ((int32_t)u64_val >=
7477c478bd9Sstevel@tonic-gate 					    (int32_t)(ep->e_value.num))
7487c478bd9Sstevel@tonic-gate 						continue;
7497c478bd9Sstevel@tonic-gate 					break;
7507c478bd9Sstevel@tonic-gate 				}
7517c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
7527c478bd9Sstevel@tonic-gate 			case ULONG:
7537c478bd9Sstevel@tonic-gate 				if ((uint32_t)u64_val >=
7547c478bd9Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
7557c478bd9Sstevel@tonic-gate 					continue;
7567c478bd9Sstevel@tonic-gate 				break;
7577c478bd9Sstevel@tonic-gate 			case LLONG:
7587c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
7597c478bd9Sstevel@tonic-gate 					if ((int64_t)u64_val >=
7607c478bd9Sstevel@tonic-gate 					    (int64_t)(ep->e_value.num))
7617c478bd9Sstevel@tonic-gate 						continue;
7627c478bd9Sstevel@tonic-gate 					break;
7637c478bd9Sstevel@tonic-gate 				}
7647c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
7657c478bd9Sstevel@tonic-gate 			case ULLONG:
7667c478bd9Sstevel@tonic-gate 				if (u64_val >= ep->e_value.num)
7677c478bd9Sstevel@tonic-gate 					continue;
7687c478bd9Sstevel@tonic-gate 				break;
7697c478bd9Sstevel@tonic-gate 			default:
7707c478bd9Sstevel@tonic-gate 				continue;
7717c478bd9Sstevel@tonic-gate 			}
7727c478bd9Sstevel@tonic-gate 			break;
7737c478bd9Sstevel@tonic-gate 		case AND:
7747c478bd9Sstevel@tonic-gate 			switch (ep->e_type) {
7757c478bd9Sstevel@tonic-gate 			case BYTE:
7767c478bd9Sstevel@tonic-gate 			case UBYTE:
7777c478bd9Sstevel@tonic-gate 				if (((uint8_t)u64_val &
7787c478bd9Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num)) ==
7797c478bd9Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
7807c478bd9Sstevel@tonic-gate 					break;
7817c478bd9Sstevel@tonic-gate 				continue;
7827c478bd9Sstevel@tonic-gate 			case SHORT:
7837c478bd9Sstevel@tonic-gate 			case USHORT:
7847c478bd9Sstevel@tonic-gate 				if (((uint16_t)u64_val &
7857c478bd9Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num)) ==
7867c478bd9Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
7877c478bd9Sstevel@tonic-gate 					break;
7887c478bd9Sstevel@tonic-gate 				continue;
7897c478bd9Sstevel@tonic-gate 			case LONG:
7907c478bd9Sstevel@tonic-gate 			case ULONG:
7917c478bd9Sstevel@tonic-gate 				if (((uint32_t)u64_val &
7927c478bd9Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num)) ==
7937c478bd9Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
7947c478bd9Sstevel@tonic-gate 					break;
7957c478bd9Sstevel@tonic-gate 				continue;
7967c478bd9Sstevel@tonic-gate 			case LLONG:
7977c478bd9Sstevel@tonic-gate 			case ULLONG:
7987c478bd9Sstevel@tonic-gate 				if ((u64_val & ep->e_value.num) ==
7997c478bd9Sstevel@tonic-gate 				    ep->e_value.num)
8007c478bd9Sstevel@tonic-gate 					break;
8017c478bd9Sstevel@tonic-gate 				continue;
8027c478bd9Sstevel@tonic-gate 			default:
8037c478bd9Sstevel@tonic-gate 				continue;
8047c478bd9Sstevel@tonic-gate 			}
8057c478bd9Sstevel@tonic-gate 			break;
8067c478bd9Sstevel@tonic-gate 		case NSET:
8077c478bd9Sstevel@tonic-gate 			switch (ep->e_type) {
8087c478bd9Sstevel@tonic-gate 			case BYTE:
8097c478bd9Sstevel@tonic-gate 			case UBYTE:
8107c478bd9Sstevel@tonic-gate 				if (((uint8_t)u64_val &
8117c478bd9Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num)) !=
8127c478bd9Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
8137c478bd9Sstevel@tonic-gate 					break;
8147c478bd9Sstevel@tonic-gate 				continue;
8157c478bd9Sstevel@tonic-gate 			case SHORT:
8167c478bd9Sstevel@tonic-gate 			case USHORT:
8177c478bd9Sstevel@tonic-gate 				if (((uint16_t)u64_val &
8187c478bd9Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num)) !=
8197c478bd9Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
8207c478bd9Sstevel@tonic-gate 					break;
8217c478bd9Sstevel@tonic-gate 				continue;
8227c478bd9Sstevel@tonic-gate 			case LONG:
8237c478bd9Sstevel@tonic-gate 			case ULONG:
8247c478bd9Sstevel@tonic-gate 				if (((uint32_t)u64_val &
8257c478bd9Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num)) !=
8267c478bd9Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
8277c478bd9Sstevel@tonic-gate 					break;
8287c478bd9Sstevel@tonic-gate 				continue;
8297c478bd9Sstevel@tonic-gate 			case LLONG:
8307c478bd9Sstevel@tonic-gate 			case ULLONG:
8317c478bd9Sstevel@tonic-gate 				if ((u64_val & ep->e_value.num) !=
8327c478bd9Sstevel@tonic-gate 				    ep->e_value.num)
8337c478bd9Sstevel@tonic-gate 					break;
8347c478bd9Sstevel@tonic-gate 				continue;
8357c478bd9Sstevel@tonic-gate 			default:
8367c478bd9Sstevel@tonic-gate 				continue;
8377c478bd9Sstevel@tonic-gate 			}
8387c478bd9Sstevel@tonic-gate 			break;
8397c478bd9Sstevel@tonic-gate 		case ANY:	/* matches anything */
8407c478bd9Sstevel@tonic-gate 			break;
8417c478bd9Sstevel@tonic-gate 		default:	/* shouldn't occur; ignore it */
8427c478bd9Sstevel@tonic-gate 			continue;
8437c478bd9Sstevel@tonic-gate 		}
8447c478bd9Sstevel@tonic-gate 		if (lev1)
8457c478bd9Sstevel@tonic-gate 			(void) putchar(' ');
8467c478bd9Sstevel@tonic-gate 		if (ep->e_opcode & SUB) {
8477c478bd9Sstevel@tonic-gate 			switch (ep->e_type) {
8487c478bd9Sstevel@tonic-gate 			case LLONG:
8497c478bd9Sstevel@tonic-gate #ifdef XPG4
8507c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
8517c478bd9Sstevel@tonic-gate 					(void) printf(ep->e_str,
8527c478bd9Sstevel@tonic-gate 						(int64_t)u64_val);
8537c478bd9Sstevel@tonic-gate 					break;
8547c478bd9Sstevel@tonic-gate 				}
8557c478bd9Sstevel@tonic-gate #endif	/* XPG4 */
8567c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
8577c478bd9Sstevel@tonic-gate 			case ULLONG:
8587c478bd9Sstevel@tonic-gate 				(void) printf(ep->e_str, u64_val);
8597c478bd9Sstevel@tonic-gate 				break;
8607c478bd9Sstevel@tonic-gate 			case LONG:
8617c478bd9Sstevel@tonic-gate #ifdef XPG4
8627c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
8637c478bd9Sstevel@tonic-gate 					(void) printf(ep->e_str,
8647c478bd9Sstevel@tonic-gate 						(int32_t)u64_val);
8657c478bd9Sstevel@tonic-gate 					break;
8667c478bd9Sstevel@tonic-gate 				}
8677c478bd9Sstevel@tonic-gate #endif	/* XPG4 */
8687c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
8697c478bd9Sstevel@tonic-gate 			case ULONG:
8707c478bd9Sstevel@tonic-gate 				(void) printf(ep->e_str,
8717c478bd9Sstevel@tonic-gate 				    (uint32_t)u64_val);
8727c478bd9Sstevel@tonic-gate 				break;
8737c478bd9Sstevel@tonic-gate 			case SHORT:
8747c478bd9Sstevel@tonic-gate #ifdef XPG4
8757c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
8767c478bd9Sstevel@tonic-gate 					(void) printf(ep->e_str,
8777c478bd9Sstevel@tonic-gate 						(int16_t)u64_val);
8787c478bd9Sstevel@tonic-gate 					break;
8797c478bd9Sstevel@tonic-gate 				}
8807c478bd9Sstevel@tonic-gate #endif	/* XPG4 */
8817c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
8827c478bd9Sstevel@tonic-gate 			case USHORT:
8837c478bd9Sstevel@tonic-gate 				(void) printf(ep->e_str,
8847c478bd9Sstevel@tonic-gate 				    (uint16_t)u64_val);
8857c478bd9Sstevel@tonic-gate 				break;
8867c478bd9Sstevel@tonic-gate 			case BYTE:
8877c478bd9Sstevel@tonic-gate #ifdef XPG4
8887c478bd9Sstevel@tonic-gate 				if (ep->e_mask == 0) {
8897c478bd9Sstevel@tonic-gate 					(void) printf(ep->e_str,
8907c478bd9Sstevel@tonic-gate 						(int8_t)u64_val);
8917c478bd9Sstevel@tonic-gate 					break;
8927c478bd9Sstevel@tonic-gate 				}
8937c478bd9Sstevel@tonic-gate #endif	/* XPG4 */
8947c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
8957c478bd9Sstevel@tonic-gate 			case UBYTE:
8967c478bd9Sstevel@tonic-gate 				(void) printf(ep->e_str,
8977c478bd9Sstevel@tonic-gate 				    (uint8_t)u64_val);
8987c478bd9Sstevel@tonic-gate 				break;
8997c478bd9Sstevel@tonic-gate 			case STR:
9007c478bd9Sstevel@tonic-gate 				/*
9017c478bd9Sstevel@tonic-gate 				 * Note: Currently can't get type
9027c478bd9Sstevel@tonic-gate 				 * STR here because we already
9037c478bd9Sstevel@tonic-gate 				 * did a 'continue' out of the
9047c478bd9Sstevel@tonic-gate 				 * loop earlier for case STR
9057c478bd9Sstevel@tonic-gate 				 */
9067c478bd9Sstevel@tonic-gate 				break;
9077c478bd9Sstevel@tonic-gate 			}
9087c478bd9Sstevel@tonic-gate 		} else
9097c478bd9Sstevel@tonic-gate 			(void) printf(ep->e_str);
9107c478bd9Sstevel@tonic-gate 		lev1 = 1;
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 	result = lev1 ? (int)(1 + ep - mtab) : 0;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	return (result);
9157c478bd9Sstevel@tonic-gate }
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate static void
9187c478bd9Sstevel@tonic-gate showstr(char *s, int width)
9197c478bd9Sstevel@tonic-gate {
9207c478bd9Sstevel@tonic-gate 	char c;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	while ((c = *s++) != '\0')
9237c478bd9Sstevel@tonic-gate 		if (c >= 040 && c < 0176) {
9247c478bd9Sstevel@tonic-gate 			(void) putchar(c);
9257c478bd9Sstevel@tonic-gate 			width--;
9267c478bd9Sstevel@tonic-gate 		} else {
9277c478bd9Sstevel@tonic-gate 			(void) putchar('\\');
9287c478bd9Sstevel@tonic-gate 			switch (c) {
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 			case '\n':
9317c478bd9Sstevel@tonic-gate 				(void) putchar('n');
9327c478bd9Sstevel@tonic-gate 				width -= 2;
9337c478bd9Sstevel@tonic-gate 				break;
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 			case '\r':
9367c478bd9Sstevel@tonic-gate 				(void) putchar('r');
9377c478bd9Sstevel@tonic-gate 				width -= 2;
9387c478bd9Sstevel@tonic-gate 				break;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 			case '\a':
9417c478bd9Sstevel@tonic-gate 				(void) putchar('a');
9427c478bd9Sstevel@tonic-gate 				width -= 2;
9437c478bd9Sstevel@tonic-gate 				break;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 			case '\b':
9467c478bd9Sstevel@tonic-gate 				(void) putchar('b');
9477c478bd9Sstevel@tonic-gate 				width -= 2;
9487c478bd9Sstevel@tonic-gate 				break;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 			case '\t':
9517c478bd9Sstevel@tonic-gate 				(void) putchar('t');
9527c478bd9Sstevel@tonic-gate 				width -= 2;
9537c478bd9Sstevel@tonic-gate 				break;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 			case '\f':
9567c478bd9Sstevel@tonic-gate 				(void) putchar('f');
9577c478bd9Sstevel@tonic-gate 				width -= 2;
9587c478bd9Sstevel@tonic-gate 				break;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 			case '\v':
9617c478bd9Sstevel@tonic-gate 				(void) putchar('v');
9627c478bd9Sstevel@tonic-gate 				width -= 2;
9637c478bd9Sstevel@tonic-gate 				break;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 			default:
9667c478bd9Sstevel@tonic-gate 				(void) printf("%.3o", c & 0377);
9677c478bd9Sstevel@tonic-gate 				width -= 4;
9687c478bd9Sstevel@tonic-gate 				break;
9697c478bd9Sstevel@tonic-gate 			}
9707c478bd9Sstevel@tonic-gate 		}
9717c478bd9Sstevel@tonic-gate 	while (width >= 0) {
9727c478bd9Sstevel@tonic-gate 		(void) putchar(' ');
9737c478bd9Sstevel@tonic-gate 		width--;
9747c478bd9Sstevel@tonic-gate 	};
9757c478bd9Sstevel@tonic-gate }
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate static char *
9787c478bd9Sstevel@tonic-gate type_to_name(Entry *ep)
9797c478bd9Sstevel@tonic-gate {
9807c478bd9Sstevel@tonic-gate 	static char buf[20];
9817c478bd9Sstevel@tonic-gate 	char	*s;
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	switch (ep->e_type) {
9847c478bd9Sstevel@tonic-gate 	case BYTE:
9857c478bd9Sstevel@tonic-gate 		s = "byte";
9867c478bd9Sstevel@tonic-gate 		break;
9877c478bd9Sstevel@tonic-gate 	case SHORT:
9887c478bd9Sstevel@tonic-gate 		s = "short";
9897c478bd9Sstevel@tonic-gate 		break;
9907c478bd9Sstevel@tonic-gate 	case LONG:
9917c478bd9Sstevel@tonic-gate 		s = "long";
9927c478bd9Sstevel@tonic-gate 		break;
9937c478bd9Sstevel@tonic-gate 	case LLONG:
9947c478bd9Sstevel@tonic-gate 		s = "llong";
9957c478bd9Sstevel@tonic-gate 		break;
9967c478bd9Sstevel@tonic-gate 	case UBYTE:
9977c478bd9Sstevel@tonic-gate 		s = "ubyte";
9987c478bd9Sstevel@tonic-gate 		break;
9997c478bd9Sstevel@tonic-gate 	case USHORT:
10007c478bd9Sstevel@tonic-gate 		s = "ushort";
10017c478bd9Sstevel@tonic-gate 		break;
10027c478bd9Sstevel@tonic-gate 	case ULONG:
10037c478bd9Sstevel@tonic-gate 		s = "ulong";
10047c478bd9Sstevel@tonic-gate 		break;
10057c478bd9Sstevel@tonic-gate 	case ULLONG:
10067c478bd9Sstevel@tonic-gate 		s = "ullong";
10077c478bd9Sstevel@tonic-gate 		break;
10087c478bd9Sstevel@tonic-gate 	case STR:
10097c478bd9Sstevel@tonic-gate 		return ("string");
10107c478bd9Sstevel@tonic-gate 	default:
10117c478bd9Sstevel@tonic-gate 		/* more of an emergency measure .. */
10127c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "%d", ep->e_type);
10137c478bd9Sstevel@tonic-gate 		return (buf);
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 	if (ep->e_mask) {
10167c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s&0x%llx", s, ep->e_mask);
10177c478bd9Sstevel@tonic-gate 		return (buf);
10187c478bd9Sstevel@tonic-gate 	} else
10197c478bd9Sstevel@tonic-gate 		return (s);
10207c478bd9Sstevel@tonic-gate }
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate static char
10237c478bd9Sstevel@tonic-gate op_to_name(char op)
10247c478bd9Sstevel@tonic-gate {
10257c478bd9Sstevel@tonic-gate 	char c;
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	switch (op & ~SUB) {
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	case EQ:
10307c478bd9Sstevel@tonic-gate 	case STRC:
10317c478bd9Sstevel@tonic-gate 		c = '=';
10327c478bd9Sstevel@tonic-gate 		break;
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	case GT:
10357c478bd9Sstevel@tonic-gate 		c = '>';
10367c478bd9Sstevel@tonic-gate 		break;
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	case LT:
10397c478bd9Sstevel@tonic-gate 		c = '<';
10407c478bd9Sstevel@tonic-gate 		break;
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	case ANY:
10437c478bd9Sstevel@tonic-gate 		c = 'x';
10447c478bd9Sstevel@tonic-gate 		break;
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	case AND:
10477c478bd9Sstevel@tonic-gate 		c = '&';
10487c478bd9Sstevel@tonic-gate 		break;
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	case NSET:
10517c478bd9Sstevel@tonic-gate 		c = '^';
10527c478bd9Sstevel@tonic-gate 		break;
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	default:
10557c478bd9Sstevel@tonic-gate 		c = '?';
10567c478bd9Sstevel@tonic-gate 		break;
10577c478bd9Sstevel@tonic-gate 	}
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	return (c);
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate /*
10637c478bd9Sstevel@tonic-gate  * f_prtmtab - Prints out a header, then entries from both magic
10647c478bd9Sstevel@tonic-gate  *	tables, mtab1 and mtab2, if any exist.
10657c478bd9Sstevel@tonic-gate  */
10667c478bd9Sstevel@tonic-gate void
10677c478bd9Sstevel@tonic-gate f_prtmtab(void)
10687c478bd9Sstevel@tonic-gate {
10697c478bd9Sstevel@tonic-gate 	Entry	*mtab;
10707c478bd9Sstevel@tonic-gate 	Entry	*ep;
10717c478bd9Sstevel@tonic-gate 	int	count;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	(void) printf("%-7s %-7s %-10s %-7s %-11s %s\n",
10747c478bd9Sstevel@tonic-gate 		"level", "off", "type", "opcode", "value", "string");
10757c478bd9Sstevel@tonic-gate 	for (mtab = mtab1, count = 1; count <= 2; count++, mtab = mtab2) {
10767c478bd9Sstevel@tonic-gate 		if (mtab == (Entry *)NULL) {
10777c478bd9Sstevel@tonic-gate 			continue;
10787c478bd9Sstevel@tonic-gate 		}
10797c478bd9Sstevel@tonic-gate 		for (ep = mtab; ep->e_off != -1L; ep++) {
10807c478bd9Sstevel@tonic-gate 			(void) printf("%-7d %-7ld %-10s %-7c ",
10817c478bd9Sstevel@tonic-gate 			    ep->e_level,
10827c478bd9Sstevel@tonic-gate 			    ep->e_off, type_to_name(ep),
10837c478bd9Sstevel@tonic-gate 			    op_to_name(ep->e_opcode));
10847c478bd9Sstevel@tonic-gate 			if (ep->e_type == STR) {
10857c478bd9Sstevel@tonic-gate 				showstr(ep->e_value.str, 10);
10867c478bd9Sstevel@tonic-gate 			} else {	/* numeric */
10877c478bd9Sstevel@tonic-gate 				(void) printf("%-#11llo", ep->e_value.num);
10887c478bd9Sstevel@tonic-gate 			}
10897c478bd9Sstevel@tonic-gate 			(void) printf(" %s", ep->e_str);
10907c478bd9Sstevel@tonic-gate 			if (ep->e_opcode & SUB)
10917c478bd9Sstevel@tonic-gate 				(void) printf("\tsubst");
10927c478bd9Sstevel@tonic-gate 			(void) printf("\n");
10937c478bd9Sstevel@tonic-gate 		}
10947c478bd9Sstevel@tonic-gate 	}
10957c478bd9Sstevel@tonic-gate }
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate intmax_t
10987c478bd9Sstevel@tonic-gate f_getmaxoffset(int first)
10997c478bd9Sstevel@tonic-gate {
11007c478bd9Sstevel@tonic-gate 	Entry *mtab;
11017c478bd9Sstevel@tonic-gate 	Entry *ep;
11027c478bd9Sstevel@tonic-gate 	intmax_t cur;
11037c478bd9Sstevel@tonic-gate 	intmax_t max = 0;
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	if (first) {
11067c478bd9Sstevel@tonic-gate 		mtab = mtab1;
11077c478bd9Sstevel@tonic-gate 	} else {
11087c478bd9Sstevel@tonic-gate 		mtab = mtab2;
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate 	if (mtab == (Entry *)NULL) {
11117c478bd9Sstevel@tonic-gate 		return (0);
11127c478bd9Sstevel@tonic-gate 	}
11137c478bd9Sstevel@tonic-gate 	for (ep = mtab; ep->e_off != -1L; ep++) {
11147c478bd9Sstevel@tonic-gate 		cur = ep->e_off;
11157c478bd9Sstevel@tonic-gate 		switch (ep->e_type) {
11167c478bd9Sstevel@tonic-gate 		case STR:
11177c478bd9Sstevel@tonic-gate 			cur += strlen(ep->e_value.str);
11187c478bd9Sstevel@tonic-gate 			break;
11197c478bd9Sstevel@tonic-gate 		case BYTE:
11207c478bd9Sstevel@tonic-gate 		case UBYTE:
11217c478bd9Sstevel@tonic-gate 			cur += sizeof (uchar_t);
11227c478bd9Sstevel@tonic-gate 			break;
11237c478bd9Sstevel@tonic-gate 		case SHORT:
11247c478bd9Sstevel@tonic-gate 		case USHORT:
11257c478bd9Sstevel@tonic-gate 			cur += sizeof (uint16_t);
11267c478bd9Sstevel@tonic-gate 			break;
11277c478bd9Sstevel@tonic-gate 		case LONG:
11287c478bd9Sstevel@tonic-gate 		case ULONG:
11297c478bd9Sstevel@tonic-gate 			cur += sizeof (uint32_t);
11307c478bd9Sstevel@tonic-gate 			break;
11317c478bd9Sstevel@tonic-gate 		case LLONG:
11327c478bd9Sstevel@tonic-gate 		case ULLONG:
11337c478bd9Sstevel@tonic-gate 			cur += sizeof (uint64_t);
11347c478bd9Sstevel@tonic-gate 			break;
11357c478bd9Sstevel@tonic-gate 		}
11367c478bd9Sstevel@tonic-gate 		if (cur <= INT_MAX && cur > max) {
11377c478bd9Sstevel@tonic-gate 			max = cur;
11387c478bd9Sstevel@tonic-gate 		}
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	return (max);
11427c478bd9Sstevel@tonic-gate }
1143