xref: /titanic_51/usr/src/cmd/file/file.c (revision c4397e6141e60895680f3bd2550f9f788cf82ade)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c13de8f6Sab196087  * Common Development and Distribution License (the "License").
6c13de8f6Sab196087  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
267c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
29ca3e8d88SDave Plauger  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
307c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
31*c4397e61SRobert Mustacchi  * Copyright (c) 2018, Joyent, Inc.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #define	_LARGEFILE64_SOURCE
357c478bd9Sstevel@tonic-gate 
36c13de8f6Sab196087 /* Get definitions for the relocation types supported. */
37c13de8f6Sab196087 #define	ELF_TARGET_ALL
38c13de8f6Sab196087 
397c478bd9Sstevel@tonic-gate #include <ctype.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <fcntl.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <libelf.h>
457c478bd9Sstevel@tonic-gate #include <stdlib.h>
467c478bd9Sstevel@tonic-gate #include <limits.h>
477c478bd9Sstevel@tonic-gate #include <locale.h>
487c478bd9Sstevel@tonic-gate #include <wctype.h>
497c478bd9Sstevel@tonic-gate #include <string.h>
507c478bd9Sstevel@tonic-gate #include <errno.h>
517c478bd9Sstevel@tonic-gate #include <door.h>
527c478bd9Sstevel@tonic-gate #include <sys/param.h>
537c478bd9Sstevel@tonic-gate #include <sys/types.h>
547c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
557c478bd9Sstevel@tonic-gate #include <sys/stat.h>
567c478bd9Sstevel@tonic-gate #include <sys/elf.h>
577c478bd9Sstevel@tonic-gate #include <procfs.h>
587c478bd9Sstevel@tonic-gate #include <sys/core.h>
597c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
607c478bd9Sstevel@tonic-gate #include <netinet/in.h>
617c478bd9Sstevel@tonic-gate #include <gelf.h>
627c478bd9Sstevel@tonic-gate #include <elfcap.h>
63c13de8f6Sab196087 #include <sgsrtcid.h>
647c478bd9Sstevel@tonic-gate #include "file.h"
65c2c65e21Sny155746 #include "elf_read.h"
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  *	Misc
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define	FBSZ		512
727c478bd9Sstevel@tonic-gate #define	MLIST_SZ	12
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * The 0x8FCA0102 magic string was used in crash dumps generated by releases
767c478bd9Sstevel@tonic-gate  * prior to Solaris 7.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate #define	OLD_DUMP_MAGIC	0x8FCA0102
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #if defined(__sparc)
817c478bd9Sstevel@tonic-gate #define	NATIVE_ISA	"SPARC"
827c478bd9Sstevel@tonic-gate #define	OTHER_ISA	"Intel"
837c478bd9Sstevel@tonic-gate #else
847c478bd9Sstevel@tonic-gate #define	NATIVE_ISA	"Intel"
857c478bd9Sstevel@tonic-gate #define	OTHER_ISA	"SPARC"
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /* Assembly language comment char */
897c478bd9Sstevel@tonic-gate #ifdef pdp11
907c478bd9Sstevel@tonic-gate #define	ASCOMCHAR '/'
917c478bd9Sstevel@tonic-gate #else
927c478bd9Sstevel@tonic-gate #define	ASCOMCHAR '!'
937c478bd9Sstevel@tonic-gate #endif
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #pragma	align	16(fbuf)
967c478bd9Sstevel@tonic-gate static char	fbuf[FBSZ];
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * Magic file variables
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate static intmax_t maxmagicoffset;
1027c478bd9Sstevel@tonic-gate static intmax_t tmpmax;
1037c478bd9Sstevel@tonic-gate static char	*magicbuf;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static char	*dfile;
1067c478bd9Sstevel@tonic-gate static char	*troff[] = {	/* new troff intermediate lang */
1077c478bd9Sstevel@tonic-gate 		"x", "T", "res", "init", "font", "202", "V0", "p1", 0};
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static char	*fort[] = {			/* FORTRAN */
1107c478bd9Sstevel@tonic-gate 		"function", "subroutine", "common", "dimension", "block",
1117c478bd9Sstevel@tonic-gate 		"integer", "real", "data", "double",
1127c478bd9Sstevel@tonic-gate 		"FUNCTION", "SUBROUTINE", "COMMON", "DIMENSION", "BLOCK",
1137c478bd9Sstevel@tonic-gate 		"INTEGER", "REAL", "DATA", "DOUBLE", 0};
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static char	*asc[] = {		/* Assembler Commands */
1167c478bd9Sstevel@tonic-gate 		"sys", "mov", "tst", "clr", "jmp", "cmp", "set", "inc",
1177c478bd9Sstevel@tonic-gate 		"dec", 0};
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate static char	*c[] = {			/* C Language */
1207c478bd9Sstevel@tonic-gate 		"int", "char", "float", "double", "short", "long", "unsigned",
1217c478bd9Sstevel@tonic-gate 		"register", "static", "struct", "extern", 0};
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static char	*as[] = {	/* Assembler Pseudo Ops, prepended with '.' */
1247c478bd9Sstevel@tonic-gate 		"globl", "global", "ident", "file", "byte", "even",
1257c478bd9Sstevel@tonic-gate 		"text", "data", "bss", "comm", 0};
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * The line and debug section names are used by the strip command.
1297c478bd9Sstevel@tonic-gate  * Any changes in the strip implementation need to be reflected here.
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate static char	*debug_sections[] = { /* Debug sections in a ELF file */
1327c478bd9Sstevel@tonic-gate 		".debug", ".stab", ".dwarf", ".line", NULL};
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /* start for MB env */
1357c478bd9Sstevel@tonic-gate static wchar_t	wchar;
1367c478bd9Sstevel@tonic-gate static int	length;
1377c478bd9Sstevel@tonic-gate static int	IS_ascii;
1387c478bd9Sstevel@tonic-gate static int	Max;
1397c478bd9Sstevel@tonic-gate /* end for MB env */
1407c478bd9Sstevel@tonic-gate static int	i;	/* global index into first 'fbsz' bytes of file */
1417c478bd9Sstevel@tonic-gate static int	fbsz;
1427c478bd9Sstevel@tonic-gate static int	ifd = -1;
1437c478bd9Sstevel@tonic-gate static int	elffd = -1;
1447c478bd9Sstevel@tonic-gate static int	tret;
1457c478bd9Sstevel@tonic-gate static int	hflg;
1467c478bd9Sstevel@tonic-gate static int	dflg;
1477c478bd9Sstevel@tonic-gate static int	mflg;
1487c478bd9Sstevel@tonic-gate static int	M_flg;
1497c478bd9Sstevel@tonic-gate static int	iflg;
1507c478bd9Sstevel@tonic-gate static struct stat64	mbuf;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static char	**mlist1;	/* 1st ordered list of magic files */
1537c478bd9Sstevel@tonic-gate static char	**mlist2;	/* 2nd ordered list of magic files */
1547c478bd9Sstevel@tonic-gate static size_t	mlist1_sz;	/* number of ptrs allocated for mlist1 */
1557c478bd9Sstevel@tonic-gate static size_t	mlist2_sz;	/* number of ptrs allocated for mlist2 */
1567c478bd9Sstevel@tonic-gate static char	**mlist1p;	/* next entry in mlist1 */
1577c478bd9Sstevel@tonic-gate static char	**mlist2p;	/* next entry in mlist2 */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static ssize_t	mread;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static void ar_coff_or_aout(int ifd);
1627c478bd9Sstevel@tonic-gate static int type(char *file);
1639a411307Srie static int def_position_tests(char *file);
1647c478bd9Sstevel@tonic-gate static void def_context_tests(void);
1657c478bd9Sstevel@tonic-gate static int troffint(char *bp, int n);
1667c478bd9Sstevel@tonic-gate static int lookup(char **tab);
1677c478bd9Sstevel@tonic-gate static int ccom(void);
1687c478bd9Sstevel@tonic-gate static int ascom(void);
1697c478bd9Sstevel@tonic-gate static int sccs(void);
1707c478bd9Sstevel@tonic-gate static int english(char *bp, int n);
1717c478bd9Sstevel@tonic-gate static int shellscript(char buf[], struct stat64 *sb);
172c2c65e21Sny155746 static int elf_check(char *file);
1737c478bd9Sstevel@tonic-gate static int get_door_target(char *, char *, size_t);
1747c478bd9Sstevel@tonic-gate static int zipfile(char *, int);
1757c478bd9Sstevel@tonic-gate static int is_crash_dump(const char *, int);
1767c478bd9Sstevel@tonic-gate static void print_dumphdr(const int, const dumphdr_t *, uint32_t (*)(uint32_t),
1777c478bd9Sstevel@tonic-gate     const char *);
1787c478bd9Sstevel@tonic-gate static uint32_t swap_uint32(uint32_t);
1797c478bd9Sstevel@tonic-gate static uint32_t return_uint32(uint32_t);
1807c478bd9Sstevel@tonic-gate static void usage(void);
1817c478bd9Sstevel@tonic-gate static void default_magic(void);
1827c478bd9Sstevel@tonic-gate static void add_to_mlist(char *, int);
1837c478bd9Sstevel@tonic-gate static void fd_cleanup(void);
184c13de8f6Sab196087 static int is_rtld_config(void);
1857c478bd9Sstevel@tonic-gate 
186c2c65e21Sny155746 /* from elf_read.c */
187c2c65e21Sny155746 int elf_read32(int elffd, Elf_Info *EInfo);
188c2c65e21Sny155746 int elf_read64(int elffd, Elf_Info *EInfo);
189c2c65e21Sny155746 
1907c478bd9Sstevel@tonic-gate #ifdef XPG4
1917c478bd9Sstevel@tonic-gate 	/* SUSv3 requires a single <space> after the colon */
1927c478bd9Sstevel@tonic-gate #define	prf(x)	(void) printf("%s: ", x);
1937c478bd9Sstevel@tonic-gate #else	/* !XPG4 */
1947c478bd9Sstevel@tonic-gate #define	prf(x)	(void) printf("%s:%s", x, (int)strlen(x) > 6 ? "\t" : "\t\t");
1957c478bd9Sstevel@tonic-gate #endif	/* XPG4 */
1967c478bd9Sstevel@tonic-gate 
1979a411307Srie /*
1989a411307Srie  * Static program identifier - used to prevent localization of the name "file"
1999a411307Srie  * within individual error messages.
2009a411307Srie  */
2019a411307Srie const char *File = "file";
2029a411307Srie 
2037c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)2047c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	char	*p;
2077c478bd9Sstevel@tonic-gate 	int	ch;
2087c478bd9Sstevel@tonic-gate 	FILE	*fl;
2096e987ca0SDavid Höppner 	int	bflg = 0;
2107c478bd9Sstevel@tonic-gate 	int	cflg = 0;
2117c478bd9Sstevel@tonic-gate 	int	eflg = 0;
2127c478bd9Sstevel@tonic-gate 	int	fflg = 0;
2137c478bd9Sstevel@tonic-gate 	char	*ap = NULL;
2147c478bd9Sstevel@tonic-gate 	int	pathlen;
2157c478bd9Sstevel@tonic-gate 	char	**filep;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2187c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2197c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2207c478bd9Sstevel@tonic-gate #endif
2217c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2227c478bd9Sstevel@tonic-gate 
2236e987ca0SDavid Höppner 	while ((ch = getopt(argc, argv, "M:bcdf:him:")) != EOF) {
2247c478bd9Sstevel@tonic-gate 		switch (ch) {
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		case 'M':
2277c478bd9Sstevel@tonic-gate 			add_to_mlist(optarg, !dflg);
2287c478bd9Sstevel@tonic-gate 			M_flg++;
2297c478bd9Sstevel@tonic-gate 			break;
2307c478bd9Sstevel@tonic-gate 
2316e987ca0SDavid Höppner 		case 'b':
2326e987ca0SDavid Höppner 			bflg++;
2336e987ca0SDavid Höppner 			break;
2346e987ca0SDavid Höppner 
2357c478bd9Sstevel@tonic-gate 		case 'c':
2367c478bd9Sstevel@tonic-gate 			cflg++;
2377c478bd9Sstevel@tonic-gate 			break;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		case 'd':
2407c478bd9Sstevel@tonic-gate 			if (!dflg) {
2417c478bd9Sstevel@tonic-gate 				default_magic();
2427c478bd9Sstevel@tonic-gate 				add_to_mlist(dfile, 0);
2437c478bd9Sstevel@tonic-gate 				dflg++;
2447c478bd9Sstevel@tonic-gate 			}
2457c478bd9Sstevel@tonic-gate 			break;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 		case 'f':
2487c478bd9Sstevel@tonic-gate 			fflg++;
2499a411307Srie 			errno = 0;
2507c478bd9Sstevel@tonic-gate 			if ((fl = fopen(optarg, "r")) == NULL) {
2519a411307Srie 				int err = errno;
2529a411307Srie 				(void) fprintf(stderr, gettext("%s: cannot "
2539a411307Srie 				    "open file %s: %s\n"), File, optarg,
2549a411307Srie 				    err ? strerror(err) : "");
2557c478bd9Sstevel@tonic-gate 				usage();
2567c478bd9Sstevel@tonic-gate 			}
2577c478bd9Sstevel@tonic-gate 			pathlen = pathconf("/", _PC_PATH_MAX);
2587c478bd9Sstevel@tonic-gate 			if (pathlen == -1) {
2599a411307Srie 				int err = errno;
2609a411307Srie 				(void) fprintf(stderr, gettext("%s: cannot "
2619a411307Srie 				    "determine maximum path length: %s\n"),
2629a411307Srie 				    File, strerror(err));
2637c478bd9Sstevel@tonic-gate 				exit(1);
2647c478bd9Sstevel@tonic-gate 			}
2657c478bd9Sstevel@tonic-gate 			pathlen += 2; /* for null and newline in fgets */
2669a411307Srie 			if ((ap = malloc(pathlen * sizeof (char))) == NULL) {
2679a411307Srie 				int err = errno;
2689a411307Srie 				(void) fprintf(stderr, gettext("%s: malloc "
2699a411307Srie 				    "failed: %s\n"), File, strerror(err));
2709a411307Srie 				exit(2);
2717c478bd9Sstevel@tonic-gate 			}
2727c478bd9Sstevel@tonic-gate 			break;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		case 'h':
2757c478bd9Sstevel@tonic-gate 			hflg++;
2767c478bd9Sstevel@tonic-gate 			break;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		case 'i':
2797c478bd9Sstevel@tonic-gate 			iflg++;
2807c478bd9Sstevel@tonic-gate 			break;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 		case 'm':
2837c478bd9Sstevel@tonic-gate 			add_to_mlist(optarg, !dflg);
2847c478bd9Sstevel@tonic-gate 			mflg++;
2857c478bd9Sstevel@tonic-gate 			break;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		case '?':
2887c478bd9Sstevel@tonic-gate 			eflg++;
2897c478bd9Sstevel@tonic-gate 			break;
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 	if (!cflg && !fflg && (eflg || optind == argc))
2937c478bd9Sstevel@tonic-gate 		usage();
2947c478bd9Sstevel@tonic-gate 	if (iflg && (dflg || mflg || M_flg)) {
2957c478bd9Sstevel@tonic-gate 		usage();
2967c478bd9Sstevel@tonic-gate 	}
2976e987ca0SDavid Höppner 	if ((iflg && cflg) || (cflg && bflg)) {
2987c478bd9Sstevel@tonic-gate 		usage();
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (!dflg && !mflg && !M_flg && !iflg) {
3027c478bd9Sstevel@tonic-gate 	/* no -d, -m, nor -M option; also -i option doesn't need magic  */
3037c478bd9Sstevel@tonic-gate 		default_magic();
3047c478bd9Sstevel@tonic-gate 		if (f_mkmtab(dfile, cflg, 0) == -1) {
3057c478bd9Sstevel@tonic-gate 			exit(2);
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	else if (mflg && !M_flg && !dflg) {
3107c478bd9Sstevel@tonic-gate 	/* -m specified without -d nor -M */
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate #ifdef XPG4	/* For SUSv3 only */
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		/*
3157c478bd9Sstevel@tonic-gate 		 * The default position-dependent magic file tests
3167c478bd9Sstevel@tonic-gate 		 * in /etc/magic will follow all the -m magic tests.
3177c478bd9Sstevel@tonic-gate 		 */
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		for (filep = mlist1; filep < mlist1p; filep++) {
3207c478bd9Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 1) == -1) {
3217c478bd9Sstevel@tonic-gate 				exit(2);
3227c478bd9Sstevel@tonic-gate 			}
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 		default_magic();
3257c478bd9Sstevel@tonic-gate 		if (f_mkmtab(dfile, cflg, 0) == -1) {
3267c478bd9Sstevel@tonic-gate 			exit(2);
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate #else	/* !XPG4 */
3297c478bd9Sstevel@tonic-gate 		/*
3307c478bd9Sstevel@tonic-gate 		 * Retain Solaris file behavior for -m before SUSv3,
3317c478bd9Sstevel@tonic-gate 		 * when the new -d and -M options are not specified.
3327c478bd9Sstevel@tonic-gate 		 * Use the -m file specified in place of the default
3337c478bd9Sstevel@tonic-gate 		 * /etc/magic file.  Solaris file will
3347c478bd9Sstevel@tonic-gate 		 * now allow more than one magic file to be specified
3357c478bd9Sstevel@tonic-gate 		 * with multiple -m options, for consistency with
3367c478bd9Sstevel@tonic-gate 		 * other behavior.
3377c478bd9Sstevel@tonic-gate 		 *
3387c478bd9Sstevel@tonic-gate 		 * Put the magic table(s) specified by -m into
3397c478bd9Sstevel@tonic-gate 		 * the second magic table instead of the first
3407c478bd9Sstevel@tonic-gate 		 * (as indicated by the last argument to f_mkmtab()),
3417c478bd9Sstevel@tonic-gate 		 * since they replace the /etc/magic tests and
3427c478bd9Sstevel@tonic-gate 		 * must be executed alongside the default
3437c478bd9Sstevel@tonic-gate 		 * position-sensitive tests.
3447c478bd9Sstevel@tonic-gate 		 */
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 		for (filep = mlist1; filep < mlist1p; filep++) {
3477c478bd9Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 0) == -1) {
3487c478bd9Sstevel@tonic-gate 				exit(2);
3497c478bd9Sstevel@tonic-gate 			}
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate #endif /* XPG4 */
3527c478bd9Sstevel@tonic-gate 	} else {
3537c478bd9Sstevel@tonic-gate 		/*
3547c478bd9Sstevel@tonic-gate 		 * For any other combination of -d, -m, and -M,
3557c478bd9Sstevel@tonic-gate 		 * use the magic files in command-line order.
3567c478bd9Sstevel@tonic-gate 		 * Store the entries from the two separate lists of magic
3577c478bd9Sstevel@tonic-gate 		 * files, if any, into two separate magic file tables.
3587c478bd9Sstevel@tonic-gate 		 * mlist1: magic tests executed before default magic tests
3597c478bd9Sstevel@tonic-gate 		 * mlist2: default magic tests and after
3607c478bd9Sstevel@tonic-gate 		 */
3617c478bd9Sstevel@tonic-gate 		for (filep = mlist1; filep && (filep < mlist1p); filep++) {
3627c478bd9Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 1) == -1) {
3637c478bd9Sstevel@tonic-gate 				exit(2);
3647c478bd9Sstevel@tonic-gate 			}
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 		for (filep = mlist2; filep && (filep < mlist2p); filep++) {
3677c478bd9Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 0) == -1) {
3687c478bd9Sstevel@tonic-gate 				exit(2);
3697c478bd9Sstevel@tonic-gate 			}
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	/* Initialize the magic file variables; check both magic tables */
3747c478bd9Sstevel@tonic-gate 	tmpmax = f_getmaxoffset(1);
3757c478bd9Sstevel@tonic-gate 	maxmagicoffset = f_getmaxoffset(0);
3767c478bd9Sstevel@tonic-gate 	if (maxmagicoffset < tmpmax) {
3777c478bd9Sstevel@tonic-gate 		maxmagicoffset = tmpmax;
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 	if (maxmagicoffset < (intmax_t)FBSZ)
3807c478bd9Sstevel@tonic-gate 		maxmagicoffset = (intmax_t)FBSZ;
3819a411307Srie 	if ((magicbuf = malloc(maxmagicoffset)) == NULL) {
3829a411307Srie 		int err = errno;
3839a411307Srie 		(void) fprintf(stderr, gettext("%s: malloc failed: %s\n"),
3849a411307Srie 		    File, strerror(err));
3857c478bd9Sstevel@tonic-gate 		exit(2);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (cflg) {
3897c478bd9Sstevel@tonic-gate 		f_prtmtab();
3907c478bd9Sstevel@tonic-gate 		if (ferror(stdout) != 0) {
3919a411307Srie 			(void) fprintf(stderr, gettext("%s: error writing to "
3929a411307Srie 			    "stdout\n"), File);
3937c478bd9Sstevel@tonic-gate 			exit(1);
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 		if (fclose(stdout) != 0) {
3969a411307Srie 			int err = errno;
3979a411307Srie 			(void) fprintf(stderr, gettext("%s: fclose "
3989a411307Srie 			    "failed: %s\n"), File, strerror(err));
3997c478bd9Sstevel@tonic-gate 			exit(1);
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 		exit(0);
4027c478bd9Sstevel@tonic-gate 	}
4039a411307Srie 
4047c478bd9Sstevel@tonic-gate 	for (; fflg || optind < argc; optind += !fflg) {
4057c478bd9Sstevel@tonic-gate 		register int	l;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 		if (fflg) {
4087c478bd9Sstevel@tonic-gate 			if ((p = fgets(ap, pathlen, fl)) == NULL) {
4097c478bd9Sstevel@tonic-gate 				fflg = 0;
4107c478bd9Sstevel@tonic-gate 				optind--;
4117c478bd9Sstevel@tonic-gate 				continue;
4127c478bd9Sstevel@tonic-gate 			}
4137c478bd9Sstevel@tonic-gate 			l = strlen(p);
4147c478bd9Sstevel@tonic-gate 			if (l > 0)
4157c478bd9Sstevel@tonic-gate 				p[l - 1] = '\0';
4167c478bd9Sstevel@tonic-gate 		} else
4177c478bd9Sstevel@tonic-gate 			p = argv[optind];
4186e987ca0SDavid Höppner 
4196e987ca0SDavid Höppner 		if (!bflg)
4207c478bd9Sstevel@tonic-gate 			prf(p);		/* print "file_name:<tab>" */
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		if (type(p))
4237c478bd9Sstevel@tonic-gate 			tret = 1;
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 	if (ap != NULL)
4267c478bd9Sstevel@tonic-gate 		free(ap);
4279a411307Srie 	if (tret != 0)
4287c478bd9Sstevel@tonic-gate 		exit(tret);
4299a411307Srie 
4307c478bd9Sstevel@tonic-gate 	if (ferror(stdout) != 0) {
4319a411307Srie 		(void) fprintf(stderr, gettext("%s: error writing to "
4329a411307Srie 		    "stdout\n"), File);
4337c478bd9Sstevel@tonic-gate 		exit(1);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 	if (fclose(stdout) != 0) {
4369a411307Srie 		int err = errno;
4379a411307Srie 		(void) fprintf(stderr, gettext("%s: fclose failed: %s\n"),
4389a411307Srie 		    File, strerror(err));
4397c478bd9Sstevel@tonic-gate 		exit(1);
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 	return (0);
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate static int
type(char * file)4457c478bd9Sstevel@tonic-gate type(char *file)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	int	cc;
4487c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
4497c478bd9Sstevel@tonic-gate 	int	(*statf)() = hflg ? lstat64 : stat64;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	i = 0;		/* reset index to beginning of file */
4527c478bd9Sstevel@tonic-gate 	ifd = -1;
4537c478bd9Sstevel@tonic-gate 	if ((*statf)(file, &mbuf) < 0) {
4547c478bd9Sstevel@tonic-gate 		if (statf == lstat64 || lstat64(file, &mbuf) < 0) {
4559a411307Srie 			int err = errno;
4567c478bd9Sstevel@tonic-gate 			(void) printf(gettext("cannot open: %s\n"),
4579a411307Srie 			    strerror(err));
4587c478bd9Sstevel@tonic-gate 			return (0);		/* POSIX.2 */
4597c478bd9Sstevel@tonic-gate 		}
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 	switch (mbuf.st_mode & S_IFMT) {
4627c478bd9Sstevel@tonic-gate 	case S_IFREG:
4637c478bd9Sstevel@tonic-gate 		if (iflg) {
4647c478bd9Sstevel@tonic-gate 			(void) printf(gettext("regular file\n"));
4657c478bd9Sstevel@tonic-gate 			return (0);
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 		break;
4687c478bd9Sstevel@tonic-gate 	case S_IFCHR:
4697c478bd9Sstevel@tonic-gate 		(void) printf(gettext("character"));
4707c478bd9Sstevel@tonic-gate 		goto spcl;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	case S_IFDIR:
4737c478bd9Sstevel@tonic-gate 		(void) printf(gettext("directory\n"));
4747c478bd9Sstevel@tonic-gate 		return (0);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	case S_IFIFO:
4777c478bd9Sstevel@tonic-gate 		(void) printf(gettext("fifo\n"));
4787c478bd9Sstevel@tonic-gate 		return (0);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	case S_IFLNK:
4817c478bd9Sstevel@tonic-gate 		if ((cc = readlink(file, buf, BUFSIZ)) < 0) {
4829a411307Srie 			int err = errno;
4837c478bd9Sstevel@tonic-gate 			(void) printf(gettext("readlink error: %s\n"),
4849a411307Srie 			    strerror(err));
4857c478bd9Sstevel@tonic-gate 			return (1);
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 		buf[cc] = '\0';
4887c478bd9Sstevel@tonic-gate 		(void) printf(gettext("symbolic link to %s\n"), buf);
4897c478bd9Sstevel@tonic-gate 		return (0);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	case S_IFBLK:
4927c478bd9Sstevel@tonic-gate 		(void) printf(gettext("block"));
4937c478bd9Sstevel@tonic-gate 					/* major and minor, see sys/mkdev.h */
4947c478bd9Sstevel@tonic-gate spcl:
4957c478bd9Sstevel@tonic-gate 		(void) printf(gettext(" special (%d/%d)\n"),
4967c478bd9Sstevel@tonic-gate 		    major(mbuf.st_rdev), minor(mbuf.st_rdev));
4977c478bd9Sstevel@tonic-gate 		return (0);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	case S_IFSOCK:
5007c478bd9Sstevel@tonic-gate 		(void) printf("socket\n");
5017c478bd9Sstevel@tonic-gate 		/* FIXME, should open and try to getsockname. */
5027c478bd9Sstevel@tonic-gate 		return (0);
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	case S_IFDOOR:
5057c478bd9Sstevel@tonic-gate 		if (get_door_target(file, buf, sizeof (buf)) == 0)
5067c478bd9Sstevel@tonic-gate 			(void) printf(gettext("door to %s\n"), buf);
5077c478bd9Sstevel@tonic-gate 		else
5087c478bd9Sstevel@tonic-gate 			(void) printf(gettext("door\n"));
5097c478bd9Sstevel@tonic-gate 		return (0);
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
5147c478bd9Sstevel@tonic-gate 		(void) printf(gettext("libelf is out of date\n"));
5157c478bd9Sstevel@tonic-gate 		return (1);
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	ifd = open64(file, O_RDONLY);
5197c478bd9Sstevel@tonic-gate 	if (ifd < 0) {
5209a411307Srie 		int err = errno;
5219a411307Srie 		(void) printf(gettext("cannot open: %s\n"), strerror(err));
5227c478bd9Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/* need another fd for elf, since we might want to read the file too */
5267c478bd9Sstevel@tonic-gate 	elffd = open64(file, O_RDONLY);
5277c478bd9Sstevel@tonic-gate 	if (elffd < 0) {
5289a411307Srie 		int err = errno;
5299a411307Srie 		(void) printf(gettext("cannot open: %s\n"), strerror(err));
5307c478bd9Sstevel@tonic-gate 		(void) close(ifd);
5317c478bd9Sstevel@tonic-gate 		ifd = -1;
5327c478bd9Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 	if ((fbsz = read(ifd, fbuf, FBSZ)) == -1) {
5359a411307Srie 		int err = errno;
5369a411307Srie 		(void) printf(gettext("cannot read: %s\n"), strerror(err));
5377c478bd9Sstevel@tonic-gate 		(void) close(ifd);
5387c478bd9Sstevel@tonic-gate 		ifd = -1;
5397c478bd9Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 	if (fbsz == 0) {
5427c478bd9Sstevel@tonic-gate 		(void) printf(gettext("empty file\n"));
5437c478bd9Sstevel@tonic-gate 		fd_cleanup();
5447c478bd9Sstevel@tonic-gate 		return (0);
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/*
5487c478bd9Sstevel@tonic-gate 	 * First try user-specified position-dependent magic tests, if any,
5497c478bd9Sstevel@tonic-gate 	 * which need to execute before the default tests.
5507c478bd9Sstevel@tonic-gate 	 */
5517c478bd9Sstevel@tonic-gate 	if ((mread = pread(ifd, (void*)magicbuf, (size_t)maxmagicoffset,
5527c478bd9Sstevel@tonic-gate 	    (off_t)0)) == -1) {
5539a411307Srie 		int err = errno;
5549a411307Srie 		(void) printf(gettext("cannot read: %s\n"), strerror(err));
5557c478bd9Sstevel@tonic-gate 		fd_cleanup();
5567c478bd9Sstevel@tonic-gate 		return (0);
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	/*
5607c478bd9Sstevel@tonic-gate 	 * ChecK against Magic Table entries.
5617c478bd9Sstevel@tonic-gate 	 * Check first magic table for magic tests to be applied
5627c478bd9Sstevel@tonic-gate 	 * before default tests.
5637c478bd9Sstevel@tonic-gate 	 * If no default tests are to be applied, all magic tests
5647c478bd9Sstevel@tonic-gate 	 * should occur in this magic table.
5657c478bd9Sstevel@tonic-gate 	 */
5667c478bd9Sstevel@tonic-gate 	switch (f_ckmtab(magicbuf, mread, 1)) {
5677c478bd9Sstevel@tonic-gate 		case -1:	/* Error */
5687c478bd9Sstevel@tonic-gate 			exit(2);
5697c478bd9Sstevel@tonic-gate 			break;
5707c478bd9Sstevel@tonic-gate 		case 0:		/* Not magic */
5717c478bd9Sstevel@tonic-gate 			break;
5727c478bd9Sstevel@tonic-gate 		default:	/* Switch is magic index */
5737c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
5747c478bd9Sstevel@tonic-gate 			fd_cleanup();
5757c478bd9Sstevel@tonic-gate 			return (0);
5767c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
5777c478bd9Sstevel@tonic-gate 			break;
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	if (dflg || !M_flg) {
5817c478bd9Sstevel@tonic-gate 		/*
5827c478bd9Sstevel@tonic-gate 		 * default position-dependent tests,
5837c478bd9Sstevel@tonic-gate 		 * plus non-default magic tests, if any
5847c478bd9Sstevel@tonic-gate 		 */
5859a411307Srie 		switch (def_position_tests(file)) {
5867c478bd9Sstevel@tonic-gate 			case -1:	/* error */
5877c478bd9Sstevel@tonic-gate 				fd_cleanup();
5887c478bd9Sstevel@tonic-gate 				return (1);
5897c478bd9Sstevel@tonic-gate 			case 1:	/* matching type found */
5907c478bd9Sstevel@tonic-gate 				fd_cleanup();
5917c478bd9Sstevel@tonic-gate 				return (0);
5927c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
5937c478bd9Sstevel@tonic-gate 				break;
5947c478bd9Sstevel@tonic-gate 			case 0:		/* no matching type found */
5957c478bd9Sstevel@tonic-gate 				break;
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 		/* default context-sensitive tests */
5987c478bd9Sstevel@tonic-gate 		def_context_tests();
5997c478bd9Sstevel@tonic-gate 	} else {
6007c478bd9Sstevel@tonic-gate 		/* no more tests to apply; no match was found */
6017c478bd9Sstevel@tonic-gate 		(void) printf(gettext("data\n"));
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 	fd_cleanup();
6047c478bd9Sstevel@tonic-gate 	return (0);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate /*
6087c478bd9Sstevel@tonic-gate  * def_position_tests() - applies default position-sensitive tests,
6097c478bd9Sstevel@tonic-gate  *	looking for values in specific positions in the file.
6107c478bd9Sstevel@tonic-gate  *	These are followed by default (followed by possibly some
6117c478bd9Sstevel@tonic-gate  *	non-default) magic file tests.
6127c478bd9Sstevel@tonic-gate  *
6137c478bd9Sstevel@tonic-gate  *	All position-sensitive tests, default or otherwise, must
6147c478bd9Sstevel@tonic-gate  *	be applied before context-sensitive tests, to avoid
6157c478bd9Sstevel@tonic-gate  *	false context-sensitive matches.
6167c478bd9Sstevel@tonic-gate  *
6177c478bd9Sstevel@tonic-gate  * 	Returns -1 on error which should result in error (non-zero)
6187c478bd9Sstevel@tonic-gate  *	exit status for the file utility.
6197c478bd9Sstevel@tonic-gate  *	Returns 0 if no matching file type found.
6207c478bd9Sstevel@tonic-gate  *	Returns 1 if matching file type found.
6217c478bd9Sstevel@tonic-gate  */
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate static int
def_position_tests(char * file)6249a411307Srie def_position_tests(char *file)
6257c478bd9Sstevel@tonic-gate {
6267c478bd9Sstevel@tonic-gate 	if (sccs()) {	/* look for "1hddddd" where d is a digit */
6277c478bd9Sstevel@tonic-gate 		(void) printf("sccs \n");
6287c478bd9Sstevel@tonic-gate 		return (1);
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 	if (fbuf[0] == '#' && fbuf[1] == '!' && shellscript(fbuf+2, &mbuf))
6317c478bd9Sstevel@tonic-gate 		return (1);
632c2c65e21Sny155746 
633c2c65e21Sny155746 	if (elf_check(file) == 0) {
6347c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
6357c478bd9Sstevel@tonic-gate 		return (1);
6367c478bd9Sstevel@tonic-gate 	/* LINTED: pointer cast may result in improper alignment */
6377c478bd9Sstevel@tonic-gate 	} else if (*(int *)fbuf == CORE_MAGIC) {
6387c478bd9Sstevel@tonic-gate 		/* LINTED: pointer cast may result in improper alignment */
6397c478bd9Sstevel@tonic-gate 		struct core *corep = (struct core *)fbuf;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		(void) printf("a.out core file");
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		if (*(corep->c_cmdname) != '\0')
6447c478bd9Sstevel@tonic-gate 			(void) printf(" from '%s'", corep->c_cmdname);
6457c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
6467c478bd9Sstevel@tonic-gate 		return (1);
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	/*
650c13de8f6Sab196087 	 * Runtime linker (ld.so.1) configuration file.
651c13de8f6Sab196087 	 */
652c13de8f6Sab196087 	if (is_rtld_config())
653c13de8f6Sab196087 		return (1);
654c13de8f6Sab196087 
655c13de8f6Sab196087 	/*
6567c478bd9Sstevel@tonic-gate 	 * ZIP files, JAR files, and Java executables
6577c478bd9Sstevel@tonic-gate 	 */
6587c478bd9Sstevel@tonic-gate 	if (zipfile(fbuf, ifd))
6597c478bd9Sstevel@tonic-gate 		return (1);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	if (is_crash_dump(fbuf, ifd))
6627c478bd9Sstevel@tonic-gate 		return (1);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	/*
6657c478bd9Sstevel@tonic-gate 	 * ChecK against Magic Table entries.
6667c478bd9Sstevel@tonic-gate 	 * The magic entries checked here always start with default
6677c478bd9Sstevel@tonic-gate 	 * magic tests and may be followed by other, non-default magic
6687c478bd9Sstevel@tonic-gate 	 * tests.  If no default tests are to be executed, all the
6697c478bd9Sstevel@tonic-gate 	 * magic tests should have been in the first magic table.
6707c478bd9Sstevel@tonic-gate 	 */
6717c478bd9Sstevel@tonic-gate 	switch (f_ckmtab(magicbuf, mread, 0)) {
6727c478bd9Sstevel@tonic-gate 		case -1:	/* Error */
6737c478bd9Sstevel@tonic-gate 			exit(2);
6747c478bd9Sstevel@tonic-gate 			break;
6757c478bd9Sstevel@tonic-gate 		case 0:		/* Not magic */
6767c478bd9Sstevel@tonic-gate 			return (0);
6777c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
6787c478bd9Sstevel@tonic-gate 			break;
6797c478bd9Sstevel@tonic-gate 		default:	/* Switch is magic index */
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 			/*
6827c478bd9Sstevel@tonic-gate 			 * f_ckmtab recognizes file type,
6837c478bd9Sstevel@tonic-gate 			 * check if it is PostScript.
6847c478bd9Sstevel@tonic-gate 			 * if not, check if elf or a.out
6857c478bd9Sstevel@tonic-gate 			 */
6867c478bd9Sstevel@tonic-gate 			if (magicbuf[0] == '%' && magicbuf[1] == '!') {
6877c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
6887c478bd9Sstevel@tonic-gate 			} else {
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 				/*
6917c478bd9Sstevel@tonic-gate 				 * Check that the file is executable (dynamic
6927c478bd9Sstevel@tonic-gate 				 * objects must be executable to be exec'ed,
6937c478bd9Sstevel@tonic-gate 				 * shared objects need not be, but by convention
6947c478bd9Sstevel@tonic-gate 				 * should be executable).
6957c478bd9Sstevel@tonic-gate 				 *
6967c478bd9Sstevel@tonic-gate 				 * Note that we should already have processed
6977c478bd9Sstevel@tonic-gate 				 * the file if it was an ELF file.
6987c478bd9Sstevel@tonic-gate 				 */
6997c478bd9Sstevel@tonic-gate 				ar_coff_or_aout(elffd);
7007c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
7017c478bd9Sstevel@tonic-gate 			}
7027c478bd9Sstevel@tonic-gate 			return (1);
7037c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
7047c478bd9Sstevel@tonic-gate 			break;
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	return (0);	/* file was not identified */
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate /*
7117c478bd9Sstevel@tonic-gate  * def_context_tests() - default context-sensitive tests.
7127c478bd9Sstevel@tonic-gate  *	These are the last tests to be applied.
7137c478bd9Sstevel@tonic-gate  *	If no match is found, prints out "data".
7147c478bd9Sstevel@tonic-gate  */
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate static void
def_context_tests(void)7177c478bd9Sstevel@tonic-gate def_context_tests(void)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate 	int	j;
7207c478bd9Sstevel@tonic-gate 	int	nl;
7217c478bd9Sstevel@tonic-gate 	char	ch;
7227c478bd9Sstevel@tonic-gate 	int	len;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	if (ccom() == 0)
7257c478bd9Sstevel@tonic-gate 		goto notc;
7267c478bd9Sstevel@tonic-gate 	while (fbuf[i] == '#') {
7277c478bd9Sstevel@tonic-gate 		j = i;
7287c478bd9Sstevel@tonic-gate 		while (fbuf[i++] != '\n') {
7297c478bd9Sstevel@tonic-gate 			if (i - j > 255) {
7307c478bd9Sstevel@tonic-gate 				(void) printf(gettext("data\n"));
7317c478bd9Sstevel@tonic-gate 				return;
7327c478bd9Sstevel@tonic-gate 			}
7337c478bd9Sstevel@tonic-gate 			if (i >= fbsz)
7347c478bd9Sstevel@tonic-gate 				goto notc;
7357c478bd9Sstevel@tonic-gate 		}
7367c478bd9Sstevel@tonic-gate 		if (ccom() == 0)
7377c478bd9Sstevel@tonic-gate 			goto notc;
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate check:
7407c478bd9Sstevel@tonic-gate 	if (lookup(c) == 1) {
7417c478bd9Sstevel@tonic-gate 		while ((ch = fbuf[i]) != ';' && ch != '{') {
7427c478bd9Sstevel@tonic-gate 			if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
7437c478bd9Sstevel@tonic-gate 				len = 1;
7447c478bd9Sstevel@tonic-gate 			i += len;
7457c478bd9Sstevel@tonic-gate 			if (i >= fbsz)
7467c478bd9Sstevel@tonic-gate 				goto notc;
7477c478bd9Sstevel@tonic-gate 		}
7487c478bd9Sstevel@tonic-gate 		(void) printf(gettext("c program text"));
7497c478bd9Sstevel@tonic-gate 		goto outa;
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate 	nl = 0;
7527c478bd9Sstevel@tonic-gate 	while (fbuf[i] != '(') {
7537c478bd9Sstevel@tonic-gate 		if (fbuf[i] <= 0)
7547c478bd9Sstevel@tonic-gate 			goto notas;
7557c478bd9Sstevel@tonic-gate 		if (fbuf[i] == ';') {
7567c478bd9Sstevel@tonic-gate 			i++;
7577c478bd9Sstevel@tonic-gate 			goto check;
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate 		if (fbuf[i++] == '\n')
7607c478bd9Sstevel@tonic-gate 			if (nl++ > 6)
7617c478bd9Sstevel@tonic-gate 				goto notc;
7627c478bd9Sstevel@tonic-gate 		if (i >= fbsz)
7637c478bd9Sstevel@tonic-gate 			goto notc;
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 	while (fbuf[i] != ')') {
7667c478bd9Sstevel@tonic-gate 		if (fbuf[i++] == '\n')
7677c478bd9Sstevel@tonic-gate 			if (nl++ > 6)
7687c478bd9Sstevel@tonic-gate 				goto notc;
7697c478bd9Sstevel@tonic-gate 		if (i >= fbsz)
7707c478bd9Sstevel@tonic-gate 			goto notc;
7717c478bd9Sstevel@tonic-gate 	}
7727c478bd9Sstevel@tonic-gate 	while (fbuf[i] != '{') {
7737c478bd9Sstevel@tonic-gate 		if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
7747c478bd9Sstevel@tonic-gate 			len = 1;
7757c478bd9Sstevel@tonic-gate 		if (fbuf[i] == '\n')
7767c478bd9Sstevel@tonic-gate 			if (nl++ > 6)
7777c478bd9Sstevel@tonic-gate 				goto notc;
7787c478bd9Sstevel@tonic-gate 		i += len;
7797c478bd9Sstevel@tonic-gate 		if (i >= fbsz)
7807c478bd9Sstevel@tonic-gate 			goto notc;
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 	(void) printf(gettext("c program text"));
7837c478bd9Sstevel@tonic-gate 	goto outa;
7847c478bd9Sstevel@tonic-gate notc:
7857c478bd9Sstevel@tonic-gate 	i = 0;			/* reset to begining of file again */
7867c478bd9Sstevel@tonic-gate 	while (fbuf[i] == 'c' || fbuf[i] == 'C'|| fbuf[i] == '!' ||
7877c478bd9Sstevel@tonic-gate 	    fbuf[i] == '*' || fbuf[i] == '\n') {
7887c478bd9Sstevel@tonic-gate 		while (fbuf[i++] != '\n')
7897c478bd9Sstevel@tonic-gate 			if (i >= fbsz)
7907c478bd9Sstevel@tonic-gate 				goto notfort;
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 	if (lookup(fort) == 1) {
7937c478bd9Sstevel@tonic-gate 		(void) printf(gettext("fortran program text"));
7947c478bd9Sstevel@tonic-gate 		goto outa;
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate notfort:			/* looking for assembler program */
7977c478bd9Sstevel@tonic-gate 	i = 0;			/* reset to beginning of file again */
7987c478bd9Sstevel@tonic-gate 	if (ccom() == 0)	/* assembler programs may contain */
7997c478bd9Sstevel@tonic-gate 				/* c-style comments */
8007c478bd9Sstevel@tonic-gate 		goto notas;
8017c478bd9Sstevel@tonic-gate 	if (ascom() == 0)
8027c478bd9Sstevel@tonic-gate 		goto notas;
8037c478bd9Sstevel@tonic-gate 	j = i - 1;
8047c478bd9Sstevel@tonic-gate 	if (fbuf[i] == '.') {
8057c478bd9Sstevel@tonic-gate 		i++;
8067c478bd9Sstevel@tonic-gate 		if (lookup(as) == 1) {
8077c478bd9Sstevel@tonic-gate 			(void) printf(gettext("assembler program text"));
8087c478bd9Sstevel@tonic-gate 			goto outa;
8097c478bd9Sstevel@tonic-gate 		} else if (j != -1 && fbuf[j] == '\n' && isalpha(fbuf[j + 2])) {
8107c478bd9Sstevel@tonic-gate 			(void) printf(
8117c478bd9Sstevel@tonic-gate 			    gettext("[nt]roff, tbl, or eqn input text"));
8127c478bd9Sstevel@tonic-gate 			goto outa;
8137c478bd9Sstevel@tonic-gate 		}
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 	while (lookup(asc) == 0) {
8167c478bd9Sstevel@tonic-gate 		if (ccom() == 0)
8177c478bd9Sstevel@tonic-gate 			goto notas;
8187c478bd9Sstevel@tonic-gate 		if (ascom() == 0)
8197c478bd9Sstevel@tonic-gate 			goto notas;
8207c478bd9Sstevel@tonic-gate 		while (fbuf[i] != '\n' && fbuf[i++] != ':') {
8217c478bd9Sstevel@tonic-gate 			if (i >= fbsz)
8227c478bd9Sstevel@tonic-gate 				goto notas;
8237c478bd9Sstevel@tonic-gate 		}
8247c478bd9Sstevel@tonic-gate 		while (fbuf[i] == '\n' || fbuf[i] == ' ' || fbuf[i] == '\t')
8257c478bd9Sstevel@tonic-gate 			if (i++ >= fbsz)
8267c478bd9Sstevel@tonic-gate 				goto notas;
8277c478bd9Sstevel@tonic-gate 		j = i - 1;
8287c478bd9Sstevel@tonic-gate 		if (fbuf[i] == '.') {
8297c478bd9Sstevel@tonic-gate 			i++;
8307c478bd9Sstevel@tonic-gate 			if (lookup(as) == 1) {
8317c478bd9Sstevel@tonic-gate 				(void) printf(
8327c478bd9Sstevel@tonic-gate 				    gettext("assembler program text"));
8337c478bd9Sstevel@tonic-gate 				goto outa;
8347c478bd9Sstevel@tonic-gate 			} else if (fbuf[j] == '\n' && isalpha(fbuf[j+2])) {
8357c478bd9Sstevel@tonic-gate 				(void) printf(
8367c478bd9Sstevel@tonic-gate 				    gettext("[nt]roff, tbl, or eqn input "
8377c478bd9Sstevel@tonic-gate 				    "text"));
8387c478bd9Sstevel@tonic-gate 				goto outa;
8397c478bd9Sstevel@tonic-gate 			}
8407c478bd9Sstevel@tonic-gate 		}
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 	(void) printf(gettext("assembler program text"));
8437c478bd9Sstevel@tonic-gate 	goto outa;
8447c478bd9Sstevel@tonic-gate notas:
8457c478bd9Sstevel@tonic-gate 	/* start modification for multibyte env */
8467c478bd9Sstevel@tonic-gate 	IS_ascii = 1;
8477c478bd9Sstevel@tonic-gate 	if (fbsz < FBSZ)
8487c478bd9Sstevel@tonic-gate 		Max = fbsz;
8497c478bd9Sstevel@tonic-gate 	else
8507c478bd9Sstevel@tonic-gate 		Max = FBSZ - MB_LEN_MAX; /* prevent cut of wchar read */
8517c478bd9Sstevel@tonic-gate 	/* end modification for multibyte env */
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	for (i = 0; i < Max; /* null */)
8547c478bd9Sstevel@tonic-gate 		if (fbuf[i] & 0200) {
8557c478bd9Sstevel@tonic-gate 			IS_ascii = 0;
8567c478bd9Sstevel@tonic-gate 			if (fbuf[0] == '\100' && fbuf[1] == '\357') {
8577c478bd9Sstevel@tonic-gate 				(void) printf(gettext("troff output\n"));
8587c478bd9Sstevel@tonic-gate 				return;
8597c478bd9Sstevel@tonic-gate 			}
8607c478bd9Sstevel@tonic-gate 		/* start modification for multibyte env */
8617c478bd9Sstevel@tonic-gate 			if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX))
8627c478bd9Sstevel@tonic-gate 			    <= 0 || !iswprint(wchar)) {
8637c478bd9Sstevel@tonic-gate 				(void) printf(gettext("data\n"));
8647c478bd9Sstevel@tonic-gate 				return;
8657c478bd9Sstevel@tonic-gate 			}
8667c478bd9Sstevel@tonic-gate 			i += length;
8677c478bd9Sstevel@tonic-gate 		}
8687c478bd9Sstevel@tonic-gate 		else
8697c478bd9Sstevel@tonic-gate 			i++;
8707c478bd9Sstevel@tonic-gate 	i = fbsz;
8717c478bd9Sstevel@tonic-gate 		/* end modification for multibyte env */
8727c478bd9Sstevel@tonic-gate 	if (mbuf.st_mode&(S_IXUSR|S_IXGRP|S_IXOTH))
8737c478bd9Sstevel@tonic-gate 		(void) printf(gettext("commands text"));
8747c478bd9Sstevel@tonic-gate 	else if (troffint(fbuf, fbsz))
8757c478bd9Sstevel@tonic-gate 		(void) printf(gettext("troff intermediate output text"));
8767c478bd9Sstevel@tonic-gate 	else if (english(fbuf, fbsz))
8777c478bd9Sstevel@tonic-gate 		(void) printf(gettext("English text"));
8787c478bd9Sstevel@tonic-gate 	else if (IS_ascii)
8797c478bd9Sstevel@tonic-gate 		(void) printf(gettext("ascii text"));
8807c478bd9Sstevel@tonic-gate 	else
8817c478bd9Sstevel@tonic-gate 		(void) printf(gettext("text")); /* for multibyte env */
8827c478bd9Sstevel@tonic-gate outa:
8837c478bd9Sstevel@tonic-gate 	/*
8847c478bd9Sstevel@tonic-gate 	 * This code is to make sure that no MB char is cut in half
8857c478bd9Sstevel@tonic-gate 	 * while still being used.
8867c478bd9Sstevel@tonic-gate 	 */
8877c478bd9Sstevel@tonic-gate 	fbsz = (fbsz < FBSZ ? fbsz : fbsz - MB_CUR_MAX + 1);
8887c478bd9Sstevel@tonic-gate 	while (i < fbsz) {
8897c478bd9Sstevel@tonic-gate 		if (isascii(fbuf[i])) {
8907c478bd9Sstevel@tonic-gate 			i++;
8917c478bd9Sstevel@tonic-gate 			continue;
8927c478bd9Sstevel@tonic-gate 		} else {
8937c478bd9Sstevel@tonic-gate 			if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX))
8947c478bd9Sstevel@tonic-gate 			    <= 0 || !iswprint(wchar)) {
8957c478bd9Sstevel@tonic-gate 				(void) printf(gettext(" with garbage\n"));
8967c478bd9Sstevel@tonic-gate 				return;
8977c478bd9Sstevel@tonic-gate 			}
8987c478bd9Sstevel@tonic-gate 			i = i + length;
8997c478bd9Sstevel@tonic-gate 		}
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 	(void) printf("\n");
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate static int
troffint(char * bp,int n)9057c478bd9Sstevel@tonic-gate troffint(char *bp, int n)
9067c478bd9Sstevel@tonic-gate {
9077c478bd9Sstevel@tonic-gate 	int k;
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	i = 0;
9107c478bd9Sstevel@tonic-gate 	for (k = 0; k < 6; k++) {
9117c478bd9Sstevel@tonic-gate 		if (lookup(troff) == 0)
9127c478bd9Sstevel@tonic-gate 			return (0);
9137c478bd9Sstevel@tonic-gate 		if (lookup(troff) == 0)
9147c478bd9Sstevel@tonic-gate 			return (0);
9157c478bd9Sstevel@tonic-gate 		while (i < n && bp[i] != '\n')
9167c478bd9Sstevel@tonic-gate 			i++;
9177c478bd9Sstevel@tonic-gate 		if (i++ >= n)
9187c478bd9Sstevel@tonic-gate 			return (0);
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate 	return (1);
9217c478bd9Sstevel@tonic-gate }
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate static void
ar_coff_or_aout(int elffd)9247c478bd9Sstevel@tonic-gate ar_coff_or_aout(int elffd)
9257c478bd9Sstevel@tonic-gate {
9267c478bd9Sstevel@tonic-gate 	Elf *elf;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	/*
9297c478bd9Sstevel@tonic-gate 	 * Get the files elf descriptor and process it as an elf or
9307c478bd9Sstevel@tonic-gate 	 * a.out (4.x) file.
9317c478bd9Sstevel@tonic-gate 	 */
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	elf = elf_begin(elffd, ELF_C_READ, (Elf *)0);
9347c478bd9Sstevel@tonic-gate 	switch (elf_kind(elf)) {
9357c478bd9Sstevel@tonic-gate 		case ELF_K_AR :
9367c478bd9Sstevel@tonic-gate 			(void) printf(gettext(", not a dynamic executable "
9377c478bd9Sstevel@tonic-gate 			    "or shared object"));
9387c478bd9Sstevel@tonic-gate 			break;
9397c478bd9Sstevel@tonic-gate 		case ELF_K_COFF:
9407c478bd9Sstevel@tonic-gate 			(void) printf(gettext(", unsupported or unknown "
9417c478bd9Sstevel@tonic-gate 			    "file type"));
9427c478bd9Sstevel@tonic-gate 			break;
9437c478bd9Sstevel@tonic-gate 		default:
9447c478bd9Sstevel@tonic-gate 			/*
9457c478bd9Sstevel@tonic-gate 			 * This is either an unknown file or an aout format
9467c478bd9Sstevel@tonic-gate 			 * At this time, we don't print dynamic/stripped
9477c478bd9Sstevel@tonic-gate 			 * info. on a.out or non-Elf binaries.
9487c478bd9Sstevel@tonic-gate 			 */
9497c478bd9Sstevel@tonic-gate 			break;
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate 	(void) elf_end(elf);
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate static void
print_elf_type(Elf_Info EI)956c2c65e21Sny155746 print_elf_type(Elf_Info EI)
9577c478bd9Sstevel@tonic-gate {
958c2c65e21Sny155746 	switch (EI.type) {
9597c478bd9Sstevel@tonic-gate 	case ET_NONE:
9607c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("unknown type"));
9617c478bd9Sstevel@tonic-gate 		break;
9627c478bd9Sstevel@tonic-gate 	case ET_REL:
9637c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("relocatable"));
9647c478bd9Sstevel@tonic-gate 		break;
9657c478bd9Sstevel@tonic-gate 	case ET_EXEC:
9667c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("executable"));
9677c478bd9Sstevel@tonic-gate 		break;
9687c478bd9Sstevel@tonic-gate 	case ET_DYN:
9697c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("dynamic lib"));
9707c478bd9Sstevel@tonic-gate 		break;
9717c478bd9Sstevel@tonic-gate 	default:
9727c478bd9Sstevel@tonic-gate 		break;
9737c478bd9Sstevel@tonic-gate 	}
9747c478bd9Sstevel@tonic-gate }
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate static void
print_elf_machine(int machine)9777c478bd9Sstevel@tonic-gate print_elf_machine(int machine)
9787c478bd9Sstevel@tonic-gate {
979ca1e0a81Sab196087 	/*
980ca1e0a81Sab196087 	 * This table must be kept in sync with the EM_ constants
981ca1e0a81Sab196087 	 * in /usr/include/sys/elf.h.
982ca1e0a81Sab196087 	 */
983ca1e0a81Sab196087 	static const char *mach_str[EM_NUM] = {
984*c4397e61SRobert Mustacchi 		[EM_NONE] = "unknown machine",
985*c4397e61SRobert Mustacchi 		[EM_M32] = "WE32100",
986*c4397e61SRobert Mustacchi 		[EM_SPARC] = "SPARC",
987*c4397e61SRobert Mustacchi 		[EM_386] = "80386",
988*c4397e61SRobert Mustacchi 		[EM_68K] = "M68000",
989*c4397e61SRobert Mustacchi 		[EM_88K] = "M88000",
990*c4397e61SRobert Mustacchi 		[EM_486] = "80486",
991*c4397e61SRobert Mustacchi 		[EM_860] = "i860",
992*c4397e61SRobert Mustacchi 		[EM_MIPS] = "MIPS RS3000 Big-Endian",
993*c4397e61SRobert Mustacchi 		[EM_S370] = "S/370",
994*c4397e61SRobert Mustacchi 		[EM_MIPS_RS3_LE] = "MIPS RS3000 Little-Endian",
995*c4397e61SRobert Mustacchi 		[EM_RS6000] = "MIPS RS6000",
996*c4397e61SRobert Mustacchi 		[EM_PA_RISC] = "PA-RISC",
997*c4397e61SRobert Mustacchi 		[EM_nCUBE] = "nCUBE",
998*c4397e61SRobert Mustacchi 		[EM_VPP500] = "VPP500",
999*c4397e61SRobert Mustacchi 		[EM_SPARC32PLUS] = "SPARC32PLUS",
1000*c4397e61SRobert Mustacchi 		[EM_960] = "i960",
1001*c4397e61SRobert Mustacchi 		[EM_PPC] = "PowerPC",
1002*c4397e61SRobert Mustacchi 		[EM_PPC64] = "PowerPC64",
1003*c4397e61SRobert Mustacchi 		[EM_S390] = "S/390",
1004*c4397e61SRobert Mustacchi 		[EM_V800] = "V800",
1005*c4397e61SRobert Mustacchi 		[EM_FR20] = "FR20",
1006*c4397e61SRobert Mustacchi 		[EM_RH32] = "RH32",
1007*c4397e61SRobert Mustacchi 		[EM_RCE] = "RCE",
1008*c4397e61SRobert Mustacchi 		[EM_ARM] = "ARM",
1009*c4397e61SRobert Mustacchi 		[EM_ALPHA] = "Alpha",
1010*c4397e61SRobert Mustacchi 		[EM_SH] = "S/390",
1011*c4397e61SRobert Mustacchi 		[EM_SPARCV9] = "SPARCV9",
1012*c4397e61SRobert Mustacchi 		[EM_TRICORE] = "Tricore",
1013*c4397e61SRobert Mustacchi 		[EM_ARC] = "ARC",
1014*c4397e61SRobert Mustacchi 		[EM_H8_300] = "H8/300",
1015*c4397e61SRobert Mustacchi 		[EM_H8_300H] = "H8/300H",
1016*c4397e61SRobert Mustacchi 		[EM_H8S] = "H8S",
1017*c4397e61SRobert Mustacchi 		[EM_H8_500] = "H8/500",
1018*c4397e61SRobert Mustacchi 		[EM_IA_64] = "IA64",
1019*c4397e61SRobert Mustacchi 		[EM_MIPS_X] = "MIPS-X",
1020*c4397e61SRobert Mustacchi 		[EM_COLDFIRE] = "Coldfire",
1021*c4397e61SRobert Mustacchi 		[EM_68HC12] = "M68HC12",
1022*c4397e61SRobert Mustacchi 		[EM_MMA] = "MMA",
1023*c4397e61SRobert Mustacchi 		[EM_PCP] = "PCP",
1024*c4397e61SRobert Mustacchi 		[EM_NCPU] = "nCPU",
1025*c4397e61SRobert Mustacchi 		[EM_NDR1] = "NDR1",
1026*c4397e61SRobert Mustacchi 		[EM_STARCORE] = "Starcore",
1027*c4397e61SRobert Mustacchi 		[EM_ME16] = "ME16",
1028*c4397e61SRobert Mustacchi 		[EM_ST100] = "ST100",
1029*c4397e61SRobert Mustacchi 		[EM_TINYJ] = "TINYJ",
1030*c4397e61SRobert Mustacchi 		[EM_AMD64] = "AMD64",
1031*c4397e61SRobert Mustacchi 		[EM_PDSP] = "PDSP",
1032*c4397e61SRobert Mustacchi 		[EM_FX66] = "FX66",
1033*c4397e61SRobert Mustacchi 		[EM_ST9PLUS] = "ST9 PLUS",
1034*c4397e61SRobert Mustacchi 		[EM_ST7] = "ST7",
1035*c4397e61SRobert Mustacchi 		[EM_68HC16] = "68HC16",
1036*c4397e61SRobert Mustacchi 		[EM_68HC11] = "68HC11",
1037*c4397e61SRobert Mustacchi 		[EM_68HC08] = "68H08",
1038*c4397e61SRobert Mustacchi 		[EM_68HC05] = "68HC05",
1039*c4397e61SRobert Mustacchi 		[EM_SVX] = "SVX",
1040*c4397e61SRobert Mustacchi 		[EM_ST19] = "ST19",
1041*c4397e61SRobert Mustacchi 		[EM_VAX] = "VAX",
1042*c4397e61SRobert Mustacchi 		[EM_CRIS] = "CRIS",
1043*c4397e61SRobert Mustacchi 		[EM_JAVELIN] = "Javelin",
1044*c4397e61SRobert Mustacchi 		[EM_FIREPATH] = "Firepath",
1045*c4397e61SRobert Mustacchi 		[EM_ZSP] = "ZSP",
1046*c4397e61SRobert Mustacchi 		[EM_MMIX] = "MMIX",
1047*c4397e61SRobert Mustacchi 		[EM_HUANY] = "HUANY",
1048*c4397e61SRobert Mustacchi 		[EM_PRISM] = "Prism",
1049*c4397e61SRobert Mustacchi 		[EM_AVR] = "AVR",
1050*c4397e61SRobert Mustacchi 		[EM_FR30] = "FR30",
1051*c4397e61SRobert Mustacchi 		[EM_D10V] = "D10V",
1052*c4397e61SRobert Mustacchi 		[EM_D30V] = "D30V",
1053*c4397e61SRobert Mustacchi 		[EM_V850] = "V850",
1054*c4397e61SRobert Mustacchi 		[EM_M32R] = "M32R",
1055*c4397e61SRobert Mustacchi 		[EM_MN10300] = "MN10300",
1056*c4397e61SRobert Mustacchi 		[EM_MN10200] = "MN10200",
1057*c4397e61SRobert Mustacchi 		[EM_PJ] = "picoJava",
1058*c4397e61SRobert Mustacchi 		[EM_OPENRISC] = "OpenRISC",
1059*c4397e61SRobert Mustacchi 		[EM_ARC_A5] = "Tangent-A5",
1060*c4397e61SRobert Mustacchi 		[EM_XTENSA] = "Xtensa",
1061*c4397e61SRobert Mustacchi 
1062*c4397e61SRobert Mustacchi 		[EM_VIDEOCORE] = "Videocore",
1063*c4397e61SRobert Mustacchi 		[EM_TMM_GPP] = "TMM_GPP",
1064*c4397e61SRobert Mustacchi 		[EM_NS32K] = "NS32K",
1065*c4397e61SRobert Mustacchi 		[EM_TPC] = "TPC",
1066*c4397e61SRobert Mustacchi 		[EM_SNP1K] = "SNP1K",
1067*c4397e61SRobert Mustacchi 		[EM_ST200] = "ST200",
1068*c4397e61SRobert Mustacchi 		[EM_IP2K] = "IP2K",
1069*c4397e61SRobert Mustacchi 		[EM_MAX] = "MAX",
1070*c4397e61SRobert Mustacchi 		[EM_CR] = "CompactRISC",
1071*c4397e61SRobert Mustacchi 		[EM_F2MC16] = "F2MC16",
1072*c4397e61SRobert Mustacchi 		[EM_MSP430] = "MSP430",
1073*c4397e61SRobert Mustacchi 		[EM_BLACKFIN] = "Blackfin",
1074*c4397e61SRobert Mustacchi 		[EM_SE_C33] = "S1C33",
1075*c4397e61SRobert Mustacchi 		[EM_SEP] = "SEP",
1076*c4397e61SRobert Mustacchi 		[EM_ARCA] = "Arca",
1077*c4397e61SRobert Mustacchi 		[EM_UNICORE] = "Unicore",
1078*c4397e61SRobert Mustacchi 		[EM_EXCESS] = "eXcess",
1079*c4397e61SRobert Mustacchi 		[EM_DXP] = "DXP",
1080*c4397e61SRobert Mustacchi 		[EM_ALTERA_NIOS2] = "Nios 2",
1081*c4397e61SRobert Mustacchi 		[EM_CRX] = "CompactRISC CRX",
1082*c4397e61SRobert Mustacchi 		[EM_XGATE] = "XGATE",
1083*c4397e61SRobert Mustacchi 		[EM_C166] = "C16x/XC16x",
1084*c4397e61SRobert Mustacchi 		[EM_M16C] = "M16C",
1085*c4397e61SRobert Mustacchi 		[EM_DSPIC30F] = "dsPIC30F",
1086*c4397e61SRobert Mustacchi 		[EM_CE] = "CE RISC",
1087*c4397e61SRobert Mustacchi 		[EM_M32C] = "M32C",
1088*c4397e61SRobert Mustacchi 		[EM_TSK3000] = "TSK3000",
1089*c4397e61SRobert Mustacchi 		[EM_RS08] = "RS08",
1090*c4397e61SRobert Mustacchi 		[EM_SHARC] = "SHARC",
1091*c4397e61SRobert Mustacchi 		[EM_ECOG2] = "eCOG2",
1092*c4397e61SRobert Mustacchi 		[EM_SCORE7] = "SCORE7",
1093*c4397e61SRobert Mustacchi 		[EM_DSP24] = "DSP24",
1094*c4397e61SRobert Mustacchi 		[EM_VIDEOCORE3] = "Videocore III",
1095*c4397e61SRobert Mustacchi 		[EM_LATTICEMICO32] = "LATTICEMICO32",
1096*c4397e61SRobert Mustacchi 		[EM_SE_C17] = "SE_C17",
1097*c4397e61SRobert Mustacchi 		[EM_TI_C6000] = "TMS320C6000",
1098*c4397e61SRobert Mustacchi 		[EM_TI_C2000] = "TMS320C2000",
1099*c4397e61SRobert Mustacchi 		[EM_TI_C5500] = "TMS320C55x",
1100*c4397e61SRobert Mustacchi 		[EM_TI_ARP32] = "ASRP32",
1101*c4397e61SRobert Mustacchi 		[EM_TI_PRU] = "TI_PRU",
1102*c4397e61SRobert Mustacchi 		[EM_MMDSP_PLUS] = "MMDSP_PLUS",
1103*c4397e61SRobert Mustacchi 		[EM_CYPRESS_M8C] = "M8C",
1104*c4397e61SRobert Mustacchi 		[EM_R32C] = "R32C",
1105*c4397e61SRobert Mustacchi 		[EM_TRIMEDIA] = "TriMedia",
1106*c4397e61SRobert Mustacchi 		[EM_QDSP6] = "QDSP6",
1107*c4397e61SRobert Mustacchi 		[EM_8051] = "8051",
1108*c4397e61SRobert Mustacchi 		[EM_STXP7X] = "STxP7x",
1109*c4397e61SRobert Mustacchi 		[EM_NDS32] = "NDS32",
1110*c4397e61SRobert Mustacchi 		[EM_ECOG1] = "eCOG1X",
1111*c4397e61SRobert Mustacchi 		[EM_MAXQ30] = "MAXQ30",
1112*c4397e61SRobert Mustacchi 		[EM_XIMO16] = "XIMO16",
1113*c4397e61SRobert Mustacchi 		[EM_MANIK] = "M2000",
1114*c4397e61SRobert Mustacchi 		[EM_CRAYNV2] = "CRAYNV2",
1115*c4397e61SRobert Mustacchi 		[EM_RX] = "RX",
1116*c4397e61SRobert Mustacchi 		[EM_METAG] = "METAG",
1117*c4397e61SRobert Mustacchi 		[EM_MCST_ELBRUS] = "Elbrus",
1118*c4397e61SRobert Mustacchi 		[EM_ECOG16] = "eCOG16",
1119*c4397e61SRobert Mustacchi 		[EM_CR16] = "CR16",
1120*c4397e61SRobert Mustacchi 		[EM_ETPU] = "ETPU",
1121*c4397e61SRobert Mustacchi 		[EM_SLE9X] = "SLE9X",
1122*c4397e61SRobert Mustacchi 		[EM_L10M] = "L10M",
1123*c4397e61SRobert Mustacchi 		[EM_K10M] = "K10M",
1124*c4397e61SRobert Mustacchi 
1125*c4397e61SRobert Mustacchi 		[EM_AARCH64] = "aarch64",
1126*c4397e61SRobert Mustacchi 
1127*c4397e61SRobert Mustacchi 		[EM_AVR32] = "AVR32",
1128*c4397e61SRobert Mustacchi 		[EM_STM8] = "STM8",
1129*c4397e61SRobert Mustacchi 		[EM_TILE64] = "TILE64",
1130*c4397e61SRobert Mustacchi 		[EM_TILEPRO] = "TILEPRO",
1131*c4397e61SRobert Mustacchi 		[EM_MICROBLAZE] = "MicroBlaze",
1132*c4397e61SRobert Mustacchi 		[EM_CUDA] = "CUDA",
1133*c4397e61SRobert Mustacchi 		[EM_TILEGX] = "TILE-Gx",
1134*c4397e61SRobert Mustacchi 		[EM_CLOUDSHIELD] = "CloudShield",
1135*c4397e61SRobert Mustacchi 		[EM_COREA_1ST] = "CORE-A 1st",
1136*c4397e61SRobert Mustacchi 		[EM_COREA_2ND] = "CORE-A 2nd",
1137*c4397e61SRobert Mustacchi 		[EM_ARC_COMPACT2] = "ARCompact V2",
1138*c4397e61SRobert Mustacchi 		[EM_OPEN8] = "Open8",
1139*c4397e61SRobert Mustacchi 		[EM_RL78] = "RL78",
1140*c4397e61SRobert Mustacchi 		[EM_VIDEOCORE5] = "VideoCore V",
1141*c4397e61SRobert Mustacchi 		[EM_78KOR] = "78KOR",
1142*c4397e61SRobert Mustacchi 		[EM_56800EX] = "56800EX",
1143*c4397e61SRobert Mustacchi 		[EM_BA1] = "BA1",
1144*c4397e61SRobert Mustacchi 		[EM_BA2] = "BA2",
1145*c4397e61SRobert Mustacchi 		[EM_XCORE] = "xCORE",
1146*c4397e61SRobert Mustacchi 		[EM_MCHP_PIC] = "MCHP_PIC",
1147*c4397e61SRobert Mustacchi 		[EM_KM32] = "KM32",
1148*c4397e61SRobert Mustacchi 		[EM_KMX32] = "KMX32",
1149*c4397e61SRobert Mustacchi 		[EM_KMX16] = "KMX16",
1150*c4397e61SRobert Mustacchi 		[EM_KMX8] = "KMX8",
1151*c4397e61SRobert Mustacchi 		[EM_KVARC] = "KVARC",
1152*c4397e61SRobert Mustacchi 		[EM_CDP] = "CDP",
1153*c4397e61SRobert Mustacchi 		[EM_COGE] = "COGE",
1154*c4397e61SRobert Mustacchi 		[EM_COOL] = "CoolEngine",
1155*c4397e61SRobert Mustacchi 		[EM_NORC] = "NORC",
1156*c4397e61SRobert Mustacchi 		[EM_CSR_KALIMBA] = "Kalimba",
1157*c4397e61SRobert Mustacchi 		[EM_Z80] = "Zilog Z80",
1158*c4397e61SRobert Mustacchi 		[EM_VISIUM] = "VISIUMcore",
1159*c4397e61SRobert Mustacchi 		[EM_FT32] = "FT32",
1160*c4397e61SRobert Mustacchi 		[EM_MOXIE] = "Moxie",
1161*c4397e61SRobert Mustacchi 		[EM_AMDGPU] = "AMD GPU",
1162*c4397e61SRobert Mustacchi 		[EM_RISCV] = "RISC-V"
1163ca1e0a81Sab196087 	};
1164ca1e0a81Sab196087 	/* If new machine is added, refuse to compile until we're updated */
1165*c4397e61SRobert Mustacchi #if EM_NUM != 244
1166ca1e0a81Sab196087 #error "Number of known ELF machine constants has changed"
1167ca1e0a81Sab196087 #endif
1168ca1e0a81Sab196087 
1169ca1e0a81Sab196087 	const char *str;
1170ca1e0a81Sab196087 
1171ca1e0a81Sab196087 	if ((machine < EM_NONE) || (machine >= EM_NUM))
1172ca1e0a81Sab196087 		machine = EM_NONE;
1173ca1e0a81Sab196087 
1174ca1e0a81Sab196087 	str = mach_str[machine];
1175ca1e0a81Sab196087 	if (str)
1176ca1e0a81Sab196087 		(void) printf(" %s", str);
11777c478bd9Sstevel@tonic-gate }
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate static void
print_elf_datatype(int datatype)11807c478bd9Sstevel@tonic-gate print_elf_datatype(int datatype)
11817c478bd9Sstevel@tonic-gate {
11827c478bd9Sstevel@tonic-gate 	switch (datatype) {
11837c478bd9Sstevel@tonic-gate 	case ELFDATA2LSB:
1184ca1e0a81Sab196087 		(void) printf(" LSB");
11857c478bd9Sstevel@tonic-gate 		break;
11867c478bd9Sstevel@tonic-gate 	case ELFDATA2MSB:
1187ca1e0a81Sab196087 		(void) printf(" MSB");
11887c478bd9Sstevel@tonic-gate 		break;
11897c478bd9Sstevel@tonic-gate 	default:
11907c478bd9Sstevel@tonic-gate 		break;
11917c478bd9Sstevel@tonic-gate 	}
11927c478bd9Sstevel@tonic-gate }
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate static void
print_elf_class(int class)11957c478bd9Sstevel@tonic-gate print_elf_class(int class)
11967c478bd9Sstevel@tonic-gate {
11977c478bd9Sstevel@tonic-gate 	switch (class) {
11987c478bd9Sstevel@tonic-gate 	case ELFCLASS32:
11997c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("32-bit"));
12007c478bd9Sstevel@tonic-gate 		break;
12017c478bd9Sstevel@tonic-gate 	case ELFCLASS64:
12027c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("64-bit"));
12037c478bd9Sstevel@tonic-gate 		break;
12047c478bd9Sstevel@tonic-gate 	default:
12057c478bd9Sstevel@tonic-gate 		break;
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate }
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate static void
print_elf_flags(Elf_Info EI)1210c2c65e21Sny155746 print_elf_flags(Elf_Info EI)
12117c478bd9Sstevel@tonic-gate {
1212c2c65e21Sny155746 	unsigned int flags;
1213c2c65e21Sny155746 
1214c2c65e21Sny155746 	flags = EI.flags;
1215c2c65e21Sny155746 	switch (EI.machine) {
12167c478bd9Sstevel@tonic-gate 	case EM_SPARCV9:
12177c478bd9Sstevel@tonic-gate 		if (flags & EF_SPARC_EXT_MASK) {
12187c478bd9Sstevel@tonic-gate 			if (flags & EF_SPARC_SUN_US3) {
12197c478bd9Sstevel@tonic-gate 				(void) printf("%s", gettext(
12207c478bd9Sstevel@tonic-gate 				    ", UltraSPARC3 Extensions Required"));
12217c478bd9Sstevel@tonic-gate 			} else if (flags & EF_SPARC_SUN_US1) {
12227c478bd9Sstevel@tonic-gate 				(void) printf("%s", gettext(
12237c478bd9Sstevel@tonic-gate 				    ", UltraSPARC1 Extensions Required"));
12247c478bd9Sstevel@tonic-gate 			}
12257c478bd9Sstevel@tonic-gate 			if (flags & EF_SPARC_HAL_R1)
12267c478bd9Sstevel@tonic-gate 				(void) printf("%s", gettext(
12277c478bd9Sstevel@tonic-gate 				    ", HaL R1 Extensions Required"));
12287c478bd9Sstevel@tonic-gate 		}
12297c478bd9Sstevel@tonic-gate 		break;
12307c478bd9Sstevel@tonic-gate 	case EM_SPARC32PLUS:
12317c478bd9Sstevel@tonic-gate 		if (flags & EF_SPARC_32PLUS)
12327c478bd9Sstevel@tonic-gate 			(void) printf("%s", gettext(", V8+ Required"));
12337c478bd9Sstevel@tonic-gate 		if (flags & EF_SPARC_SUN_US3) {
12347c478bd9Sstevel@tonic-gate 			(void) printf("%s",
12357c478bd9Sstevel@tonic-gate 			    gettext(", UltraSPARC3 Extensions Required"));
12367c478bd9Sstevel@tonic-gate 		} else if (flags & EF_SPARC_SUN_US1) {
12377c478bd9Sstevel@tonic-gate 			(void) printf("%s",
12387c478bd9Sstevel@tonic-gate 			    gettext(", UltraSPARC1 Extensions Required"));
12397c478bd9Sstevel@tonic-gate 		}
12407c478bd9Sstevel@tonic-gate 		if (flags & EF_SPARC_HAL_R1)
12417c478bd9Sstevel@tonic-gate 			(void) printf("%s",
12427c478bd9Sstevel@tonic-gate 			    gettext(", HaL R1 Extensions Required"));
12437c478bd9Sstevel@tonic-gate 		break;
12447c478bd9Sstevel@tonic-gate 	default:
12457c478bd9Sstevel@tonic-gate 		break;
12467c478bd9Sstevel@tonic-gate 	}
12477c478bd9Sstevel@tonic-gate }
12487c478bd9Sstevel@tonic-gate 
1249c2c65e21Sny155746 /*
1250c2c65e21Sny155746  * check_ident:	checks the ident field of the presumeably
1251c2c65e21Sny155746  *		elf file. If check fails, this is not an
1252c2c65e21Sny155746  *		elf file.
1253c2c65e21Sny155746  */
12547c478bd9Sstevel@tonic-gate static int
check_ident(unsigned char * ident,int fd)1255c2c65e21Sny155746 check_ident(unsigned char *ident, int fd)
12567c478bd9Sstevel@tonic-gate {
1257c2c65e21Sny155746 	int class;
1258c2c65e21Sny155746 	if (pread64(fd, ident, EI_NIDENT, 0) != EI_NIDENT)
1259c2c65e21Sny155746 		return (ELF_READ_FAIL);
1260c2c65e21Sny155746 	class = ident[EI_CLASS];
1261c2c65e21Sny155746 	if (class != ELFCLASS32 && class != ELFCLASS64)
1262c2c65e21Sny155746 		return (ELF_READ_FAIL);
1263c2c65e21Sny155746 	if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
1264c2c65e21Sny155746 	    ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3)
1265c2c65e21Sny155746 		return (ELF_READ_FAIL);
1266c2c65e21Sny155746 
1267c2c65e21Sny155746 	return (ELF_READ_OKAY);
1268c2c65e21Sny155746 }
1269c2c65e21Sny155746 
1270c2c65e21Sny155746 static int
elf_check(char * file)1271c2c65e21Sny155746 elf_check(char *file)
1272c2c65e21Sny155746 {
1273c2c65e21Sny155746 	Elf_Info EInfo;
1274c2c65e21Sny155746 	int class, version, format;
1275c2c65e21Sny155746 	unsigned char ident[EI_NIDENT];
1276c2c65e21Sny155746 
1277c2c65e21Sny155746 	(void) memset(&EInfo, 0, sizeof (Elf_Info));
1278c2c65e21Sny155746 	EInfo.file = file;
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	/*
1281c2c65e21Sny155746 	 * Verify information in file indentifier.
1282c2c65e21Sny155746 	 * Return quietly if not elf; Different type of file.
12837c478bd9Sstevel@tonic-gate 	 */
1284c2c65e21Sny155746 	if (check_ident(ident, elffd) == ELF_READ_FAIL)
12857c478bd9Sstevel@tonic-gate 		return (1);
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	/*
1288c2c65e21Sny155746 	 * Read the elf headers for processing and get the
1289c2c65e21Sny155746 	 * get the needed information in Elf_Info struct.
12907c478bd9Sstevel@tonic-gate 	 */
1291c2c65e21Sny155746 	class = ident[EI_CLASS];
1292c2c65e21Sny155746 	if (class == ELFCLASS32) {
1293c2c65e21Sny155746 		if (elf_read32(elffd, &EInfo) == ELF_READ_FAIL) {
12949a411307Srie 			(void) fprintf(stderr, gettext("%s: %s: can't "
1295c2c65e21Sny155746 			    "read ELF header\n"), File, file);
12967c478bd9Sstevel@tonic-gate 			return (1);
12977c478bd9Sstevel@tonic-gate 		}
1298c2c65e21Sny155746 	} else if (class == ELFCLASS64) {
1299c2c65e21Sny155746 		if (elf_read64(elffd, &EInfo) == ELF_READ_FAIL) {
1300c2c65e21Sny155746 			(void) fprintf(stderr, gettext("%s: %s: can't "
1301c2c65e21Sny155746 			    "read ELF header\n"), File, file);
13027c478bd9Sstevel@tonic-gate 			return (1);
13037c478bd9Sstevel@tonic-gate 		}
1304c2c65e21Sny155746 	} else {
1305c2c65e21Sny155746 		/* something wrong */
1306c2c65e21Sny155746 		return (1);
1307c2c65e21Sny155746 	}
1308c2c65e21Sny155746 
1309c2c65e21Sny155746 	/* version not in ident then 1 */
1310c2c65e21Sny155746 	version = ident[EI_VERSION] ? ident[EI_VERSION] : 1;
1311c2c65e21Sny155746 
1312c2c65e21Sny155746 	format = ident[EI_DATA];
13137c478bd9Sstevel@tonic-gate 	(void) printf("%s", gettext("ELF"));
1314c2c65e21Sny155746 	print_elf_class(class);
1315c2c65e21Sny155746 	print_elf_datatype(format);
1316c2c65e21Sny155746 	print_elf_type(EInfo);
13177c478bd9Sstevel@tonic-gate 
1318c2c65e21Sny155746 	if (EInfo.core_type != EC_NOTCORE) {
1319c2c65e21Sny155746 		/* Print what kind of core is this */
1320c2c65e21Sny155746 		if (EInfo.core_type == EC_OLDCORE)
1321c2c65e21Sny155746 			(void) printf(" %s", gettext("pre-2.6 core file"));
1322c2c65e21Sny155746 		else
1323c2c65e21Sny155746 			(void) printf(" %s", gettext("core file"));
1324c2c65e21Sny155746 	}
1325c2c65e21Sny155746 
1326c2c65e21Sny155746 	/* Print machine info */
1327c2c65e21Sny155746 	print_elf_machine(EInfo.machine);
1328c2c65e21Sny155746 
1329c2c65e21Sny155746 	/* Print Version */
1330c2c65e21Sny155746 	if (version == 1)
1331c2c65e21Sny155746 		(void) printf(" %s %d", gettext("Version"), version);
1332c2c65e21Sny155746 
1333c2c65e21Sny155746 	/* Print Flags */
1334c2c65e21Sny155746 	print_elf_flags(EInfo);
1335c2c65e21Sny155746 
1336c2c65e21Sny155746 	/* Last bit, if it is a core */
1337c2c65e21Sny155746 	if (EInfo.core_type != EC_NOTCORE) {
1338c2c65e21Sny155746 		/* Print the program name that dumped this core */
1339c2c65e21Sny155746 		(void) printf(gettext(", from '%s'"), EInfo.fname);
1340c2c65e21Sny155746 		return (0);
1341c2c65e21Sny155746 	}
1342c2c65e21Sny155746 
1343c2c65e21Sny155746 	/* Print Capabilities */
1344c2c65e21Sny155746 	if (EInfo.cap_str[0] != '\0')
1345c2c65e21Sny155746 		(void) printf(" [%s]", EInfo.cap_str);
1346c2c65e21Sny155746 
1347c2c65e21Sny155746 	if ((EInfo.type != ET_EXEC) && (EInfo.type != ET_DYN))
13487c478bd9Sstevel@tonic-gate 		return (0);
13497c478bd9Sstevel@tonic-gate 
1350c2c65e21Sny155746 	/* Print if it is dynamically linked */
1351c2c65e21Sny155746 	if (EInfo.dynamic)
13527c478bd9Sstevel@tonic-gate 		(void) printf(gettext(", dynamically linked"));
13537c478bd9Sstevel@tonic-gate 	else
13547c478bd9Sstevel@tonic-gate 		(void) printf(gettext(", statically linked"));
13557c478bd9Sstevel@tonic-gate 
1356c2c65e21Sny155746 	/* Printf it it is stripped */
1357c2c65e21Sny155746 	if (EInfo.stripped & E_SYMTAB) {
13587c478bd9Sstevel@tonic-gate 		(void) printf(gettext(", not stripped"));
1359c2c65e21Sny155746 		if (!(EInfo.stripped & E_DBGINF)) {
13607c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
13617c478bd9Sstevel@tonic-gate 			    ", no debugging information available"));
13627c478bd9Sstevel@tonic-gate 		}
13637c478bd9Sstevel@tonic-gate 	} else {
13647c478bd9Sstevel@tonic-gate 		(void) printf(gettext(", stripped"));
13657c478bd9Sstevel@tonic-gate 	}
1366c2c65e21Sny155746 
1367c2c65e21Sny155746 	return (0);
13687c478bd9Sstevel@tonic-gate }
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate /*
1371c13de8f6Sab196087  * is_rtld_config - If file is a runtime linker config file, prints
1372c13de8f6Sab196087  * the description and returns True (1). Otherwise, silently returns
1373c13de8f6Sab196087  * False (0).
1374c13de8f6Sab196087  */
1375c13de8f6Sab196087 int
is_rtld_config(void)1376c13de8f6Sab196087 is_rtld_config(void)
1377c13de8f6Sab196087 {
1378c13de8f6Sab196087 	Rtc_id *id;
1379c13de8f6Sab196087 
1380c13de8f6Sab196087 	if ((fbsz >= sizeof (*id)) && RTC_ID_TEST(fbuf)) {
1381c13de8f6Sab196087 		(void) printf(gettext("Runtime Linking Configuration"));
1382c13de8f6Sab196087 		id = (Rtc_id *) fbuf;
1383c13de8f6Sab196087 		print_elf_class(id->id_class);
1384c13de8f6Sab196087 		print_elf_datatype(id->id_data);
1385c13de8f6Sab196087 		print_elf_machine(id->id_machine);
1386c13de8f6Sab196087 		(void) printf("\n");
1387c13de8f6Sab196087 		return (1);
1388c13de8f6Sab196087 	}
1389c13de8f6Sab196087 
1390c13de8f6Sab196087 	return (0);
1391c13de8f6Sab196087 }
1392c13de8f6Sab196087 
1393c13de8f6Sab196087 /*
13947c478bd9Sstevel@tonic-gate  * lookup -
13957c478bd9Sstevel@tonic-gate  * Attempts to match one of the strings from a list, 'tab',
13967c478bd9Sstevel@tonic-gate  * with what is in the file, starting at the current index position 'i'.
13977c478bd9Sstevel@tonic-gate  * Looks past any initial whitespace and expects whitespace or other
13987c478bd9Sstevel@tonic-gate  * delimiting characters to follow the matched string.
13997c478bd9Sstevel@tonic-gate  * A match identifies the file as being 'assembler', 'fortran', 'c', etc.
14007c478bd9Sstevel@tonic-gate  * Returns 1 for a successful match, 0 otherwise.
14017c478bd9Sstevel@tonic-gate  */
14027c478bd9Sstevel@tonic-gate static int
lookup(char ** tab)14037c478bd9Sstevel@tonic-gate lookup(char **tab)
14047c478bd9Sstevel@tonic-gate {
14057c478bd9Sstevel@tonic-gate 	register char	r;
14067c478bd9Sstevel@tonic-gate 	register int	k, j, l;
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	while (fbuf[i] == ' ' || fbuf[i] == '\t' || fbuf[i] == '\n')
14097c478bd9Sstevel@tonic-gate 		i++;
14107c478bd9Sstevel@tonic-gate 	for (j = 0; tab[j] != 0; j++) {
14117c478bd9Sstevel@tonic-gate 		l = 0;
1412ca3e8d88SDave Plauger 		for (k = i; ((r = tab[j][l++]) == fbuf[k] && r != '\0'); k++)
1413ca3e8d88SDave Plauger 			;
14147c478bd9Sstevel@tonic-gate 		if (r == '\0')
14157c478bd9Sstevel@tonic-gate 			if (fbuf[k] == ' ' || fbuf[k] == '\n' ||
14167c478bd9Sstevel@tonic-gate 			    fbuf[k] == '\t' || fbuf[k] == '{' ||
14177c478bd9Sstevel@tonic-gate 			    fbuf[k] == '/') {
14187c478bd9Sstevel@tonic-gate 				i = k;
14197c478bd9Sstevel@tonic-gate 				return (1);
14207c478bd9Sstevel@tonic-gate 			}
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 	return (0);
14237c478bd9Sstevel@tonic-gate }
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate /*
14267c478bd9Sstevel@tonic-gate  * ccom -
14277c478bd9Sstevel@tonic-gate  * Increments the current index 'i' into the file buffer 'fbuf' past any
14287c478bd9Sstevel@tonic-gate  * whitespace lines and C-style comments found, starting at the current
14297c478bd9Sstevel@tonic-gate  * position of 'i'.  Returns 1 as long as we don't increment i past the
14307c478bd9Sstevel@tonic-gate  * size of fbuf (fbsz).  Otherwise, returns 0.
14317c478bd9Sstevel@tonic-gate  */
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate static int
ccom(void)14347c478bd9Sstevel@tonic-gate ccom(void)
14357c478bd9Sstevel@tonic-gate {
14367c478bd9Sstevel@tonic-gate 	register char	cc;
14377c478bd9Sstevel@tonic-gate 	int		len;
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	while ((cc = fbuf[i]) == ' ' || cc == '\t' || cc == '\n')
14407c478bd9Sstevel@tonic-gate 		if (i++ >= fbsz)
14417c478bd9Sstevel@tonic-gate 			return (0);
14427c478bd9Sstevel@tonic-gate 	if (fbuf[i] == '/' && fbuf[i+1] == '*') {
14437c478bd9Sstevel@tonic-gate 		i += 2;
14447c478bd9Sstevel@tonic-gate 		while (fbuf[i] != '*' || fbuf[i+1] != '/') {
14457c478bd9Sstevel@tonic-gate 			if (fbuf[i] == '\\')
14467c478bd9Sstevel@tonic-gate 				i++;
14477c478bd9Sstevel@tonic-gate 			if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
14487c478bd9Sstevel@tonic-gate 				len = 1;
14497c478bd9Sstevel@tonic-gate 			i += len;
14507c478bd9Sstevel@tonic-gate 			if (i >= fbsz)
14517c478bd9Sstevel@tonic-gate 				return (0);
14527c478bd9Sstevel@tonic-gate 		}
14537c478bd9Sstevel@tonic-gate 		if ((i += 2) >= fbsz)
14547c478bd9Sstevel@tonic-gate 			return (0);
14557c478bd9Sstevel@tonic-gate 	}
14567c478bd9Sstevel@tonic-gate 	if (fbuf[i] == '\n')
14577c478bd9Sstevel@tonic-gate 		if (ccom() == 0)
14587c478bd9Sstevel@tonic-gate 			return (0);
14597c478bd9Sstevel@tonic-gate 	return (1);
14607c478bd9Sstevel@tonic-gate }
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate /*
14637c478bd9Sstevel@tonic-gate  * ascom -
14647c478bd9Sstevel@tonic-gate  * Increments the current index 'i' into the file buffer 'fbuf' past
14657c478bd9Sstevel@tonic-gate  * consecutive assembler program comment lines starting with ASCOMCHAR,
14667c478bd9Sstevel@tonic-gate  * starting at the current position of 'i'.
14677c478bd9Sstevel@tonic-gate  * Returns 1 as long as we don't increment i past the
14687c478bd9Sstevel@tonic-gate  * size of fbuf (fbsz).  Otherwise returns 0.
14697c478bd9Sstevel@tonic-gate  */
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate static int
ascom(void)14727c478bd9Sstevel@tonic-gate ascom(void)
14737c478bd9Sstevel@tonic-gate {
14747c478bd9Sstevel@tonic-gate 	while (fbuf[i] == ASCOMCHAR) {
14757c478bd9Sstevel@tonic-gate 		i++;
14767c478bd9Sstevel@tonic-gate 		while (fbuf[i++] != '\n')
14777c478bd9Sstevel@tonic-gate 			if (i >= fbsz)
14787c478bd9Sstevel@tonic-gate 				return (0);
14797c478bd9Sstevel@tonic-gate 		while (fbuf[i] == '\n')
14807c478bd9Sstevel@tonic-gate 			if (i++ >= fbsz)
14817c478bd9Sstevel@tonic-gate 				return (0);
14827c478bd9Sstevel@tonic-gate 	}
14837c478bd9Sstevel@tonic-gate 	return (1);
14847c478bd9Sstevel@tonic-gate }
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate static int
sccs(void)14877c478bd9Sstevel@tonic-gate sccs(void)
14887c478bd9Sstevel@tonic-gate {				/* look for "1hddddd" where d is a digit */
14897c478bd9Sstevel@tonic-gate 	register int j;
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	if (fbuf[0] == 1 && fbuf[1] == 'h') {
14927c478bd9Sstevel@tonic-gate 		for (j = 2; j <= 6; j++) {
14937c478bd9Sstevel@tonic-gate 			if (isdigit(fbuf[j]))
14947c478bd9Sstevel@tonic-gate 				continue;
14957c478bd9Sstevel@tonic-gate 			else
14967c478bd9Sstevel@tonic-gate 				return (0);
14977c478bd9Sstevel@tonic-gate 		}
14987c478bd9Sstevel@tonic-gate 	} else {
14997c478bd9Sstevel@tonic-gate 		return (0);
15007c478bd9Sstevel@tonic-gate 	}
15017c478bd9Sstevel@tonic-gate 	return (1);
15027c478bd9Sstevel@tonic-gate }
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate static int
english(char * bp,int n)15057c478bd9Sstevel@tonic-gate english(char *bp, int n)
15067c478bd9Sstevel@tonic-gate {
15077c478bd9Sstevel@tonic-gate #define	NASC 128		/* number of ascii char ?? */
15087c478bd9Sstevel@tonic-gate 	register int	j, vow, freq, rare, len;
15097c478bd9Sstevel@tonic-gate 	register int	badpun = 0, punct = 0;
15107c478bd9Sstevel@tonic-gate 	int	ct[NASC];
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	if (n < 50)
15137c478bd9Sstevel@tonic-gate 		return (0); /* no point in statistics on squibs */
15147c478bd9Sstevel@tonic-gate 	for (j = 0; j < NASC; j++)
15157c478bd9Sstevel@tonic-gate 		ct[j] = 0;
15167c478bd9Sstevel@tonic-gate 	for (j = 0; j < n; j += len) {
15177c478bd9Sstevel@tonic-gate 		if ((unsigned char)bp[j] < NASC)
15187c478bd9Sstevel@tonic-gate 			ct[bp[j]|040]++;
15197c478bd9Sstevel@tonic-gate 		switch (bp[j]) {
15207c478bd9Sstevel@tonic-gate 		case '.':
15217c478bd9Sstevel@tonic-gate 		case ',':
15227c478bd9Sstevel@tonic-gate 		case ')':
15237c478bd9Sstevel@tonic-gate 		case '%':
15247c478bd9Sstevel@tonic-gate 		case ';':
15257c478bd9Sstevel@tonic-gate 		case ':':
15267c478bd9Sstevel@tonic-gate 		case '?':
15277c478bd9Sstevel@tonic-gate 			punct++;
15287c478bd9Sstevel@tonic-gate 			if (j < n-1 && bp[j+1] != ' ' && bp[j+1] != '\n')
15297c478bd9Sstevel@tonic-gate 				badpun++;
15307c478bd9Sstevel@tonic-gate 		}
15317c478bd9Sstevel@tonic-gate 		if ((len = mblen(&bp[j], MB_CUR_MAX)) <= 0)
15327c478bd9Sstevel@tonic-gate 			len = 1;
15337c478bd9Sstevel@tonic-gate 	}
15347c478bd9Sstevel@tonic-gate 	if (badpun*5 > punct)
15357c478bd9Sstevel@tonic-gate 		return (0);
15367c478bd9Sstevel@tonic-gate 	vow = ct['a'] + ct['e'] + ct['i'] + ct['o'] + ct['u'];
15377c478bd9Sstevel@tonic-gate 	freq = ct['e'] + ct['t'] + ct['a'] + ct['i'] + ct['o'] + ct['n'];
15387c478bd9Sstevel@tonic-gate 	rare = ct['v'] + ct['j'] + ct['k'] + ct['q'] + ct['x'] + ct['z'];
15397c478bd9Sstevel@tonic-gate 	if (2*ct[';'] > ct['e'])
15407c478bd9Sstevel@tonic-gate 		return (0);
15417c478bd9Sstevel@tonic-gate 	if ((ct['>'] + ct['<'] + ct['/']) > ct['e'])
15427c478bd9Sstevel@tonic-gate 		return (0);	/* shell file test */
15437c478bd9Sstevel@tonic-gate 	return (vow * 5 >= n - ct[' '] && freq >= 10 * rare);
15447c478bd9Sstevel@tonic-gate }
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate static int
shellscript(char buf[],struct stat64 * sb)15487c478bd9Sstevel@tonic-gate shellscript(char buf[], struct stat64 *sb)
15497c478bd9Sstevel@tonic-gate {
15507c478bd9Sstevel@tonic-gate 	char *tp, *cp, *xp, *up, *gp;
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	cp = strchr(buf, '\n');
15537c478bd9Sstevel@tonic-gate 	if (cp == NULL || cp - fbuf > fbsz)
15547c478bd9Sstevel@tonic-gate 		return (0);
15557c478bd9Sstevel@tonic-gate 	for (tp = buf; tp != cp && isspace((unsigned char)*tp); tp++)
15567c478bd9Sstevel@tonic-gate 		if (!isascii(*tp))
15577c478bd9Sstevel@tonic-gate 			return (0);
15587c478bd9Sstevel@tonic-gate 	for (xp = tp; tp != cp && !isspace((unsigned char)*tp); tp++)
15597c478bd9Sstevel@tonic-gate 		if (!isascii(*tp))
15607c478bd9Sstevel@tonic-gate 			return (0);
15617c478bd9Sstevel@tonic-gate 	if (tp == xp)
15627c478bd9Sstevel@tonic-gate 		return (0);
15637c478bd9Sstevel@tonic-gate 	if (sb->st_mode & S_ISUID)
15647c478bd9Sstevel@tonic-gate 		up = gettext("set-uid ");
15657c478bd9Sstevel@tonic-gate 	else
15667c478bd9Sstevel@tonic-gate 		up = "";
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	if (sb->st_mode & S_ISGID)
15697c478bd9Sstevel@tonic-gate 		gp = gettext("set-gid ");
15707c478bd9Sstevel@tonic-gate 	else
15717c478bd9Sstevel@tonic-gate 		gp = "";
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	if (strncmp(xp, "/bin/sh", tp - xp) == 0)
15747c478bd9Sstevel@tonic-gate 		xp = gettext("shell");
15757c478bd9Sstevel@tonic-gate 	else if (strncmp(xp, "/bin/csh", tp - xp) == 0)
15767c478bd9Sstevel@tonic-gate 		xp = gettext("c-shell");
15777c478bd9Sstevel@tonic-gate 	else if (strncmp(xp, "/usr/sbin/dtrace", tp - xp) == 0)
15787c478bd9Sstevel@tonic-gate 		xp = gettext("DTrace");
15797c478bd9Sstevel@tonic-gate 	else
15807c478bd9Sstevel@tonic-gate 		*tp = '\0';
15817c478bd9Sstevel@tonic-gate 	/*
15827c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
15837c478bd9Sstevel@tonic-gate 	 * This message is printed by file command for shell scripts.
15847c478bd9Sstevel@tonic-gate 	 * The first %s is for the translation for "set-uid " (if the script
15857c478bd9Sstevel@tonic-gate 	 *   has the set-uid bit set), or is for an empty string (if the
15867c478bd9Sstevel@tonic-gate 	 *   script does not have the set-uid bit set).
15877c478bd9Sstevel@tonic-gate 	 * Similarly, the second %s is for the translation for "set-gid ",
15887c478bd9Sstevel@tonic-gate 	 *   or is for an empty string.
15897c478bd9Sstevel@tonic-gate 	 * The third %s is for the translation for either: "shell", "c-shell",
15907c478bd9Sstevel@tonic-gate 	 *   or "DTrace", or is for the pathname of the program the script
15917c478bd9Sstevel@tonic-gate 	 *   executes.
15927c478bd9Sstevel@tonic-gate 	 */
15937c478bd9Sstevel@tonic-gate 	(void) printf(gettext("%s%sexecutable %s script\n"), up, gp, xp);
15947c478bd9Sstevel@tonic-gate 	return (1);
15957c478bd9Sstevel@tonic-gate }
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate static int
get_door_target(char * file,char * buf,size_t bufsize)15987c478bd9Sstevel@tonic-gate get_door_target(char *file, char *buf, size_t bufsize)
15997c478bd9Sstevel@tonic-gate {
16007c478bd9Sstevel@tonic-gate 	int fd;
16017c478bd9Sstevel@tonic-gate 	door_info_t di;
16027c478bd9Sstevel@tonic-gate 	psinfo_t psinfo;
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	if ((fd = open64(file, O_RDONLY)) < 0 ||
16057c478bd9Sstevel@tonic-gate 	    door_info(fd, &di) != 0) {
16067c478bd9Sstevel@tonic-gate 		if (fd >= 0)
16077c478bd9Sstevel@tonic-gate 			(void) close(fd);
16087c478bd9Sstevel@tonic-gate 		return (-1);
16097c478bd9Sstevel@tonic-gate 	}
16107c478bd9Sstevel@tonic-gate 	(void) close(fd);
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "/proc/%ld/psinfo", di.di_target);
16137c478bd9Sstevel@tonic-gate 	if ((fd = open64(buf, O_RDONLY)) < 0 ||
16147c478bd9Sstevel@tonic-gate 	    read(fd, &psinfo, sizeof (psinfo)) != sizeof (psinfo)) {
16157c478bd9Sstevel@tonic-gate 		if (fd >= 0)
16167c478bd9Sstevel@tonic-gate 			(void) close(fd);
16177c478bd9Sstevel@tonic-gate 		return (-1);
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 	(void) close(fd);
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, bufsize, "%s[%ld]", psinfo.pr_fname, di.di_target);
16227c478bd9Sstevel@tonic-gate 	return (0);
16237c478bd9Sstevel@tonic-gate }
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate /*
16267c478bd9Sstevel@tonic-gate  * ZIP file header information
16277c478bd9Sstevel@tonic-gate  */
16287c478bd9Sstevel@tonic-gate #define	SIGSIZ		4
16297c478bd9Sstevel@tonic-gate #define	LOCSIG		"PK\003\004"
16307c478bd9Sstevel@tonic-gate #define	LOCHDRSIZ	30
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate #define	CH(b, n)	(((unsigned char *)(b))[n])
16337c478bd9Sstevel@tonic-gate #define	SH(b, n)	(CH(b, n) | (CH(b, n+1) << 8))
16347c478bd9Sstevel@tonic-gate #define	LG(b, n)	(SH(b, n) | (SH(b, n+2) << 16))
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate #define	LOCNAM(b)	(SH(b, 26))	/* filename size */
16377c478bd9Sstevel@tonic-gate #define	LOCEXT(b)	(SH(b, 28))	/* extra field size */
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate #define	XFHSIZ		4		/* header id, data size */
16407c478bd9Sstevel@tonic-gate #define	XFHID(b)	(SH(b, 0))	/* extract field header id */
16417c478bd9Sstevel@tonic-gate #define	XFDATASIZ(b)	(SH(b, 2))	/* extract field data size */
16427c478bd9Sstevel@tonic-gate #define	XFJAVASIG	0xcafe		/* java executables */
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate static int
zipfile(char * fbuf,int fd)16457c478bd9Sstevel@tonic-gate zipfile(char *fbuf, int fd)
16467c478bd9Sstevel@tonic-gate {
16477c478bd9Sstevel@tonic-gate 	off_t xoff, xoff_end;
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	if (strncmp(fbuf, LOCSIG, SIGSIZ) != 0)
16507c478bd9Sstevel@tonic-gate 		return (0);
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	xoff = LOCHDRSIZ + LOCNAM(fbuf);
16537c478bd9Sstevel@tonic-gate 	xoff_end = xoff + LOCEXT(fbuf);
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	while (xoff < xoff_end) {
16567c478bd9Sstevel@tonic-gate 		char xfhdr[XFHSIZ];
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 		if (pread(fd, xfhdr, XFHSIZ, xoff) != XFHSIZ)
16597c478bd9Sstevel@tonic-gate 			break;
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 		if (XFHID(xfhdr) == XFJAVASIG) {
1662ea51a530Sny155746 			(void) printf("%s\n", gettext("java archive file"));
16637c478bd9Sstevel@tonic-gate 			return (1);
16647c478bd9Sstevel@tonic-gate 		}
16657c478bd9Sstevel@tonic-gate 		xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr);
16667c478bd9Sstevel@tonic-gate 	}
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	/*
16697c478bd9Sstevel@tonic-gate 	 * We could just print "ZIP archive" here.
16707c478bd9Sstevel@tonic-gate 	 *
16717c478bd9Sstevel@tonic-gate 	 * However, customers may be using their own entries in
16727c478bd9Sstevel@tonic-gate 	 * /etc/magic to distinguish one kind of ZIP file from another, so
16737c478bd9Sstevel@tonic-gate 	 * let's defer the printing of "ZIP archive" to there.
16747c478bd9Sstevel@tonic-gate 	 */
16757c478bd9Sstevel@tonic-gate 	return (0);
16767c478bd9Sstevel@tonic-gate }
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate static int
is_crash_dump(const char * buf,int fd)16797c478bd9Sstevel@tonic-gate is_crash_dump(const char *buf, int fd)
16807c478bd9Sstevel@tonic-gate {
16817c478bd9Sstevel@tonic-gate 	/* LINTED: pointer cast may result in improper alignment */
16827c478bd9Sstevel@tonic-gate 	const dumphdr_t *dhp = (const dumphdr_t *)buf;
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	/*
16857c478bd9Sstevel@tonic-gate 	 * The current DUMP_MAGIC string covers Solaris 7 and later releases.
16867c478bd9Sstevel@tonic-gate 	 * The utsname struct is only present in dumphdr_t's with dump_version
16877c478bd9Sstevel@tonic-gate 	 * greater than or equal to 9.
16887c478bd9Sstevel@tonic-gate 	 */
16897c478bd9Sstevel@tonic-gate 	if (dhp->dump_magic == DUMP_MAGIC) {
16907c478bd9Sstevel@tonic-gate 		print_dumphdr(fd, dhp, return_uint32, NATIVE_ISA);
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	} else if (dhp->dump_magic == swap_uint32(DUMP_MAGIC)) {
16937c478bd9Sstevel@tonic-gate 		print_dumphdr(fd, dhp, swap_uint32, OTHER_ISA);
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate 	} else if (dhp->dump_magic == OLD_DUMP_MAGIC ||
16967c478bd9Sstevel@tonic-gate 	    dhp->dump_magic == swap_uint32(OLD_DUMP_MAGIC)) {
16977c478bd9Sstevel@tonic-gate 		char *isa = (dhp->dump_magic == OLD_DUMP_MAGIC ?
16987c478bd9Sstevel@tonic-gate 		    NATIVE_ISA : OTHER_ISA);
16997c478bd9Sstevel@tonic-gate 		(void) printf(gettext("SunOS 32-bit %s crash dump\n"), isa);
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	} else {
17027c478bd9Sstevel@tonic-gate 		return (0);
17037c478bd9Sstevel@tonic-gate 	}
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	return (1);
17067c478bd9Sstevel@tonic-gate }
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate static void
print_dumphdr(const int fd,const dumphdr_t * dhp,uint32_t (* swap)(uint32_t),const char * isa)17097c478bd9Sstevel@tonic-gate print_dumphdr(const int fd, const dumphdr_t *dhp, uint32_t (*swap)(uint32_t),
17107c478bd9Sstevel@tonic-gate     const char *isa)
17117c478bd9Sstevel@tonic-gate {
17127c478bd9Sstevel@tonic-gate 	dumphdr_t dh;
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	/*
17157c478bd9Sstevel@tonic-gate 	 * A dumphdr_t is bigger than FBSZ, so we have to manually read the
17167c478bd9Sstevel@tonic-gate 	 * rest of it.
17177c478bd9Sstevel@tonic-gate 	 */
17187c478bd9Sstevel@tonic-gate 	if (swap(dhp->dump_version) > 8 && pread(fd, &dh, sizeof (dumphdr_t),
17197c478bd9Sstevel@tonic-gate 	    (off_t)0) == sizeof (dumphdr_t)) {
1720ca3e8d88SDave Plauger 		const char *c = swap(dh.dump_flags) & DF_COMPRESSED ?
1721ca3e8d88SDave Plauger 		    "compressed " : "";
1722ca3e8d88SDave Plauger 		const char *l = swap(dh.dump_flags) & DF_LIVE ?
1723ca3e8d88SDave Plauger 		    "live" : "crash";
1724ca3e8d88SDave Plauger 
17257c478bd9Sstevel@tonic-gate 		(void) printf(gettext(
1726ca3e8d88SDave Plauger 		    "%s %s %s %u-bit %s %s%s dump from '%s'\n"),
17277c478bd9Sstevel@tonic-gate 		    dh.dump_utsname.sysname, dh.dump_utsname.release,
17287c478bd9Sstevel@tonic-gate 		    dh.dump_utsname.version, swap(dh.dump_wordsize), isa,
1729ca3e8d88SDave Plauger 		    c, l, dh.dump_utsname.nodename);
17307c478bd9Sstevel@tonic-gate 	} else {
17317c478bd9Sstevel@tonic-gate 		(void) printf(gettext("SunOS %u-bit %s crash dump\n"),
17327c478bd9Sstevel@tonic-gate 		    swap(dhp->dump_wordsize), isa);
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate }
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate static void
usage(void)17377c478bd9Sstevel@tonic-gate usage(void)
17387c478bd9Sstevel@tonic-gate {
17397c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
17406e987ca0SDavid Höppner 	    "usage: file [-bdh] [-M mfile] [-m mfile] [-f ffile] file ...\n"
17416e987ca0SDavid Höppner 	    "       file [-bdh] [-M mfile] [-m mfile] -f ffile\n"
17426e987ca0SDavid Höppner 	    "       file -i [-bh] [-f ffile] file ...\n"
17436e987ca0SDavid Höppner 	    "       file -i [-bh] -f ffile\n"
17447c478bd9Sstevel@tonic-gate 	    "       file -c [-d] [-M mfile] [-m mfile]\n"));
17457c478bd9Sstevel@tonic-gate 	exit(2);
17467c478bd9Sstevel@tonic-gate }
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate static uint32_t
swap_uint32(uint32_t in)17497c478bd9Sstevel@tonic-gate swap_uint32(uint32_t in)
17507c478bd9Sstevel@tonic-gate {
17517c478bd9Sstevel@tonic-gate 	uint32_t out;
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	out = (in & 0x000000ff) << 24;
17547c478bd9Sstevel@tonic-gate 	out |= (in & 0x0000ff00) << 8; /* >> 8 << 16 */
17557c478bd9Sstevel@tonic-gate 	out |= (in & 0x00ff0000) >> 8; /* >> 16 << 8 */
17567c478bd9Sstevel@tonic-gate 	out |= (in & 0xff000000) >> 24;
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	return (out);
17597c478bd9Sstevel@tonic-gate }
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate static uint32_t
return_uint32(uint32_t in)17627c478bd9Sstevel@tonic-gate return_uint32(uint32_t in)
17637c478bd9Sstevel@tonic-gate {
17647c478bd9Sstevel@tonic-gate 	return (in);
17657c478bd9Sstevel@tonic-gate }
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate /*
17687c478bd9Sstevel@tonic-gate  * Check if str is in the string list str_list.
17697c478bd9Sstevel@tonic-gate  */
1770c2c65e21Sny155746 int
is_in_list(char * str)1771c2c65e21Sny155746 is_in_list(char *str)
17727c478bd9Sstevel@tonic-gate {
17737c478bd9Sstevel@tonic-gate 	int i;
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	/*
17767c478bd9Sstevel@tonic-gate 	 * Only need to compare the strlen(str_list[i]) bytes.
17777c478bd9Sstevel@tonic-gate 	 * That way .stab will match on .stab* sections, and
17787c478bd9Sstevel@tonic-gate 	 * .debug will match on .debug* sections.
17797c478bd9Sstevel@tonic-gate 	 */
1780c2c65e21Sny155746 	for (i = 0; debug_sections[i] != NULL; i++) {
1781c2c65e21Sny155746 		if (strncmp(debug_sections[i], str,
1782c2c65e21Sny155746 		    strlen(debug_sections[i])) == 0) {
17837c478bd9Sstevel@tonic-gate 			return (1);
17847c478bd9Sstevel@tonic-gate 		}
17857c478bd9Sstevel@tonic-gate 	}
17867c478bd9Sstevel@tonic-gate 	return (0);
17877c478bd9Sstevel@tonic-gate }
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate /*
17907c478bd9Sstevel@tonic-gate  * default_magic -
17917c478bd9Sstevel@tonic-gate  *	allocate space for and create the default magic file
17927c478bd9Sstevel@tonic-gate  *	name string.
17937c478bd9Sstevel@tonic-gate  */
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate static void
default_magic(void)17967c478bd9Sstevel@tonic-gate default_magic(void)
17977c478bd9Sstevel@tonic-gate {
17987c478bd9Sstevel@tonic-gate 	const char *msg_locale = setlocale(LC_MESSAGES, NULL);
17997c478bd9Sstevel@tonic-gate 	struct stat	statbuf;
18007c478bd9Sstevel@tonic-gate 
18019a411307Srie 	if ((dfile = malloc(strlen(msg_locale) + 35)) == NULL) {
18029a411307Srie 		int err = errno;
18039a411307Srie 		(void) fprintf(stderr, gettext("%s: malloc failed: %s\n"),
18049a411307Srie 		    File, strerror(err));
18057c478bd9Sstevel@tonic-gate 		exit(2);
18067c478bd9Sstevel@tonic-gate 	}
18077c478bd9Sstevel@tonic-gate 	(void) snprintf(dfile, strlen(msg_locale) + 35,
18087c478bd9Sstevel@tonic-gate 	    "/usr/lib/locale/%s/LC_MESSAGES/magic", msg_locale);
18097c478bd9Sstevel@tonic-gate 	if (stat(dfile, &statbuf) != 0) {
18107c478bd9Sstevel@tonic-gate 		(void) strcpy(dfile, "/etc/magic");
18117c478bd9Sstevel@tonic-gate 	}
18127c478bd9Sstevel@tonic-gate }
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate /*
18157c478bd9Sstevel@tonic-gate  * add_to_mlist -
18167c478bd9Sstevel@tonic-gate  *	Add the given magic_file filename string to the list of magic
18177c478bd9Sstevel@tonic-gate  *	files (mlist).  This list of files will later be examined, and
18187c478bd9Sstevel@tonic-gate  *	each magic file's entries will be added in order to
18197c478bd9Sstevel@tonic-gate  *	the mtab table.
18207c478bd9Sstevel@tonic-gate  *
18217c478bd9Sstevel@tonic-gate  *	The first flag is set to 1 to add to the first list, mlist1.
18227c478bd9Sstevel@tonic-gate  *	The first flag is set to 0 to add to the second list, mlist2.
18237c478bd9Sstevel@tonic-gate  */
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate static void
add_to_mlist(char * magic_file,int first)18267c478bd9Sstevel@tonic-gate add_to_mlist(char *magic_file, int first)
18277c478bd9Sstevel@tonic-gate {
18287c478bd9Sstevel@tonic-gate 	char	**mlist;	/* ordered list of magic files */
18297c478bd9Sstevel@tonic-gate 	size_t	mlist_sz;	/* number of pointers allocated  for mlist */
18307c478bd9Sstevel@tonic-gate 	char	**mlistp;	/* next entry in mlist */
18317c478bd9Sstevel@tonic-gate 	size_t mlistp_off;
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	if (first) {
18347c478bd9Sstevel@tonic-gate 		mlist = mlist1;
18357c478bd9Sstevel@tonic-gate 		mlist_sz = mlist1_sz;
18367c478bd9Sstevel@tonic-gate 		mlistp = mlist1p;
18377c478bd9Sstevel@tonic-gate 	} else {
18387c478bd9Sstevel@tonic-gate 		mlist = mlist2;
18397c478bd9Sstevel@tonic-gate 		mlist_sz = mlist2_sz;
18407c478bd9Sstevel@tonic-gate 		mlistp = mlist2p;
18417c478bd9Sstevel@tonic-gate 	}
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 	if (mlist == NULL) {	/* initial mlist allocation */
18449a411307Srie 		if ((mlist = calloc(MLIST_SZ, sizeof (char *))) == NULL) {
18459a411307Srie 			int err = errno;
18469a411307Srie 			(void) fprintf(stderr, gettext("%s: malloc "
18479a411307Srie 			    "failed: %s\n"), File, strerror(err));
18487c478bd9Sstevel@tonic-gate 			exit(2);
18497c478bd9Sstevel@tonic-gate 		}
18507c478bd9Sstevel@tonic-gate 		mlist_sz = MLIST_SZ;
18517c478bd9Sstevel@tonic-gate 		mlistp = mlist;
18527c478bd9Sstevel@tonic-gate 	}
18537c478bd9Sstevel@tonic-gate 	if ((mlistp - mlist) >= mlist_sz) {
18547c478bd9Sstevel@tonic-gate 		mlistp_off = mlistp - mlist;
18557c478bd9Sstevel@tonic-gate 		mlist_sz *= 2;
18569a411307Srie 		if ((mlist = realloc(mlist,
18577c478bd9Sstevel@tonic-gate 		    mlist_sz * sizeof (char *))) == NULL) {
18589a411307Srie 			int err = errno;
18599a411307Srie 			(void) fprintf(stderr, gettext("%s: malloc "
18609a411307Srie 			    "failed: %s\n"), File, strerror(err));
18617c478bd9Sstevel@tonic-gate 			exit(2);
18627c478bd9Sstevel@tonic-gate 		}
18637c478bd9Sstevel@tonic-gate 		mlistp = mlist + mlistp_off;
18647c478bd9Sstevel@tonic-gate 	}
18657c478bd9Sstevel@tonic-gate 	/*
18667c478bd9Sstevel@tonic-gate 	 * now allocate memory for and copy the
18677c478bd9Sstevel@tonic-gate 	 * magic file name string
18687c478bd9Sstevel@tonic-gate 	 */
18697c478bd9Sstevel@tonic-gate 	if ((*mlistp = malloc(strlen(magic_file) + 1)) == NULL) {
18709a411307Srie 		int err = errno;
18719a411307Srie 		(void) fprintf(stderr, gettext("%s: malloc failed: %s\n"),
18729a411307Srie 		    File, strerror(err));
18737c478bd9Sstevel@tonic-gate 		exit(2);
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate 	(void) strlcpy(*mlistp, magic_file, strlen(magic_file) + 1);
18767c478bd9Sstevel@tonic-gate 	mlistp++;
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	if (first) {
18797c478bd9Sstevel@tonic-gate 		mlist1 = mlist;
18807c478bd9Sstevel@tonic-gate 		mlist1_sz = mlist_sz;
18817c478bd9Sstevel@tonic-gate 		mlist1p = mlistp;
18827c478bd9Sstevel@tonic-gate 	} else {
18837c478bd9Sstevel@tonic-gate 		mlist2 = mlist;
18847c478bd9Sstevel@tonic-gate 		mlist2_sz = mlist_sz;
18857c478bd9Sstevel@tonic-gate 		mlist2p = mlistp;
18867c478bd9Sstevel@tonic-gate 	}
18877c478bd9Sstevel@tonic-gate }
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate static void
fd_cleanup(void)18907c478bd9Sstevel@tonic-gate fd_cleanup(void)
18917c478bd9Sstevel@tonic-gate {
18927c478bd9Sstevel@tonic-gate 	if (ifd != -1) {
18937c478bd9Sstevel@tonic-gate 		(void) close(ifd);
18947c478bd9Sstevel@tonic-gate 		ifd = -1;
18957c478bd9Sstevel@tonic-gate 	}
18967c478bd9Sstevel@tonic-gate 	if (elffd != -1) {
18977c478bd9Sstevel@tonic-gate 		(void) close(elffd);
18987c478bd9Sstevel@tonic-gate 		elffd = -1;
18997c478bd9Sstevel@tonic-gate 	}
19007c478bd9Sstevel@tonic-gate }
1901