xref: /onnv-gate/usr/src/cmd/file/file.c (revision 1976)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1976Sab196087  * Common Development and Distribution License (the "License").
6*1976Sab196087  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
220Sstevel@tonic-gate /*	  All Rights Reserved  	*/
230Sstevel@tonic-gate 
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
260Sstevel@tonic-gate /*	  All Rights Reserved	*/
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
29*1976Sab196087  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
300Sstevel@tonic-gate  * Use is subject to license terms.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #define	_LARGEFILE64_SOURCE
360Sstevel@tonic-gate 
37*1976Sab196087 /* Get definitions for the relocation types supported. */
38*1976Sab196087 #define	ELF_TARGET_ALL
39*1976Sab196087 
400Sstevel@tonic-gate #include <ctype.h>
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <fcntl.h>
430Sstevel@tonic-gate #include <signal.h>
440Sstevel@tonic-gate #include <stdio.h>
450Sstevel@tonic-gate #include <libelf.h>
460Sstevel@tonic-gate #include <stdlib.h>
470Sstevel@tonic-gate #include <limits.h>
480Sstevel@tonic-gate #include <locale.h>
490Sstevel@tonic-gate #include <wctype.h>
500Sstevel@tonic-gate #include <string.h>
510Sstevel@tonic-gate #include <errno.h>
520Sstevel@tonic-gate #include <door.h>
530Sstevel@tonic-gate #include <sys/param.h>
540Sstevel@tonic-gate #include <sys/types.h>
550Sstevel@tonic-gate #include <sys/mkdev.h>
560Sstevel@tonic-gate #include <sys/stat.h>
570Sstevel@tonic-gate #include <sys/elf.h>
580Sstevel@tonic-gate #include <procfs.h>
590Sstevel@tonic-gate #include <sys/core.h>
600Sstevel@tonic-gate #include <sys/dumphdr.h>
610Sstevel@tonic-gate #include <netinet/in.h>
620Sstevel@tonic-gate #include <gelf.h>
630Sstevel@tonic-gate #include <elfcap.h>
64*1976Sab196087 #include <sgsrtcid.h>
650Sstevel@tonic-gate #include "file.h"
660Sstevel@tonic-gate 
670Sstevel@tonic-gate typedef Elf64_Nhdr	GElf_Nhdr;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  *	Misc
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #define	FBSZ		512
740Sstevel@tonic-gate #define	MLIST_SZ	12
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * The 0x8FCA0102 magic string was used in crash dumps generated by releases
780Sstevel@tonic-gate  * prior to Solaris 7.
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate #define	OLD_DUMP_MAGIC	0x8FCA0102
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #if defined(__sparc)
830Sstevel@tonic-gate #define	NATIVE_ISA	"SPARC"
840Sstevel@tonic-gate #define	OTHER_ISA	"Intel"
850Sstevel@tonic-gate #else
860Sstevel@tonic-gate #define	NATIVE_ISA	"Intel"
870Sstevel@tonic-gate #define	OTHER_ISA	"SPARC"
880Sstevel@tonic-gate #endif
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /* Assembly language comment char */
910Sstevel@tonic-gate #ifdef pdp11
920Sstevel@tonic-gate #define	ASCOMCHAR '/'
930Sstevel@tonic-gate #else
940Sstevel@tonic-gate #define	ASCOMCHAR '!'
950Sstevel@tonic-gate #endif
960Sstevel@tonic-gate 
970Sstevel@tonic-gate #pragma	align	16(fbuf)
980Sstevel@tonic-gate static char	fbuf[FBSZ];
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * Magic file variables
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate static intmax_t maxmagicoffset;
1040Sstevel@tonic-gate static intmax_t tmpmax;
1050Sstevel@tonic-gate static char	*magicbuf;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static char	*dfile;
1080Sstevel@tonic-gate static char	*troff[] = {	/* new troff intermediate lang */
1090Sstevel@tonic-gate 		"x", "T", "res", "init", "font", "202", "V0", "p1", 0};
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static char	*fort[] = {			/* FORTRAN */
1120Sstevel@tonic-gate 		"function", "subroutine", "common", "dimension", "block",
1130Sstevel@tonic-gate 		"integer", "real", "data", "double",
1140Sstevel@tonic-gate 		"FUNCTION", "SUBROUTINE", "COMMON", "DIMENSION", "BLOCK",
1150Sstevel@tonic-gate 		"INTEGER", "REAL", "DATA", "DOUBLE", 0};
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate static char	*asc[] = {		/* Assembler Commands */
1180Sstevel@tonic-gate 		"sys", "mov", "tst", "clr", "jmp", "cmp", "set", "inc",
1190Sstevel@tonic-gate 		"dec", 0};
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate static char	*c[] = {			/* C Language */
1220Sstevel@tonic-gate 		"int", "char", "float", "double", "short", "long", "unsigned",
1230Sstevel@tonic-gate 		"register", "static", "struct", "extern", 0};
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate static char	*as[] = {	/* Assembler Pseudo Ops, prepended with '.' */
1260Sstevel@tonic-gate 		"globl", "global", "ident", "file", "byte", "even",
1270Sstevel@tonic-gate 		"text", "data", "bss", "comm", 0};
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  * The line and debug section names are used by the strip command.
1310Sstevel@tonic-gate  * Any changes in the strip implementation need to be reflected here.
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate static char	*debug_sections[] = { /* Debug sections in a ELF file */
1340Sstevel@tonic-gate 		".debug", ".stab", ".dwarf", ".line", NULL};
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /* start for MB env */
1380Sstevel@tonic-gate static wchar_t wchar;
1390Sstevel@tonic-gate static int	length;
1400Sstevel@tonic-gate static int	IS_ascii;
1410Sstevel@tonic-gate static int	Max;
1420Sstevel@tonic-gate /* end for MB env */
1430Sstevel@tonic-gate static int	i;	/* global index into first 'fbsz' bytes of file */
1440Sstevel@tonic-gate static int	fbsz;
1450Sstevel@tonic-gate static int	ifd = -1;
1460Sstevel@tonic-gate static int	elffd = -1;
1470Sstevel@tonic-gate static int	tret;
1480Sstevel@tonic-gate static int	hflg;
1490Sstevel@tonic-gate static int	dflg;
1500Sstevel@tonic-gate static int	mflg;
1510Sstevel@tonic-gate static int	M_flg;
1520Sstevel@tonic-gate static int	iflg;
1530Sstevel@tonic-gate static struct stat64	mbuf;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate static char	**mlist1;	/* 1st ordered list of magic files */
1560Sstevel@tonic-gate static char	**mlist2;	/* 2nd ordered list of magic files */
1570Sstevel@tonic-gate static size_t	mlist1_sz;	/* number of ptrs allocated for mlist1 */
1580Sstevel@tonic-gate static size_t	mlist2_sz;	/* number of ptrs allocated for mlist2 */
1590Sstevel@tonic-gate static char	**mlist1p;	/* next entry in mlist1 */
1600Sstevel@tonic-gate static char	**mlist2p;	/* next entry in mlist2 */
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate static ssize_t	mread;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate static void is_stripped(Elf *elf);
1650Sstevel@tonic-gate static Elf *is_elf_file(int elffd);
1660Sstevel@tonic-gate static void ar_coff_or_aout(int ifd);
1670Sstevel@tonic-gate static int type(char *file);
1680Sstevel@tonic-gate static int def_position_tests(void);
1690Sstevel@tonic-gate static void def_context_tests(void);
1700Sstevel@tonic-gate static int troffint(char *bp, int n);
1710Sstevel@tonic-gate static int lookup(char **tab);
1720Sstevel@tonic-gate static int ccom(void);
1730Sstevel@tonic-gate static int ascom(void);
1740Sstevel@tonic-gate static int sccs(void);
1750Sstevel@tonic-gate static int english(char *bp, int n);
1760Sstevel@tonic-gate static int old_core(Elf *elf, GElf_Ehdr *ehdr, int format);
1770Sstevel@tonic-gate static int core(Elf *elf, GElf_Ehdr *ehdr, int format);
1780Sstevel@tonic-gate static int shellscript(char buf[], struct stat64 *sb);
1790Sstevel@tonic-gate static int elf_check(Elf *elf);
1800Sstevel@tonic-gate static int get_door_target(char *, char *, size_t);
1810Sstevel@tonic-gate static int zipfile(char *, int);
1820Sstevel@tonic-gate static int is_crash_dump(const char *, int);
1830Sstevel@tonic-gate static void print_dumphdr(const int, const dumphdr_t *, uint32_t (*)(uint32_t),
1840Sstevel@tonic-gate     const char *);
1850Sstevel@tonic-gate static uint32_t swap_uint32(uint32_t);
1860Sstevel@tonic-gate static uint32_t return_uint32(uint32_t);
1870Sstevel@tonic-gate static int is_in_list(char *[], char *);
1880Sstevel@tonic-gate static void usage(void);
1890Sstevel@tonic-gate static void default_magic(void);
1900Sstevel@tonic-gate static void add_to_mlist(char *, int);
1910Sstevel@tonic-gate static void fd_cleanup(void);
192*1976Sab196087 static int is_rtld_config(void);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate #ifdef XPG4
1950Sstevel@tonic-gate 	/* SUSv3 requires a single <space> after the colon */
1960Sstevel@tonic-gate #define	prf(x)	(void) printf("%s: ", x);
1970Sstevel@tonic-gate #else	/* !XPG4 */
1980Sstevel@tonic-gate #define	prf(x)	(void) printf("%s:%s", x, (int)strlen(x) > 6 ? "\t" : "\t\t");
1990Sstevel@tonic-gate #endif	/* XPG4 */
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate int
2020Sstevel@tonic-gate main(int argc, char **argv)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate 	char	*p;
2050Sstevel@tonic-gate 	int	ch;
2060Sstevel@tonic-gate 	FILE	*fl;
2070Sstevel@tonic-gate 	int	cflg = 0;
2080Sstevel@tonic-gate 	int	eflg = 0;
2090Sstevel@tonic-gate 	int	fflg = 0;
2100Sstevel@tonic-gate 	char	*ap = NULL;
2110Sstevel@tonic-gate 	int	pathlen;
2120Sstevel@tonic-gate 	char	**filep;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2150Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2160Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2170Sstevel@tonic-gate #endif
2180Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "M:cdf:him:")) != EOF) {
2210Sstevel@tonic-gate 		switch (ch) {
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 		case 'M':
2240Sstevel@tonic-gate 			add_to_mlist(optarg, !dflg);
2250Sstevel@tonic-gate 			M_flg++;
2260Sstevel@tonic-gate 			break;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 		case 'c':
2290Sstevel@tonic-gate 			cflg++;
2300Sstevel@tonic-gate 			break;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		case 'd':
2330Sstevel@tonic-gate 			if (!dflg) {
2340Sstevel@tonic-gate 				default_magic();
2350Sstevel@tonic-gate 				add_to_mlist(dfile, 0);
2360Sstevel@tonic-gate 				dflg++;
2370Sstevel@tonic-gate 			}
2380Sstevel@tonic-gate 			break;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 		case 'f':
2410Sstevel@tonic-gate 			fflg++;
2420Sstevel@tonic-gate 			if ((fl = fopen(optarg, "r")) == NULL) {
2430Sstevel@tonic-gate 				(void) fprintf(stderr,
2440Sstevel@tonic-gate 					gettext("cannot open %s\n"), optarg);
2450Sstevel@tonic-gate 				usage();
2460Sstevel@tonic-gate 			}
2470Sstevel@tonic-gate 			pathlen = pathconf("/", _PC_PATH_MAX);
2480Sstevel@tonic-gate 			if (pathlen == -1) {
2490Sstevel@tonic-gate 				(void) fprintf(stderr,
2500Sstevel@tonic-gate 				    gettext("pathconf: cannot determine "
2510Sstevel@tonic-gate 					"maximum path length\n"));
2520Sstevel@tonic-gate 				exit(1);
2530Sstevel@tonic-gate 			}
2540Sstevel@tonic-gate 			pathlen += 2; /* for null and newline in fgets */
2550Sstevel@tonic-gate 			ap = malloc(pathlen * sizeof (char));
2560Sstevel@tonic-gate 			if (ap == NULL) {
2570Sstevel@tonic-gate 				perror("malloc");
2580Sstevel@tonic-gate 				exit(1);
2590Sstevel@tonic-gate 			}
2600Sstevel@tonic-gate 			break;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 		case 'h':
2630Sstevel@tonic-gate 			hflg++;
2640Sstevel@tonic-gate 			break;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 		case 'i':
2670Sstevel@tonic-gate 			iflg++;
2680Sstevel@tonic-gate 			break;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 		case 'm':
2710Sstevel@tonic-gate 			add_to_mlist(optarg, !dflg);
2720Sstevel@tonic-gate 			mflg++;
2730Sstevel@tonic-gate 			break;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 		case '?':
2760Sstevel@tonic-gate 			eflg++;
2770Sstevel@tonic-gate 			break;
2780Sstevel@tonic-gate 		}
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 	if (!cflg && !fflg && (eflg || optind == argc))
2810Sstevel@tonic-gate 		usage();
2820Sstevel@tonic-gate 	if (iflg && (dflg || mflg || M_flg)) {
2830Sstevel@tonic-gate 		usage();
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 	if (iflg && cflg) {
2860Sstevel@tonic-gate 		usage();
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if (!dflg && !mflg && !M_flg && !iflg) {
2900Sstevel@tonic-gate 	/* no -d, -m, nor -M option; also -i option doesn't need magic  */
2910Sstevel@tonic-gate 		default_magic();
2920Sstevel@tonic-gate 		if (f_mkmtab(dfile, cflg, 0) == -1) {
2930Sstevel@tonic-gate 			exit(2);
2940Sstevel@tonic-gate 		}
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	else if (mflg && !M_flg && !dflg) {
2980Sstevel@tonic-gate 	/* -m specified without -d nor -M */
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate #ifdef XPG4	/* For SUSv3 only */
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 		/*
3030Sstevel@tonic-gate 		 * The default position-dependent magic file tests
3040Sstevel@tonic-gate 		 * in /etc/magic will follow all the -m magic tests.
3050Sstevel@tonic-gate 		 */
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		for (filep = mlist1; filep < mlist1p; filep++) {
3080Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 1) == -1) {
3090Sstevel@tonic-gate 				exit(2);
3100Sstevel@tonic-gate 			}
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate 		default_magic();
3130Sstevel@tonic-gate 		if (f_mkmtab(dfile, cflg, 0) == -1) {
3140Sstevel@tonic-gate 			exit(2);
3150Sstevel@tonic-gate 		}
3160Sstevel@tonic-gate #else	/* !XPG4 */
3170Sstevel@tonic-gate 		/*
3180Sstevel@tonic-gate 		 * Retain Solaris file behavior for -m before SUSv3,
3190Sstevel@tonic-gate 		 * when the new -d and -M options are not specified.
3200Sstevel@tonic-gate 		 * Use the -m file specified in place of the default
3210Sstevel@tonic-gate 		 * /etc/magic file.  Solaris file will
3220Sstevel@tonic-gate 		 * now allow more than one magic file to be specified
3230Sstevel@tonic-gate 		 * with multiple -m options, for consistency with
3240Sstevel@tonic-gate 		 * other behavior.
3250Sstevel@tonic-gate 		 *
3260Sstevel@tonic-gate 		 * Put the magic table(s) specified by -m into
3270Sstevel@tonic-gate 		 * the second magic table instead of the first
3280Sstevel@tonic-gate 		 * (as indicated by the last argument to f_mkmtab()),
3290Sstevel@tonic-gate 		 * since they replace the /etc/magic tests and
3300Sstevel@tonic-gate 		 * must be executed alongside the default
3310Sstevel@tonic-gate 		 * position-sensitive tests.
3320Sstevel@tonic-gate 		 */
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 		for (filep = mlist1; filep < mlist1p; filep++) {
3350Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 0) == -1) {
3360Sstevel@tonic-gate 				exit(2);
3370Sstevel@tonic-gate 			}
3380Sstevel@tonic-gate 		}
3390Sstevel@tonic-gate #endif /* XPG4 */
3400Sstevel@tonic-gate 	} else {
3410Sstevel@tonic-gate 		/*
3420Sstevel@tonic-gate 		 * For any other combination of -d, -m, and -M,
3430Sstevel@tonic-gate 		 * use the magic files in command-line order.
3440Sstevel@tonic-gate 		 * Store the entries from the two separate lists of magic
3450Sstevel@tonic-gate 		 * files, if any, into two separate magic file tables.
3460Sstevel@tonic-gate 		 * mlist1: magic tests executed before default magic tests
3470Sstevel@tonic-gate 		 * mlist2: default magic tests and after
3480Sstevel@tonic-gate 		 */
3490Sstevel@tonic-gate 		for (filep = mlist1; filep && (filep < mlist1p); filep++) {
3500Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 1) == -1) {
3510Sstevel@tonic-gate 				exit(2);
3520Sstevel@tonic-gate 			}
3530Sstevel@tonic-gate 		}
3540Sstevel@tonic-gate 		for (filep = mlist2; filep && (filep < mlist2p); filep++) {
3550Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 0) == -1) {
3560Sstevel@tonic-gate 				exit(2);
3570Sstevel@tonic-gate 			}
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	/* Initialize the magic file variables; check both magic tables */
3620Sstevel@tonic-gate 	tmpmax = f_getmaxoffset(1);
3630Sstevel@tonic-gate 	maxmagicoffset = f_getmaxoffset(0);
3640Sstevel@tonic-gate 	if (maxmagicoffset < tmpmax) {
3650Sstevel@tonic-gate 		maxmagicoffset = tmpmax;
3660Sstevel@tonic-gate 	}
3670Sstevel@tonic-gate 	if (maxmagicoffset < (intmax_t)FBSZ)
3680Sstevel@tonic-gate 		maxmagicoffset = (intmax_t)FBSZ;
3690Sstevel@tonic-gate 	if ((magicbuf = (char *)malloc(maxmagicoffset)) == NULL) {
3700Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("malloc failed\n"));
3710Sstevel@tonic-gate 		exit(2);
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	if (cflg) {
3750Sstevel@tonic-gate 		f_prtmtab();
3760Sstevel@tonic-gate 		if (ferror(stdout) != 0) {
3770Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("file: error writing to "
3780Sstevel@tonic-gate 			    "stdout\n"));
3790Sstevel@tonic-gate 			exit(1);
3800Sstevel@tonic-gate 		}
3810Sstevel@tonic-gate 		if (fclose(stdout) != 0) {
3820Sstevel@tonic-gate 			perror(gettext("file: fclose failed"));
3830Sstevel@tonic-gate 			exit(1);
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 		exit(0);
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 	for (; fflg || optind < argc; optind += !fflg) {
3880Sstevel@tonic-gate 		register int	l;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 		if (fflg) {
3910Sstevel@tonic-gate 			if ((p = fgets(ap, pathlen, fl)) == NULL) {
3920Sstevel@tonic-gate 				fflg = 0;
3930Sstevel@tonic-gate 				optind--;
3940Sstevel@tonic-gate 				continue;
3950Sstevel@tonic-gate 			}
3960Sstevel@tonic-gate 			l = strlen(p);
3970Sstevel@tonic-gate 			if (l > 0)
3980Sstevel@tonic-gate 				p[l - 1] = '\0';
3990Sstevel@tonic-gate 		} else
4000Sstevel@tonic-gate 			p = argv[optind];
4010Sstevel@tonic-gate 		prf(p);				/* print "file_name:<tab>" */
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 		if (type(p))
4040Sstevel@tonic-gate 			tret = 1;
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate 	if (ap != NULL)
4070Sstevel@tonic-gate 		free(ap);
4080Sstevel@tonic-gate 	if (tret != 0) {
4090Sstevel@tonic-gate 		exit(tret);
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 	if (ferror(stdout) != 0) {
4120Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("file: error writing to "
4130Sstevel@tonic-gate 		    "stdout\n"));
4140Sstevel@tonic-gate 		exit(1);
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 	if (fclose(stdout) != 0) {
4170Sstevel@tonic-gate 		perror(gettext("file: fclose failed"));
4180Sstevel@tonic-gate 		exit(1);
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 	return (0);
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate static int
4240Sstevel@tonic-gate type(char *file)
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate 	int	cc;
4270Sstevel@tonic-gate 	char	buf[BUFSIZ];
4280Sstevel@tonic-gate 	int	(*statf)() = hflg ? lstat64 : stat64;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	i = 0;		/* reset index to beginning of file */
4310Sstevel@tonic-gate 	ifd = -1;
4320Sstevel@tonic-gate 	if ((*statf)(file, &mbuf) < 0) {
4330Sstevel@tonic-gate 		if (statf == lstat64 || lstat64(file, &mbuf) < 0) {
4340Sstevel@tonic-gate 			(void) printf(gettext("cannot open: %s\n"),
4350Sstevel@tonic-gate 			    strerror(errno));
4360Sstevel@tonic-gate 			return (0);		/* POSIX.2 */
4370Sstevel@tonic-gate 		}
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate 	switch (mbuf.st_mode & S_IFMT) {
4400Sstevel@tonic-gate 	case S_IFREG:
4410Sstevel@tonic-gate 		if (iflg) {
4420Sstevel@tonic-gate 			(void) printf(gettext("regular file\n"));
4430Sstevel@tonic-gate 			return (0);
4440Sstevel@tonic-gate 		}
4450Sstevel@tonic-gate 		break;
4460Sstevel@tonic-gate 	case S_IFCHR:
4470Sstevel@tonic-gate 		(void) printf(gettext("character"));
4480Sstevel@tonic-gate 		goto spcl;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	case S_IFDIR:
4510Sstevel@tonic-gate 		(void) printf(gettext("directory\n"));
4520Sstevel@tonic-gate 		return (0);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	case S_IFIFO:
4550Sstevel@tonic-gate 		(void) printf(gettext("fifo\n"));
4560Sstevel@tonic-gate 		return (0);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	case S_IFLNK:
4590Sstevel@tonic-gate 		if ((cc = readlink(file, buf, BUFSIZ)) < 0) {
4600Sstevel@tonic-gate 			(void) printf(gettext("readlink error: %s\n"),
4610Sstevel@tonic-gate 				strerror(errno));
4620Sstevel@tonic-gate 			return (1);
4630Sstevel@tonic-gate 		}
4640Sstevel@tonic-gate 		buf[cc] = '\0';
4650Sstevel@tonic-gate 		(void) printf(gettext("symbolic link to %s\n"), buf);
4660Sstevel@tonic-gate 		return (0);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	case S_IFBLK:
4690Sstevel@tonic-gate 		(void) printf(gettext("block"));
4700Sstevel@tonic-gate 					/* major and minor, see sys/mkdev.h */
4710Sstevel@tonic-gate spcl:
4720Sstevel@tonic-gate 		(void) printf(gettext(" special (%d/%d)\n"),
4730Sstevel@tonic-gate 		    major(mbuf.st_rdev), minor(mbuf.st_rdev));
4740Sstevel@tonic-gate 		return (0);
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	case S_IFSOCK:
4770Sstevel@tonic-gate 		(void) printf("socket\n");
4780Sstevel@tonic-gate 		/* FIXME, should open and try to getsockname. */
4790Sstevel@tonic-gate 		return (0);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	case S_IFDOOR:
4820Sstevel@tonic-gate 		if (get_door_target(file, buf, sizeof (buf)) == 0)
4830Sstevel@tonic-gate 			(void) printf(gettext("door to %s\n"), buf);
4840Sstevel@tonic-gate 		else
4850Sstevel@tonic-gate 			(void) printf(gettext("door\n"));
4860Sstevel@tonic-gate 		return (0);
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	}
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
4910Sstevel@tonic-gate 		(void) printf(gettext("libelf is out of date\n"));
4920Sstevel@tonic-gate 		return (1);
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	ifd = open64(file, O_RDONLY);
4960Sstevel@tonic-gate 	if (ifd < 0) {
4970Sstevel@tonic-gate 		(void) printf(gettext("cannot open: %s\n"), strerror(errno));
4980Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	/* need another fd for elf, since we might want to read the file too */
5020Sstevel@tonic-gate 	elffd = open64(file, O_RDONLY);
5030Sstevel@tonic-gate 	if (elffd < 0) {
5040Sstevel@tonic-gate 		(void) printf(gettext("cannot open: %s\n"), strerror(errno));
5050Sstevel@tonic-gate 		(void) close(ifd);
5060Sstevel@tonic-gate 		ifd = -1;
5070Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
5080Sstevel@tonic-gate 	}
5090Sstevel@tonic-gate 	if ((fbsz = read(ifd, fbuf, FBSZ)) == -1) {
5100Sstevel@tonic-gate 		(void) printf(gettext("cannot read: %s\n"), strerror(errno));
5110Sstevel@tonic-gate 		(void) close(ifd);
5120Sstevel@tonic-gate 		ifd = -1;
5130Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 	if (fbsz == 0) {
5160Sstevel@tonic-gate 		(void) printf(gettext("empty file\n"));
5170Sstevel@tonic-gate 		fd_cleanup();
5180Sstevel@tonic-gate 		return (0);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/*
5220Sstevel@tonic-gate 	 * First try user-specified position-dependent magic tests, if any,
5230Sstevel@tonic-gate 	 * which need to execute before the default tests.
5240Sstevel@tonic-gate 	 */
5250Sstevel@tonic-gate 	if ((mread = pread(ifd, (void*)magicbuf, (size_t)maxmagicoffset,
5260Sstevel@tonic-gate 			(off_t)0)) == -1) {
5270Sstevel@tonic-gate 		(void) printf(gettext("cannot read: %s\n"), strerror(errno));
5280Sstevel@tonic-gate 		fd_cleanup();
5290Sstevel@tonic-gate 		return (0);
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	/*
5330Sstevel@tonic-gate 	 * ChecK against Magic Table entries.
5340Sstevel@tonic-gate 	 * Check first magic table for magic tests to be applied
5350Sstevel@tonic-gate 	 * before default tests.
5360Sstevel@tonic-gate 	 * If no default tests are to be applied, all magic tests
5370Sstevel@tonic-gate 	 * should occur in this magic table.
5380Sstevel@tonic-gate 	 */
5390Sstevel@tonic-gate 	switch (f_ckmtab(magicbuf, mread, 1)) {
5400Sstevel@tonic-gate 		case -1:	/* Error */
5410Sstevel@tonic-gate 			exit(2);
5420Sstevel@tonic-gate 			break;
5430Sstevel@tonic-gate 		case 0:		/* Not magic */
5440Sstevel@tonic-gate 			break;
5450Sstevel@tonic-gate 		default:	/* Switch is magic index */
5460Sstevel@tonic-gate 			(void) putchar('\n');
5470Sstevel@tonic-gate 			fd_cleanup();
5480Sstevel@tonic-gate 			return (0);
5490Sstevel@tonic-gate 			/* NOTREACHED */
5500Sstevel@tonic-gate 			break;
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	if (dflg || !M_flg) {
5540Sstevel@tonic-gate 		/*
5550Sstevel@tonic-gate 		 * default position-dependent tests,
5560Sstevel@tonic-gate 		 * plus non-default magic tests, if any
5570Sstevel@tonic-gate 		 */
5580Sstevel@tonic-gate 		switch (def_position_tests()) {
5590Sstevel@tonic-gate 			case -1:	/* error */
5600Sstevel@tonic-gate 				fd_cleanup();
5610Sstevel@tonic-gate 				return (1);
5620Sstevel@tonic-gate 			case 1:	/* matching type found */
5630Sstevel@tonic-gate 				fd_cleanup();
5640Sstevel@tonic-gate 				return (0);
5650Sstevel@tonic-gate 				/* NOTREACHED */
5660Sstevel@tonic-gate 				break;
5670Sstevel@tonic-gate 			case 0:		/* no matching type found */
5680Sstevel@tonic-gate 				break;
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 		/* default context-sensitive tests */
5710Sstevel@tonic-gate 		def_context_tests();
5720Sstevel@tonic-gate 	} else {
5730Sstevel@tonic-gate 		/* no more tests to apply; no match was found */
5740Sstevel@tonic-gate 		(void) printf(gettext("data\n"));
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate 	fd_cleanup();
5770Sstevel@tonic-gate 	return (0);
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate /*
5810Sstevel@tonic-gate  * def_position_tests() - applies default position-sensitive tests,
5820Sstevel@tonic-gate  *	looking for values in specific positions in the file.
5830Sstevel@tonic-gate  *	These are followed by default (followed by possibly some
5840Sstevel@tonic-gate  *	non-default) magic file tests.
5850Sstevel@tonic-gate  *
5860Sstevel@tonic-gate  *	All position-sensitive tests, default or otherwise, must
5870Sstevel@tonic-gate  *	be applied before context-sensitive tests, to avoid
5880Sstevel@tonic-gate  *	false context-sensitive matches.
5890Sstevel@tonic-gate  *
5900Sstevel@tonic-gate  * 	Returns -1 on error which should result in error (non-zero)
5910Sstevel@tonic-gate  *	exit status for the file utility.
5920Sstevel@tonic-gate  *	Returns 0 if no matching file type found.
5930Sstevel@tonic-gate  *	Returns 1 if matching file type found.
5940Sstevel@tonic-gate  */
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate static int
5970Sstevel@tonic-gate def_position_tests(void)
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate 	Elf	*elf;
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	if (sccs()) {	/* look for "1hddddd" where d is a digit */
6020Sstevel@tonic-gate 		(void) printf("sccs \n");
6030Sstevel@tonic-gate 		return (1);
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 	if (fbuf[0] == '#' && fbuf[1] == '!' && shellscript(fbuf+2, &mbuf))
6060Sstevel@tonic-gate 		return (1);
6070Sstevel@tonic-gate 	if ((elf = is_elf_file(elffd)) != NULL) {
6080Sstevel@tonic-gate 		(void) elf_check(elf);
6090Sstevel@tonic-gate 		(void) elf_end(elf);
6100Sstevel@tonic-gate 		(void) putchar('\n');
6110Sstevel@tonic-gate 		return (1);
6120Sstevel@tonic-gate 
613*1976Sab196087 
6140Sstevel@tonic-gate 	/* LINTED: pointer cast may result in improper alignment */
6150Sstevel@tonic-gate 	} else if (*(int *)fbuf == CORE_MAGIC) {
6160Sstevel@tonic-gate 		/* LINTED: pointer cast may result in improper alignment */
6170Sstevel@tonic-gate 		struct core *corep = (struct core *)fbuf;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 		(void) printf("a.out core file");
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 		if (*(corep->c_cmdname) != '\0')
6220Sstevel@tonic-gate 			(void) printf(" from '%s'", corep->c_cmdname);
6230Sstevel@tonic-gate 		(void) putchar('\n');
6240Sstevel@tonic-gate 		return (1);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	/*
628*1976Sab196087 	 * Runtime linker (ld.so.1) configuration file.
629*1976Sab196087 	 */
630*1976Sab196087 	if (is_rtld_config())
631*1976Sab196087 		return (1);
632*1976Sab196087 
633*1976Sab196087 	/*
6340Sstevel@tonic-gate 	 * ZIP files, JAR files, and Java executables
6350Sstevel@tonic-gate 	 */
6360Sstevel@tonic-gate 	if (zipfile(fbuf, ifd))
6370Sstevel@tonic-gate 		return (1);
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	if (is_crash_dump(fbuf, ifd))
6400Sstevel@tonic-gate 		return (1);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	/*
6430Sstevel@tonic-gate 	 * ChecK against Magic Table entries.
6440Sstevel@tonic-gate 	 * The magic entries checked here always start with default
6450Sstevel@tonic-gate 	 * magic tests and may be followed by other, non-default magic
6460Sstevel@tonic-gate 	 * tests.  If no default tests are to be executed, all the
6470Sstevel@tonic-gate 	 * magic tests should have been in the first magic table.
6480Sstevel@tonic-gate 	 */
6490Sstevel@tonic-gate 	switch (f_ckmtab(magicbuf, mread, 0)) {
6500Sstevel@tonic-gate 		case -1:	/* Error */
6510Sstevel@tonic-gate 			exit(2);
6520Sstevel@tonic-gate 			break;
6530Sstevel@tonic-gate 		case 0:		/* Not magic */
6540Sstevel@tonic-gate 			return (0);
6550Sstevel@tonic-gate 			/* NOTREACHED */
6560Sstevel@tonic-gate 			break;
6570Sstevel@tonic-gate 		default:	/* Switch is magic index */
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 			/*
6600Sstevel@tonic-gate 			 * f_ckmtab recognizes file type,
6610Sstevel@tonic-gate 			 * check if it is PostScript.
6620Sstevel@tonic-gate 			 * if not, check if elf or a.out
6630Sstevel@tonic-gate 			 */
6640Sstevel@tonic-gate 			if (magicbuf[0] == '%' && magicbuf[1] == '!') {
6650Sstevel@tonic-gate 				(void) putchar('\n');
6660Sstevel@tonic-gate 			} else {
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 				/*
6690Sstevel@tonic-gate 				 * Check that the file is executable (dynamic
6700Sstevel@tonic-gate 				 * objects must be executable to be exec'ed,
6710Sstevel@tonic-gate 				 * shared objects need not be, but by convention
6720Sstevel@tonic-gate 				 * should be executable).
6730Sstevel@tonic-gate 				 *
6740Sstevel@tonic-gate 				 * Note that we should already have processed
6750Sstevel@tonic-gate 				 * the file if it was an ELF file.
6760Sstevel@tonic-gate 				 */
6770Sstevel@tonic-gate 				ar_coff_or_aout(elffd);
6780Sstevel@tonic-gate 				(void) putchar('\n');
6790Sstevel@tonic-gate 			}
6800Sstevel@tonic-gate 			return (1);
6810Sstevel@tonic-gate 			/* NOTREACHED */
6820Sstevel@tonic-gate 			break;
6830Sstevel@tonic-gate 	}
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	return (0);	/* file was not identified */
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate /*
6890Sstevel@tonic-gate  * def_context_tests() - default context-sensitive tests.
6900Sstevel@tonic-gate  *	These are the last tests to be applied.
6910Sstevel@tonic-gate  *	If no match is found, prints out "data".
6920Sstevel@tonic-gate  */
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate static void
6950Sstevel@tonic-gate def_context_tests(void)
6960Sstevel@tonic-gate {
6970Sstevel@tonic-gate 	int	j;
6980Sstevel@tonic-gate 	int	nl;
6990Sstevel@tonic-gate 	char	ch;
7000Sstevel@tonic-gate 	int	len;
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	if (ccom() == 0)
7030Sstevel@tonic-gate 		goto notc;
7040Sstevel@tonic-gate 	while (fbuf[i] == '#') {
7050Sstevel@tonic-gate 		j = i;
7060Sstevel@tonic-gate 		while (fbuf[i++] != '\n') {
7070Sstevel@tonic-gate 			if (i - j > 255) {
7080Sstevel@tonic-gate 				(void) printf(gettext("data\n"));
7090Sstevel@tonic-gate 				return;
7100Sstevel@tonic-gate 			}
7110Sstevel@tonic-gate 			if (i >= fbsz)
7120Sstevel@tonic-gate 				goto notc;
7130Sstevel@tonic-gate 		}
7140Sstevel@tonic-gate 		if (ccom() == 0)
7150Sstevel@tonic-gate 			goto notc;
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate check:
7180Sstevel@tonic-gate 	if (lookup(c) == 1) {
7190Sstevel@tonic-gate 		while ((ch = fbuf[i]) != ';' && ch != '{') {
7200Sstevel@tonic-gate 			if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
7210Sstevel@tonic-gate 				len = 1;
7220Sstevel@tonic-gate 			i += len;
7230Sstevel@tonic-gate 			if (i >= fbsz)
7240Sstevel@tonic-gate 				goto notc;
7250Sstevel@tonic-gate 		}
7260Sstevel@tonic-gate 		(void) printf(gettext("c program text"));
7270Sstevel@tonic-gate 		goto outa;
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 	nl = 0;
7300Sstevel@tonic-gate 	while (fbuf[i] != '(') {
7310Sstevel@tonic-gate 		if (fbuf[i] <= 0)
7320Sstevel@tonic-gate 			goto notas;
7330Sstevel@tonic-gate 		if (fbuf[i] == ';') {
7340Sstevel@tonic-gate 			i++;
7350Sstevel@tonic-gate 			goto check;
7360Sstevel@tonic-gate 		}
7370Sstevel@tonic-gate 		if (fbuf[i++] == '\n')
7380Sstevel@tonic-gate 			if (nl++ > 6)
7390Sstevel@tonic-gate 				goto notc;
7400Sstevel@tonic-gate 		if (i >= fbsz)
7410Sstevel@tonic-gate 			goto notc;
7420Sstevel@tonic-gate 	}
7430Sstevel@tonic-gate 	while (fbuf[i] != ')') {
7440Sstevel@tonic-gate 		if (fbuf[i++] == '\n')
7450Sstevel@tonic-gate 			if (nl++ > 6)
7460Sstevel@tonic-gate 				goto notc;
7470Sstevel@tonic-gate 		if (i >= fbsz)
7480Sstevel@tonic-gate 			goto notc;
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 	while (fbuf[i] != '{') {
7510Sstevel@tonic-gate 		if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
7520Sstevel@tonic-gate 			len = 1;
7530Sstevel@tonic-gate 		if (fbuf[i] == '\n')
7540Sstevel@tonic-gate 			if (nl++ > 6)
7550Sstevel@tonic-gate 				goto notc;
7560Sstevel@tonic-gate 		i += len;
7570Sstevel@tonic-gate 		if (i >= fbsz)
7580Sstevel@tonic-gate 			goto notc;
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 	(void) printf(gettext("c program text"));
7610Sstevel@tonic-gate 	goto outa;
7620Sstevel@tonic-gate notc:
7630Sstevel@tonic-gate 	i = 0;			/* reset to begining of file again */
7640Sstevel@tonic-gate 	while (fbuf[i] == 'c' || fbuf[i] == 'C'|| fbuf[i] == '!' ||
7650Sstevel@tonic-gate 	    fbuf[i] == '*' || fbuf[i] == '\n') {
7660Sstevel@tonic-gate 		while (fbuf[i++] != '\n')
7670Sstevel@tonic-gate 			if (i >= fbsz)
7680Sstevel@tonic-gate 				goto notfort;
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	if (lookup(fort) == 1) {
7710Sstevel@tonic-gate 		(void) printf(gettext("fortran program text"));
7720Sstevel@tonic-gate 		goto outa;
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate notfort:			/* looking for assembler program */
7750Sstevel@tonic-gate 	i = 0;			/* reset to beginning of file again */
7760Sstevel@tonic-gate 	if (ccom() == 0)	/* assembler programs may contain */
7770Sstevel@tonic-gate 				/* c-style comments */
7780Sstevel@tonic-gate 		goto notas;
7790Sstevel@tonic-gate 	if (ascom() == 0)
7800Sstevel@tonic-gate 		goto notas;
7810Sstevel@tonic-gate 	j = i - 1;
7820Sstevel@tonic-gate 	if (fbuf[i] == '.') {
7830Sstevel@tonic-gate 		i++;
7840Sstevel@tonic-gate 		if (lookup(as) == 1) {
7850Sstevel@tonic-gate 			(void) printf(gettext("assembler program text"));
7860Sstevel@tonic-gate 			goto outa;
7870Sstevel@tonic-gate 		} else if (j != -1 && fbuf[j] == '\n' && isalpha(fbuf[j + 2])) {
7880Sstevel@tonic-gate 			(void) printf(
7890Sstevel@tonic-gate 			    gettext("[nt]roff, tbl, or eqn input text"));
7900Sstevel@tonic-gate 			goto outa;
7910Sstevel@tonic-gate 		}
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 	while (lookup(asc) == 0) {
7940Sstevel@tonic-gate 		if (ccom() == 0)
7950Sstevel@tonic-gate 			goto notas;
7960Sstevel@tonic-gate 		if (ascom() == 0)
7970Sstevel@tonic-gate 			goto notas;
7980Sstevel@tonic-gate 		while (fbuf[i] != '\n' && fbuf[i++] != ':') {
7990Sstevel@tonic-gate 			if (i >= fbsz)
8000Sstevel@tonic-gate 				goto notas;
8010Sstevel@tonic-gate 		}
8020Sstevel@tonic-gate 		while (fbuf[i] == '\n' || fbuf[i] == ' ' || fbuf[i] == '\t')
8030Sstevel@tonic-gate 			if (i++ >= fbsz)
8040Sstevel@tonic-gate 				goto notas;
8050Sstevel@tonic-gate 		j = i - 1;
8060Sstevel@tonic-gate 		if (fbuf[i] == '.') {
8070Sstevel@tonic-gate 			i++;
8080Sstevel@tonic-gate 			if (lookup(as) == 1) {
8090Sstevel@tonic-gate 				(void) printf(
8100Sstevel@tonic-gate 				    gettext("assembler program text"));
8110Sstevel@tonic-gate 				goto outa;
8120Sstevel@tonic-gate 			} else if (fbuf[j] == '\n' && isalpha(fbuf[j+2])) {
8130Sstevel@tonic-gate 				(void) printf(
8140Sstevel@tonic-gate 				    gettext("[nt]roff, tbl, or eqn input "
8150Sstevel@tonic-gate 				    "text"));
8160Sstevel@tonic-gate 				goto outa;
8170Sstevel@tonic-gate 			}
8180Sstevel@tonic-gate 		}
8190Sstevel@tonic-gate 	}
8200Sstevel@tonic-gate 	(void) printf(gettext("assembler program text"));
8210Sstevel@tonic-gate 	goto outa;
8220Sstevel@tonic-gate notas:
8230Sstevel@tonic-gate 	/* start modification for multibyte env */
8240Sstevel@tonic-gate 	IS_ascii = 1;
8250Sstevel@tonic-gate 	if (fbsz < FBSZ)
8260Sstevel@tonic-gate 		Max = fbsz;
8270Sstevel@tonic-gate 	else
8280Sstevel@tonic-gate 		Max = FBSZ - MB_LEN_MAX; /* prevent cut of wchar read */
8290Sstevel@tonic-gate 	/* end modification for multibyte env */
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	for (i = 0; i < Max; /* null */)
8320Sstevel@tonic-gate 		if (fbuf[i] & 0200) {
8330Sstevel@tonic-gate 			IS_ascii = 0;
8340Sstevel@tonic-gate 			if (fbuf[0] == '\100' && fbuf[1] == '\357') {
8350Sstevel@tonic-gate 				(void) printf(gettext("troff output\n"));
8360Sstevel@tonic-gate 				return;
8370Sstevel@tonic-gate 			}
8380Sstevel@tonic-gate 		/* start modification for multibyte env */
8390Sstevel@tonic-gate 			if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX))
8400Sstevel@tonic-gate 			    <= 0 || !iswprint(wchar)) {
8410Sstevel@tonic-gate 				(void) printf(gettext("data\n"));
8420Sstevel@tonic-gate 				return;
8430Sstevel@tonic-gate 			}
8440Sstevel@tonic-gate 			i += length;
8450Sstevel@tonic-gate 		}
8460Sstevel@tonic-gate 		else
8470Sstevel@tonic-gate 			i++;
8480Sstevel@tonic-gate 	i = fbsz;
8490Sstevel@tonic-gate 		/* end modification for multibyte env */
8500Sstevel@tonic-gate 	if (mbuf.st_mode&(S_IXUSR|S_IXGRP|S_IXOTH))
8510Sstevel@tonic-gate 		(void) printf(gettext("commands text"));
8520Sstevel@tonic-gate 	else if (troffint(fbuf, fbsz))
8530Sstevel@tonic-gate 		(void) printf(gettext("troff intermediate output text"));
8540Sstevel@tonic-gate 	else if (english(fbuf, fbsz))
8550Sstevel@tonic-gate 		(void) printf(gettext("English text"));
8560Sstevel@tonic-gate 	else if (IS_ascii)
8570Sstevel@tonic-gate 		(void) printf(gettext("ascii text"));
8580Sstevel@tonic-gate 	else
8590Sstevel@tonic-gate 		(void) printf(gettext("text")); /* for multibyte env */
8600Sstevel@tonic-gate outa:
8610Sstevel@tonic-gate 	/*
8620Sstevel@tonic-gate 	 * This code is to make sure that no MB char is cut in half
8630Sstevel@tonic-gate 	 * while still being used.
8640Sstevel@tonic-gate 	 */
8650Sstevel@tonic-gate 	fbsz = (fbsz < FBSZ ? fbsz : fbsz - MB_CUR_MAX + 1);
8660Sstevel@tonic-gate 	while (i < fbsz) {
8670Sstevel@tonic-gate 		if (isascii(fbuf[i])) {
8680Sstevel@tonic-gate 			i++;
8690Sstevel@tonic-gate 			continue;
8700Sstevel@tonic-gate 		} else {
8710Sstevel@tonic-gate 			if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX))
8720Sstevel@tonic-gate 			    <= 0 || !iswprint(wchar)) {
8730Sstevel@tonic-gate 				(void) printf(gettext(" with garbage\n"));
8740Sstevel@tonic-gate 				return;
8750Sstevel@tonic-gate 			}
8760Sstevel@tonic-gate 			i = i + length;
8770Sstevel@tonic-gate 		}
8780Sstevel@tonic-gate 	}
8790Sstevel@tonic-gate 	(void) printf("\n");
8800Sstevel@tonic-gate }
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate static int
8830Sstevel@tonic-gate troffint(char *bp, int n)
8840Sstevel@tonic-gate {
8850Sstevel@tonic-gate 	int k;
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	i = 0;
8880Sstevel@tonic-gate 	for (k = 0; k < 6; k++) {
8890Sstevel@tonic-gate 		if (lookup(troff) == 0)
8900Sstevel@tonic-gate 			return (0);
8910Sstevel@tonic-gate 		if (lookup(troff) == 0)
8920Sstevel@tonic-gate 			return (0);
8930Sstevel@tonic-gate 		while (i < n && bp[i] != '\n')
8940Sstevel@tonic-gate 			i++;
8950Sstevel@tonic-gate 		if (i++ >= n)
8960Sstevel@tonic-gate 			return (0);
8970Sstevel@tonic-gate 	}
8980Sstevel@tonic-gate 	return (1);
8990Sstevel@tonic-gate }
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate /*
9020Sstevel@tonic-gate  * Determine if the passed descriptor describes an ELF file.
9030Sstevel@tonic-gate  * If so, return the Elf handle.
9040Sstevel@tonic-gate  */
9050Sstevel@tonic-gate static Elf *
9060Sstevel@tonic-gate is_elf_file(int elffd)
9070Sstevel@tonic-gate {
9080Sstevel@tonic-gate 	Elf *elf;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	elf = elf_begin(elffd, ELF_C_READ, (Elf *)0);
9110Sstevel@tonic-gate 	switch (elf_kind(elf)) {
9120Sstevel@tonic-gate 	case ELF_K_ELF:
9130Sstevel@tonic-gate 		break;
9140Sstevel@tonic-gate 	default:
9150Sstevel@tonic-gate 		(void) elf_end(elf);
9160Sstevel@tonic-gate 		elf = NULL;
9170Sstevel@tonic-gate 		break;
9180Sstevel@tonic-gate 	}
9190Sstevel@tonic-gate 	return (elf);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate static void
9230Sstevel@tonic-gate ar_coff_or_aout(int elffd)
9240Sstevel@tonic-gate {
9250Sstevel@tonic-gate 	Elf *elf;
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	/*
9280Sstevel@tonic-gate 	 * Get the files elf descriptor and process it as an elf or
9290Sstevel@tonic-gate 	 * a.out (4.x) file.
9300Sstevel@tonic-gate 	 */
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	elf = elf_begin(elffd, ELF_C_READ, (Elf *)0);
9330Sstevel@tonic-gate 	switch (elf_kind(elf)) {
9340Sstevel@tonic-gate 		case ELF_K_AR :
9350Sstevel@tonic-gate 			(void) printf(gettext(", not a dynamic executable "
9360Sstevel@tonic-gate 			    "or shared object"));
9370Sstevel@tonic-gate 			break;
9380Sstevel@tonic-gate 		case ELF_K_COFF:
9390Sstevel@tonic-gate 			(void) printf(gettext(", unsupported or unknown "
9400Sstevel@tonic-gate 			    "file type"));
9410Sstevel@tonic-gate 			break;
9420Sstevel@tonic-gate 		default:
9430Sstevel@tonic-gate 			/*
9440Sstevel@tonic-gate 			 * This is either an unknown file or an aout format
9450Sstevel@tonic-gate 			 * At this time, we don't print dynamic/stripped
9460Sstevel@tonic-gate 			 * info. on a.out or non-Elf binaries.
9470Sstevel@tonic-gate 			 */
9480Sstevel@tonic-gate 			break;
9490Sstevel@tonic-gate 	}
9500Sstevel@tonic-gate 	(void) elf_end(elf);
9510Sstevel@tonic-gate }
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate static void
9550Sstevel@tonic-gate print_elf_type(Elf *elf, GElf_Ehdr *ehdr, int format)
9560Sstevel@tonic-gate {
9570Sstevel@tonic-gate 	switch (ehdr->e_type) {
9580Sstevel@tonic-gate 	case ET_NONE:
9590Sstevel@tonic-gate 		(void) printf(" %s", gettext("unknown type"));
9600Sstevel@tonic-gate 		break;
9610Sstevel@tonic-gate 	case ET_REL:
9620Sstevel@tonic-gate 		(void) printf(" %s", gettext("relocatable"));
9630Sstevel@tonic-gate 		break;
9640Sstevel@tonic-gate 	case ET_EXEC:
9650Sstevel@tonic-gate 		(void) printf(" %s", gettext("executable"));
9660Sstevel@tonic-gate 		break;
9670Sstevel@tonic-gate 	case ET_DYN:
9680Sstevel@tonic-gate 		(void) printf(" %s", gettext("dynamic lib"));
9690Sstevel@tonic-gate 		break;
9700Sstevel@tonic-gate 	case ET_CORE:
9710Sstevel@tonic-gate 		if (old_core(elf, ehdr, format))
9720Sstevel@tonic-gate 			(void) printf(" %s", gettext("pre-2.6 core file"));
9730Sstevel@tonic-gate 		else
9740Sstevel@tonic-gate 			(void) printf(" %s", gettext("core file"));
9750Sstevel@tonic-gate 		break;
9760Sstevel@tonic-gate 	default:
9770Sstevel@tonic-gate 		break;
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate static void
9820Sstevel@tonic-gate print_elf_machine(int machine)
9830Sstevel@tonic-gate {
9840Sstevel@tonic-gate 	switch (machine) {
9850Sstevel@tonic-gate 	case EM_NONE:
9860Sstevel@tonic-gate 		(void) printf(" %s", gettext("unknown machine"));
9870Sstevel@tonic-gate 		break;
9880Sstevel@tonic-gate 	case EM_M32:
9890Sstevel@tonic-gate 		(void) printf(" %s", gettext("WE32100"));
9900Sstevel@tonic-gate 		break;
9910Sstevel@tonic-gate 	case EM_SPARC:
9920Sstevel@tonic-gate 		(void) printf(" %s", gettext("SPARC"));
9930Sstevel@tonic-gate 		break;
9940Sstevel@tonic-gate 	case EM_386:
9950Sstevel@tonic-gate 		(void) printf(" %s", gettext("80386"));
9960Sstevel@tonic-gate 		break;
9970Sstevel@tonic-gate 	case EM_68K:
9980Sstevel@tonic-gate 		(void) printf(" %s", gettext("M68000"));
9990Sstevel@tonic-gate 		break;
10000Sstevel@tonic-gate 	case EM_88K:
10010Sstevel@tonic-gate 		(void) printf(" %s", gettext("M88000"));
10020Sstevel@tonic-gate 		break;
10030Sstevel@tonic-gate 	case EM_486:
10040Sstevel@tonic-gate 		(void) printf(" %s", gettext("80486"));
10050Sstevel@tonic-gate 		break;
10060Sstevel@tonic-gate 	case EM_860:
10070Sstevel@tonic-gate 		(void) printf(" %s", gettext("i860"));
10080Sstevel@tonic-gate 		break;
10090Sstevel@tonic-gate 	case EM_MIPS:
10100Sstevel@tonic-gate 		(void) printf(" %s", gettext("MIPS RS3000 Big-Endian"));
10110Sstevel@tonic-gate 		break;
10120Sstevel@tonic-gate 	case EM_MIPS_RS3_LE:
10130Sstevel@tonic-gate 		(void) printf(" %s", gettext("MIPS RS3000 Little-Endian"));
10140Sstevel@tonic-gate 		break;
10150Sstevel@tonic-gate 	case EM_RS6000:
10160Sstevel@tonic-gate 		(void) printf(" %s", gettext("MIPS RS6000"));
10170Sstevel@tonic-gate 		break;
10180Sstevel@tonic-gate 	case EM_PA_RISC:
10190Sstevel@tonic-gate 		(void) printf(" %s", gettext("PA-RISC"));
10200Sstevel@tonic-gate 		break;
10210Sstevel@tonic-gate 	case EM_nCUBE:
10220Sstevel@tonic-gate 		(void) printf(" %s", gettext("nCUBE"));
10230Sstevel@tonic-gate 		break;
10240Sstevel@tonic-gate 	case EM_VPP500:
10250Sstevel@tonic-gate 		(void) printf(" %s", gettext("VPP500"));
10260Sstevel@tonic-gate 		break;
10270Sstevel@tonic-gate 	case EM_SPARC32PLUS:
10280Sstevel@tonic-gate 		(void) printf(" %s", gettext("SPARC32PLUS"));
10290Sstevel@tonic-gate 		break;
10300Sstevel@tonic-gate 	case EM_PPC:
10310Sstevel@tonic-gate 		(void) printf(" %s", gettext("PowerPC"));
10320Sstevel@tonic-gate 		break;
10330Sstevel@tonic-gate 	case EM_SPARCV9:
10340Sstevel@tonic-gate 		(void) printf(" %s", gettext("SPARCV9"));
10350Sstevel@tonic-gate 		break;
10360Sstevel@tonic-gate 	case EM_IA_64:
10370Sstevel@tonic-gate 		(void) printf(" %s", gettext("IA64"));
10380Sstevel@tonic-gate 		break;
10390Sstevel@tonic-gate 	case EM_AMD64:
10400Sstevel@tonic-gate 		(void) printf(" %s", gettext("AMD64"));
10410Sstevel@tonic-gate 		break;
10420Sstevel@tonic-gate 	default:
10430Sstevel@tonic-gate 		break;
10440Sstevel@tonic-gate 	}
10450Sstevel@tonic-gate }
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate static void
10480Sstevel@tonic-gate print_elf_datatype(int datatype)
10490Sstevel@tonic-gate {
10500Sstevel@tonic-gate 	switch (datatype) {
10510Sstevel@tonic-gate 	case ELFDATA2LSB:
10520Sstevel@tonic-gate 		(void) printf(" %s", gettext("LSB"));
10530Sstevel@tonic-gate 		break;
10540Sstevel@tonic-gate 	case ELFDATA2MSB:
10550Sstevel@tonic-gate 		(void) printf(" %s", gettext("MSB"));
10560Sstevel@tonic-gate 		break;
10570Sstevel@tonic-gate 	default:
10580Sstevel@tonic-gate 		break;
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate static void
10630Sstevel@tonic-gate print_elf_class(int class)
10640Sstevel@tonic-gate {
10650Sstevel@tonic-gate 	switch (class) {
10660Sstevel@tonic-gate 	case ELFCLASS32:
10670Sstevel@tonic-gate 		(void) printf(" %s", gettext("32-bit"));
10680Sstevel@tonic-gate 		break;
10690Sstevel@tonic-gate 	case ELFCLASS64:
10700Sstevel@tonic-gate 		(void) printf(" %s", gettext("64-bit"));
10710Sstevel@tonic-gate 		break;
10720Sstevel@tonic-gate 	default:
10730Sstevel@tonic-gate 		break;
10740Sstevel@tonic-gate 	}
10750Sstevel@tonic-gate }
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate static void
10780Sstevel@tonic-gate print_elf_flags(int machine, unsigned int flags)
10790Sstevel@tonic-gate {
10800Sstevel@tonic-gate 	switch (machine) {
10810Sstevel@tonic-gate 	case EM_SPARCV9:
10820Sstevel@tonic-gate 		if (flags & EF_SPARC_EXT_MASK) {
10830Sstevel@tonic-gate 			if (flags & EF_SPARC_SUN_US3) {
10840Sstevel@tonic-gate 				(void) printf("%s", gettext(
10850Sstevel@tonic-gate 				    ", UltraSPARC3 Extensions Required"));
10860Sstevel@tonic-gate 			} else if (flags & EF_SPARC_SUN_US1) {
10870Sstevel@tonic-gate 				(void) printf("%s", gettext(
10880Sstevel@tonic-gate 				    ", UltraSPARC1 Extensions Required"));
10890Sstevel@tonic-gate 			}
10900Sstevel@tonic-gate 			if (flags & EF_SPARC_HAL_R1)
10910Sstevel@tonic-gate 				(void) printf("%s", gettext(
10920Sstevel@tonic-gate 				    ", HaL R1 Extensions Required"));
10930Sstevel@tonic-gate 		}
10940Sstevel@tonic-gate 		break;
10950Sstevel@tonic-gate 	case EM_SPARC32PLUS:
10960Sstevel@tonic-gate 		if (flags & EF_SPARC_32PLUS)
10970Sstevel@tonic-gate 			(void) printf("%s", gettext(", V8+ Required"));
10980Sstevel@tonic-gate 		if (flags & EF_SPARC_SUN_US3) {
10990Sstevel@tonic-gate 			(void) printf("%s",
11000Sstevel@tonic-gate 				gettext(", UltraSPARC3 Extensions Required"));
11010Sstevel@tonic-gate 		} else if (flags & EF_SPARC_SUN_US1) {
11020Sstevel@tonic-gate 			(void) printf("%s",
11030Sstevel@tonic-gate 				gettext(", UltraSPARC1 Extensions Required"));
11040Sstevel@tonic-gate 		}
11050Sstevel@tonic-gate 		if (flags & EF_SPARC_HAL_R1)
11060Sstevel@tonic-gate 			(void) printf("%s",
11070Sstevel@tonic-gate 				gettext(", HaL R1 Extensions Required"));
11080Sstevel@tonic-gate 		break;
11090Sstevel@tonic-gate 	default:
11100Sstevel@tonic-gate 		break;
11110Sstevel@tonic-gate 	}
11120Sstevel@tonic-gate }
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate static int
11150Sstevel@tonic-gate print_cap(Elf *elf, GElf_Ehdr *ehdr)
11160Sstevel@tonic-gate {
11170Sstevel@tonic-gate 	Elf_Scn	*scn = 0;
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	/*
11200Sstevel@tonic-gate 	 * Traverse the files sections to see if any software/hardware
11210Sstevel@tonic-gate 	 * capabilities are available.
11220Sstevel@tonic-gate 	 */
11230Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf, scn)) != 0) {
11240Sstevel@tonic-gate 		GElf_Word	ndx, capn;
11250Sstevel@tonic-gate 		GElf_Shdr	shdr;
11260Sstevel@tonic-gate 		Elf_Data	*data;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr) == 0) {
11290Sstevel@tonic-gate 			(void) fprintf(stderr,
11300Sstevel@tonic-gate 			    gettext("can't read ELF section header\n"));
11310Sstevel@tonic-gate 			return (1);
11320Sstevel@tonic-gate 		}
11330Sstevel@tonic-gate 		if (shdr.sh_type != SHT_SUNW_cap)
11340Sstevel@tonic-gate 			continue;
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 		/*
11370Sstevel@tonic-gate 		 * Get the data associated with the .cap section.
11380Sstevel@tonic-gate 		 */
11390Sstevel@tonic-gate 		if ((data = elf_getdata(scn, 0)) == 0) {
11400Sstevel@tonic-gate 			(void) fprintf(stderr,
11410Sstevel@tonic-gate 				gettext("can't read ELF section data\n"));
11420Sstevel@tonic-gate 			return (1);
11430Sstevel@tonic-gate 		}
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 		capn = (GElf_Word)(shdr.sh_size / shdr.sh_entsize);
11460Sstevel@tonic-gate 		for (ndx = 0; ndx < capn; ndx++) {
11470Sstevel@tonic-gate 			char		str[100];
11480Sstevel@tonic-gate 			GElf_Cap	cap;
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 			if (gelf_getcap(data, ndx, &cap) == NULL) {
11510Sstevel@tonic-gate 				(void) fprintf(stderr,
11520Sstevel@tonic-gate 				    gettext("can't read capabilities data\n"));
11530Sstevel@tonic-gate 				return (1);
11540Sstevel@tonic-gate 			}
11550Sstevel@tonic-gate 			if (cap.c_tag != CA_SUNW_NULL) {
11560Sstevel@tonic-gate 				(void) cap_val2str(cap.c_tag, cap.c_un.c_val,
11570Sstevel@tonic-gate 				    str, sizeof (str), 0, ehdr->e_machine);
11580Sstevel@tonic-gate 				(void) printf(" [%s]", str);
11590Sstevel@tonic-gate 			}
11600Sstevel@tonic-gate 		}
11610Sstevel@tonic-gate 	}
11620Sstevel@tonic-gate 	return (0);
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate static int
11660Sstevel@tonic-gate elf_check(Elf *elf)
11670Sstevel@tonic-gate {
11680Sstevel@tonic-gate 	GElf_Ehdr	ehdr;
11690Sstevel@tonic-gate 	GElf_Phdr	phdr;
11700Sstevel@tonic-gate 	int		dynamic, cnt;
11710Sstevel@tonic-gate 	char	*ident;
11720Sstevel@tonic-gate 	size_t	size;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	/*
11750Sstevel@tonic-gate 	 * verify information in file header
11760Sstevel@tonic-gate 	 */
11770Sstevel@tonic-gate 	if (gelf_getehdr(elf, &ehdr) == (GElf_Ehdr *)0) {
11780Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("can't read ELF header\n"));
11790Sstevel@tonic-gate 		return (1);
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 	ident = elf_getident(elf, &size);
11820Sstevel@tonic-gate 	(void) printf("%s", gettext("ELF"));
11830Sstevel@tonic-gate 	print_elf_class(ident[EI_CLASS]);
11840Sstevel@tonic-gate 	print_elf_datatype(ident[EI_DATA]);
11850Sstevel@tonic-gate 	print_elf_type(elf, &ehdr, ident[EI_DATA]);
11860Sstevel@tonic-gate 	print_elf_machine(ehdr.e_machine);
11870Sstevel@tonic-gate 	if (ehdr.e_version == 1)
11880Sstevel@tonic-gate 		(void) printf(" %s %d",
11890Sstevel@tonic-gate 		    gettext("Version"), (int)ehdr.e_version);
11900Sstevel@tonic-gate 	print_elf_flags(ehdr.e_machine, ehdr.e_flags);
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	if (core(elf, &ehdr, ident[EI_DATA]))	/* check for core file */
11930Sstevel@tonic-gate 		return (0);
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	if (print_cap(elf, &ehdr))
11960Sstevel@tonic-gate 		return (1);
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	/*
11990Sstevel@tonic-gate 	 * check type
12000Sstevel@tonic-gate 	 */
12010Sstevel@tonic-gate 	if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN))
12020Sstevel@tonic-gate 		return (1);
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	/*
12050Sstevel@tonic-gate 	 * read program header and check for dynamic section
12060Sstevel@tonic-gate 	 */
12070Sstevel@tonic-gate 	if (ehdr.e_phnum == 0) {
12080Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("can't read program header\n"));
12090Sstevel@tonic-gate 		return (1);
12100Sstevel@tonic-gate 	}
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	for (dynamic = 0, cnt = 0; cnt < (int)ehdr.e_phnum; cnt++) {
12130Sstevel@tonic-gate 		if (gelf_getphdr(elf, cnt, &phdr) == NULL) {
12140Sstevel@tonic-gate 			(void) fprintf(stderr,
12150Sstevel@tonic-gate 				gettext("can't read program header\n"));
12160Sstevel@tonic-gate 			return (1);
12170Sstevel@tonic-gate 		}
12180Sstevel@tonic-gate 		if (phdr.p_type == PT_DYNAMIC) {
12190Sstevel@tonic-gate 			dynamic = 1;
12200Sstevel@tonic-gate 			break;
12210Sstevel@tonic-gate 		}
12220Sstevel@tonic-gate 	}
12230Sstevel@tonic-gate 	if (dynamic)
12240Sstevel@tonic-gate 		(void) printf(gettext(", dynamically linked"));
12250Sstevel@tonic-gate 	else
12260Sstevel@tonic-gate 		(void) printf(gettext(", statically linked"));
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	is_stripped(elf);
12290Sstevel@tonic-gate 	return (0);
12300Sstevel@tonic-gate }
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate /*
12330Sstevel@tonic-gate  * is_stripped prints information on whether the executable has
12340Sstevel@tonic-gate  * been stripped.
12350Sstevel@tonic-gate  */
12360Sstevel@tonic-gate static void
12370Sstevel@tonic-gate is_stripped(Elf *elf)
12380Sstevel@tonic-gate {
12390Sstevel@tonic-gate 	GElf_Shdr	shdr;
12400Sstevel@tonic-gate 	GElf_Ehdr	ehdr;
12410Sstevel@tonic-gate 	Elf_Scn		*scn, *nextscn;
12420Sstevel@tonic-gate 	char		*section_name;
12430Sstevel@tonic-gate 	int		symtab = 0;
12440Sstevel@tonic-gate 	int		debuginfo = 0;
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	if (gelf_getehdr(elf, &ehdr) == NULL) {
12480Sstevel@tonic-gate 		return;
12490Sstevel@tonic-gate 	}
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	/*
12520Sstevel@tonic-gate 	 * Definition time:
12530Sstevel@tonic-gate 	 *	- "not stripped" means that an executable file
12540Sstevel@tonic-gate 	 *	contains a Symbol Table (.symtab)
12550Sstevel@tonic-gate 	 *	- "stripped" means that an executable file
12560Sstevel@tonic-gate 	 *	does not contain a Symbol Table.
12570Sstevel@tonic-gate 	 * When strip -l or strip -x is run, it strips the
12580Sstevel@tonic-gate 	 * debugging information (.line section name (strip -l),
12590Sstevel@tonic-gate 	 * .line, .debug*, .stabs*, .dwarf* section names
12600Sstevel@tonic-gate 	 * and SHT_SUNW_DEBUGSTR and SHT_SUNW_DEBUG
12610Sstevel@tonic-gate 	 * section types (strip -x), however the Symbol
12620Sstevel@tonic-gate 	 * Table will still be present.
12630Sstevel@tonic-gate 	 * Therefore, if
12640Sstevel@tonic-gate 	 *	- No Symbol Table present, then report
12650Sstevel@tonic-gate 	 *		"stripped"
12660Sstevel@tonic-gate 	 *	- Symbol Table present with debugging
12670Sstevel@tonic-gate 	 *	information (line number or debug section names,
12680Sstevel@tonic-gate 	 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
12690Sstevel@tonic-gate 	 *	types) then report:
12700Sstevel@tonic-gate 	 *		"not stripped"
12710Sstevel@tonic-gate 	 *	- Symbol Table present with no debugging
12720Sstevel@tonic-gate 	 *	information (line number or debug section names,
12730Sstevel@tonic-gate 	 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
12740Sstevel@tonic-gate 	 *	types) then report:
12750Sstevel@tonic-gate 	 *		"not stripped, no debugging information
12760Sstevel@tonic-gate 	 *		available"
12770Sstevel@tonic-gate 	 */
12780Sstevel@tonic-gate 	scn = NULL;
12790Sstevel@tonic-gate 	while ((nextscn = elf_nextscn(elf, scn)) != NULL) {
12800Sstevel@tonic-gate 		if (symtab && debuginfo) {
12810Sstevel@tonic-gate 			break;
12820Sstevel@tonic-gate 		}
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 		scn = nextscn;
12850Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr) == NULL) {
12860Sstevel@tonic-gate 			continue;
12870Sstevel@tonic-gate 		}
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 		if (!symtab && (shdr.sh_type == SHT_SYMTAB)) {
12900Sstevel@tonic-gate 			symtab++;
12910Sstevel@tonic-gate 			continue;
12920Sstevel@tonic-gate 		}
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 		if (!debuginfo &&
12950Sstevel@tonic-gate 		    ((shdr.sh_type == SHT_SUNW_DEBUG) ||
12960Sstevel@tonic-gate 		    (shdr.sh_type == SHT_SUNW_DEBUGSTR) ||
12970Sstevel@tonic-gate 		    (((section_name = elf_strptr(elf, ehdr.e_shstrndx,
12980Sstevel@tonic-gate 		    (size_t)shdr.sh_name)) != NULL) &&
12990Sstevel@tonic-gate 		    (is_in_list(debug_sections, section_name))))) {
13000Sstevel@tonic-gate 			debuginfo++;
13010Sstevel@tonic-gate 		}
13020Sstevel@tonic-gate 	}
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	/*
13050Sstevel@tonic-gate 	 * Now that we've scanned all sections, print out the appropriate
13060Sstevel@tonic-gate 	 * diagnostic.
13070Sstevel@tonic-gate 	 */
13080Sstevel@tonic-gate 	if (symtab) {
13090Sstevel@tonic-gate 		(void) printf(gettext(", not stripped"));
13100Sstevel@tonic-gate 		if (!debuginfo) {
13110Sstevel@tonic-gate 			(void) printf(gettext(
13120Sstevel@tonic-gate 			    ", no debugging information available"));
13130Sstevel@tonic-gate 		}
13140Sstevel@tonic-gate 	} else {
13150Sstevel@tonic-gate 		(void) printf(gettext(", stripped"));
13160Sstevel@tonic-gate 	}
13170Sstevel@tonic-gate }
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate /*
1320*1976Sab196087  * is_rtld_config - If file is a runtime linker config file, prints
1321*1976Sab196087  * the description and returns True (1). Otherwise, silently returns
1322*1976Sab196087  * False (0).
1323*1976Sab196087  */
1324*1976Sab196087 int
1325*1976Sab196087 is_rtld_config(void)
1326*1976Sab196087 {
1327*1976Sab196087 	Rtc_id *id;
1328*1976Sab196087 
1329*1976Sab196087 	if ((fbsz >= sizeof (*id)) && RTC_ID_TEST(fbuf)) {
1330*1976Sab196087 		(void) printf(gettext("Runtime Linking Configuration"));
1331*1976Sab196087 		/* LINTED: pointer cast may result in improper alignment */
1332*1976Sab196087 		id = (Rtc_id *) fbuf;
1333*1976Sab196087 		print_elf_class(id->id_class);
1334*1976Sab196087 		print_elf_datatype(id->id_data);
1335*1976Sab196087 		print_elf_machine(id->id_machine);
1336*1976Sab196087 		(void) printf("\n");
1337*1976Sab196087 		return (1);
1338*1976Sab196087 	}
1339*1976Sab196087 
1340*1976Sab196087 	return (0);
1341*1976Sab196087 }
1342*1976Sab196087 
1343*1976Sab196087 /*
13440Sstevel@tonic-gate  * lookup -
13450Sstevel@tonic-gate  * Attempts to match one of the strings from a list, 'tab',
13460Sstevel@tonic-gate  * with what is in the file, starting at the current index position 'i'.
13470Sstevel@tonic-gate  * Looks past any initial whitespace and expects whitespace or other
13480Sstevel@tonic-gate  * delimiting characters to follow the matched string.
13490Sstevel@tonic-gate  * A match identifies the file as being 'assembler', 'fortran', 'c', etc.
13500Sstevel@tonic-gate  * Returns 1 for a successful match, 0 otherwise.
13510Sstevel@tonic-gate  */
13520Sstevel@tonic-gate static int
13530Sstevel@tonic-gate lookup(char **tab)
13540Sstevel@tonic-gate {
13550Sstevel@tonic-gate 	register char	r;
13560Sstevel@tonic-gate 	register int	k, j, l;
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	while (fbuf[i] == ' ' || fbuf[i] == '\t' || fbuf[i] == '\n')
13590Sstevel@tonic-gate 		i++;
13600Sstevel@tonic-gate 	for (j = 0; tab[j] != 0; j++) {
13610Sstevel@tonic-gate 		l = 0;
13620Sstevel@tonic-gate 		for (k = i; ((r = tab[j][l++]) == fbuf[k] && r != '\0'); k++);
13630Sstevel@tonic-gate 		if (r == '\0')
13640Sstevel@tonic-gate 			if (fbuf[k] == ' ' || fbuf[k] == '\n' ||
13650Sstevel@tonic-gate 			    fbuf[k] == '\t' || fbuf[k] == '{' ||
13660Sstevel@tonic-gate 			    fbuf[k] == '/') {
13670Sstevel@tonic-gate 				i = k;
13680Sstevel@tonic-gate 				return (1);
13690Sstevel@tonic-gate 			}
13700Sstevel@tonic-gate 	}
13710Sstevel@tonic-gate 	return (0);
13720Sstevel@tonic-gate }
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate /*
13750Sstevel@tonic-gate  * ccom -
13760Sstevel@tonic-gate  * Increments the current index 'i' into the file buffer 'fbuf' past any
13770Sstevel@tonic-gate  * whitespace lines and C-style comments found, starting at the current
13780Sstevel@tonic-gate  * position of 'i'.  Returns 1 as long as we don't increment i past the
13790Sstevel@tonic-gate  * size of fbuf (fbsz).  Otherwise, returns 0.
13800Sstevel@tonic-gate  */
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate static int
13830Sstevel@tonic-gate ccom(void)
13840Sstevel@tonic-gate {
13850Sstevel@tonic-gate 	register char	cc;
13860Sstevel@tonic-gate 	int		len;
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	while ((cc = fbuf[i]) == ' ' || cc == '\t' || cc == '\n')
13890Sstevel@tonic-gate 		if (i++ >= fbsz)
13900Sstevel@tonic-gate 			return (0);
13910Sstevel@tonic-gate 	if (fbuf[i] == '/' && fbuf[i+1] == '*') {
13920Sstevel@tonic-gate 		i += 2;
13930Sstevel@tonic-gate 		while (fbuf[i] != '*' || fbuf[i+1] != '/') {
13940Sstevel@tonic-gate 			if (fbuf[i] == '\\')
13950Sstevel@tonic-gate 				i++;
13960Sstevel@tonic-gate 			if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
13970Sstevel@tonic-gate 				len = 1;
13980Sstevel@tonic-gate 			i += len;
13990Sstevel@tonic-gate 			if (i >= fbsz)
14000Sstevel@tonic-gate 				return (0);
14010Sstevel@tonic-gate 		}
14020Sstevel@tonic-gate 		if ((i += 2) >= fbsz)
14030Sstevel@tonic-gate 			return (0);
14040Sstevel@tonic-gate 	}
14050Sstevel@tonic-gate 	if (fbuf[i] == '\n')
14060Sstevel@tonic-gate 		if (ccom() == 0)
14070Sstevel@tonic-gate 			return (0);
14080Sstevel@tonic-gate 	return (1);
14090Sstevel@tonic-gate }
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate /*
14120Sstevel@tonic-gate  * ascom -
14130Sstevel@tonic-gate  * Increments the current index 'i' into the file buffer 'fbuf' past
14140Sstevel@tonic-gate  * consecutive assembler program comment lines starting with ASCOMCHAR,
14150Sstevel@tonic-gate  * starting at the current position of 'i'.
14160Sstevel@tonic-gate  * Returns 1 as long as we don't increment i past the
14170Sstevel@tonic-gate  * size of fbuf (fbsz).  Otherwise returns 0.
14180Sstevel@tonic-gate  */
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate static int
14210Sstevel@tonic-gate ascom(void)
14220Sstevel@tonic-gate {
14230Sstevel@tonic-gate 	while (fbuf[i] == ASCOMCHAR) {
14240Sstevel@tonic-gate 		i++;
14250Sstevel@tonic-gate 		while (fbuf[i++] != '\n')
14260Sstevel@tonic-gate 			if (i >= fbsz)
14270Sstevel@tonic-gate 				return (0);
14280Sstevel@tonic-gate 		while (fbuf[i] == '\n')
14290Sstevel@tonic-gate 			if (i++ >= fbsz)
14300Sstevel@tonic-gate 				return (0);
14310Sstevel@tonic-gate 	}
14320Sstevel@tonic-gate 	return (1);
14330Sstevel@tonic-gate }
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate static int
14360Sstevel@tonic-gate sccs(void)
14370Sstevel@tonic-gate {				/* look for "1hddddd" where d is a digit */
14380Sstevel@tonic-gate 	register int j;
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	if (fbuf[0] == 1 && fbuf[1] == 'h') {
14410Sstevel@tonic-gate 		for (j = 2; j <= 6; j++) {
14420Sstevel@tonic-gate 			if (isdigit(fbuf[j]))
14430Sstevel@tonic-gate 				continue;
14440Sstevel@tonic-gate 			else
14450Sstevel@tonic-gate 				return (0);
14460Sstevel@tonic-gate 		}
14470Sstevel@tonic-gate 	} else {
14480Sstevel@tonic-gate 		return (0);
14490Sstevel@tonic-gate 	}
14500Sstevel@tonic-gate 	return (1);
14510Sstevel@tonic-gate }
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate static int
14540Sstevel@tonic-gate english(char *bp, int n)
14550Sstevel@tonic-gate {
14560Sstevel@tonic-gate #define	NASC 128		/* number of ascii char ?? */
14570Sstevel@tonic-gate 	register int	j, vow, freq, rare, len;
14580Sstevel@tonic-gate 	register int	badpun = 0, punct = 0;
14590Sstevel@tonic-gate 	int	ct[NASC];
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 	if (n < 50)
14620Sstevel@tonic-gate 		return (0); /* no point in statistics on squibs */
14630Sstevel@tonic-gate 	for (j = 0; j < NASC; j++)
14640Sstevel@tonic-gate 		ct[j] = 0;
14650Sstevel@tonic-gate 	for (j = 0; j < n; j += len) {
14660Sstevel@tonic-gate 		if ((unsigned char)bp[j] < NASC)
14670Sstevel@tonic-gate 			ct[bp[j]|040]++;
14680Sstevel@tonic-gate 		switch (bp[j]) {
14690Sstevel@tonic-gate 		case '.':
14700Sstevel@tonic-gate 		case ',':
14710Sstevel@tonic-gate 		case ')':
14720Sstevel@tonic-gate 		case '%':
14730Sstevel@tonic-gate 		case ';':
14740Sstevel@tonic-gate 		case ':':
14750Sstevel@tonic-gate 		case '?':
14760Sstevel@tonic-gate 			punct++;
14770Sstevel@tonic-gate 			if (j < n-1 && bp[j+1] != ' ' && bp[j+1] != '\n')
14780Sstevel@tonic-gate 				badpun++;
14790Sstevel@tonic-gate 		}
14800Sstevel@tonic-gate 		if ((len = mblen(&bp[j], MB_CUR_MAX)) <= 0)
14810Sstevel@tonic-gate 			len = 1;
14820Sstevel@tonic-gate 	}
14830Sstevel@tonic-gate 	if (badpun*5 > punct)
14840Sstevel@tonic-gate 		return (0);
14850Sstevel@tonic-gate 	vow = ct['a'] + ct['e'] + ct['i'] + ct['o'] + ct['u'];
14860Sstevel@tonic-gate 	freq = ct['e'] + ct['t'] + ct['a'] + ct['i'] + ct['o'] + ct['n'];
14870Sstevel@tonic-gate 	rare = ct['v'] + ct['j'] + ct['k'] + ct['q'] + ct['x'] + ct['z'];
14880Sstevel@tonic-gate 	if (2*ct[';'] > ct['e'])
14890Sstevel@tonic-gate 		return (0);
14900Sstevel@tonic-gate 	if ((ct['>'] + ct['<'] + ct['/']) > ct['e'])
14910Sstevel@tonic-gate 		return (0);	/* shell file test */
14920Sstevel@tonic-gate 	return (vow * 5 >= n - ct[' '] && freq >= 10 * rare);
14930Sstevel@tonic-gate }
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate /*
14960Sstevel@tonic-gate  * Convert a word from an elf file to native format.
14970Sstevel@tonic-gate  * This is needed because there's no elf routine to
14980Sstevel@tonic-gate  * get and decode a Note section header.
14990Sstevel@tonic-gate  */
15000Sstevel@tonic-gate static void
15010Sstevel@tonic-gate convert_gelf_word(Elf *elf, GElf_Word *data, int version, int format)
15020Sstevel@tonic-gate {
15030Sstevel@tonic-gate 	Elf_Data src, dst;
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	dst.d_buf = data;
15060Sstevel@tonic-gate 	dst.d_version = version;
15070Sstevel@tonic-gate 	dst.d_size = sizeof (GElf_Word);
15080Sstevel@tonic-gate 	dst.d_type = ELF_T_WORD;
15090Sstevel@tonic-gate 	src.d_buf = data;
15100Sstevel@tonic-gate 	src.d_version = version;
15110Sstevel@tonic-gate 	src.d_size = sizeof (GElf_Word);
15120Sstevel@tonic-gate 	src.d_type = ELF_T_WORD;
15130Sstevel@tonic-gate 	(void) gelf_xlatetom(elf, &dst, &src, format);
15140Sstevel@tonic-gate }
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate static void
15170Sstevel@tonic-gate convert_gelf_nhdr(Elf *elf, GElf_Nhdr *nhdr, GElf_Word version, int format)
15180Sstevel@tonic-gate {
15190Sstevel@tonic-gate 	convert_gelf_word(elf, &nhdr->n_namesz, version, format);
15200Sstevel@tonic-gate 	convert_gelf_word(elf, &nhdr->n_descsz, version, format);
15210Sstevel@tonic-gate 	convert_gelf_word(elf, &nhdr->n_type, version, format);
15220Sstevel@tonic-gate }
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate /*
15250Sstevel@tonic-gate  * Return true if it is an old (pre-restructured /proc) core file.
15260Sstevel@tonic-gate  */
15270Sstevel@tonic-gate static int
15280Sstevel@tonic-gate old_core(Elf *elf, GElf_Ehdr *ehdr, int format)
15290Sstevel@tonic-gate {
15300Sstevel@tonic-gate 	register int inx;
15310Sstevel@tonic-gate 	GElf_Phdr phdr;
15320Sstevel@tonic-gate 	GElf_Phdr nphdr;
15330Sstevel@tonic-gate 	GElf_Nhdr nhdr;
15340Sstevel@tonic-gate 	off_t offset;
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	if (ehdr->e_type != ET_CORE)
15370Sstevel@tonic-gate 		return (0);
15380Sstevel@tonic-gate 	for (inx = 0; inx < (int)ehdr->e_phnum; inx++) {
15390Sstevel@tonic-gate 		if (gelf_getphdr(elf, inx, &phdr) == NULL) {
15400Sstevel@tonic-gate 			return (0);
15410Sstevel@tonic-gate 		}
15420Sstevel@tonic-gate 		if (phdr.p_type == PT_NOTE) {
15430Sstevel@tonic-gate 			/*
15440Sstevel@tonic-gate 			 * If the next segment is also a note, use it instead.
15450Sstevel@tonic-gate 			 */
15460Sstevel@tonic-gate 			if (gelf_getphdr(elf, inx+1, &nphdr) == NULL) {
15470Sstevel@tonic-gate 				return (0);
15480Sstevel@tonic-gate 			}
15490Sstevel@tonic-gate 			if (nphdr.p_type == PT_NOTE)
15500Sstevel@tonic-gate 				phdr = nphdr;
15510Sstevel@tonic-gate 			offset = (off_t)phdr.p_offset;
15520Sstevel@tonic-gate 			(void) pread(ifd, &nhdr, sizeof (GElf_Nhdr), offset);
15530Sstevel@tonic-gate 			convert_gelf_nhdr(elf, &nhdr, ehdr->e_version, format);
15540Sstevel@tonic-gate 			/*
15550Sstevel@tonic-gate 			 * Old core files have type NT_PRPSINFO.
15560Sstevel@tonic-gate 			 */
15570Sstevel@tonic-gate 			if (nhdr.n_type == NT_PRPSINFO)
15580Sstevel@tonic-gate 				return (1);
15590Sstevel@tonic-gate 			return (0);
15600Sstevel@tonic-gate 		}
15610Sstevel@tonic-gate 	}
15620Sstevel@tonic-gate 	return (0);
15630Sstevel@tonic-gate }
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate /*
15660Sstevel@tonic-gate  * If it's a core file, print out the name of the file that dumped core.
15670Sstevel@tonic-gate  */
15680Sstevel@tonic-gate static int
15690Sstevel@tonic-gate core(Elf *elf, GElf_Ehdr *ehdr, int format)
15700Sstevel@tonic-gate {
15710Sstevel@tonic-gate 	register int inx;
15720Sstevel@tonic-gate 	char *psinfo;
15730Sstevel@tonic-gate 	GElf_Phdr phdr;
15740Sstevel@tonic-gate 	GElf_Phdr nphdr;
15750Sstevel@tonic-gate 	GElf_Nhdr nhdr;
15760Sstevel@tonic-gate 	off_t offset;
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 	if (ehdr->e_type != ET_CORE)
15790Sstevel@tonic-gate 		return (0);
15800Sstevel@tonic-gate 	for (inx = 0; inx < (int)ehdr->e_phnum; inx++) {
15810Sstevel@tonic-gate 		if (gelf_getphdr(elf, inx, &phdr) == NULL) {
15820Sstevel@tonic-gate 			(void) fprintf(stderr,
15830Sstevel@tonic-gate 				gettext("can't read program header\n"));
15840Sstevel@tonic-gate 			return (0);
15850Sstevel@tonic-gate 		}
15860Sstevel@tonic-gate 		if (phdr.p_type == PT_NOTE) {
15870Sstevel@tonic-gate 			char *fname;
15880Sstevel@tonic-gate 			size_t size;
15890Sstevel@tonic-gate 			/*
15900Sstevel@tonic-gate 			 * If the next segment is also a note, use it instead.
15910Sstevel@tonic-gate 			 */
15920Sstevel@tonic-gate 			if (gelf_getphdr(elf, inx+1, &nphdr) == NULL) {
15930Sstevel@tonic-gate 				(void) fprintf(stderr,
15940Sstevel@tonic-gate 				    gettext("can't read program header\n"));
15950Sstevel@tonic-gate 				return (0);
15960Sstevel@tonic-gate 			}
15970Sstevel@tonic-gate 			if (nphdr.p_type == PT_NOTE)
15980Sstevel@tonic-gate 				phdr = nphdr;
15990Sstevel@tonic-gate 			offset = (off_t)phdr.p_offset;
16000Sstevel@tonic-gate 			(void) pread(ifd, &nhdr, sizeof (GElf_Nhdr), offset);
16010Sstevel@tonic-gate 			convert_gelf_nhdr(elf, &nhdr, ehdr->e_version, format);
16020Sstevel@tonic-gate 			/*
16030Sstevel@tonic-gate 			 * Note: the ABI states that n_namesz must
16040Sstevel@tonic-gate 			 * be rounded up to a 4 byte boundary.
16050Sstevel@tonic-gate 			 */
16060Sstevel@tonic-gate 			offset += sizeof (GElf_Nhdr) +
16070Sstevel@tonic-gate 			    ((nhdr.n_namesz + 0x03) & ~0x3);
16080Sstevel@tonic-gate 			size = nhdr.n_descsz;
16090Sstevel@tonic-gate 			psinfo = malloc(size);
16100Sstevel@tonic-gate 			(void) pread(ifd, psinfo, size, offset);
16110Sstevel@tonic-gate 			/*
16120Sstevel@tonic-gate 			 * We want to print the string contained
16130Sstevel@tonic-gate 			 * in psinfo->pr_fname[], where 'psinfo'
16140Sstevel@tonic-gate 			 * is either an old NT_PRPSINFO structure
16150Sstevel@tonic-gate 			 * or a new NT_PSINFO structure.
16160Sstevel@tonic-gate 			 *
16170Sstevel@tonic-gate 			 * Old core files have only type NT_PRPSINFO.
16180Sstevel@tonic-gate 			 * New core files have type NT_PSINFO.
16190Sstevel@tonic-gate 			 *
16200Sstevel@tonic-gate 			 * These structures are also different by
16210Sstevel@tonic-gate 			 * virtue of being contained in a core file
16220Sstevel@tonic-gate 			 * of either 32-bit or 64-bit type.
16230Sstevel@tonic-gate 			 *
16240Sstevel@tonic-gate 			 * To further complicate matters, we ourself
16250Sstevel@tonic-gate 			 * might be compiled either 32-bit or 64-bit.
16260Sstevel@tonic-gate 			 *
16270Sstevel@tonic-gate 			 * For these reason, we just *know* the offsets of
16280Sstevel@tonic-gate 			 * pr_fname[] into the four different structures
16290Sstevel@tonic-gate 			 * here, regardless of how we are compiled.
16300Sstevel@tonic-gate 			 */
16310Sstevel@tonic-gate 			if (gelf_getclass(elf) == ELFCLASS32) {
16320Sstevel@tonic-gate 				/* 32-bit core file, 32-bit structures */
16330Sstevel@tonic-gate 				if (nhdr.n_type == NT_PSINFO)
16340Sstevel@tonic-gate 					fname = psinfo + 88;
16350Sstevel@tonic-gate 				else	/* old: NT_PRPSINFO */
16360Sstevel@tonic-gate 					fname = psinfo + 84;
16370Sstevel@tonic-gate 			} else if (gelf_getclass(elf) == ELFCLASS64) {
16380Sstevel@tonic-gate 				/* 64-bit core file, 64-bit structures */
16390Sstevel@tonic-gate 				if (nhdr.n_type == NT_PSINFO)
16400Sstevel@tonic-gate 					fname = psinfo + 136;
16410Sstevel@tonic-gate 				else	/* old: NT_PRPSINFO */
16420Sstevel@tonic-gate 					fname = psinfo + 120;
16430Sstevel@tonic-gate 			} else {
16440Sstevel@tonic-gate 				free(psinfo);
16450Sstevel@tonic-gate 				break;
16460Sstevel@tonic-gate 			}
16470Sstevel@tonic-gate 			(void) printf(gettext(", from '%s'"), fname);
16480Sstevel@tonic-gate 			free(psinfo);
16490Sstevel@tonic-gate 			break;
16500Sstevel@tonic-gate 		}
16510Sstevel@tonic-gate 	}
16520Sstevel@tonic-gate 	return (1);
16530Sstevel@tonic-gate }
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate static int
16560Sstevel@tonic-gate shellscript(char buf[], struct stat64 *sb)
16570Sstevel@tonic-gate {
16580Sstevel@tonic-gate 	char *tp, *cp, *xp, *up, *gp;
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	cp = strchr(buf, '\n');
16610Sstevel@tonic-gate 	if (cp == NULL || cp - fbuf > fbsz)
16620Sstevel@tonic-gate 		return (0);
16630Sstevel@tonic-gate 	for (tp = buf; tp != cp && isspace((unsigned char)*tp); tp++)
16640Sstevel@tonic-gate 		if (!isascii(*tp))
16650Sstevel@tonic-gate 			return (0);
16660Sstevel@tonic-gate 	for (xp = tp; tp != cp && !isspace((unsigned char)*tp); tp++)
16670Sstevel@tonic-gate 		if (!isascii(*tp))
16680Sstevel@tonic-gate 			return (0);
16690Sstevel@tonic-gate 	if (tp == xp)
16700Sstevel@tonic-gate 		return (0);
16710Sstevel@tonic-gate 	if (sb->st_mode & S_ISUID)
16720Sstevel@tonic-gate 		up = gettext("set-uid ");
16730Sstevel@tonic-gate 	else
16740Sstevel@tonic-gate 		up = "";
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 	if (sb->st_mode & S_ISGID)
16770Sstevel@tonic-gate 		gp = gettext("set-gid ");
16780Sstevel@tonic-gate 	else
16790Sstevel@tonic-gate 		gp = "";
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	if (strncmp(xp, "/bin/sh", tp - xp) == 0)
16820Sstevel@tonic-gate 		xp = gettext("shell");
16830Sstevel@tonic-gate 	else if (strncmp(xp, "/bin/csh", tp - xp) == 0)
16840Sstevel@tonic-gate 		xp = gettext("c-shell");
16850Sstevel@tonic-gate 	else if (strncmp(xp, "/usr/sbin/dtrace", tp - xp) == 0)
16860Sstevel@tonic-gate 		xp = gettext("DTrace");
16870Sstevel@tonic-gate 	else
16880Sstevel@tonic-gate 		*tp = '\0';
16890Sstevel@tonic-gate 	/*
16900Sstevel@tonic-gate 	 * TRANSLATION_NOTE
16910Sstevel@tonic-gate 	 * This message is printed by file command for shell scripts.
16920Sstevel@tonic-gate 	 * The first %s is for the translation for "set-uid " (if the script
16930Sstevel@tonic-gate 	 *   has the set-uid bit set), or is for an empty string (if the
16940Sstevel@tonic-gate 	 *   script does not have the set-uid bit set).
16950Sstevel@tonic-gate 	 * Similarly, the second %s is for the translation for "set-gid ",
16960Sstevel@tonic-gate 	 *   or is for an empty string.
16970Sstevel@tonic-gate 	 * The third %s is for the translation for either: "shell", "c-shell",
16980Sstevel@tonic-gate 	 *   or "DTrace", or is for the pathname of the program the script
16990Sstevel@tonic-gate 	 *   executes.
17000Sstevel@tonic-gate 	 */
17010Sstevel@tonic-gate 	(void) printf(gettext("%s%sexecutable %s script\n"), up, gp, xp);
17020Sstevel@tonic-gate 	return (1);
17030Sstevel@tonic-gate }
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate static int
17060Sstevel@tonic-gate get_door_target(char *file, char *buf, size_t bufsize)
17070Sstevel@tonic-gate {
17080Sstevel@tonic-gate 	int fd;
17090Sstevel@tonic-gate 	door_info_t di;
17100Sstevel@tonic-gate 	psinfo_t psinfo;
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 	if ((fd = open64(file, O_RDONLY)) < 0 ||
17130Sstevel@tonic-gate 	    door_info(fd, &di) != 0) {
17140Sstevel@tonic-gate 		if (fd >= 0)
17150Sstevel@tonic-gate 			(void) close(fd);
17160Sstevel@tonic-gate 		return (-1);
17170Sstevel@tonic-gate 	}
17180Sstevel@tonic-gate 	(void) close(fd);
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	(void) sprintf(buf, "/proc/%ld/psinfo", di.di_target);
17210Sstevel@tonic-gate 	if ((fd = open64(buf, O_RDONLY)) < 0 ||
17220Sstevel@tonic-gate 	    read(fd, &psinfo, sizeof (psinfo)) != sizeof (psinfo)) {
17230Sstevel@tonic-gate 		if (fd >= 0)
17240Sstevel@tonic-gate 			(void) close(fd);
17250Sstevel@tonic-gate 		return (-1);
17260Sstevel@tonic-gate 	}
17270Sstevel@tonic-gate 	(void) close(fd);
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	(void) snprintf(buf, bufsize, "%s[%ld]", psinfo.pr_fname, di.di_target);
17300Sstevel@tonic-gate 	return (0);
17310Sstevel@tonic-gate }
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate /*
17340Sstevel@tonic-gate  * ZIP file header information
17350Sstevel@tonic-gate  */
17360Sstevel@tonic-gate #define	SIGSIZ		4
17370Sstevel@tonic-gate #define	LOCSIG		"PK\003\004"
17380Sstevel@tonic-gate #define	LOCHDRSIZ	30
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate #define	CH(b, n)	(((unsigned char *)(b))[n])
17410Sstevel@tonic-gate #define	SH(b, n)	(CH(b, n) | (CH(b, n+1) << 8))
17420Sstevel@tonic-gate #define	LG(b, n)	(SH(b, n) | (SH(b, n+2) << 16))
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate #define	LOCNAM(b)	(SH(b, 26))	/* filename size */
17450Sstevel@tonic-gate #define	LOCEXT(b)	(SH(b, 28))	/* extra field size */
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate #define	XFHSIZ		4		/* header id, data size */
17480Sstevel@tonic-gate #define	XFHID(b)	(SH(b, 0))	/* extract field header id */
17490Sstevel@tonic-gate #define	XFDATASIZ(b)	(SH(b, 2))	/* extract field data size */
17500Sstevel@tonic-gate #define	XFJAVASIG	0xcafe		/* java executables */
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate static int
17530Sstevel@tonic-gate zipfile(char *fbuf, int fd)
17540Sstevel@tonic-gate {
17550Sstevel@tonic-gate 	off_t xoff, xoff_end;
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	if (strncmp(fbuf, LOCSIG, SIGSIZ) != 0)
17580Sstevel@tonic-gate 		return (0);
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 	xoff = LOCHDRSIZ + LOCNAM(fbuf);
17610Sstevel@tonic-gate 	xoff_end = xoff + LOCEXT(fbuf);
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 	while (xoff < xoff_end) {
17640Sstevel@tonic-gate 		char xfhdr[XFHSIZ];
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 		if (pread(fd, xfhdr, XFHSIZ, xoff) != XFHSIZ)
17670Sstevel@tonic-gate 			break;
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 		if (XFHID(xfhdr) == XFJAVASIG) {
17700Sstevel@tonic-gate 			(void) printf("%s\n", gettext("java program"));
17710Sstevel@tonic-gate 			return (1);
17720Sstevel@tonic-gate 		}
17730Sstevel@tonic-gate 		xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr);
17740Sstevel@tonic-gate 	}
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	/*
17770Sstevel@tonic-gate 	 * We could just print "ZIP archive" here.
17780Sstevel@tonic-gate 	 *
17790Sstevel@tonic-gate 	 * However, customers may be using their own entries in
17800Sstevel@tonic-gate 	 * /etc/magic to distinguish one kind of ZIP file from another, so
17810Sstevel@tonic-gate 	 * let's defer the printing of "ZIP archive" to there.
17820Sstevel@tonic-gate 	 */
17830Sstevel@tonic-gate 	return (0);
17840Sstevel@tonic-gate }
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate static int
17870Sstevel@tonic-gate is_crash_dump(const char *buf, int fd)
17880Sstevel@tonic-gate {
17890Sstevel@tonic-gate 	/* LINTED: pointer cast may result in improper alignment */
17900Sstevel@tonic-gate 	const dumphdr_t *dhp = (const dumphdr_t *)buf;
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 	/*
17930Sstevel@tonic-gate 	 * The current DUMP_MAGIC string covers Solaris 7 and later releases.
17940Sstevel@tonic-gate 	 * The utsname struct is only present in dumphdr_t's with dump_version
17950Sstevel@tonic-gate 	 * greater than or equal to 9.
17960Sstevel@tonic-gate 	 */
17970Sstevel@tonic-gate 	if (dhp->dump_magic == DUMP_MAGIC) {
17980Sstevel@tonic-gate 		print_dumphdr(fd, dhp, return_uint32, NATIVE_ISA);
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 	} else if (dhp->dump_magic == swap_uint32(DUMP_MAGIC)) {
18010Sstevel@tonic-gate 		print_dumphdr(fd, dhp, swap_uint32, OTHER_ISA);
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 	} else if (dhp->dump_magic == OLD_DUMP_MAGIC ||
18040Sstevel@tonic-gate 	    dhp->dump_magic == swap_uint32(OLD_DUMP_MAGIC)) {
18050Sstevel@tonic-gate 		char *isa = (dhp->dump_magic == OLD_DUMP_MAGIC ?
18060Sstevel@tonic-gate 		    NATIVE_ISA : OTHER_ISA);
18070Sstevel@tonic-gate 		(void) printf(gettext("SunOS 32-bit %s crash dump\n"), isa);
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 	} else {
18100Sstevel@tonic-gate 		return (0);
18110Sstevel@tonic-gate 	}
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	return (1);
18140Sstevel@tonic-gate }
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate static void
18170Sstevel@tonic-gate print_dumphdr(const int fd, const dumphdr_t *dhp, uint32_t (*swap)(uint32_t),
18180Sstevel@tonic-gate     const char *isa)
18190Sstevel@tonic-gate {
18200Sstevel@tonic-gate 	dumphdr_t dh;
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	/*
18230Sstevel@tonic-gate 	 * A dumphdr_t is bigger than FBSZ, so we have to manually read the
18240Sstevel@tonic-gate 	 * rest of it.
18250Sstevel@tonic-gate 	 */
18260Sstevel@tonic-gate 	if (swap(dhp->dump_version) > 8 && pread(fd, &dh, sizeof (dumphdr_t),
18270Sstevel@tonic-gate 	    (off_t)0) == sizeof (dumphdr_t)) {
18280Sstevel@tonic-gate 		(void) printf(gettext(
18290Sstevel@tonic-gate 		    "%s %s %s %u-bit %s crash dump from '%s'\n"),
18300Sstevel@tonic-gate 		    dh.dump_utsname.sysname, dh.dump_utsname.release,
18310Sstevel@tonic-gate 		    dh.dump_utsname.version, swap(dh.dump_wordsize), isa,
18320Sstevel@tonic-gate 		    dh.dump_utsname.nodename);
18330Sstevel@tonic-gate 	} else {
18340Sstevel@tonic-gate 		(void) printf(gettext("SunOS %u-bit %s crash dump\n"),
18350Sstevel@tonic-gate 		    swap(dhp->dump_wordsize), isa);
18360Sstevel@tonic-gate 	}
18370Sstevel@tonic-gate }
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate static void
18400Sstevel@tonic-gate usage(void)
18410Sstevel@tonic-gate {
18420Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
18430Sstevel@tonic-gate 		"usage: file [-dh] [-M mfile] [-m mfile] [-f ffile] file ...\n"
18440Sstevel@tonic-gate 		"       file [-dh] [-M mfile] [-m mfile] -f ffile\n"
18450Sstevel@tonic-gate 		"       file -i [-h] [-f ffile] file ...\n"
18460Sstevel@tonic-gate 		"       file -i [-h] -f ffile\n"
18470Sstevel@tonic-gate 		"       file -c [-d] [-M mfile] [-m mfile]\n"));
18480Sstevel@tonic-gate 	exit(2);
18490Sstevel@tonic-gate }
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate static uint32_t
18520Sstevel@tonic-gate swap_uint32(uint32_t in)
18530Sstevel@tonic-gate {
18540Sstevel@tonic-gate 	uint32_t out;
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	out = (in & 0x000000ff) << 24;
18570Sstevel@tonic-gate 	out |= (in & 0x0000ff00) << 8; /* >> 8 << 16 */
18580Sstevel@tonic-gate 	out |= (in & 0x00ff0000) >> 8; /* >> 16 << 8 */
18590Sstevel@tonic-gate 	out |= (in & 0xff000000) >> 24;
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate 	return (out);
18620Sstevel@tonic-gate }
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate static uint32_t
18650Sstevel@tonic-gate return_uint32(uint32_t in)
18660Sstevel@tonic-gate {
18670Sstevel@tonic-gate 	return (in);
18680Sstevel@tonic-gate }
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate /*
18710Sstevel@tonic-gate  * Check if str is in the string list str_list.
18720Sstevel@tonic-gate  */
18730Sstevel@tonic-gate static int
18740Sstevel@tonic-gate is_in_list(char *str_list[], char *str)
18750Sstevel@tonic-gate {
18760Sstevel@tonic-gate 	int i;
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	/*
18790Sstevel@tonic-gate 	 * Only need to compare the strlen(str_list[i]) bytes.
18800Sstevel@tonic-gate 	 * That way .stab will match on .stab* sections, and
18810Sstevel@tonic-gate 	 * .debug will match on .debug* sections.
18820Sstevel@tonic-gate 	 */
18830Sstevel@tonic-gate 	for (i = 0; str_list[i] != NULL; i++) {
18840Sstevel@tonic-gate 		if (strncmp(str_list[i], str, strlen(str_list[i])) == 0) {
18850Sstevel@tonic-gate 			return (1);
18860Sstevel@tonic-gate 		}
18870Sstevel@tonic-gate 	}
18880Sstevel@tonic-gate 	return (0);
18890Sstevel@tonic-gate }
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate /*
18920Sstevel@tonic-gate  * default_magic -
18930Sstevel@tonic-gate  *	allocate space for and create the default magic file
18940Sstevel@tonic-gate  *	name string.
18950Sstevel@tonic-gate  */
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate static void
18980Sstevel@tonic-gate default_magic(void)
18990Sstevel@tonic-gate {
19000Sstevel@tonic-gate 	const char *msg_locale = setlocale(LC_MESSAGES, NULL);
19010Sstevel@tonic-gate 	struct stat	statbuf;
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 	if ((dfile = (char *)malloc(strlen(msg_locale) + 35)) == NULL) {
19040Sstevel@tonic-gate 		perror("file");
19050Sstevel@tonic-gate 		exit(2);
19060Sstevel@tonic-gate 	}
19070Sstevel@tonic-gate 	(void) snprintf(dfile, strlen(msg_locale) + 35,
19080Sstevel@tonic-gate 	    "/usr/lib/locale/%s/LC_MESSAGES/magic", msg_locale);
19090Sstevel@tonic-gate 	if (stat(dfile, &statbuf) != 0) {
19100Sstevel@tonic-gate 		(void) strcpy(dfile, "/etc/magic");
19110Sstevel@tonic-gate 	}
19120Sstevel@tonic-gate }
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate /*
19150Sstevel@tonic-gate  * add_to_mlist -
19160Sstevel@tonic-gate  *	Add the given magic_file filename string to the list of magic
19170Sstevel@tonic-gate  *	files (mlist).  This list of files will later be examined, and
19180Sstevel@tonic-gate  *	each magic file's entries will be added in order to
19190Sstevel@tonic-gate  *	the mtab table.
19200Sstevel@tonic-gate  *
19210Sstevel@tonic-gate  *	The first flag is set to 1 to add to the first list, mlist1.
19220Sstevel@tonic-gate  *	The first flag is set to 0 to add to the second list, mlist2.
19230Sstevel@tonic-gate  */
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate static void
19260Sstevel@tonic-gate add_to_mlist(char *magic_file, int first)
19270Sstevel@tonic-gate {
19280Sstevel@tonic-gate 	char	**mlist;	/* ordered list of magic files */
19290Sstevel@tonic-gate 	size_t	mlist_sz;	/* number of pointers allocated  for mlist */
19300Sstevel@tonic-gate 	char	**mlistp;	/* next entry in mlist */
19310Sstevel@tonic-gate 	size_t mlistp_off;
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	if (first) {
19340Sstevel@tonic-gate 		mlist = mlist1;
19350Sstevel@tonic-gate 		mlist_sz = mlist1_sz;
19360Sstevel@tonic-gate 		mlistp = mlist1p;
19370Sstevel@tonic-gate 	} else {
19380Sstevel@tonic-gate 		mlist = mlist2;
19390Sstevel@tonic-gate 		mlist_sz = mlist2_sz;
19400Sstevel@tonic-gate 		mlistp = mlist2p;
19410Sstevel@tonic-gate 	}
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 	if (mlist == NULL) {	/* initial mlist allocation */
19440Sstevel@tonic-gate 		if ((mlist = (char **)calloc(MLIST_SZ, sizeof (char *)))
19450Sstevel@tonic-gate 		    == NULL) {
19460Sstevel@tonic-gate 			perror("file");
19470Sstevel@tonic-gate 			exit(2);
19480Sstevel@tonic-gate 		}
19490Sstevel@tonic-gate 		mlist_sz = MLIST_SZ;
19500Sstevel@tonic-gate 		mlistp = mlist;
19510Sstevel@tonic-gate 	}
19520Sstevel@tonic-gate 	if ((mlistp - mlist) >= mlist_sz) {
19530Sstevel@tonic-gate 		mlistp_off = mlistp - mlist;
19540Sstevel@tonic-gate 		mlist_sz *= 2;
19550Sstevel@tonic-gate 		if ((mlist = (char **)realloc(mlist,
19560Sstevel@tonic-gate 		    mlist_sz * sizeof (char *))) == NULL) {
19570Sstevel@tonic-gate 			perror("file");
19580Sstevel@tonic-gate 			exit(2);
19590Sstevel@tonic-gate 		}
19600Sstevel@tonic-gate 		mlistp = mlist + mlistp_off;
19610Sstevel@tonic-gate 	}
19620Sstevel@tonic-gate 	/*
19630Sstevel@tonic-gate 	 * now allocate memory for and copy the
19640Sstevel@tonic-gate 	 * magic file name string
19650Sstevel@tonic-gate 	 */
19660Sstevel@tonic-gate 	if ((*mlistp = malloc(strlen(magic_file) + 1)) == NULL) {
19670Sstevel@tonic-gate 		perror("file");
19680Sstevel@tonic-gate 		exit(2);
19690Sstevel@tonic-gate 	}
19700Sstevel@tonic-gate 	(void) strlcpy(*mlistp, magic_file, strlen(magic_file) + 1);
19710Sstevel@tonic-gate 	mlistp++;
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 	if (first) {
19740Sstevel@tonic-gate 		mlist1 = mlist;
19750Sstevel@tonic-gate 		mlist1_sz = mlist_sz;
19760Sstevel@tonic-gate 		mlist1p = mlistp;
19770Sstevel@tonic-gate 	} else {
19780Sstevel@tonic-gate 		mlist2 = mlist;
19790Sstevel@tonic-gate 		mlist2_sz = mlist_sz;
19800Sstevel@tonic-gate 		mlist2p = mlistp;
19810Sstevel@tonic-gate 	}
19820Sstevel@tonic-gate }
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate static void
19850Sstevel@tonic-gate fd_cleanup(void)
19860Sstevel@tonic-gate {
19870Sstevel@tonic-gate 	if (ifd != -1) {
19880Sstevel@tonic-gate 		(void) close(ifd);
19890Sstevel@tonic-gate 		ifd = -1;
19900Sstevel@tonic-gate 	}
19910Sstevel@tonic-gate 	if (elffd != -1) {
19920Sstevel@tonic-gate 		(void) close(elffd);
19930Sstevel@tonic-gate 		elffd = -1;
19940Sstevel@tonic-gate 	}
19950Sstevel@tonic-gate }
1996