xref: /onnv-gate/usr/src/cmd/file/file.c (revision 10843:eb166ea6e74e)
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
51976Sab196087  * Common Development and Distribution License (the "License").
61976Sab196087  * 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*10843SDave.Plauger@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
300Sstevel@tonic-gate  * Use is subject to license terms.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #define	_LARGEFILE64_SOURCE
340Sstevel@tonic-gate 
351976Sab196087 /* Get definitions for the relocation types supported. */
361976Sab196087 #define	ELF_TARGET_ALL
371976Sab196087 
380Sstevel@tonic-gate #include <ctype.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include <fcntl.h>
410Sstevel@tonic-gate #include <signal.h>
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <libelf.h>
440Sstevel@tonic-gate #include <stdlib.h>
450Sstevel@tonic-gate #include <limits.h>
460Sstevel@tonic-gate #include <locale.h>
470Sstevel@tonic-gate #include <wctype.h>
480Sstevel@tonic-gate #include <string.h>
490Sstevel@tonic-gate #include <errno.h>
500Sstevel@tonic-gate #include <door.h>
510Sstevel@tonic-gate #include <sys/param.h>
520Sstevel@tonic-gate #include <sys/types.h>
530Sstevel@tonic-gate #include <sys/mkdev.h>
540Sstevel@tonic-gate #include <sys/stat.h>
550Sstevel@tonic-gate #include <sys/elf.h>
560Sstevel@tonic-gate #include <procfs.h>
570Sstevel@tonic-gate #include <sys/core.h>
580Sstevel@tonic-gate #include <sys/dumphdr.h>
590Sstevel@tonic-gate #include <netinet/in.h>
600Sstevel@tonic-gate #include <gelf.h>
610Sstevel@tonic-gate #include <elfcap.h>
621976Sab196087 #include <sgsrtcid.h>
630Sstevel@tonic-gate #include "file.h"
644195Sny155746 #include "elf_read.h"
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  *	Misc
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #define	FBSZ		512
710Sstevel@tonic-gate #define	MLIST_SZ	12
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * The 0x8FCA0102 magic string was used in crash dumps generated by releases
750Sstevel@tonic-gate  * prior to Solaris 7.
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate #define	OLD_DUMP_MAGIC	0x8FCA0102
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #if defined(__sparc)
800Sstevel@tonic-gate #define	NATIVE_ISA	"SPARC"
810Sstevel@tonic-gate #define	OTHER_ISA	"Intel"
820Sstevel@tonic-gate #else
830Sstevel@tonic-gate #define	NATIVE_ISA	"Intel"
840Sstevel@tonic-gate #define	OTHER_ISA	"SPARC"
850Sstevel@tonic-gate #endif
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /* Assembly language comment char */
880Sstevel@tonic-gate #ifdef pdp11
890Sstevel@tonic-gate #define	ASCOMCHAR '/'
900Sstevel@tonic-gate #else
910Sstevel@tonic-gate #define	ASCOMCHAR '!'
920Sstevel@tonic-gate #endif
930Sstevel@tonic-gate 
940Sstevel@tonic-gate #pragma	align	16(fbuf)
950Sstevel@tonic-gate static char	fbuf[FBSZ];
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * Magic file variables
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate static intmax_t maxmagicoffset;
1010Sstevel@tonic-gate static intmax_t tmpmax;
1020Sstevel@tonic-gate static char	*magicbuf;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static char	*dfile;
1050Sstevel@tonic-gate static char	*troff[] = {	/* new troff intermediate lang */
1060Sstevel@tonic-gate 		"x", "T", "res", "init", "font", "202", "V0", "p1", 0};
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static char	*fort[] = {			/* FORTRAN */
1090Sstevel@tonic-gate 		"function", "subroutine", "common", "dimension", "block",
1100Sstevel@tonic-gate 		"integer", "real", "data", "double",
1110Sstevel@tonic-gate 		"FUNCTION", "SUBROUTINE", "COMMON", "DIMENSION", "BLOCK",
1120Sstevel@tonic-gate 		"INTEGER", "REAL", "DATA", "DOUBLE", 0};
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate static char	*asc[] = {		/* Assembler Commands */
1150Sstevel@tonic-gate 		"sys", "mov", "tst", "clr", "jmp", "cmp", "set", "inc",
1160Sstevel@tonic-gate 		"dec", 0};
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static char	*c[] = {			/* C Language */
1190Sstevel@tonic-gate 		"int", "char", "float", "double", "short", "long", "unsigned",
1200Sstevel@tonic-gate 		"register", "static", "struct", "extern", 0};
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate static char	*as[] = {	/* Assembler Pseudo Ops, prepended with '.' */
1230Sstevel@tonic-gate 		"globl", "global", "ident", "file", "byte", "even",
1240Sstevel@tonic-gate 		"text", "data", "bss", "comm", 0};
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * The line and debug section names are used by the strip command.
1280Sstevel@tonic-gate  * Any changes in the strip implementation need to be reflected here.
1290Sstevel@tonic-gate  */
1300Sstevel@tonic-gate static char	*debug_sections[] = { /* Debug sections in a ELF file */
1310Sstevel@tonic-gate 		".debug", ".stab", ".dwarf", ".line", NULL};
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /* start for MB env */
1343466Srie static wchar_t	wchar;
1350Sstevel@tonic-gate static int	length;
1360Sstevel@tonic-gate static int	IS_ascii;
1370Sstevel@tonic-gate static int	Max;
1380Sstevel@tonic-gate /* end for MB env */
1390Sstevel@tonic-gate static int	i;	/* global index into first 'fbsz' bytes of file */
1400Sstevel@tonic-gate static int	fbsz;
1410Sstevel@tonic-gate static int	ifd = -1;
1420Sstevel@tonic-gate static int	elffd = -1;
1430Sstevel@tonic-gate static int	tret;
1440Sstevel@tonic-gate static int	hflg;
1450Sstevel@tonic-gate static int	dflg;
1460Sstevel@tonic-gate static int	mflg;
1470Sstevel@tonic-gate static int	M_flg;
1480Sstevel@tonic-gate static int	iflg;
1490Sstevel@tonic-gate static struct stat64	mbuf;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static char	**mlist1;	/* 1st ordered list of magic files */
1520Sstevel@tonic-gate static char	**mlist2;	/* 2nd ordered list of magic files */
1530Sstevel@tonic-gate static size_t	mlist1_sz;	/* number of ptrs allocated for mlist1 */
1540Sstevel@tonic-gate static size_t	mlist2_sz;	/* number of ptrs allocated for mlist2 */
1550Sstevel@tonic-gate static char	**mlist1p;	/* next entry in mlist1 */
1560Sstevel@tonic-gate static char	**mlist2p;	/* next entry in mlist2 */
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate static ssize_t	mread;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate static void ar_coff_or_aout(int ifd);
1610Sstevel@tonic-gate static int type(char *file);
1623466Srie static int def_position_tests(char *file);
1630Sstevel@tonic-gate static void def_context_tests(void);
1640Sstevel@tonic-gate static int troffint(char *bp, int n);
1650Sstevel@tonic-gate static int lookup(char **tab);
1660Sstevel@tonic-gate static int ccom(void);
1670Sstevel@tonic-gate static int ascom(void);
1680Sstevel@tonic-gate static int sccs(void);
1690Sstevel@tonic-gate static int english(char *bp, int n);
1700Sstevel@tonic-gate static int shellscript(char buf[], struct stat64 *sb);
1714195Sny155746 static int elf_check(char *file);
1720Sstevel@tonic-gate static int get_door_target(char *, char *, size_t);
1730Sstevel@tonic-gate static int zipfile(char *, int);
1740Sstevel@tonic-gate static int is_crash_dump(const char *, int);
1750Sstevel@tonic-gate static void print_dumphdr(const int, const dumphdr_t *, uint32_t (*)(uint32_t),
1760Sstevel@tonic-gate     const char *);
1770Sstevel@tonic-gate static uint32_t swap_uint32(uint32_t);
1780Sstevel@tonic-gate static uint32_t return_uint32(uint32_t);
1790Sstevel@tonic-gate static void usage(void);
1800Sstevel@tonic-gate static void default_magic(void);
1810Sstevel@tonic-gate static void add_to_mlist(char *, int);
1820Sstevel@tonic-gate static void fd_cleanup(void);
1831976Sab196087 static int is_rtld_config(void);
1840Sstevel@tonic-gate 
1854195Sny155746 /* from elf_read.c */
1864195Sny155746 int elf_read32(int elffd, Elf_Info *EInfo);
1874195Sny155746 int elf_read64(int elffd, Elf_Info *EInfo);
1884195Sny155746 
1890Sstevel@tonic-gate #ifdef XPG4
1900Sstevel@tonic-gate 	/* SUSv3 requires a single <space> after the colon */
1910Sstevel@tonic-gate #define	prf(x)	(void) printf("%s: ", x);
1920Sstevel@tonic-gate #else	/* !XPG4 */
1930Sstevel@tonic-gate #define	prf(x)	(void) printf("%s:%s", x, (int)strlen(x) > 6 ? "\t" : "\t\t");
1940Sstevel@tonic-gate #endif	/* XPG4 */
1950Sstevel@tonic-gate 
1963466Srie /*
1973466Srie  * Static program identifier - used to prevent localization of the name "file"
1983466Srie  * within individual error messages.
1993466Srie  */
2003466Srie const char *File = "file";
2013466Srie 
2020Sstevel@tonic-gate int
main(int argc,char ** argv)2030Sstevel@tonic-gate main(int argc, char **argv)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	char	*p;
2060Sstevel@tonic-gate 	int	ch;
2070Sstevel@tonic-gate 	FILE	*fl;
2080Sstevel@tonic-gate 	int	cflg = 0;
2090Sstevel@tonic-gate 	int	eflg = 0;
2100Sstevel@tonic-gate 	int	fflg = 0;
2110Sstevel@tonic-gate 	char	*ap = NULL;
2120Sstevel@tonic-gate 	int	pathlen;
2130Sstevel@tonic-gate 	char	**filep;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2160Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2170Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2180Sstevel@tonic-gate #endif
2190Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "M:cdf:him:")) != EOF) {
2220Sstevel@tonic-gate 		switch (ch) {
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 		case 'M':
2250Sstevel@tonic-gate 			add_to_mlist(optarg, !dflg);
2260Sstevel@tonic-gate 			M_flg++;
2270Sstevel@tonic-gate 			break;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 		case 'c':
2300Sstevel@tonic-gate 			cflg++;
2310Sstevel@tonic-gate 			break;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 		case 'd':
2340Sstevel@tonic-gate 			if (!dflg) {
2350Sstevel@tonic-gate 				default_magic();
2360Sstevel@tonic-gate 				add_to_mlist(dfile, 0);
2370Sstevel@tonic-gate 				dflg++;
2380Sstevel@tonic-gate 			}
2390Sstevel@tonic-gate 			break;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 		case 'f':
2420Sstevel@tonic-gate 			fflg++;
2433466Srie 			errno = 0;
2440Sstevel@tonic-gate 			if ((fl = fopen(optarg, "r")) == NULL) {
2453466Srie 				int err = errno;
2463466Srie 				(void) fprintf(stderr, gettext("%s: cannot "
2473466Srie 				    "open file %s: %s\n"), File, optarg,
2483466Srie 				    err ? strerror(err) : "");
2490Sstevel@tonic-gate 				usage();
2500Sstevel@tonic-gate 			}
2510Sstevel@tonic-gate 			pathlen = pathconf("/", _PC_PATH_MAX);
2520Sstevel@tonic-gate 			if (pathlen == -1) {
2533466Srie 				int err = errno;
2543466Srie 				(void) fprintf(stderr, gettext("%s: cannot "
2553466Srie 				    "determine maximum path length: %s\n"),
2563466Srie 				    File, strerror(err));
2570Sstevel@tonic-gate 				exit(1);
2580Sstevel@tonic-gate 			}
2590Sstevel@tonic-gate 			pathlen += 2; /* for null and newline in fgets */
2603466Srie 			if ((ap = malloc(pathlen * sizeof (char))) == NULL) {
2613466Srie 				int err = errno;
2623466Srie 				(void) fprintf(stderr, gettext("%s: malloc "
2633466Srie 				    "failed: %s\n"), File, strerror(err));
2643466Srie 				exit(2);
2650Sstevel@tonic-gate 			}
2660Sstevel@tonic-gate 			break;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 		case 'h':
2690Sstevel@tonic-gate 			hflg++;
2700Sstevel@tonic-gate 			break;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		case 'i':
2730Sstevel@tonic-gate 			iflg++;
2740Sstevel@tonic-gate 			break;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 		case 'm':
2770Sstevel@tonic-gate 			add_to_mlist(optarg, !dflg);
2780Sstevel@tonic-gate 			mflg++;
2790Sstevel@tonic-gate 			break;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 		case '?':
2820Sstevel@tonic-gate 			eflg++;
2830Sstevel@tonic-gate 			break;
2840Sstevel@tonic-gate 		}
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 	if (!cflg && !fflg && (eflg || optind == argc))
2870Sstevel@tonic-gate 		usage();
2880Sstevel@tonic-gate 	if (iflg && (dflg || mflg || M_flg)) {
2890Sstevel@tonic-gate 		usage();
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 	if (iflg && cflg) {
2920Sstevel@tonic-gate 		usage();
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if (!dflg && !mflg && !M_flg && !iflg) {
2960Sstevel@tonic-gate 	/* no -d, -m, nor -M option; also -i option doesn't need magic  */
2970Sstevel@tonic-gate 		default_magic();
2980Sstevel@tonic-gate 		if (f_mkmtab(dfile, cflg, 0) == -1) {
2990Sstevel@tonic-gate 			exit(2);
3000Sstevel@tonic-gate 		}
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	else if (mflg && !M_flg && !dflg) {
3040Sstevel@tonic-gate 	/* -m specified without -d nor -M */
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate #ifdef XPG4	/* For SUSv3 only */
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 		/*
3090Sstevel@tonic-gate 		 * The default position-dependent magic file tests
3100Sstevel@tonic-gate 		 * in /etc/magic will follow all the -m magic tests.
3110Sstevel@tonic-gate 		 */
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 		for (filep = mlist1; filep < mlist1p; filep++) {
3140Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 1) == -1) {
3150Sstevel@tonic-gate 				exit(2);
3160Sstevel@tonic-gate 			}
3170Sstevel@tonic-gate 		}
3180Sstevel@tonic-gate 		default_magic();
3190Sstevel@tonic-gate 		if (f_mkmtab(dfile, cflg, 0) == -1) {
3200Sstevel@tonic-gate 			exit(2);
3210Sstevel@tonic-gate 		}
3220Sstevel@tonic-gate #else	/* !XPG4 */
3230Sstevel@tonic-gate 		/*
3240Sstevel@tonic-gate 		 * Retain Solaris file behavior for -m before SUSv3,
3250Sstevel@tonic-gate 		 * when the new -d and -M options are not specified.
3260Sstevel@tonic-gate 		 * Use the -m file specified in place of the default
3270Sstevel@tonic-gate 		 * /etc/magic file.  Solaris file will
3280Sstevel@tonic-gate 		 * now allow more than one magic file to be specified
3290Sstevel@tonic-gate 		 * with multiple -m options, for consistency with
3300Sstevel@tonic-gate 		 * other behavior.
3310Sstevel@tonic-gate 		 *
3320Sstevel@tonic-gate 		 * Put the magic table(s) specified by -m into
3330Sstevel@tonic-gate 		 * the second magic table instead of the first
3340Sstevel@tonic-gate 		 * (as indicated by the last argument to f_mkmtab()),
3350Sstevel@tonic-gate 		 * since they replace the /etc/magic tests and
3360Sstevel@tonic-gate 		 * must be executed alongside the default
3370Sstevel@tonic-gate 		 * position-sensitive tests.
3380Sstevel@tonic-gate 		 */
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 		for (filep = mlist1; filep < mlist1p; filep++) {
3410Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 0) == -1) {
3420Sstevel@tonic-gate 				exit(2);
3430Sstevel@tonic-gate 			}
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate #endif /* XPG4 */
3460Sstevel@tonic-gate 	} else {
3470Sstevel@tonic-gate 		/*
3480Sstevel@tonic-gate 		 * For any other combination of -d, -m, and -M,
3490Sstevel@tonic-gate 		 * use the magic files in command-line order.
3500Sstevel@tonic-gate 		 * Store the entries from the two separate lists of magic
3510Sstevel@tonic-gate 		 * files, if any, into two separate magic file tables.
3520Sstevel@tonic-gate 		 * mlist1: magic tests executed before default magic tests
3530Sstevel@tonic-gate 		 * mlist2: default magic tests and after
3540Sstevel@tonic-gate 		 */
3550Sstevel@tonic-gate 		for (filep = mlist1; filep && (filep < mlist1p); filep++) {
3560Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 1) == -1) {
3570Sstevel@tonic-gate 				exit(2);
3580Sstevel@tonic-gate 			}
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 		for (filep = mlist2; filep && (filep < mlist2p); filep++) {
3610Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 0) == -1) {
3620Sstevel@tonic-gate 				exit(2);
3630Sstevel@tonic-gate 			}
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	/* Initialize the magic file variables; check both magic tables */
3680Sstevel@tonic-gate 	tmpmax = f_getmaxoffset(1);
3690Sstevel@tonic-gate 	maxmagicoffset = f_getmaxoffset(0);
3700Sstevel@tonic-gate 	if (maxmagicoffset < tmpmax) {
3710Sstevel@tonic-gate 		maxmagicoffset = tmpmax;
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 	if (maxmagicoffset < (intmax_t)FBSZ)
3740Sstevel@tonic-gate 		maxmagicoffset = (intmax_t)FBSZ;
3753466Srie 	if ((magicbuf = malloc(maxmagicoffset)) == NULL) {
3763466Srie 		int err = errno;
3773466Srie 		(void) fprintf(stderr, gettext("%s: malloc failed: %s\n"),
3783466Srie 		    File, strerror(err));
3790Sstevel@tonic-gate 		exit(2);
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if (cflg) {
3830Sstevel@tonic-gate 		f_prtmtab();
3840Sstevel@tonic-gate 		if (ferror(stdout) != 0) {
3853466Srie 			(void) fprintf(stderr, gettext("%s: error writing to "
3863466Srie 			    "stdout\n"), File);
3870Sstevel@tonic-gate 			exit(1);
3880Sstevel@tonic-gate 		}
3890Sstevel@tonic-gate 		if (fclose(stdout) != 0) {
3903466Srie 			int err = errno;
3913466Srie 			(void) fprintf(stderr, gettext("%s: fclose "
3923466Srie 			    "failed: %s\n"), File, strerror(err));
3930Sstevel@tonic-gate 			exit(1);
3940Sstevel@tonic-gate 		}
3950Sstevel@tonic-gate 		exit(0);
3960Sstevel@tonic-gate 	}
3973466Srie 
3980Sstevel@tonic-gate 	for (; fflg || optind < argc; optind += !fflg) {
3990Sstevel@tonic-gate 		register int	l;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 		if (fflg) {
4020Sstevel@tonic-gate 			if ((p = fgets(ap, pathlen, fl)) == NULL) {
4030Sstevel@tonic-gate 				fflg = 0;
4040Sstevel@tonic-gate 				optind--;
4050Sstevel@tonic-gate 				continue;
4060Sstevel@tonic-gate 			}
4070Sstevel@tonic-gate 			l = strlen(p);
4080Sstevel@tonic-gate 			if (l > 0)
4090Sstevel@tonic-gate 				p[l - 1] = '\0';
4100Sstevel@tonic-gate 		} else
4110Sstevel@tonic-gate 			p = argv[optind];
4120Sstevel@tonic-gate 		prf(p);				/* print "file_name:<tab>" */
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 		if (type(p))
4150Sstevel@tonic-gate 			tret = 1;
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 	if (ap != NULL)
4180Sstevel@tonic-gate 		free(ap);
4193466Srie 	if (tret != 0)
4200Sstevel@tonic-gate 		exit(tret);
4213466Srie 
4220Sstevel@tonic-gate 	if (ferror(stdout) != 0) {
4233466Srie 		(void) fprintf(stderr, gettext("%s: error writing to "
4243466Srie 		    "stdout\n"), File);
4250Sstevel@tonic-gate 		exit(1);
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 	if (fclose(stdout) != 0) {
4283466Srie 		int err = errno;
4293466Srie 		(void) fprintf(stderr, gettext("%s: fclose failed: %s\n"),
4303466Srie 		    File, strerror(err));
4310Sstevel@tonic-gate 		exit(1);
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 	return (0);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate static int
type(char * file)4370Sstevel@tonic-gate type(char *file)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	int	cc;
4400Sstevel@tonic-gate 	char	buf[BUFSIZ];
4410Sstevel@tonic-gate 	int	(*statf)() = hflg ? lstat64 : stat64;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	i = 0;		/* reset index to beginning of file */
4440Sstevel@tonic-gate 	ifd = -1;
4450Sstevel@tonic-gate 	if ((*statf)(file, &mbuf) < 0) {
4460Sstevel@tonic-gate 		if (statf == lstat64 || lstat64(file, &mbuf) < 0) {
4473466Srie 			int err = errno;
4480Sstevel@tonic-gate 			(void) printf(gettext("cannot open: %s\n"),
4493466Srie 			    strerror(err));
4500Sstevel@tonic-gate 			return (0);		/* POSIX.2 */
4510Sstevel@tonic-gate 		}
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 	switch (mbuf.st_mode & S_IFMT) {
4540Sstevel@tonic-gate 	case S_IFREG:
4550Sstevel@tonic-gate 		if (iflg) {
4560Sstevel@tonic-gate 			(void) printf(gettext("regular file\n"));
4570Sstevel@tonic-gate 			return (0);
4580Sstevel@tonic-gate 		}
4590Sstevel@tonic-gate 		break;
4600Sstevel@tonic-gate 	case S_IFCHR:
4610Sstevel@tonic-gate 		(void) printf(gettext("character"));
4620Sstevel@tonic-gate 		goto spcl;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	case S_IFDIR:
4650Sstevel@tonic-gate 		(void) printf(gettext("directory\n"));
4660Sstevel@tonic-gate 		return (0);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	case S_IFIFO:
4690Sstevel@tonic-gate 		(void) printf(gettext("fifo\n"));
4700Sstevel@tonic-gate 		return (0);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	case S_IFLNK:
4730Sstevel@tonic-gate 		if ((cc = readlink(file, buf, BUFSIZ)) < 0) {
4743466Srie 			int err = errno;
4750Sstevel@tonic-gate 			(void) printf(gettext("readlink error: %s\n"),
4763466Srie 			    strerror(err));
4770Sstevel@tonic-gate 			return (1);
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 		buf[cc] = '\0';
4800Sstevel@tonic-gate 		(void) printf(gettext("symbolic link to %s\n"), buf);
4810Sstevel@tonic-gate 		return (0);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	case S_IFBLK:
4840Sstevel@tonic-gate 		(void) printf(gettext("block"));
4850Sstevel@tonic-gate 					/* major and minor, see sys/mkdev.h */
4860Sstevel@tonic-gate spcl:
4870Sstevel@tonic-gate 		(void) printf(gettext(" special (%d/%d)\n"),
4880Sstevel@tonic-gate 		    major(mbuf.st_rdev), minor(mbuf.st_rdev));
4890Sstevel@tonic-gate 		return (0);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	case S_IFSOCK:
4920Sstevel@tonic-gate 		(void) printf("socket\n");
4930Sstevel@tonic-gate 		/* FIXME, should open and try to getsockname. */
4940Sstevel@tonic-gate 		return (0);
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	case S_IFDOOR:
4970Sstevel@tonic-gate 		if (get_door_target(file, buf, sizeof (buf)) == 0)
4980Sstevel@tonic-gate 			(void) printf(gettext("door to %s\n"), buf);
4990Sstevel@tonic-gate 		else
5000Sstevel@tonic-gate 			(void) printf(gettext("door\n"));
5010Sstevel@tonic-gate 		return (0);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
5060Sstevel@tonic-gate 		(void) printf(gettext("libelf is out of date\n"));
5070Sstevel@tonic-gate 		return (1);
5080Sstevel@tonic-gate 	}
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	ifd = open64(file, O_RDONLY);
5110Sstevel@tonic-gate 	if (ifd < 0) {
5123466Srie 		int err = errno;
5133466Srie 		(void) printf(gettext("cannot open: %s\n"), strerror(err));
5140Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	/* need another fd for elf, since we might want to read the file too */
5180Sstevel@tonic-gate 	elffd = open64(file, O_RDONLY);
5190Sstevel@tonic-gate 	if (elffd < 0) {
5203466Srie 		int err = errno;
5213466Srie 		(void) printf(gettext("cannot open: %s\n"), strerror(err));
5220Sstevel@tonic-gate 		(void) close(ifd);
5230Sstevel@tonic-gate 		ifd = -1;
5240Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 	if ((fbsz = read(ifd, fbuf, FBSZ)) == -1) {
5273466Srie 		int err = errno;
5283466Srie 		(void) printf(gettext("cannot read: %s\n"), strerror(err));
5290Sstevel@tonic-gate 		(void) close(ifd);
5300Sstevel@tonic-gate 		ifd = -1;
5310Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 	if (fbsz == 0) {
5340Sstevel@tonic-gate 		(void) printf(gettext("empty file\n"));
5350Sstevel@tonic-gate 		fd_cleanup();
5360Sstevel@tonic-gate 		return (0);
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	/*
5400Sstevel@tonic-gate 	 * First try user-specified position-dependent magic tests, if any,
5410Sstevel@tonic-gate 	 * which need to execute before the default tests.
5420Sstevel@tonic-gate 	 */
5430Sstevel@tonic-gate 	if ((mread = pread(ifd, (void*)magicbuf, (size_t)maxmagicoffset,
5443466Srie 	    (off_t)0)) == -1) {
5453466Srie 		int err = errno;
5463466Srie 		(void) printf(gettext("cannot read: %s\n"), strerror(err));
5470Sstevel@tonic-gate 		fd_cleanup();
5480Sstevel@tonic-gate 		return (0);
5490Sstevel@tonic-gate 	}
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	/*
5520Sstevel@tonic-gate 	 * ChecK against Magic Table entries.
5530Sstevel@tonic-gate 	 * Check first magic table for magic tests to be applied
5540Sstevel@tonic-gate 	 * before default tests.
5550Sstevel@tonic-gate 	 * If no default tests are to be applied, all magic tests
5560Sstevel@tonic-gate 	 * should occur in this magic table.
5570Sstevel@tonic-gate 	 */
5580Sstevel@tonic-gate 	switch (f_ckmtab(magicbuf, mread, 1)) {
5590Sstevel@tonic-gate 		case -1:	/* Error */
5600Sstevel@tonic-gate 			exit(2);
5610Sstevel@tonic-gate 			break;
5620Sstevel@tonic-gate 		case 0:		/* Not magic */
5630Sstevel@tonic-gate 			break;
5640Sstevel@tonic-gate 		default:	/* Switch is magic index */
5650Sstevel@tonic-gate 			(void) putchar('\n');
5660Sstevel@tonic-gate 			fd_cleanup();
5670Sstevel@tonic-gate 			return (0);
5680Sstevel@tonic-gate 			/* NOTREACHED */
5690Sstevel@tonic-gate 			break;
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	if (dflg || !M_flg) {
5730Sstevel@tonic-gate 		/*
5740Sstevel@tonic-gate 		 * default position-dependent tests,
5750Sstevel@tonic-gate 		 * plus non-default magic tests, if any
5760Sstevel@tonic-gate 		 */
5773466Srie 		switch (def_position_tests(file)) {
5780Sstevel@tonic-gate 			case -1:	/* error */
5790Sstevel@tonic-gate 				fd_cleanup();
5800Sstevel@tonic-gate 				return (1);
5810Sstevel@tonic-gate 			case 1:	/* matching type found */
5820Sstevel@tonic-gate 				fd_cleanup();
5830Sstevel@tonic-gate 				return (0);
5840Sstevel@tonic-gate 				/* NOTREACHED */
5850Sstevel@tonic-gate 				break;
5860Sstevel@tonic-gate 			case 0:		/* no matching type found */
5870Sstevel@tonic-gate 				break;
5880Sstevel@tonic-gate 		}
5890Sstevel@tonic-gate 		/* default context-sensitive tests */
5900Sstevel@tonic-gate 		def_context_tests();
5910Sstevel@tonic-gate 	} else {
5920Sstevel@tonic-gate 		/* no more tests to apply; no match was found */
5930Sstevel@tonic-gate 		(void) printf(gettext("data\n"));
5940Sstevel@tonic-gate 	}
5950Sstevel@tonic-gate 	fd_cleanup();
5960Sstevel@tonic-gate 	return (0);
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate /*
6000Sstevel@tonic-gate  * def_position_tests() - applies default position-sensitive tests,
6010Sstevel@tonic-gate  *	looking for values in specific positions in the file.
6020Sstevel@tonic-gate  *	These are followed by default (followed by possibly some
6030Sstevel@tonic-gate  *	non-default) magic file tests.
6040Sstevel@tonic-gate  *
6050Sstevel@tonic-gate  *	All position-sensitive tests, default or otherwise, must
6060Sstevel@tonic-gate  *	be applied before context-sensitive tests, to avoid
6070Sstevel@tonic-gate  *	false context-sensitive matches.
6080Sstevel@tonic-gate  *
6090Sstevel@tonic-gate  * 	Returns -1 on error which should result in error (non-zero)
6100Sstevel@tonic-gate  *	exit status for the file utility.
6110Sstevel@tonic-gate  *	Returns 0 if no matching file type found.
6120Sstevel@tonic-gate  *	Returns 1 if matching file type found.
6130Sstevel@tonic-gate  */
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate static int
def_position_tests(char * file)6163466Srie def_position_tests(char *file)
6170Sstevel@tonic-gate {
6180Sstevel@tonic-gate 	if (sccs()) {	/* look for "1hddddd" where d is a digit */
6190Sstevel@tonic-gate 		(void) printf("sccs \n");
6200Sstevel@tonic-gate 		return (1);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 	if (fbuf[0] == '#' && fbuf[1] == '!' && shellscript(fbuf+2, &mbuf))
6230Sstevel@tonic-gate 		return (1);
6244195Sny155746 
6254195Sny155746 	if (elf_check(file) == 0) {
6260Sstevel@tonic-gate 		(void) putchar('\n');
6270Sstevel@tonic-gate 		return (1);
6280Sstevel@tonic-gate 	/* LINTED: pointer cast may result in improper alignment */
6290Sstevel@tonic-gate 	} else if (*(int *)fbuf == CORE_MAGIC) {
6300Sstevel@tonic-gate 		/* LINTED: pointer cast may result in improper alignment */
6310Sstevel@tonic-gate 		struct core *corep = (struct core *)fbuf;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 		(void) printf("a.out core file");
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 		if (*(corep->c_cmdname) != '\0')
6360Sstevel@tonic-gate 			(void) printf(" from '%s'", corep->c_cmdname);
6370Sstevel@tonic-gate 		(void) putchar('\n');
6380Sstevel@tonic-gate 		return (1);
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	/*
6421976Sab196087 	 * Runtime linker (ld.so.1) configuration file.
6431976Sab196087 	 */
6441976Sab196087 	if (is_rtld_config())
6451976Sab196087 		return (1);
6461976Sab196087 
6471976Sab196087 	/*
6480Sstevel@tonic-gate 	 * ZIP files, JAR files, and Java executables
6490Sstevel@tonic-gate 	 */
6500Sstevel@tonic-gate 	if (zipfile(fbuf, ifd))
6510Sstevel@tonic-gate 		return (1);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	if (is_crash_dump(fbuf, ifd))
6540Sstevel@tonic-gate 		return (1);
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	/*
6570Sstevel@tonic-gate 	 * ChecK against Magic Table entries.
6580Sstevel@tonic-gate 	 * The magic entries checked here always start with default
6590Sstevel@tonic-gate 	 * magic tests and may be followed by other, non-default magic
6600Sstevel@tonic-gate 	 * tests.  If no default tests are to be executed, all the
6610Sstevel@tonic-gate 	 * magic tests should have been in the first magic table.
6620Sstevel@tonic-gate 	 */
6630Sstevel@tonic-gate 	switch (f_ckmtab(magicbuf, mread, 0)) {
6640Sstevel@tonic-gate 		case -1:	/* Error */
6650Sstevel@tonic-gate 			exit(2);
6660Sstevel@tonic-gate 			break;
6670Sstevel@tonic-gate 		case 0:		/* Not magic */
6680Sstevel@tonic-gate 			return (0);
6690Sstevel@tonic-gate 			/* NOTREACHED */
6700Sstevel@tonic-gate 			break;
6710Sstevel@tonic-gate 		default:	/* Switch is magic index */
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 			/*
6740Sstevel@tonic-gate 			 * f_ckmtab recognizes file type,
6750Sstevel@tonic-gate 			 * check if it is PostScript.
6760Sstevel@tonic-gate 			 * if not, check if elf or a.out
6770Sstevel@tonic-gate 			 */
6780Sstevel@tonic-gate 			if (magicbuf[0] == '%' && magicbuf[1] == '!') {
6790Sstevel@tonic-gate 				(void) putchar('\n');
6800Sstevel@tonic-gate 			} else {
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 				/*
6830Sstevel@tonic-gate 				 * Check that the file is executable (dynamic
6840Sstevel@tonic-gate 				 * objects must be executable to be exec'ed,
6850Sstevel@tonic-gate 				 * shared objects need not be, but by convention
6860Sstevel@tonic-gate 				 * should be executable).
6870Sstevel@tonic-gate 				 *
6880Sstevel@tonic-gate 				 * Note that we should already have processed
6890Sstevel@tonic-gate 				 * the file if it was an ELF file.
6900Sstevel@tonic-gate 				 */
6910Sstevel@tonic-gate 				ar_coff_or_aout(elffd);
6920Sstevel@tonic-gate 				(void) putchar('\n');
6930Sstevel@tonic-gate 			}
6940Sstevel@tonic-gate 			return (1);
6950Sstevel@tonic-gate 			/* NOTREACHED */
6960Sstevel@tonic-gate 			break;
6970Sstevel@tonic-gate 	}
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	return (0);	/* file was not identified */
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate /*
7030Sstevel@tonic-gate  * def_context_tests() - default context-sensitive tests.
7040Sstevel@tonic-gate  *	These are the last tests to be applied.
7050Sstevel@tonic-gate  *	If no match is found, prints out "data".
7060Sstevel@tonic-gate  */
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate static void
def_context_tests(void)7090Sstevel@tonic-gate def_context_tests(void)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	int	j;
7120Sstevel@tonic-gate 	int	nl;
7130Sstevel@tonic-gate 	char	ch;
7140Sstevel@tonic-gate 	int	len;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	if (ccom() == 0)
7170Sstevel@tonic-gate 		goto notc;
7180Sstevel@tonic-gate 	while (fbuf[i] == '#') {
7190Sstevel@tonic-gate 		j = i;
7200Sstevel@tonic-gate 		while (fbuf[i++] != '\n') {
7210Sstevel@tonic-gate 			if (i - j > 255) {
7220Sstevel@tonic-gate 				(void) printf(gettext("data\n"));
7230Sstevel@tonic-gate 				return;
7240Sstevel@tonic-gate 			}
7250Sstevel@tonic-gate 			if (i >= fbsz)
7260Sstevel@tonic-gate 				goto notc;
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 		if (ccom() == 0)
7290Sstevel@tonic-gate 			goto notc;
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate check:
7320Sstevel@tonic-gate 	if (lookup(c) == 1) {
7330Sstevel@tonic-gate 		while ((ch = fbuf[i]) != ';' && ch != '{') {
7340Sstevel@tonic-gate 			if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
7350Sstevel@tonic-gate 				len = 1;
7360Sstevel@tonic-gate 			i += len;
7370Sstevel@tonic-gate 			if (i >= fbsz)
7380Sstevel@tonic-gate 				goto notc;
7390Sstevel@tonic-gate 		}
7400Sstevel@tonic-gate 		(void) printf(gettext("c program text"));
7410Sstevel@tonic-gate 		goto outa;
7420Sstevel@tonic-gate 	}
7430Sstevel@tonic-gate 	nl = 0;
7440Sstevel@tonic-gate 	while (fbuf[i] != '(') {
7450Sstevel@tonic-gate 		if (fbuf[i] <= 0)
7460Sstevel@tonic-gate 			goto notas;
7470Sstevel@tonic-gate 		if (fbuf[i] == ';') {
7480Sstevel@tonic-gate 			i++;
7490Sstevel@tonic-gate 			goto check;
7500Sstevel@tonic-gate 		}
7510Sstevel@tonic-gate 		if (fbuf[i++] == '\n')
7520Sstevel@tonic-gate 			if (nl++ > 6)
7530Sstevel@tonic-gate 				goto notc;
7540Sstevel@tonic-gate 		if (i >= fbsz)
7550Sstevel@tonic-gate 			goto notc;
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 	while (fbuf[i] != ')') {
7580Sstevel@tonic-gate 		if (fbuf[i++] == '\n')
7590Sstevel@tonic-gate 			if (nl++ > 6)
7600Sstevel@tonic-gate 				goto notc;
7610Sstevel@tonic-gate 		if (i >= fbsz)
7620Sstevel@tonic-gate 			goto notc;
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate 	while (fbuf[i] != '{') {
7650Sstevel@tonic-gate 		if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
7660Sstevel@tonic-gate 			len = 1;
7670Sstevel@tonic-gate 		if (fbuf[i] == '\n')
7680Sstevel@tonic-gate 			if (nl++ > 6)
7690Sstevel@tonic-gate 				goto notc;
7700Sstevel@tonic-gate 		i += len;
7710Sstevel@tonic-gate 		if (i >= fbsz)
7720Sstevel@tonic-gate 			goto notc;
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 	(void) printf(gettext("c program text"));
7750Sstevel@tonic-gate 	goto outa;
7760Sstevel@tonic-gate notc:
7770Sstevel@tonic-gate 	i = 0;			/* reset to begining of file again */
7780Sstevel@tonic-gate 	while (fbuf[i] == 'c' || fbuf[i] == 'C'|| fbuf[i] == '!' ||
7790Sstevel@tonic-gate 	    fbuf[i] == '*' || fbuf[i] == '\n') {
7800Sstevel@tonic-gate 		while (fbuf[i++] != '\n')
7810Sstevel@tonic-gate 			if (i >= fbsz)
7820Sstevel@tonic-gate 				goto notfort;
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 	if (lookup(fort) == 1) {
7850Sstevel@tonic-gate 		(void) printf(gettext("fortran program text"));
7860Sstevel@tonic-gate 		goto outa;
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate notfort:			/* looking for assembler program */
7890Sstevel@tonic-gate 	i = 0;			/* reset to beginning of file again */
7900Sstevel@tonic-gate 	if (ccom() == 0)	/* assembler programs may contain */
7910Sstevel@tonic-gate 				/* c-style comments */
7920Sstevel@tonic-gate 		goto notas;
7930Sstevel@tonic-gate 	if (ascom() == 0)
7940Sstevel@tonic-gate 		goto notas;
7950Sstevel@tonic-gate 	j = i - 1;
7960Sstevel@tonic-gate 	if (fbuf[i] == '.') {
7970Sstevel@tonic-gate 		i++;
7980Sstevel@tonic-gate 		if (lookup(as) == 1) {
7990Sstevel@tonic-gate 			(void) printf(gettext("assembler program text"));
8000Sstevel@tonic-gate 			goto outa;
8010Sstevel@tonic-gate 		} else if (j != -1 && fbuf[j] == '\n' && isalpha(fbuf[j + 2])) {
8020Sstevel@tonic-gate 			(void) printf(
8030Sstevel@tonic-gate 			    gettext("[nt]roff, tbl, or eqn input text"));
8040Sstevel@tonic-gate 			goto outa;
8050Sstevel@tonic-gate 		}
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 	while (lookup(asc) == 0) {
8080Sstevel@tonic-gate 		if (ccom() == 0)
8090Sstevel@tonic-gate 			goto notas;
8100Sstevel@tonic-gate 		if (ascom() == 0)
8110Sstevel@tonic-gate 			goto notas;
8120Sstevel@tonic-gate 		while (fbuf[i] != '\n' && fbuf[i++] != ':') {
8130Sstevel@tonic-gate 			if (i >= fbsz)
8140Sstevel@tonic-gate 				goto notas;
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 		while (fbuf[i] == '\n' || fbuf[i] == ' ' || fbuf[i] == '\t')
8170Sstevel@tonic-gate 			if (i++ >= fbsz)
8180Sstevel@tonic-gate 				goto notas;
8190Sstevel@tonic-gate 		j = i - 1;
8200Sstevel@tonic-gate 		if (fbuf[i] == '.') {
8210Sstevel@tonic-gate 			i++;
8220Sstevel@tonic-gate 			if (lookup(as) == 1) {
8230Sstevel@tonic-gate 				(void) printf(
8240Sstevel@tonic-gate 				    gettext("assembler program text"));
8250Sstevel@tonic-gate 				goto outa;
8260Sstevel@tonic-gate 			} else if (fbuf[j] == '\n' && isalpha(fbuf[j+2])) {
8270Sstevel@tonic-gate 				(void) printf(
8280Sstevel@tonic-gate 				    gettext("[nt]roff, tbl, or eqn input "
8290Sstevel@tonic-gate 				    "text"));
8300Sstevel@tonic-gate 				goto outa;
8310Sstevel@tonic-gate 			}
8320Sstevel@tonic-gate 		}
8330Sstevel@tonic-gate 	}
8340Sstevel@tonic-gate 	(void) printf(gettext("assembler program text"));
8350Sstevel@tonic-gate 	goto outa;
8360Sstevel@tonic-gate notas:
8370Sstevel@tonic-gate 	/* start modification for multibyte env */
8380Sstevel@tonic-gate 	IS_ascii = 1;
8390Sstevel@tonic-gate 	if (fbsz < FBSZ)
8400Sstevel@tonic-gate 		Max = fbsz;
8410Sstevel@tonic-gate 	else
8420Sstevel@tonic-gate 		Max = FBSZ - MB_LEN_MAX; /* prevent cut of wchar read */
8430Sstevel@tonic-gate 	/* end modification for multibyte env */
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	for (i = 0; i < Max; /* null */)
8460Sstevel@tonic-gate 		if (fbuf[i] & 0200) {
8470Sstevel@tonic-gate 			IS_ascii = 0;
8480Sstevel@tonic-gate 			if (fbuf[0] == '\100' && fbuf[1] == '\357') {
8490Sstevel@tonic-gate 				(void) printf(gettext("troff output\n"));
8500Sstevel@tonic-gate 				return;
8510Sstevel@tonic-gate 			}
8520Sstevel@tonic-gate 		/* start modification for multibyte env */
8530Sstevel@tonic-gate 			if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX))
8540Sstevel@tonic-gate 			    <= 0 || !iswprint(wchar)) {
8550Sstevel@tonic-gate 				(void) printf(gettext("data\n"));
8560Sstevel@tonic-gate 				return;
8570Sstevel@tonic-gate 			}
8580Sstevel@tonic-gate 			i += length;
8590Sstevel@tonic-gate 		}
8600Sstevel@tonic-gate 		else
8610Sstevel@tonic-gate 			i++;
8620Sstevel@tonic-gate 	i = fbsz;
8630Sstevel@tonic-gate 		/* end modification for multibyte env */
8640Sstevel@tonic-gate 	if (mbuf.st_mode&(S_IXUSR|S_IXGRP|S_IXOTH))
8650Sstevel@tonic-gate 		(void) printf(gettext("commands text"));
8660Sstevel@tonic-gate 	else if (troffint(fbuf, fbsz))
8670Sstevel@tonic-gate 		(void) printf(gettext("troff intermediate output text"));
8680Sstevel@tonic-gate 	else if (english(fbuf, fbsz))
8690Sstevel@tonic-gate 		(void) printf(gettext("English text"));
8700Sstevel@tonic-gate 	else if (IS_ascii)
8710Sstevel@tonic-gate 		(void) printf(gettext("ascii text"));
8720Sstevel@tonic-gate 	else
8730Sstevel@tonic-gate 		(void) printf(gettext("text")); /* for multibyte env */
8740Sstevel@tonic-gate outa:
8750Sstevel@tonic-gate 	/*
8760Sstevel@tonic-gate 	 * This code is to make sure that no MB char is cut in half
8770Sstevel@tonic-gate 	 * while still being used.
8780Sstevel@tonic-gate 	 */
8790Sstevel@tonic-gate 	fbsz = (fbsz < FBSZ ? fbsz : fbsz - MB_CUR_MAX + 1);
8800Sstevel@tonic-gate 	while (i < fbsz) {
8810Sstevel@tonic-gate 		if (isascii(fbuf[i])) {
8820Sstevel@tonic-gate 			i++;
8830Sstevel@tonic-gate 			continue;
8840Sstevel@tonic-gate 		} else {
8850Sstevel@tonic-gate 			if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX))
8860Sstevel@tonic-gate 			    <= 0 || !iswprint(wchar)) {
8870Sstevel@tonic-gate 				(void) printf(gettext(" with garbage\n"));
8880Sstevel@tonic-gate 				return;
8890Sstevel@tonic-gate 			}
8900Sstevel@tonic-gate 			i = i + length;
8910Sstevel@tonic-gate 		}
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate 	(void) printf("\n");
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate static int
troffint(char * bp,int n)8970Sstevel@tonic-gate troffint(char *bp, int n)
8980Sstevel@tonic-gate {
8990Sstevel@tonic-gate 	int k;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	i = 0;
9020Sstevel@tonic-gate 	for (k = 0; k < 6; k++) {
9030Sstevel@tonic-gate 		if (lookup(troff) == 0)
9040Sstevel@tonic-gate 			return (0);
9050Sstevel@tonic-gate 		if (lookup(troff) == 0)
9060Sstevel@tonic-gate 			return (0);
9070Sstevel@tonic-gate 		while (i < n && bp[i] != '\n')
9080Sstevel@tonic-gate 			i++;
9090Sstevel@tonic-gate 		if (i++ >= n)
9100Sstevel@tonic-gate 			return (0);
9110Sstevel@tonic-gate 	}
9120Sstevel@tonic-gate 	return (1);
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate static void
ar_coff_or_aout(int elffd)9160Sstevel@tonic-gate ar_coff_or_aout(int elffd)
9170Sstevel@tonic-gate {
9180Sstevel@tonic-gate 	Elf *elf;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	/*
9210Sstevel@tonic-gate 	 * Get the files elf descriptor and process it as an elf or
9220Sstevel@tonic-gate 	 * a.out (4.x) file.
9230Sstevel@tonic-gate 	 */
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	elf = elf_begin(elffd, ELF_C_READ, (Elf *)0);
9260Sstevel@tonic-gate 	switch (elf_kind(elf)) {
9270Sstevel@tonic-gate 		case ELF_K_AR :
9280Sstevel@tonic-gate 			(void) printf(gettext(", not a dynamic executable "
9290Sstevel@tonic-gate 			    "or shared object"));
9300Sstevel@tonic-gate 			break;
9310Sstevel@tonic-gate 		case ELF_K_COFF:
9320Sstevel@tonic-gate 			(void) printf(gettext(", unsupported or unknown "
9330Sstevel@tonic-gate 			    "file type"));
9340Sstevel@tonic-gate 			break;
9350Sstevel@tonic-gate 		default:
9360Sstevel@tonic-gate 			/*
9370Sstevel@tonic-gate 			 * This is either an unknown file or an aout format
9380Sstevel@tonic-gate 			 * At this time, we don't print dynamic/stripped
9390Sstevel@tonic-gate 			 * info. on a.out or non-Elf binaries.
9400Sstevel@tonic-gate 			 */
9410Sstevel@tonic-gate 			break;
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 	(void) elf_end(elf);
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate static void
print_elf_type(Elf_Info EI)9484195Sny155746 print_elf_type(Elf_Info EI)
9490Sstevel@tonic-gate {
9504195Sny155746 	switch (EI.type) {
9510Sstevel@tonic-gate 	case ET_NONE:
9520Sstevel@tonic-gate 		(void) printf(" %s", gettext("unknown type"));
9530Sstevel@tonic-gate 		break;
9540Sstevel@tonic-gate 	case ET_REL:
9550Sstevel@tonic-gate 		(void) printf(" %s", gettext("relocatable"));
9560Sstevel@tonic-gate 		break;
9570Sstevel@tonic-gate 	case ET_EXEC:
9580Sstevel@tonic-gate 		(void) printf(" %s", gettext("executable"));
9590Sstevel@tonic-gate 		break;
9600Sstevel@tonic-gate 	case ET_DYN:
9610Sstevel@tonic-gate 		(void) printf(" %s", gettext("dynamic lib"));
9620Sstevel@tonic-gate 		break;
9630Sstevel@tonic-gate 	default:
9640Sstevel@tonic-gate 		break;
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate static void
print_elf_machine(int machine)9690Sstevel@tonic-gate print_elf_machine(int machine)
9700Sstevel@tonic-gate {
9712351Sab196087 	/*
9722351Sab196087 	 * This table must be kept in sync with the EM_ constants
9732351Sab196087 	 * in /usr/include/sys/elf.h.
9742351Sab196087 	 */
9752351Sab196087 	static const char *mach_str[EM_NUM] = {
9762351Sab196087 		"unknown machine",		/* 0 - EM_NONE */
9772351Sab196087 		"WE32100",			/* 1 - EM_M32 */
9782351Sab196087 		"SPARC",			/* 2 - EM_SPARC */
9792351Sab196087 		"80386",			/* 3 - EM_386 */
9802351Sab196087 		"M68000",			/* 4 - EM_68K */
9812351Sab196087 		"M88000",			/* 5 - EM_88K */
9822351Sab196087 		"80486",			/* 6 - EM_486 */
9832351Sab196087 		"i860",				/* 7 - EM_860 */
9842351Sab196087 		"MIPS RS3000 Big-Endian",	/* 8 - EM_MIPS */
9852351Sab196087 		"S/370",			/* 9 - EM_S370 */
9862351Sab196087 		"MIPS RS3000 Little-Endian",	/* 10 - EM_MIPS_RS3_LE */
9872351Sab196087 		"MIPS RS6000",			/* 11 - EM_RS6000 */
9882351Sab196087 		NULL,				/* 12 - EM_UNKNOWN12 */
9892351Sab196087 		NULL,				/* 13 - EM_UNKNOWN13 */
9902351Sab196087 		NULL,				/* 14 - EM_UNKNOWN14 */
9912351Sab196087 		"PA-RISC",			/* 15 - EM_PA_RISC */
9922351Sab196087 		"nCUBE",			/* 16 - EM_nCUBE */
9932351Sab196087 		"VPP500",			/* 17 - EM_VPP500 */
9942351Sab196087 		"SPARC32PLUS",			/* 18 - EM_SPARC32PLUS */
9952351Sab196087 		"i960",				/* 19 - EM_960 */
9962351Sab196087 		"PowerPC",			/* 20 - EM_PPC */
9972351Sab196087 		"PowerPC64",			/* 21 - EM_PPC64 */
9982802Sab196087 		"S/390",			/* 22 - EM_S390 */
9992351Sab196087 		NULL,				/* 23 - EM_UNKNOWN23 */
10002351Sab196087 		NULL,				/* 24 - EM_UNKNOWN24 */
10012351Sab196087 		NULL,				/* 25 - EM_UNKNOWN25 */
10022351Sab196087 		NULL,				/* 26 - EM_UNKNOWN26 */
10032351Sab196087 		NULL,				/* 27 - EM_UNKNOWN27 */
10042351Sab196087 		NULL,				/* 28 - EM_UNKNOWN28 */
10052351Sab196087 		NULL,				/* 29 - EM_UNKNOWN29 */
10062351Sab196087 		NULL,				/* 30 - EM_UNKNOWN30 */
10072351Sab196087 		NULL,				/* 31 - EM_UNKNOWN31 */
10082351Sab196087 		NULL,				/* 32 - EM_UNKNOWN32 */
10092351Sab196087 		NULL,				/* 33 - EM_UNKNOWN33 */
10102351Sab196087 		NULL,				/* 34 - EM_UNKNOWN34 */
10112351Sab196087 		NULL,				/* 35 - EM_UNKNOWN35 */
10122351Sab196087 		"V800",				/* 36 - EM_V800 */
10132351Sab196087 		"FR20",				/* 37 - EM_FR20 */
10142351Sab196087 		"RH32",				/* 38 - EM_RH32 */
10152351Sab196087 		"RCE",				/* 39 - EM_RCE */
10162351Sab196087 		"ARM",				/* 40 - EM_ARM */
10172351Sab196087 		"Alpha",			/* 41 - EM_ALPHA */
10182351Sab196087 		"S/390",			/* 42 - EM_SH */
10192351Sab196087 		"SPARCV9",			/* 43 - EM_SPARCV9 */
10202351Sab196087 		"Tricore",			/* 44 - EM_TRICORE */
10212351Sab196087 		"ARC",				/* 45 - EM_ARC */
10222351Sab196087 		"H8/300",			/* 46 - EM_H8_300 */
10232351Sab196087 		"H8/300H",			/* 47 - EM_H8_300H */
10242351Sab196087 		"H8S",				/* 48 - EM_H8S */
10252351Sab196087 		"H8/500",			/* 49 - EM_H8_500 */
10262351Sab196087 		"IA64",				/* 50 - EM_IA_64 */
10272351Sab196087 		"MIPS-X",			/* 51 - EM_MIPS_X */
10282351Sab196087 		"Coldfire",			/* 52 - EM_COLDFIRE */
10292351Sab196087 		"M68HC12",			/* 53 - EM_68HC12 */
10302351Sab196087 		"MMA",				/* 54 - EM_MMA */
10312351Sab196087 		"PCP",				/* 55 - EM_PCP */
10322351Sab196087 		"nCPU",				/* 56 - EM_NCPU */
10332351Sab196087 		"NDR1",				/* 57 - EM_NDR1 */
10342351Sab196087 		"Starcore",			/* 58 - EM_STARCORE */
10352351Sab196087 		"ME16",				/* 59 - EM_ME16 */
10362351Sab196087 		"ST100",			/* 60 - EM_ST100 */
10372351Sab196087 		"TINYJ",			/* 61 - EM_TINYJ */
10382351Sab196087 		"AMD64",			/* 62 - EM_AMD64 */
10392351Sab196087 		"PDSP",				/* 63 - EM_PDSP */
10402351Sab196087 		NULL,				/* 64 - EM_UNKNOWN64 */
10412351Sab196087 		NULL,				/* 65 - EM_UNKNOWN65 */
10422351Sab196087 		"FX66",				/* 66 - EM_FX66 */
10432351Sab196087 		"ST9 PLUS",			/* 67 - EM_ST9PLUS */
10442351Sab196087 		"ST7",				/* 68 - EM_ST7 */
10452351Sab196087 		"68HC16",			/* 69 - EM_68HC16 */
10462351Sab196087 		"68HC11",			/* 70 - EM_68HC11 */
10472351Sab196087 		"68H08",			/* 71 - EM_68HC08 */
10482351Sab196087 		"68HC05",			/* 72 - EM_68HC05 */
10492351Sab196087 		"SVX",				/* 73 - EM_SVX */
10502351Sab196087 		"ST19",				/* 74 - EM_ST19 */
10512351Sab196087 		"VAX",				/* 75 - EM_VAX */
10522351Sab196087 		"CRIS",				/* 76 - EM_CRIS */
10532351Sab196087 		"Javelin",			/* 77 - EM_JAVELIN */
10542351Sab196087 		"Firepath",			/* 78 - EM_FIREPATH */
10552351Sab196087 		"ZSP",				/* 79 - EM_ZSP */
10562351Sab196087 		"MMIX",				/* 80 - EM_MMIX */
10572351Sab196087 		"HUANY",			/* 81 - EM_HUANY */
10582351Sab196087 		"Prism",			/* 82 - EM_PRISM */
10592351Sab196087 		"AVR",				/* 83 - EM_AVR */
10602351Sab196087 		"FR30",				/* 84 - EM_FR30 */
10612351Sab196087 		"D10V",				/* 85 - EM_D10V */
10622351Sab196087 		"D30V",				/* 86 - EM_D30V */
10632351Sab196087 		"V850",				/* 87 - EM_V850 */
10642351Sab196087 		"M32R",				/* 88 - EM_M32R */
10652351Sab196087 		"MN10300",			/* 89 - EM_MN10300 */
10662351Sab196087 		"MN10200",			/* 90 - EM_MN10200 */
10672351Sab196087 		"picoJava",			/* 91 - EM_PJ */
10682351Sab196087 		"OpenRISC",			/* 92 - EM_OPENRISC */
10692351Sab196087 		"Tangent-A5",			/* 93 - EM_ARC_A5 */
10702351Sab196087 		"Xtensa"			/* 94 - EM_XTENSA */
10712351Sab196087 	};
10722351Sab196087 	/* If new machine is added, refuse to compile until we're updated */
10732351Sab196087 #if EM_NUM != 95
10742351Sab196087 #error "Number of known ELF machine constants has changed"
10752351Sab196087 #endif
10762351Sab196087 
10772351Sab196087 	const char *str;
10782351Sab196087 
10792351Sab196087 	if ((machine < EM_NONE) || (machine >= EM_NUM))
10802351Sab196087 		machine = EM_NONE;
10812351Sab196087 
10822351Sab196087 	str = mach_str[machine];
10832351Sab196087 	if (str)
10842351Sab196087 		(void) printf(" %s", str);
10850Sstevel@tonic-gate }
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate static void
print_elf_datatype(int datatype)10880Sstevel@tonic-gate print_elf_datatype(int datatype)
10890Sstevel@tonic-gate {
10900Sstevel@tonic-gate 	switch (datatype) {
10910Sstevel@tonic-gate 	case ELFDATA2LSB:
10922351Sab196087 		(void) printf(" LSB");
10930Sstevel@tonic-gate 		break;
10940Sstevel@tonic-gate 	case ELFDATA2MSB:
10952351Sab196087 		(void) printf(" MSB");
10960Sstevel@tonic-gate 		break;
10970Sstevel@tonic-gate 	default:
10980Sstevel@tonic-gate 		break;
10990Sstevel@tonic-gate 	}
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate static void
print_elf_class(int class)11030Sstevel@tonic-gate print_elf_class(int class)
11040Sstevel@tonic-gate {
11050Sstevel@tonic-gate 	switch (class) {
11060Sstevel@tonic-gate 	case ELFCLASS32:
11070Sstevel@tonic-gate 		(void) printf(" %s", gettext("32-bit"));
11080Sstevel@tonic-gate 		break;
11090Sstevel@tonic-gate 	case ELFCLASS64:
11100Sstevel@tonic-gate 		(void) printf(" %s", gettext("64-bit"));
11110Sstevel@tonic-gate 		break;
11120Sstevel@tonic-gate 	default:
11130Sstevel@tonic-gate 		break;
11140Sstevel@tonic-gate 	}
11150Sstevel@tonic-gate }
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate static void
print_elf_flags(Elf_Info EI)11184195Sny155746 print_elf_flags(Elf_Info EI)
11190Sstevel@tonic-gate {
11204195Sny155746 	unsigned int flags;
11214195Sny155746 
11224195Sny155746 	flags = EI.flags;
11234195Sny155746 	switch (EI.machine) {
11240Sstevel@tonic-gate 	case EM_SPARCV9:
11250Sstevel@tonic-gate 		if (flags & EF_SPARC_EXT_MASK) {
11260Sstevel@tonic-gate 			if (flags & EF_SPARC_SUN_US3) {
11270Sstevel@tonic-gate 				(void) printf("%s", gettext(
11280Sstevel@tonic-gate 				    ", UltraSPARC3 Extensions Required"));
11290Sstevel@tonic-gate 			} else if (flags & EF_SPARC_SUN_US1) {
11300Sstevel@tonic-gate 				(void) printf("%s", gettext(
11310Sstevel@tonic-gate 				    ", UltraSPARC1 Extensions Required"));
11320Sstevel@tonic-gate 			}
11330Sstevel@tonic-gate 			if (flags & EF_SPARC_HAL_R1)
11340Sstevel@tonic-gate 				(void) printf("%s", gettext(
11350Sstevel@tonic-gate 				    ", HaL R1 Extensions Required"));
11360Sstevel@tonic-gate 		}
11370Sstevel@tonic-gate 		break;
11380Sstevel@tonic-gate 	case EM_SPARC32PLUS:
11390Sstevel@tonic-gate 		if (flags & EF_SPARC_32PLUS)
11400Sstevel@tonic-gate 			(void) printf("%s", gettext(", V8+ Required"));
11410Sstevel@tonic-gate 		if (flags & EF_SPARC_SUN_US3) {
11420Sstevel@tonic-gate 			(void) printf("%s",
1143*10843SDave.Plauger@Sun.COM 			    gettext(", UltraSPARC3 Extensions Required"));
11440Sstevel@tonic-gate 		} else if (flags & EF_SPARC_SUN_US1) {
11450Sstevel@tonic-gate 			(void) printf("%s",
1146*10843SDave.Plauger@Sun.COM 			    gettext(", UltraSPARC1 Extensions Required"));
11470Sstevel@tonic-gate 		}
11480Sstevel@tonic-gate 		if (flags & EF_SPARC_HAL_R1)
11490Sstevel@tonic-gate 			(void) printf("%s",
1150*10843SDave.Plauger@Sun.COM 			    gettext(", HaL R1 Extensions Required"));
11510Sstevel@tonic-gate 		break;
11520Sstevel@tonic-gate 	default:
11530Sstevel@tonic-gate 		break;
11540Sstevel@tonic-gate 	}
11550Sstevel@tonic-gate }
11560Sstevel@tonic-gate 
11574195Sny155746 /*
11584195Sny155746  * check_ident:	checks the ident field of the presumeably
11594195Sny155746  *		elf file. If check fails, this is not an
11604195Sny155746  *		elf file.
11614195Sny155746  */
11620Sstevel@tonic-gate static int
check_ident(unsigned char * ident,int fd)11634195Sny155746 check_ident(unsigned char *ident, int fd)
11640Sstevel@tonic-gate {
11654195Sny155746 	int class;
11664195Sny155746 	if (pread64(fd, ident, EI_NIDENT, 0) != EI_NIDENT)
11674195Sny155746 		return (ELF_READ_FAIL);
11684195Sny155746 	class = ident[EI_CLASS];
11694195Sny155746 	if (class != ELFCLASS32 && class != ELFCLASS64)
11704195Sny155746 		return (ELF_READ_FAIL);
11714195Sny155746 	if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
11724195Sny155746 	    ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3)
11734195Sny155746 		return (ELF_READ_FAIL);
11740Sstevel@tonic-gate 
11754195Sny155746 	return (ELF_READ_OKAY);
11760Sstevel@tonic-gate }
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate static int
elf_check(char * file)11794195Sny155746 elf_check(char *file)
11800Sstevel@tonic-gate {
11814195Sny155746 	Elf_Info EInfo;
11824195Sny155746 	int class, version, format;
11834195Sny155746 	unsigned char ident[EI_NIDENT];
11844195Sny155746 
11854195Sny155746 	(void) memset(&EInfo, 0, sizeof (Elf_Info));
11864195Sny155746 	EInfo.file = file;
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	/*
11894195Sny155746 	 * Verify information in file indentifier.
11904195Sny155746 	 * Return quietly if not elf; Different type of file.
11910Sstevel@tonic-gate 	 */
11924195Sny155746 	if (check_ident(ident, elffd) == ELF_READ_FAIL)
11930Sstevel@tonic-gate 		return (1);
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	/*
11964195Sny155746 	 * Read the elf headers for processing and get the
11974195Sny155746 	 * get the needed information in Elf_Info struct.
11980Sstevel@tonic-gate 	 */
11994195Sny155746 	class = ident[EI_CLASS];
12004195Sny155746 	if (class == ELFCLASS32) {
12014195Sny155746 		if (elf_read32(elffd, &EInfo) == ELF_READ_FAIL) {
12024195Sny155746 			(void) fprintf(stderr, gettext("%s: %s: can't "
12034195Sny155746 			    "read ELF header\n"), File, file);
12044195Sny155746 			return (1);
12054195Sny155746 		}
12064195Sny155746 	} else if (class == ELFCLASS64) {
12074195Sny155746 		if (elf_read64(elffd, &EInfo) == ELF_READ_FAIL) {
12084195Sny155746 			(void) fprintf(stderr, gettext("%s: %s: can't "
12094195Sny155746 			    "read ELF header\n"), File, file);
12104195Sny155746 			return (1);
12114195Sny155746 		}
12124195Sny155746 	} else {
12134195Sny155746 		/* something wrong */
12140Sstevel@tonic-gate 		return (1);
12150Sstevel@tonic-gate 	}
12160Sstevel@tonic-gate 
12174195Sny155746 	/* version not in ident then 1 */
12184195Sny155746 	version = ident[EI_VERSION] ? ident[EI_VERSION] : 1;
12194195Sny155746 
12204195Sny155746 	format = ident[EI_DATA];
12214195Sny155746 	(void) printf("%s", gettext("ELF"));
12224195Sny155746 	print_elf_class(class);
12234195Sny155746 	print_elf_datatype(format);
12244195Sny155746 	print_elf_type(EInfo);
12254195Sny155746 
12264195Sny155746 	if (EInfo.core_type != EC_NOTCORE) {
12274195Sny155746 		/* Print what kind of core is this */
12284195Sny155746 		if (EInfo.core_type == EC_OLDCORE)
12294195Sny155746 			(void) printf(" %s", gettext("pre-2.6 core file"));
12304195Sny155746 		else
12314195Sny155746 			(void) printf(" %s", gettext("core file"));
12320Sstevel@tonic-gate 	}
12334195Sny155746 
12344195Sny155746 	/* Print machine info */
12354195Sny155746 	print_elf_machine(EInfo.machine);
12364195Sny155746 
12374195Sny155746 	/* Print Version */
12384195Sny155746 	if (version == 1)
12394195Sny155746 		(void) printf(" %s %d", gettext("Version"), version);
12404195Sny155746 
12414195Sny155746 	/* Print Flags */
12424195Sny155746 	print_elf_flags(EInfo);
12434195Sny155746 
12444195Sny155746 	/* Last bit, if it is a core */
12454195Sny155746 	if (EInfo.core_type != EC_NOTCORE) {
12464195Sny155746 		/* Print the program name that dumped this core */
12474195Sny155746 		(void) printf(gettext(", from '%s'"), EInfo.fname);
12484195Sny155746 		return (0);
12494195Sny155746 	}
12504195Sny155746 
12514195Sny155746 	/* Print Capabilities */
12524195Sny155746 	if (EInfo.cap_str[0] != '\0')
12534195Sny155746 		(void) printf(" [%s]", EInfo.cap_str);
12544195Sny155746 
12554195Sny155746 	if ((EInfo.type != ET_EXEC) && (EInfo.type != ET_DYN))
12564195Sny155746 		return (0);
12574195Sny155746 
12584195Sny155746 	/* Print if it is dynamically linked */
12594195Sny155746 	if (EInfo.dynamic)
12600Sstevel@tonic-gate 		(void) printf(gettext(", dynamically linked"));
12610Sstevel@tonic-gate 	else
12620Sstevel@tonic-gate 		(void) printf(gettext(", statically linked"));
12630Sstevel@tonic-gate 
12644195Sny155746 	/* Printf it it is stripped */
12654195Sny155746 	if (EInfo.stripped & E_SYMTAB) {
12660Sstevel@tonic-gate 		(void) printf(gettext(", not stripped"));
12674195Sny155746 		if (!(EInfo.stripped & E_DBGINF)) {
12680Sstevel@tonic-gate 			(void) printf(gettext(
12690Sstevel@tonic-gate 			    ", no debugging information available"));
12700Sstevel@tonic-gate 		}
12710Sstevel@tonic-gate 	} else {
12720Sstevel@tonic-gate 		(void) printf(gettext(", stripped"));
12730Sstevel@tonic-gate 	}
12744195Sny155746 
12754195Sny155746 	return (0);
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate /*
12791976Sab196087  * is_rtld_config - If file is a runtime linker config file, prints
12801976Sab196087  * the description and returns True (1). Otherwise, silently returns
12811976Sab196087  * False (0).
12821976Sab196087  */
12831976Sab196087 int
is_rtld_config(void)12841976Sab196087 is_rtld_config(void)
12851976Sab196087 {
12861976Sab196087 	Rtc_id *id;
12871976Sab196087 
12881976Sab196087 	if ((fbsz >= sizeof (*id)) && RTC_ID_TEST(fbuf)) {
12891976Sab196087 		(void) printf(gettext("Runtime Linking Configuration"));
12901976Sab196087 		id = (Rtc_id *) fbuf;
12911976Sab196087 		print_elf_class(id->id_class);
12921976Sab196087 		print_elf_datatype(id->id_data);
12931976Sab196087 		print_elf_machine(id->id_machine);
12941976Sab196087 		(void) printf("\n");
12951976Sab196087 		return (1);
12961976Sab196087 	}
12971976Sab196087 
12981976Sab196087 	return (0);
12991976Sab196087 }
13001976Sab196087 
13011976Sab196087 /*
13020Sstevel@tonic-gate  * lookup -
13030Sstevel@tonic-gate  * Attempts to match one of the strings from a list, 'tab',
13040Sstevel@tonic-gate  * with what is in the file, starting at the current index position 'i'.
13050Sstevel@tonic-gate  * Looks past any initial whitespace and expects whitespace or other
13060Sstevel@tonic-gate  * delimiting characters to follow the matched string.
13070Sstevel@tonic-gate  * A match identifies the file as being 'assembler', 'fortran', 'c', etc.
13080Sstevel@tonic-gate  * Returns 1 for a successful match, 0 otherwise.
13090Sstevel@tonic-gate  */
13100Sstevel@tonic-gate static int
lookup(char ** tab)13110Sstevel@tonic-gate lookup(char **tab)
13120Sstevel@tonic-gate {
13130Sstevel@tonic-gate 	register char	r;
13140Sstevel@tonic-gate 	register int	k, j, l;
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	while (fbuf[i] == ' ' || fbuf[i] == '\t' || fbuf[i] == '\n')
13170Sstevel@tonic-gate 		i++;
13180Sstevel@tonic-gate 	for (j = 0; tab[j] != 0; j++) {
13190Sstevel@tonic-gate 		l = 0;
1320*10843SDave.Plauger@Sun.COM 		for (k = i; ((r = tab[j][l++]) == fbuf[k] && r != '\0'); k++)
1321*10843SDave.Plauger@Sun.COM 			;
13220Sstevel@tonic-gate 		if (r == '\0')
13230Sstevel@tonic-gate 			if (fbuf[k] == ' ' || fbuf[k] == '\n' ||
13240Sstevel@tonic-gate 			    fbuf[k] == '\t' || fbuf[k] == '{' ||
13250Sstevel@tonic-gate 			    fbuf[k] == '/') {
13260Sstevel@tonic-gate 				i = k;
13270Sstevel@tonic-gate 				return (1);
13280Sstevel@tonic-gate 			}
13290Sstevel@tonic-gate 	}
13300Sstevel@tonic-gate 	return (0);
13310Sstevel@tonic-gate }
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate /*
13340Sstevel@tonic-gate  * ccom -
13350Sstevel@tonic-gate  * Increments the current index 'i' into the file buffer 'fbuf' past any
13360Sstevel@tonic-gate  * whitespace lines and C-style comments found, starting at the current
13370Sstevel@tonic-gate  * position of 'i'.  Returns 1 as long as we don't increment i past the
13380Sstevel@tonic-gate  * size of fbuf (fbsz).  Otherwise, returns 0.
13390Sstevel@tonic-gate  */
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate static int
ccom(void)13420Sstevel@tonic-gate ccom(void)
13430Sstevel@tonic-gate {
13440Sstevel@tonic-gate 	register char	cc;
13450Sstevel@tonic-gate 	int		len;
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	while ((cc = fbuf[i]) == ' ' || cc == '\t' || cc == '\n')
13480Sstevel@tonic-gate 		if (i++ >= fbsz)
13490Sstevel@tonic-gate 			return (0);
13500Sstevel@tonic-gate 	if (fbuf[i] == '/' && fbuf[i+1] == '*') {
13510Sstevel@tonic-gate 		i += 2;
13520Sstevel@tonic-gate 		while (fbuf[i] != '*' || fbuf[i+1] != '/') {
13530Sstevel@tonic-gate 			if (fbuf[i] == '\\')
13540Sstevel@tonic-gate 				i++;
13550Sstevel@tonic-gate 			if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
13560Sstevel@tonic-gate 				len = 1;
13570Sstevel@tonic-gate 			i += len;
13580Sstevel@tonic-gate 			if (i >= fbsz)
13590Sstevel@tonic-gate 				return (0);
13600Sstevel@tonic-gate 		}
13610Sstevel@tonic-gate 		if ((i += 2) >= fbsz)
13620Sstevel@tonic-gate 			return (0);
13630Sstevel@tonic-gate 	}
13640Sstevel@tonic-gate 	if (fbuf[i] == '\n')
13650Sstevel@tonic-gate 		if (ccom() == 0)
13660Sstevel@tonic-gate 			return (0);
13670Sstevel@tonic-gate 	return (1);
13680Sstevel@tonic-gate }
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate /*
13710Sstevel@tonic-gate  * ascom -
13720Sstevel@tonic-gate  * Increments the current index 'i' into the file buffer 'fbuf' past
13730Sstevel@tonic-gate  * consecutive assembler program comment lines starting with ASCOMCHAR,
13740Sstevel@tonic-gate  * starting at the current position of 'i'.
13750Sstevel@tonic-gate  * Returns 1 as long as we don't increment i past the
13760Sstevel@tonic-gate  * size of fbuf (fbsz).  Otherwise returns 0.
13770Sstevel@tonic-gate  */
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate static int
ascom(void)13800Sstevel@tonic-gate ascom(void)
13810Sstevel@tonic-gate {
13820Sstevel@tonic-gate 	while (fbuf[i] == ASCOMCHAR) {
13830Sstevel@tonic-gate 		i++;
13840Sstevel@tonic-gate 		while (fbuf[i++] != '\n')
13850Sstevel@tonic-gate 			if (i >= fbsz)
13860Sstevel@tonic-gate 				return (0);
13870Sstevel@tonic-gate 		while (fbuf[i] == '\n')
13880Sstevel@tonic-gate 			if (i++ >= fbsz)
13890Sstevel@tonic-gate 				return (0);
13900Sstevel@tonic-gate 	}
13910Sstevel@tonic-gate 	return (1);
13920Sstevel@tonic-gate }
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate static int
sccs(void)13950Sstevel@tonic-gate sccs(void)
13960Sstevel@tonic-gate {				/* look for "1hddddd" where d is a digit */
13970Sstevel@tonic-gate 	register int j;
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	if (fbuf[0] == 1 && fbuf[1] == 'h') {
14000Sstevel@tonic-gate 		for (j = 2; j <= 6; j++) {
14010Sstevel@tonic-gate 			if (isdigit(fbuf[j]))
14020Sstevel@tonic-gate 				continue;
14030Sstevel@tonic-gate 			else
14040Sstevel@tonic-gate 				return (0);
14050Sstevel@tonic-gate 		}
14060Sstevel@tonic-gate 	} else {
14070Sstevel@tonic-gate 		return (0);
14080Sstevel@tonic-gate 	}
14090Sstevel@tonic-gate 	return (1);
14100Sstevel@tonic-gate }
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate static int
english(char * bp,int n)14130Sstevel@tonic-gate english(char *bp, int n)
14140Sstevel@tonic-gate {
14150Sstevel@tonic-gate #define	NASC 128		/* number of ascii char ?? */
14160Sstevel@tonic-gate 	register int	j, vow, freq, rare, len;
14170Sstevel@tonic-gate 	register int	badpun = 0, punct = 0;
14180Sstevel@tonic-gate 	int	ct[NASC];
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	if (n < 50)
14210Sstevel@tonic-gate 		return (0); /* no point in statistics on squibs */
14220Sstevel@tonic-gate 	for (j = 0; j < NASC; j++)
14230Sstevel@tonic-gate 		ct[j] = 0;
14240Sstevel@tonic-gate 	for (j = 0; j < n; j += len) {
14250Sstevel@tonic-gate 		if ((unsigned char)bp[j] < NASC)
14260Sstevel@tonic-gate 			ct[bp[j]|040]++;
14270Sstevel@tonic-gate 		switch (bp[j]) {
14280Sstevel@tonic-gate 		case '.':
14290Sstevel@tonic-gate 		case ',':
14300Sstevel@tonic-gate 		case ')':
14310Sstevel@tonic-gate 		case '%':
14320Sstevel@tonic-gate 		case ';':
14330Sstevel@tonic-gate 		case ':':
14340Sstevel@tonic-gate 		case '?':
14350Sstevel@tonic-gate 			punct++;
14360Sstevel@tonic-gate 			if (j < n-1 && bp[j+1] != ' ' && bp[j+1] != '\n')
14370Sstevel@tonic-gate 				badpun++;
14380Sstevel@tonic-gate 		}
14390Sstevel@tonic-gate 		if ((len = mblen(&bp[j], MB_CUR_MAX)) <= 0)
14400Sstevel@tonic-gate 			len = 1;
14410Sstevel@tonic-gate 	}
14420Sstevel@tonic-gate 	if (badpun*5 > punct)
14430Sstevel@tonic-gate 		return (0);
14440Sstevel@tonic-gate 	vow = ct['a'] + ct['e'] + ct['i'] + ct['o'] + ct['u'];
14450Sstevel@tonic-gate 	freq = ct['e'] + ct['t'] + ct['a'] + ct['i'] + ct['o'] + ct['n'];
14460Sstevel@tonic-gate 	rare = ct['v'] + ct['j'] + ct['k'] + ct['q'] + ct['x'] + ct['z'];
14470Sstevel@tonic-gate 	if (2*ct[';'] > ct['e'])
14480Sstevel@tonic-gate 		return (0);
14490Sstevel@tonic-gate 	if ((ct['>'] + ct['<'] + ct['/']) > ct['e'])
14500Sstevel@tonic-gate 		return (0);	/* shell file test */
14510Sstevel@tonic-gate 	return (vow * 5 >= n - ct[' '] && freq >= 10 * rare);
14520Sstevel@tonic-gate }
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate static int
shellscript(char buf[],struct stat64 * sb)14560Sstevel@tonic-gate shellscript(char buf[], struct stat64 *sb)
14570Sstevel@tonic-gate {
14580Sstevel@tonic-gate 	char *tp, *cp, *xp, *up, *gp;
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	cp = strchr(buf, '\n');
14610Sstevel@tonic-gate 	if (cp == NULL || cp - fbuf > fbsz)
14620Sstevel@tonic-gate 		return (0);
14630Sstevel@tonic-gate 	for (tp = buf; tp != cp && isspace((unsigned char)*tp); tp++)
14640Sstevel@tonic-gate 		if (!isascii(*tp))
14650Sstevel@tonic-gate 			return (0);
14660Sstevel@tonic-gate 	for (xp = tp; tp != cp && !isspace((unsigned char)*tp); tp++)
14670Sstevel@tonic-gate 		if (!isascii(*tp))
14680Sstevel@tonic-gate 			return (0);
14690Sstevel@tonic-gate 	if (tp == xp)
14700Sstevel@tonic-gate 		return (0);
14710Sstevel@tonic-gate 	if (sb->st_mode & S_ISUID)
14720Sstevel@tonic-gate 		up = gettext("set-uid ");
14730Sstevel@tonic-gate 	else
14740Sstevel@tonic-gate 		up = "";
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	if (sb->st_mode & S_ISGID)
14770Sstevel@tonic-gate 		gp = gettext("set-gid ");
14780Sstevel@tonic-gate 	else
14790Sstevel@tonic-gate 		gp = "";
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	if (strncmp(xp, "/bin/sh", tp - xp) == 0)
14820Sstevel@tonic-gate 		xp = gettext("shell");
14830Sstevel@tonic-gate 	else if (strncmp(xp, "/bin/csh", tp - xp) == 0)
14840Sstevel@tonic-gate 		xp = gettext("c-shell");
14850Sstevel@tonic-gate 	else if (strncmp(xp, "/usr/sbin/dtrace", tp - xp) == 0)
14860Sstevel@tonic-gate 		xp = gettext("DTrace");
14870Sstevel@tonic-gate 	else
14880Sstevel@tonic-gate 		*tp = '\0';
14890Sstevel@tonic-gate 	/*
14900Sstevel@tonic-gate 	 * TRANSLATION_NOTE
14910Sstevel@tonic-gate 	 * This message is printed by file command for shell scripts.
14920Sstevel@tonic-gate 	 * The first %s is for the translation for "set-uid " (if the script
14930Sstevel@tonic-gate 	 *   has the set-uid bit set), or is for an empty string (if the
14940Sstevel@tonic-gate 	 *   script does not have the set-uid bit set).
14950Sstevel@tonic-gate 	 * Similarly, the second %s is for the translation for "set-gid ",
14960Sstevel@tonic-gate 	 *   or is for an empty string.
14970Sstevel@tonic-gate 	 * The third %s is for the translation for either: "shell", "c-shell",
14980Sstevel@tonic-gate 	 *   or "DTrace", or is for the pathname of the program the script
14990Sstevel@tonic-gate 	 *   executes.
15000Sstevel@tonic-gate 	 */
15010Sstevel@tonic-gate 	(void) printf(gettext("%s%sexecutable %s script\n"), up, gp, xp);
15020Sstevel@tonic-gate 	return (1);
15030Sstevel@tonic-gate }
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate static int
get_door_target(char * file,char * buf,size_t bufsize)15060Sstevel@tonic-gate get_door_target(char *file, char *buf, size_t bufsize)
15070Sstevel@tonic-gate {
15080Sstevel@tonic-gate 	int fd;
15090Sstevel@tonic-gate 	door_info_t di;
15100Sstevel@tonic-gate 	psinfo_t psinfo;
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	if ((fd = open64(file, O_RDONLY)) < 0 ||
15130Sstevel@tonic-gate 	    door_info(fd, &di) != 0) {
15140Sstevel@tonic-gate 		if (fd >= 0)
15150Sstevel@tonic-gate 			(void) close(fd);
15160Sstevel@tonic-gate 		return (-1);
15170Sstevel@tonic-gate 	}
15180Sstevel@tonic-gate 	(void) close(fd);
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	(void) sprintf(buf, "/proc/%ld/psinfo", di.di_target);
15210Sstevel@tonic-gate 	if ((fd = open64(buf, O_RDONLY)) < 0 ||
15220Sstevel@tonic-gate 	    read(fd, &psinfo, sizeof (psinfo)) != sizeof (psinfo)) {
15230Sstevel@tonic-gate 		if (fd >= 0)
15240Sstevel@tonic-gate 			(void) close(fd);
15250Sstevel@tonic-gate 		return (-1);
15260Sstevel@tonic-gate 	}
15270Sstevel@tonic-gate 	(void) close(fd);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	(void) snprintf(buf, bufsize, "%s[%ld]", psinfo.pr_fname, di.di_target);
15300Sstevel@tonic-gate 	return (0);
15310Sstevel@tonic-gate }
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate /*
15340Sstevel@tonic-gate  * ZIP file header information
15350Sstevel@tonic-gate  */
15360Sstevel@tonic-gate #define	SIGSIZ		4
15370Sstevel@tonic-gate #define	LOCSIG		"PK\003\004"
15380Sstevel@tonic-gate #define	LOCHDRSIZ	30
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate #define	CH(b, n)	(((unsigned char *)(b))[n])
15410Sstevel@tonic-gate #define	SH(b, n)	(CH(b, n) | (CH(b, n+1) << 8))
15420Sstevel@tonic-gate #define	LG(b, n)	(SH(b, n) | (SH(b, n+2) << 16))
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate #define	LOCNAM(b)	(SH(b, 26))	/* filename size */
15450Sstevel@tonic-gate #define	LOCEXT(b)	(SH(b, 28))	/* extra field size */
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate #define	XFHSIZ		4		/* header id, data size */
15480Sstevel@tonic-gate #define	XFHID(b)	(SH(b, 0))	/* extract field header id */
15490Sstevel@tonic-gate #define	XFDATASIZ(b)	(SH(b, 2))	/* extract field data size */
15500Sstevel@tonic-gate #define	XFJAVASIG	0xcafe		/* java executables */
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate static int
zipfile(char * fbuf,int fd)15530Sstevel@tonic-gate zipfile(char *fbuf, int fd)
15540Sstevel@tonic-gate {
15550Sstevel@tonic-gate 	off_t xoff, xoff_end;
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	if (strncmp(fbuf, LOCSIG, SIGSIZ) != 0)
15580Sstevel@tonic-gate 		return (0);
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 	xoff = LOCHDRSIZ + LOCNAM(fbuf);
15610Sstevel@tonic-gate 	xoff_end = xoff + LOCEXT(fbuf);
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	while (xoff < xoff_end) {
15640Sstevel@tonic-gate 		char xfhdr[XFHSIZ];
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 		if (pread(fd, xfhdr, XFHSIZ, xoff) != XFHSIZ)
15670Sstevel@tonic-gate 			break;
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 		if (XFHID(xfhdr) == XFJAVASIG) {
15706078Sny155746 			(void) printf("%s\n", gettext("java archive file"));
15710Sstevel@tonic-gate 			return (1);
15720Sstevel@tonic-gate 		}
15730Sstevel@tonic-gate 		xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr);
15740Sstevel@tonic-gate 	}
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	/*
15770Sstevel@tonic-gate 	 * We could just print "ZIP archive" here.
15780Sstevel@tonic-gate 	 *
15790Sstevel@tonic-gate 	 * However, customers may be using their own entries in
15800Sstevel@tonic-gate 	 * /etc/magic to distinguish one kind of ZIP file from another, so
15810Sstevel@tonic-gate 	 * let's defer the printing of "ZIP archive" to there.
15820Sstevel@tonic-gate 	 */
15830Sstevel@tonic-gate 	return (0);
15840Sstevel@tonic-gate }
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate static int
is_crash_dump(const char * buf,int fd)15870Sstevel@tonic-gate is_crash_dump(const char *buf, int fd)
15880Sstevel@tonic-gate {
15890Sstevel@tonic-gate 	/* LINTED: pointer cast may result in improper alignment */
15900Sstevel@tonic-gate 	const dumphdr_t *dhp = (const dumphdr_t *)buf;
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	/*
15930Sstevel@tonic-gate 	 * The current DUMP_MAGIC string covers Solaris 7 and later releases.
15940Sstevel@tonic-gate 	 * The utsname struct is only present in dumphdr_t's with dump_version
15950Sstevel@tonic-gate 	 * greater than or equal to 9.
15960Sstevel@tonic-gate 	 */
15970Sstevel@tonic-gate 	if (dhp->dump_magic == DUMP_MAGIC) {
15980Sstevel@tonic-gate 		print_dumphdr(fd, dhp, return_uint32, NATIVE_ISA);
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	} else if (dhp->dump_magic == swap_uint32(DUMP_MAGIC)) {
16010Sstevel@tonic-gate 		print_dumphdr(fd, dhp, swap_uint32, OTHER_ISA);
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 	} else if (dhp->dump_magic == OLD_DUMP_MAGIC ||
16040Sstevel@tonic-gate 	    dhp->dump_magic == swap_uint32(OLD_DUMP_MAGIC)) {
16050Sstevel@tonic-gate 		char *isa = (dhp->dump_magic == OLD_DUMP_MAGIC ?
16060Sstevel@tonic-gate 		    NATIVE_ISA : OTHER_ISA);
16070Sstevel@tonic-gate 		(void) printf(gettext("SunOS 32-bit %s crash dump\n"), isa);
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	} else {
16100Sstevel@tonic-gate 		return (0);
16110Sstevel@tonic-gate 	}
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 	return (1);
16140Sstevel@tonic-gate }
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate static void
print_dumphdr(const int fd,const dumphdr_t * dhp,uint32_t (* swap)(uint32_t),const char * isa)16170Sstevel@tonic-gate print_dumphdr(const int fd, const dumphdr_t *dhp, uint32_t (*swap)(uint32_t),
16180Sstevel@tonic-gate     const char *isa)
16190Sstevel@tonic-gate {
16200Sstevel@tonic-gate 	dumphdr_t dh;
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	/*
16230Sstevel@tonic-gate 	 * A dumphdr_t is bigger than FBSZ, so we have to manually read the
16240Sstevel@tonic-gate 	 * rest of it.
16250Sstevel@tonic-gate 	 */
16260Sstevel@tonic-gate 	if (swap(dhp->dump_version) > 8 && pread(fd, &dh, sizeof (dumphdr_t),
16270Sstevel@tonic-gate 	    (off_t)0) == sizeof (dumphdr_t)) {
1628*10843SDave.Plauger@Sun.COM 		const char *c = swap(dh.dump_flags) & DF_COMPRESSED ?
1629*10843SDave.Plauger@Sun.COM 		    "compressed " : "";
1630*10843SDave.Plauger@Sun.COM 		const char *l = swap(dh.dump_flags) & DF_LIVE ?
1631*10843SDave.Plauger@Sun.COM 		    "live" : "crash";
1632*10843SDave.Plauger@Sun.COM 
16330Sstevel@tonic-gate 		(void) printf(gettext(
1634*10843SDave.Plauger@Sun.COM 		    "%s %s %s %u-bit %s %s%s dump from '%s'\n"),
16350Sstevel@tonic-gate 		    dh.dump_utsname.sysname, dh.dump_utsname.release,
16360Sstevel@tonic-gate 		    dh.dump_utsname.version, swap(dh.dump_wordsize), isa,
1637*10843SDave.Plauger@Sun.COM 		    c, l, dh.dump_utsname.nodename);
16380Sstevel@tonic-gate 	} else {
16390Sstevel@tonic-gate 		(void) printf(gettext("SunOS %u-bit %s crash dump\n"),
16400Sstevel@tonic-gate 		    swap(dhp->dump_wordsize), isa);
16410Sstevel@tonic-gate 	}
16420Sstevel@tonic-gate }
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate static void
usage(void)16450Sstevel@tonic-gate usage(void)
16460Sstevel@tonic-gate {
16470Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
1648*10843SDave.Plauger@Sun.COM 	    "usage: file [-dh] [-M mfile] [-m mfile] [-f ffile] file ...\n"
1649*10843SDave.Plauger@Sun.COM 	    "       file [-dh] [-M mfile] [-m mfile] -f ffile\n"
1650*10843SDave.Plauger@Sun.COM 	    "       file -i [-h] [-f ffile] file ...\n"
1651*10843SDave.Plauger@Sun.COM 	    "       file -i [-h] -f ffile\n"
1652*10843SDave.Plauger@Sun.COM 	    "       file -c [-d] [-M mfile] [-m mfile]\n"));
16530Sstevel@tonic-gate 	exit(2);
16540Sstevel@tonic-gate }
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate static uint32_t
swap_uint32(uint32_t in)16570Sstevel@tonic-gate swap_uint32(uint32_t in)
16580Sstevel@tonic-gate {
16590Sstevel@tonic-gate 	uint32_t out;
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	out = (in & 0x000000ff) << 24;
16620Sstevel@tonic-gate 	out |= (in & 0x0000ff00) << 8; /* >> 8 << 16 */
16630Sstevel@tonic-gate 	out |= (in & 0x00ff0000) >> 8; /* >> 16 << 8 */
16640Sstevel@tonic-gate 	out |= (in & 0xff000000) >> 24;
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	return (out);
16670Sstevel@tonic-gate }
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate static uint32_t
return_uint32(uint32_t in)16700Sstevel@tonic-gate return_uint32(uint32_t in)
16710Sstevel@tonic-gate {
16720Sstevel@tonic-gate 	return (in);
16730Sstevel@tonic-gate }
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate /*
16760Sstevel@tonic-gate  * Check if str is in the string list str_list.
16770Sstevel@tonic-gate  */
16784195Sny155746 int
is_in_list(char * str)16794195Sny155746 is_in_list(char *str)
16800Sstevel@tonic-gate {
16810Sstevel@tonic-gate 	int i;
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	/*
16840Sstevel@tonic-gate 	 * Only need to compare the strlen(str_list[i]) bytes.
16850Sstevel@tonic-gate 	 * That way .stab will match on .stab* sections, and
16860Sstevel@tonic-gate 	 * .debug will match on .debug* sections.
16870Sstevel@tonic-gate 	 */
16884195Sny155746 	for (i = 0; debug_sections[i] != NULL; i++) {
16894195Sny155746 		if (strncmp(debug_sections[i], str,
1690*10843SDave.Plauger@Sun.COM 		    strlen(debug_sections[i])) == 0) {
16910Sstevel@tonic-gate 			return (1);
16920Sstevel@tonic-gate 		}
16930Sstevel@tonic-gate 	}
16940Sstevel@tonic-gate 	return (0);
16950Sstevel@tonic-gate }
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate /*
16980Sstevel@tonic-gate  * default_magic -
16990Sstevel@tonic-gate  *	allocate space for and create the default magic file
17000Sstevel@tonic-gate  *	name string.
17010Sstevel@tonic-gate  */
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate static void
default_magic(void)17040Sstevel@tonic-gate default_magic(void)
17050Sstevel@tonic-gate {
17060Sstevel@tonic-gate 	const char *msg_locale = setlocale(LC_MESSAGES, NULL);
17070Sstevel@tonic-gate 	struct stat	statbuf;
17080Sstevel@tonic-gate 
17093466Srie 	if ((dfile = malloc(strlen(msg_locale) + 35)) == NULL) {
17103466Srie 		int err = errno;
17113466Srie 		(void) fprintf(stderr, gettext("%s: malloc failed: %s\n"),
17123466Srie 		    File, strerror(err));
17130Sstevel@tonic-gate 		exit(2);
17140Sstevel@tonic-gate 	}
17150Sstevel@tonic-gate 	(void) snprintf(dfile, strlen(msg_locale) + 35,
17160Sstevel@tonic-gate 	    "/usr/lib/locale/%s/LC_MESSAGES/magic", msg_locale);
17170Sstevel@tonic-gate 	if (stat(dfile, &statbuf) != 0) {
17180Sstevel@tonic-gate 		(void) strcpy(dfile, "/etc/magic");
17190Sstevel@tonic-gate 	}
17200Sstevel@tonic-gate }
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate /*
17230Sstevel@tonic-gate  * add_to_mlist -
17240Sstevel@tonic-gate  *	Add the given magic_file filename string to the list of magic
17250Sstevel@tonic-gate  *	files (mlist).  This list of files will later be examined, and
17260Sstevel@tonic-gate  *	each magic file's entries will be added in order to
17270Sstevel@tonic-gate  *	the mtab table.
17280Sstevel@tonic-gate  *
17290Sstevel@tonic-gate  *	The first flag is set to 1 to add to the first list, mlist1.
17300Sstevel@tonic-gate  *	The first flag is set to 0 to add to the second list, mlist2.
17310Sstevel@tonic-gate  */
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate static void
add_to_mlist(char * magic_file,int first)17340Sstevel@tonic-gate add_to_mlist(char *magic_file, int first)
17350Sstevel@tonic-gate {
17360Sstevel@tonic-gate 	char	**mlist;	/* ordered list of magic files */
17370Sstevel@tonic-gate 	size_t	mlist_sz;	/* number of pointers allocated  for mlist */
17380Sstevel@tonic-gate 	char	**mlistp;	/* next entry in mlist */
17390Sstevel@tonic-gate 	size_t mlistp_off;
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 	if (first) {
17420Sstevel@tonic-gate 		mlist = mlist1;
17430Sstevel@tonic-gate 		mlist_sz = mlist1_sz;
17440Sstevel@tonic-gate 		mlistp = mlist1p;
17450Sstevel@tonic-gate 	} else {
17460Sstevel@tonic-gate 		mlist = mlist2;
17470Sstevel@tonic-gate 		mlist_sz = mlist2_sz;
17480Sstevel@tonic-gate 		mlistp = mlist2p;
17490Sstevel@tonic-gate 	}
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate 	if (mlist == NULL) {	/* initial mlist allocation */
17523466Srie 		if ((mlist = calloc(MLIST_SZ, sizeof (char *))) == NULL) {
17533466Srie 			int err = errno;
17543466Srie 			(void) fprintf(stderr, gettext("%s: malloc "
17553466Srie 			    "failed: %s\n"), File, strerror(err));
17560Sstevel@tonic-gate 			exit(2);
17570Sstevel@tonic-gate 		}
17580Sstevel@tonic-gate 		mlist_sz = MLIST_SZ;
17590Sstevel@tonic-gate 		mlistp = mlist;
17600Sstevel@tonic-gate 	}
17610Sstevel@tonic-gate 	if ((mlistp - mlist) >= mlist_sz) {
17620Sstevel@tonic-gate 		mlistp_off = mlistp - mlist;
17630Sstevel@tonic-gate 		mlist_sz *= 2;
17643466Srie 		if ((mlist = realloc(mlist,
17650Sstevel@tonic-gate 		    mlist_sz * sizeof (char *))) == NULL) {
17663466Srie 			int err = errno;
17673466Srie 			(void) fprintf(stderr, gettext("%s: malloc "
17683466Srie 			    "failed: %s\n"), File, strerror(err));
17690Sstevel@tonic-gate 			exit(2);
17700Sstevel@tonic-gate 		}
17710Sstevel@tonic-gate 		mlistp = mlist + mlistp_off;
17720Sstevel@tonic-gate 	}
17730Sstevel@tonic-gate 	/*
17740Sstevel@tonic-gate 	 * now allocate memory for and copy the
17750Sstevel@tonic-gate 	 * magic file name string
17760Sstevel@tonic-gate 	 */
17770Sstevel@tonic-gate 	if ((*mlistp = malloc(strlen(magic_file) + 1)) == NULL) {
17783466Srie 		int err = errno;
17793466Srie 		(void) fprintf(stderr, gettext("%s: malloc failed: %s\n"),
17803466Srie 		    File, strerror(err));
17810Sstevel@tonic-gate 		exit(2);
17820Sstevel@tonic-gate 	}
17830Sstevel@tonic-gate 	(void) strlcpy(*mlistp, magic_file, strlen(magic_file) + 1);
17840Sstevel@tonic-gate 	mlistp++;
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 	if (first) {
17870Sstevel@tonic-gate 		mlist1 = mlist;
17880Sstevel@tonic-gate 		mlist1_sz = mlist_sz;
17890Sstevel@tonic-gate 		mlist1p = mlistp;
17900Sstevel@tonic-gate 	} else {
17910Sstevel@tonic-gate 		mlist2 = mlist;
17920Sstevel@tonic-gate 		mlist2_sz = mlist_sz;
17930Sstevel@tonic-gate 		mlist2p = mlistp;
17940Sstevel@tonic-gate 	}
17950Sstevel@tonic-gate }
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate static void
fd_cleanup(void)17980Sstevel@tonic-gate fd_cleanup(void)
17990Sstevel@tonic-gate {
18000Sstevel@tonic-gate 	if (ifd != -1) {
18010Sstevel@tonic-gate 		(void) close(ifd);
18020Sstevel@tonic-gate 		ifd = -1;
18030Sstevel@tonic-gate 	}
18040Sstevel@tonic-gate 	if (elffd != -1) {
18050Sstevel@tonic-gate 		(void) close(elffd);
18060Sstevel@tonic-gate 		elffd = -1;
18070Sstevel@tonic-gate 	}
18080Sstevel@tonic-gate }
1809