xref: /titanic_53/usr/src/cmd/file/file.c (revision c13de8f6a88563211bd4432ca11ca38ed3bf0fc0)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*c13de8f6Sab196087  * Common Development and Distribution License (the "License").
6*c13de8f6Sab196087  * 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 /*
29*c13de8f6Sab196087  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
307c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #define	_LARGEFILE64_SOURCE
367c478bd9Sstevel@tonic-gate 
37*c13de8f6Sab196087 /* Get definitions for the relocation types supported. */
38*c13de8f6Sab196087 #define	ELF_TARGET_ALL
39*c13de8f6Sab196087 
407c478bd9Sstevel@tonic-gate #include <ctype.h>
417c478bd9Sstevel@tonic-gate #include <unistd.h>
427c478bd9Sstevel@tonic-gate #include <fcntl.h>
437c478bd9Sstevel@tonic-gate #include <signal.h>
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <libelf.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <limits.h>
487c478bd9Sstevel@tonic-gate #include <locale.h>
497c478bd9Sstevel@tonic-gate #include <wctype.h>
507c478bd9Sstevel@tonic-gate #include <string.h>
517c478bd9Sstevel@tonic-gate #include <errno.h>
527c478bd9Sstevel@tonic-gate #include <door.h>
537c478bd9Sstevel@tonic-gate #include <sys/param.h>
547c478bd9Sstevel@tonic-gate #include <sys/types.h>
557c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
567c478bd9Sstevel@tonic-gate #include <sys/stat.h>
577c478bd9Sstevel@tonic-gate #include <sys/elf.h>
587c478bd9Sstevel@tonic-gate #include <procfs.h>
597c478bd9Sstevel@tonic-gate #include <sys/core.h>
607c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
617c478bd9Sstevel@tonic-gate #include <netinet/in.h>
627c478bd9Sstevel@tonic-gate #include <gelf.h>
637c478bd9Sstevel@tonic-gate #include <elfcap.h>
64*c13de8f6Sab196087 #include <sgsrtcid.h>
657c478bd9Sstevel@tonic-gate #include "file.h"
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate typedef Elf64_Nhdr	GElf_Nhdr;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  *	Misc
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	FBSZ		512
747c478bd9Sstevel@tonic-gate #define	MLIST_SZ	12
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * The 0x8FCA0102 magic string was used in crash dumps generated by releases
787c478bd9Sstevel@tonic-gate  * prior to Solaris 7.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate #define	OLD_DUMP_MAGIC	0x8FCA0102
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #if defined(__sparc)
837c478bd9Sstevel@tonic-gate #define	NATIVE_ISA	"SPARC"
847c478bd9Sstevel@tonic-gate #define	OTHER_ISA	"Intel"
857c478bd9Sstevel@tonic-gate #else
867c478bd9Sstevel@tonic-gate #define	NATIVE_ISA	"Intel"
877c478bd9Sstevel@tonic-gate #define	OTHER_ISA	"SPARC"
887c478bd9Sstevel@tonic-gate #endif
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* Assembly language comment char */
917c478bd9Sstevel@tonic-gate #ifdef pdp11
927c478bd9Sstevel@tonic-gate #define	ASCOMCHAR '/'
937c478bd9Sstevel@tonic-gate #else
947c478bd9Sstevel@tonic-gate #define	ASCOMCHAR '!'
957c478bd9Sstevel@tonic-gate #endif
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #pragma	align	16(fbuf)
987c478bd9Sstevel@tonic-gate static char	fbuf[FBSZ];
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * Magic file variables
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate static intmax_t maxmagicoffset;
1047c478bd9Sstevel@tonic-gate static intmax_t tmpmax;
1057c478bd9Sstevel@tonic-gate static char	*magicbuf;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static char	*dfile;
1087c478bd9Sstevel@tonic-gate static char	*troff[] = {	/* new troff intermediate lang */
1097c478bd9Sstevel@tonic-gate 		"x", "T", "res", "init", "font", "202", "V0", "p1", 0};
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static char	*fort[] = {			/* FORTRAN */
1127c478bd9Sstevel@tonic-gate 		"function", "subroutine", "common", "dimension", "block",
1137c478bd9Sstevel@tonic-gate 		"integer", "real", "data", "double",
1147c478bd9Sstevel@tonic-gate 		"FUNCTION", "SUBROUTINE", "COMMON", "DIMENSION", "BLOCK",
1157c478bd9Sstevel@tonic-gate 		"INTEGER", "REAL", "DATA", "DOUBLE", 0};
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static char	*asc[] = {		/* Assembler Commands */
1187c478bd9Sstevel@tonic-gate 		"sys", "mov", "tst", "clr", "jmp", "cmp", "set", "inc",
1197c478bd9Sstevel@tonic-gate 		"dec", 0};
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static char	*c[] = {			/* C Language */
1227c478bd9Sstevel@tonic-gate 		"int", "char", "float", "double", "short", "long", "unsigned",
1237c478bd9Sstevel@tonic-gate 		"register", "static", "struct", "extern", 0};
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static char	*as[] = {	/* Assembler Pseudo Ops, prepended with '.' */
1267c478bd9Sstevel@tonic-gate 		"globl", "global", "ident", "file", "byte", "even",
1277c478bd9Sstevel@tonic-gate 		"text", "data", "bss", "comm", 0};
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * The line and debug section names are used by the strip command.
1317c478bd9Sstevel@tonic-gate  * Any changes in the strip implementation need to be reflected here.
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate static char	*debug_sections[] = { /* Debug sections in a ELF file */
1347c478bd9Sstevel@tonic-gate 		".debug", ".stab", ".dwarf", ".line", NULL};
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /* start for MB env */
1387c478bd9Sstevel@tonic-gate static wchar_t wchar;
1397c478bd9Sstevel@tonic-gate static int	length;
1407c478bd9Sstevel@tonic-gate static int	IS_ascii;
1417c478bd9Sstevel@tonic-gate static int	Max;
1427c478bd9Sstevel@tonic-gate /* end for MB env */
1437c478bd9Sstevel@tonic-gate static int	i;	/* global index into first 'fbsz' bytes of file */
1447c478bd9Sstevel@tonic-gate static int	fbsz;
1457c478bd9Sstevel@tonic-gate static int	ifd = -1;
1467c478bd9Sstevel@tonic-gate static int	elffd = -1;
1477c478bd9Sstevel@tonic-gate static int	tret;
1487c478bd9Sstevel@tonic-gate static int	hflg;
1497c478bd9Sstevel@tonic-gate static int	dflg;
1507c478bd9Sstevel@tonic-gate static int	mflg;
1517c478bd9Sstevel@tonic-gate static int	M_flg;
1527c478bd9Sstevel@tonic-gate static int	iflg;
1537c478bd9Sstevel@tonic-gate static struct stat64	mbuf;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate static char	**mlist1;	/* 1st ordered list of magic files */
1567c478bd9Sstevel@tonic-gate static char	**mlist2;	/* 2nd ordered list of magic files */
1577c478bd9Sstevel@tonic-gate static size_t	mlist1_sz;	/* number of ptrs allocated for mlist1 */
1587c478bd9Sstevel@tonic-gate static size_t	mlist2_sz;	/* number of ptrs allocated for mlist2 */
1597c478bd9Sstevel@tonic-gate static char	**mlist1p;	/* next entry in mlist1 */
1607c478bd9Sstevel@tonic-gate static char	**mlist2p;	/* next entry in mlist2 */
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate static ssize_t	mread;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate static void is_stripped(Elf *elf);
1657c478bd9Sstevel@tonic-gate static Elf *is_elf_file(int elffd);
1667c478bd9Sstevel@tonic-gate static void ar_coff_or_aout(int ifd);
1677c478bd9Sstevel@tonic-gate static int type(char *file);
1687c478bd9Sstevel@tonic-gate static int def_position_tests(void);
1697c478bd9Sstevel@tonic-gate static void def_context_tests(void);
1707c478bd9Sstevel@tonic-gate static int troffint(char *bp, int n);
1717c478bd9Sstevel@tonic-gate static int lookup(char **tab);
1727c478bd9Sstevel@tonic-gate static int ccom(void);
1737c478bd9Sstevel@tonic-gate static int ascom(void);
1747c478bd9Sstevel@tonic-gate static int sccs(void);
1757c478bd9Sstevel@tonic-gate static int english(char *bp, int n);
1767c478bd9Sstevel@tonic-gate static int old_core(Elf *elf, GElf_Ehdr *ehdr, int format);
1777c478bd9Sstevel@tonic-gate static int core(Elf *elf, GElf_Ehdr *ehdr, int format);
1787c478bd9Sstevel@tonic-gate static int shellscript(char buf[], struct stat64 *sb);
1797c478bd9Sstevel@tonic-gate static int elf_check(Elf *elf);
1807c478bd9Sstevel@tonic-gate static int get_door_target(char *, char *, size_t);
1817c478bd9Sstevel@tonic-gate static int zipfile(char *, int);
1827c478bd9Sstevel@tonic-gate static int is_crash_dump(const char *, int);
1837c478bd9Sstevel@tonic-gate static void print_dumphdr(const int, const dumphdr_t *, uint32_t (*)(uint32_t),
1847c478bd9Sstevel@tonic-gate     const char *);
1857c478bd9Sstevel@tonic-gate static uint32_t swap_uint32(uint32_t);
1867c478bd9Sstevel@tonic-gate static uint32_t return_uint32(uint32_t);
1877c478bd9Sstevel@tonic-gate static int is_in_list(char *[], char *);
1887c478bd9Sstevel@tonic-gate static void usage(void);
1897c478bd9Sstevel@tonic-gate static void default_magic(void);
1907c478bd9Sstevel@tonic-gate static void add_to_mlist(char *, int);
1917c478bd9Sstevel@tonic-gate static void fd_cleanup(void);
192*c13de8f6Sab196087 static int is_rtld_config(void);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate #ifdef XPG4
1957c478bd9Sstevel@tonic-gate 	/* SUSv3 requires a single <space> after the colon */
1967c478bd9Sstevel@tonic-gate #define	prf(x)	(void) printf("%s: ", x);
1977c478bd9Sstevel@tonic-gate #else	/* !XPG4 */
1987c478bd9Sstevel@tonic-gate #define	prf(x)	(void) printf("%s:%s", x, (int)strlen(x) > 6 ? "\t" : "\t\t");
1997c478bd9Sstevel@tonic-gate #endif	/* XPG4 */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate int
2027c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	char	*p;
2057c478bd9Sstevel@tonic-gate 	int	ch;
2067c478bd9Sstevel@tonic-gate 	FILE	*fl;
2077c478bd9Sstevel@tonic-gate 	int	cflg = 0;
2087c478bd9Sstevel@tonic-gate 	int	eflg = 0;
2097c478bd9Sstevel@tonic-gate 	int	fflg = 0;
2107c478bd9Sstevel@tonic-gate 	char	*ap = NULL;
2117c478bd9Sstevel@tonic-gate 	int	pathlen;
2127c478bd9Sstevel@tonic-gate 	char	**filep;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2157c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2167c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2177c478bd9Sstevel@tonic-gate #endif
2187c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "M:cdf:him:")) != EOF) {
2217c478bd9Sstevel@tonic-gate 		switch (ch) {
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		case 'M':
2247c478bd9Sstevel@tonic-gate 			add_to_mlist(optarg, !dflg);
2257c478bd9Sstevel@tonic-gate 			M_flg++;
2267c478bd9Sstevel@tonic-gate 			break;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		case 'c':
2297c478bd9Sstevel@tonic-gate 			cflg++;
2307c478bd9Sstevel@tonic-gate 			break;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		case 'd':
2337c478bd9Sstevel@tonic-gate 			if (!dflg) {
2347c478bd9Sstevel@tonic-gate 				default_magic();
2357c478bd9Sstevel@tonic-gate 				add_to_mlist(dfile, 0);
2367c478bd9Sstevel@tonic-gate 				dflg++;
2377c478bd9Sstevel@tonic-gate 			}
2387c478bd9Sstevel@tonic-gate 			break;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		case 'f':
2417c478bd9Sstevel@tonic-gate 			fflg++;
2427c478bd9Sstevel@tonic-gate 			if ((fl = fopen(optarg, "r")) == NULL) {
2437c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2447c478bd9Sstevel@tonic-gate 					gettext("cannot open %s\n"), optarg);
2457c478bd9Sstevel@tonic-gate 				usage();
2467c478bd9Sstevel@tonic-gate 			}
2477c478bd9Sstevel@tonic-gate 			pathlen = pathconf("/", _PC_PATH_MAX);
2487c478bd9Sstevel@tonic-gate 			if (pathlen == -1) {
2497c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2507c478bd9Sstevel@tonic-gate 				    gettext("pathconf: cannot determine "
2517c478bd9Sstevel@tonic-gate 					"maximum path length\n"));
2527c478bd9Sstevel@tonic-gate 				exit(1);
2537c478bd9Sstevel@tonic-gate 			}
2547c478bd9Sstevel@tonic-gate 			pathlen += 2; /* for null and newline in fgets */
2557c478bd9Sstevel@tonic-gate 			ap = malloc(pathlen * sizeof (char));
2567c478bd9Sstevel@tonic-gate 			if (ap == NULL) {
2577c478bd9Sstevel@tonic-gate 				perror("malloc");
2587c478bd9Sstevel@tonic-gate 				exit(1);
2597c478bd9Sstevel@tonic-gate 			}
2607c478bd9Sstevel@tonic-gate 			break;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		case 'h':
2637c478bd9Sstevel@tonic-gate 			hflg++;
2647c478bd9Sstevel@tonic-gate 			break;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		case 'i':
2677c478bd9Sstevel@tonic-gate 			iflg++;
2687c478bd9Sstevel@tonic-gate 			break;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		case 'm':
2717c478bd9Sstevel@tonic-gate 			add_to_mlist(optarg, !dflg);
2727c478bd9Sstevel@tonic-gate 			mflg++;
2737c478bd9Sstevel@tonic-gate 			break;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		case '?':
2767c478bd9Sstevel@tonic-gate 			eflg++;
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 		}
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	if (!cflg && !fflg && (eflg || optind == argc))
2817c478bd9Sstevel@tonic-gate 		usage();
2827c478bd9Sstevel@tonic-gate 	if (iflg && (dflg || mflg || M_flg)) {
2837c478bd9Sstevel@tonic-gate 		usage();
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 	if (iflg && cflg) {
2867c478bd9Sstevel@tonic-gate 		usage();
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	if (!dflg && !mflg && !M_flg && !iflg) {
2907c478bd9Sstevel@tonic-gate 	/* no -d, -m, nor -M option; also -i option doesn't need magic  */
2917c478bd9Sstevel@tonic-gate 		default_magic();
2927c478bd9Sstevel@tonic-gate 		if (f_mkmtab(dfile, cflg, 0) == -1) {
2937c478bd9Sstevel@tonic-gate 			exit(2);
2947c478bd9Sstevel@tonic-gate 		}
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	else if (mflg && !M_flg && !dflg) {
2987c478bd9Sstevel@tonic-gate 	/* -m specified without -d nor -M */
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate #ifdef XPG4	/* For SUSv3 only */
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		/*
3037c478bd9Sstevel@tonic-gate 		 * The default position-dependent magic file tests
3047c478bd9Sstevel@tonic-gate 		 * in /etc/magic will follow all the -m magic tests.
3057c478bd9Sstevel@tonic-gate 		 */
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		for (filep = mlist1; filep < mlist1p; filep++) {
3087c478bd9Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 1) == -1) {
3097c478bd9Sstevel@tonic-gate 				exit(2);
3107c478bd9Sstevel@tonic-gate 			}
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 		default_magic();
3137c478bd9Sstevel@tonic-gate 		if (f_mkmtab(dfile, cflg, 0) == -1) {
3147c478bd9Sstevel@tonic-gate 			exit(2);
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate #else	/* !XPG4 */
3177c478bd9Sstevel@tonic-gate 		/*
3187c478bd9Sstevel@tonic-gate 		 * Retain Solaris file behavior for -m before SUSv3,
3197c478bd9Sstevel@tonic-gate 		 * when the new -d and -M options are not specified.
3207c478bd9Sstevel@tonic-gate 		 * Use the -m file specified in place of the default
3217c478bd9Sstevel@tonic-gate 		 * /etc/magic file.  Solaris file will
3227c478bd9Sstevel@tonic-gate 		 * now allow more than one magic file to be specified
3237c478bd9Sstevel@tonic-gate 		 * with multiple -m options, for consistency with
3247c478bd9Sstevel@tonic-gate 		 * other behavior.
3257c478bd9Sstevel@tonic-gate 		 *
3267c478bd9Sstevel@tonic-gate 		 * Put the magic table(s) specified by -m into
3277c478bd9Sstevel@tonic-gate 		 * the second magic table instead of the first
3287c478bd9Sstevel@tonic-gate 		 * (as indicated by the last argument to f_mkmtab()),
3297c478bd9Sstevel@tonic-gate 		 * since they replace the /etc/magic tests and
3307c478bd9Sstevel@tonic-gate 		 * must be executed alongside the default
3317c478bd9Sstevel@tonic-gate 		 * position-sensitive tests.
3327c478bd9Sstevel@tonic-gate 		 */
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		for (filep = mlist1; filep < mlist1p; filep++) {
3357c478bd9Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 0) == -1) {
3367c478bd9Sstevel@tonic-gate 				exit(2);
3377c478bd9Sstevel@tonic-gate 			}
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate #endif /* XPG4 */
3407c478bd9Sstevel@tonic-gate 	} else {
3417c478bd9Sstevel@tonic-gate 		/*
3427c478bd9Sstevel@tonic-gate 		 * For any other combination of -d, -m, and -M,
3437c478bd9Sstevel@tonic-gate 		 * use the magic files in command-line order.
3447c478bd9Sstevel@tonic-gate 		 * Store the entries from the two separate lists of magic
3457c478bd9Sstevel@tonic-gate 		 * files, if any, into two separate magic file tables.
3467c478bd9Sstevel@tonic-gate 		 * mlist1: magic tests executed before default magic tests
3477c478bd9Sstevel@tonic-gate 		 * mlist2: default magic tests and after
3487c478bd9Sstevel@tonic-gate 		 */
3497c478bd9Sstevel@tonic-gate 		for (filep = mlist1; filep && (filep < mlist1p); filep++) {
3507c478bd9Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 1) == -1) {
3517c478bd9Sstevel@tonic-gate 				exit(2);
3527c478bd9Sstevel@tonic-gate 			}
3537c478bd9Sstevel@tonic-gate 		}
3547c478bd9Sstevel@tonic-gate 		for (filep = mlist2; filep && (filep < mlist2p); filep++) {
3557c478bd9Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 0) == -1) {
3567c478bd9Sstevel@tonic-gate 				exit(2);
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/* Initialize the magic file variables; check both magic tables */
3627c478bd9Sstevel@tonic-gate 	tmpmax = f_getmaxoffset(1);
3637c478bd9Sstevel@tonic-gate 	maxmagicoffset = f_getmaxoffset(0);
3647c478bd9Sstevel@tonic-gate 	if (maxmagicoffset < tmpmax) {
3657c478bd9Sstevel@tonic-gate 		maxmagicoffset = tmpmax;
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 	if (maxmagicoffset < (intmax_t)FBSZ)
3687c478bd9Sstevel@tonic-gate 		maxmagicoffset = (intmax_t)FBSZ;
3697c478bd9Sstevel@tonic-gate 	if ((magicbuf = (char *)malloc(maxmagicoffset)) == NULL) {
3707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("malloc failed\n"));
3717c478bd9Sstevel@tonic-gate 		exit(2);
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (cflg) {
3757c478bd9Sstevel@tonic-gate 		f_prtmtab();
3767c478bd9Sstevel@tonic-gate 		if (ferror(stdout) != 0) {
3777c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("file: error writing to "
3787c478bd9Sstevel@tonic-gate 			    "stdout\n"));
3797c478bd9Sstevel@tonic-gate 			exit(1);
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 		if (fclose(stdout) != 0) {
3827c478bd9Sstevel@tonic-gate 			perror(gettext("file: fclose failed"));
3837c478bd9Sstevel@tonic-gate 			exit(1);
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 		exit(0);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 	for (; fflg || optind < argc; optind += !fflg) {
3887c478bd9Sstevel@tonic-gate 		register int	l;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		if (fflg) {
3917c478bd9Sstevel@tonic-gate 			if ((p = fgets(ap, pathlen, fl)) == NULL) {
3927c478bd9Sstevel@tonic-gate 				fflg = 0;
3937c478bd9Sstevel@tonic-gate 				optind--;
3947c478bd9Sstevel@tonic-gate 				continue;
3957c478bd9Sstevel@tonic-gate 			}
3967c478bd9Sstevel@tonic-gate 			l = strlen(p);
3977c478bd9Sstevel@tonic-gate 			if (l > 0)
3987c478bd9Sstevel@tonic-gate 				p[l - 1] = '\0';
3997c478bd9Sstevel@tonic-gate 		} else
4007c478bd9Sstevel@tonic-gate 			p = argv[optind];
4017c478bd9Sstevel@tonic-gate 		prf(p);				/* print "file_name:<tab>" */
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		if (type(p))
4047c478bd9Sstevel@tonic-gate 			tret = 1;
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 	if (ap != NULL)
4077c478bd9Sstevel@tonic-gate 		free(ap);
4087c478bd9Sstevel@tonic-gate 	if (tret != 0) {
4097c478bd9Sstevel@tonic-gate 		exit(tret);
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 	if (ferror(stdout) != 0) {
4127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("file: error writing to "
4137c478bd9Sstevel@tonic-gate 		    "stdout\n"));
4147c478bd9Sstevel@tonic-gate 		exit(1);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 	if (fclose(stdout) != 0) {
4177c478bd9Sstevel@tonic-gate 		perror(gettext("file: fclose failed"));
4187c478bd9Sstevel@tonic-gate 		exit(1);
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 	return (0);
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate static int
4247c478bd9Sstevel@tonic-gate type(char *file)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate 	int	cc;
4277c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
4287c478bd9Sstevel@tonic-gate 	int	(*statf)() = hflg ? lstat64 : stat64;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	i = 0;		/* reset index to beginning of file */
4317c478bd9Sstevel@tonic-gate 	ifd = -1;
4327c478bd9Sstevel@tonic-gate 	if ((*statf)(file, &mbuf) < 0) {
4337c478bd9Sstevel@tonic-gate 		if (statf == lstat64 || lstat64(file, &mbuf) < 0) {
4347c478bd9Sstevel@tonic-gate 			(void) printf(gettext("cannot open: %s\n"),
4357c478bd9Sstevel@tonic-gate 			    strerror(errno));
4367c478bd9Sstevel@tonic-gate 			return (0);		/* POSIX.2 */
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 	switch (mbuf.st_mode & S_IFMT) {
4407c478bd9Sstevel@tonic-gate 	case S_IFREG:
4417c478bd9Sstevel@tonic-gate 		if (iflg) {
4427c478bd9Sstevel@tonic-gate 			(void) printf(gettext("regular file\n"));
4437c478bd9Sstevel@tonic-gate 			return (0);
4447c478bd9Sstevel@tonic-gate 		}
4457c478bd9Sstevel@tonic-gate 		break;
4467c478bd9Sstevel@tonic-gate 	case S_IFCHR:
4477c478bd9Sstevel@tonic-gate 		(void) printf(gettext("character"));
4487c478bd9Sstevel@tonic-gate 		goto spcl;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	case S_IFDIR:
4517c478bd9Sstevel@tonic-gate 		(void) printf(gettext("directory\n"));
4527c478bd9Sstevel@tonic-gate 		return (0);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	case S_IFIFO:
4557c478bd9Sstevel@tonic-gate 		(void) printf(gettext("fifo\n"));
4567c478bd9Sstevel@tonic-gate 		return (0);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	case S_IFLNK:
4597c478bd9Sstevel@tonic-gate 		if ((cc = readlink(file, buf, BUFSIZ)) < 0) {
4607c478bd9Sstevel@tonic-gate 			(void) printf(gettext("readlink error: %s\n"),
4617c478bd9Sstevel@tonic-gate 				strerror(errno));
4627c478bd9Sstevel@tonic-gate 			return (1);
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 		buf[cc] = '\0';
4657c478bd9Sstevel@tonic-gate 		(void) printf(gettext("symbolic link to %s\n"), buf);
4667c478bd9Sstevel@tonic-gate 		return (0);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	case S_IFBLK:
4697c478bd9Sstevel@tonic-gate 		(void) printf(gettext("block"));
4707c478bd9Sstevel@tonic-gate 					/* major and minor, see sys/mkdev.h */
4717c478bd9Sstevel@tonic-gate spcl:
4727c478bd9Sstevel@tonic-gate 		(void) printf(gettext(" special (%d/%d)\n"),
4737c478bd9Sstevel@tonic-gate 		    major(mbuf.st_rdev), minor(mbuf.st_rdev));
4747c478bd9Sstevel@tonic-gate 		return (0);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	case S_IFSOCK:
4777c478bd9Sstevel@tonic-gate 		(void) printf("socket\n");
4787c478bd9Sstevel@tonic-gate 		/* FIXME, should open and try to getsockname. */
4797c478bd9Sstevel@tonic-gate 		return (0);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	case S_IFDOOR:
4827c478bd9Sstevel@tonic-gate 		if (get_door_target(file, buf, sizeof (buf)) == 0)
4837c478bd9Sstevel@tonic-gate 			(void) printf(gettext("door to %s\n"), buf);
4847c478bd9Sstevel@tonic-gate 		else
4857c478bd9Sstevel@tonic-gate 			(void) printf(gettext("door\n"));
4867c478bd9Sstevel@tonic-gate 		return (0);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
4917c478bd9Sstevel@tonic-gate 		(void) printf(gettext("libelf is out of date\n"));
4927c478bd9Sstevel@tonic-gate 		return (1);
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	ifd = open64(file, O_RDONLY);
4967c478bd9Sstevel@tonic-gate 	if (ifd < 0) {
4977c478bd9Sstevel@tonic-gate 		(void) printf(gettext("cannot open: %s\n"), strerror(errno));
4987c478bd9Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	/* need another fd for elf, since we might want to read the file too */
5027c478bd9Sstevel@tonic-gate 	elffd = open64(file, O_RDONLY);
5037c478bd9Sstevel@tonic-gate 	if (elffd < 0) {
5047c478bd9Sstevel@tonic-gate 		(void) printf(gettext("cannot open: %s\n"), strerror(errno));
5057c478bd9Sstevel@tonic-gate 		(void) close(ifd);
5067c478bd9Sstevel@tonic-gate 		ifd = -1;
5077c478bd9Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 	if ((fbsz = read(ifd, fbuf, FBSZ)) == -1) {
5107c478bd9Sstevel@tonic-gate 		(void) printf(gettext("cannot read: %s\n"), strerror(errno));
5117c478bd9Sstevel@tonic-gate 		(void) close(ifd);
5127c478bd9Sstevel@tonic-gate 		ifd = -1;
5137c478bd9Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 	if (fbsz == 0) {
5167c478bd9Sstevel@tonic-gate 		(void) printf(gettext("empty file\n"));
5177c478bd9Sstevel@tonic-gate 		fd_cleanup();
5187c478bd9Sstevel@tonic-gate 		return (0);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/*
5227c478bd9Sstevel@tonic-gate 	 * First try user-specified position-dependent magic tests, if any,
5237c478bd9Sstevel@tonic-gate 	 * which need to execute before the default tests.
5247c478bd9Sstevel@tonic-gate 	 */
5257c478bd9Sstevel@tonic-gate 	if ((mread = pread(ifd, (void*)magicbuf, (size_t)maxmagicoffset,
5267c478bd9Sstevel@tonic-gate 			(off_t)0)) == -1) {
5277c478bd9Sstevel@tonic-gate 		(void) printf(gettext("cannot read: %s\n"), strerror(errno));
5287c478bd9Sstevel@tonic-gate 		fd_cleanup();
5297c478bd9Sstevel@tonic-gate 		return (0);
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * ChecK against Magic Table entries.
5347c478bd9Sstevel@tonic-gate 	 * Check first magic table for magic tests to be applied
5357c478bd9Sstevel@tonic-gate 	 * before default tests.
5367c478bd9Sstevel@tonic-gate 	 * If no default tests are to be applied, all magic tests
5377c478bd9Sstevel@tonic-gate 	 * should occur in this magic table.
5387c478bd9Sstevel@tonic-gate 	 */
5397c478bd9Sstevel@tonic-gate 	switch (f_ckmtab(magicbuf, mread, 1)) {
5407c478bd9Sstevel@tonic-gate 		case -1:	/* Error */
5417c478bd9Sstevel@tonic-gate 			exit(2);
5427c478bd9Sstevel@tonic-gate 			break;
5437c478bd9Sstevel@tonic-gate 		case 0:		/* Not magic */
5447c478bd9Sstevel@tonic-gate 			break;
5457c478bd9Sstevel@tonic-gate 		default:	/* Switch is magic index */
5467c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
5477c478bd9Sstevel@tonic-gate 			fd_cleanup();
5487c478bd9Sstevel@tonic-gate 			return (0);
5497c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
5507c478bd9Sstevel@tonic-gate 			break;
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	if (dflg || !M_flg) {
5547c478bd9Sstevel@tonic-gate 		/*
5557c478bd9Sstevel@tonic-gate 		 * default position-dependent tests,
5567c478bd9Sstevel@tonic-gate 		 * plus non-default magic tests, if any
5577c478bd9Sstevel@tonic-gate 		 */
5587c478bd9Sstevel@tonic-gate 		switch (def_position_tests()) {
5597c478bd9Sstevel@tonic-gate 			case -1:	/* error */
5607c478bd9Sstevel@tonic-gate 				fd_cleanup();
5617c478bd9Sstevel@tonic-gate 				return (1);
5627c478bd9Sstevel@tonic-gate 			case 1:	/* matching type found */
5637c478bd9Sstevel@tonic-gate 				fd_cleanup();
5647c478bd9Sstevel@tonic-gate 				return (0);
5657c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
5667c478bd9Sstevel@tonic-gate 				break;
5677c478bd9Sstevel@tonic-gate 			case 0:		/* no matching type found */
5687c478bd9Sstevel@tonic-gate 				break;
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 		/* default context-sensitive tests */
5717c478bd9Sstevel@tonic-gate 		def_context_tests();
5727c478bd9Sstevel@tonic-gate 	} else {
5737c478bd9Sstevel@tonic-gate 		/* no more tests to apply; no match was found */
5747c478bd9Sstevel@tonic-gate 		(void) printf(gettext("data\n"));
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 	fd_cleanup();
5777c478bd9Sstevel@tonic-gate 	return (0);
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate  * def_position_tests() - applies default position-sensitive tests,
5827c478bd9Sstevel@tonic-gate  *	looking for values in specific positions in the file.
5837c478bd9Sstevel@tonic-gate  *	These are followed by default (followed by possibly some
5847c478bd9Sstevel@tonic-gate  *	non-default) magic file tests.
5857c478bd9Sstevel@tonic-gate  *
5867c478bd9Sstevel@tonic-gate  *	All position-sensitive tests, default or otherwise, must
5877c478bd9Sstevel@tonic-gate  *	be applied before context-sensitive tests, to avoid
5887c478bd9Sstevel@tonic-gate  *	false context-sensitive matches.
5897c478bd9Sstevel@tonic-gate  *
5907c478bd9Sstevel@tonic-gate  * 	Returns -1 on error which should result in error (non-zero)
5917c478bd9Sstevel@tonic-gate  *	exit status for the file utility.
5927c478bd9Sstevel@tonic-gate  *	Returns 0 if no matching file type found.
5937c478bd9Sstevel@tonic-gate  *	Returns 1 if matching file type found.
5947c478bd9Sstevel@tonic-gate  */
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate static int
5977c478bd9Sstevel@tonic-gate def_position_tests(void)
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate 	Elf	*elf;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if (sccs()) {	/* look for "1hddddd" where d is a digit */
6027c478bd9Sstevel@tonic-gate 		(void) printf("sccs \n");
6037c478bd9Sstevel@tonic-gate 		return (1);
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 	if (fbuf[0] == '#' && fbuf[1] == '!' && shellscript(fbuf+2, &mbuf))
6067c478bd9Sstevel@tonic-gate 		return (1);
6077c478bd9Sstevel@tonic-gate 	if ((elf = is_elf_file(elffd)) != NULL) {
6087c478bd9Sstevel@tonic-gate 		(void) elf_check(elf);
6097c478bd9Sstevel@tonic-gate 		(void) elf_end(elf);
6107c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
6117c478bd9Sstevel@tonic-gate 		return (1);
6127c478bd9Sstevel@tonic-gate 
613*c13de8f6Sab196087 
6147c478bd9Sstevel@tonic-gate 	/* LINTED: pointer cast may result in improper alignment */
6157c478bd9Sstevel@tonic-gate 	} else if (*(int *)fbuf == CORE_MAGIC) {
6167c478bd9Sstevel@tonic-gate 		/* LINTED: pointer cast may result in improper alignment */
6177c478bd9Sstevel@tonic-gate 		struct core *corep = (struct core *)fbuf;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		(void) printf("a.out core file");
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		if (*(corep->c_cmdname) != '\0')
6227c478bd9Sstevel@tonic-gate 			(void) printf(" from '%s'", corep->c_cmdname);
6237c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
6247c478bd9Sstevel@tonic-gate 		return (1);
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	/*
628*c13de8f6Sab196087 	 * Runtime linker (ld.so.1) configuration file.
629*c13de8f6Sab196087 	 */
630*c13de8f6Sab196087 	if (is_rtld_config())
631*c13de8f6Sab196087 		return (1);
632*c13de8f6Sab196087 
633*c13de8f6Sab196087 	/*
6347c478bd9Sstevel@tonic-gate 	 * ZIP files, JAR files, and Java executables
6357c478bd9Sstevel@tonic-gate 	 */
6367c478bd9Sstevel@tonic-gate 	if (zipfile(fbuf, ifd))
6377c478bd9Sstevel@tonic-gate 		return (1);
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	if (is_crash_dump(fbuf, ifd))
6407c478bd9Sstevel@tonic-gate 		return (1);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	/*
6437c478bd9Sstevel@tonic-gate 	 * ChecK against Magic Table entries.
6447c478bd9Sstevel@tonic-gate 	 * The magic entries checked here always start with default
6457c478bd9Sstevel@tonic-gate 	 * magic tests and may be followed by other, non-default magic
6467c478bd9Sstevel@tonic-gate 	 * tests.  If no default tests are to be executed, all the
6477c478bd9Sstevel@tonic-gate 	 * magic tests should have been in the first magic table.
6487c478bd9Sstevel@tonic-gate 	 */
6497c478bd9Sstevel@tonic-gate 	switch (f_ckmtab(magicbuf, mread, 0)) {
6507c478bd9Sstevel@tonic-gate 		case -1:	/* Error */
6517c478bd9Sstevel@tonic-gate 			exit(2);
6527c478bd9Sstevel@tonic-gate 			break;
6537c478bd9Sstevel@tonic-gate 		case 0:		/* Not magic */
6547c478bd9Sstevel@tonic-gate 			return (0);
6557c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
6567c478bd9Sstevel@tonic-gate 			break;
6577c478bd9Sstevel@tonic-gate 		default:	/* Switch is magic index */
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 			/*
6607c478bd9Sstevel@tonic-gate 			 * f_ckmtab recognizes file type,
6617c478bd9Sstevel@tonic-gate 			 * check if it is PostScript.
6627c478bd9Sstevel@tonic-gate 			 * if not, check if elf or a.out
6637c478bd9Sstevel@tonic-gate 			 */
6647c478bd9Sstevel@tonic-gate 			if (magicbuf[0] == '%' && magicbuf[1] == '!') {
6657c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
6667c478bd9Sstevel@tonic-gate 			} else {
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 				/*
6697c478bd9Sstevel@tonic-gate 				 * Check that the file is executable (dynamic
6707c478bd9Sstevel@tonic-gate 				 * objects must be executable to be exec'ed,
6717c478bd9Sstevel@tonic-gate 				 * shared objects need not be, but by convention
6727c478bd9Sstevel@tonic-gate 				 * should be executable).
6737c478bd9Sstevel@tonic-gate 				 *
6747c478bd9Sstevel@tonic-gate 				 * Note that we should already have processed
6757c478bd9Sstevel@tonic-gate 				 * the file if it was an ELF file.
6767c478bd9Sstevel@tonic-gate 				 */
6777c478bd9Sstevel@tonic-gate 				ar_coff_or_aout(elffd);
6787c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
6797c478bd9Sstevel@tonic-gate 			}
6807c478bd9Sstevel@tonic-gate 			return (1);
6817c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
6827c478bd9Sstevel@tonic-gate 			break;
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	return (0);	/* file was not identified */
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate /*
6897c478bd9Sstevel@tonic-gate  * def_context_tests() - default context-sensitive tests.
6907c478bd9Sstevel@tonic-gate  *	These are the last tests to be applied.
6917c478bd9Sstevel@tonic-gate  *	If no match is found, prints out "data".
6927c478bd9Sstevel@tonic-gate  */
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate static void
6957c478bd9Sstevel@tonic-gate def_context_tests(void)
6967c478bd9Sstevel@tonic-gate {
6977c478bd9Sstevel@tonic-gate 	int	j;
6987c478bd9Sstevel@tonic-gate 	int	nl;
6997c478bd9Sstevel@tonic-gate 	char	ch;
7007c478bd9Sstevel@tonic-gate 	int	len;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if (ccom() == 0)
7037c478bd9Sstevel@tonic-gate 		goto notc;
7047c478bd9Sstevel@tonic-gate 	while (fbuf[i] == '#') {
7057c478bd9Sstevel@tonic-gate 		j = i;
7067c478bd9Sstevel@tonic-gate 		while (fbuf[i++] != '\n') {
7077c478bd9Sstevel@tonic-gate 			if (i - j > 255) {
7087c478bd9Sstevel@tonic-gate 				(void) printf(gettext("data\n"));
7097c478bd9Sstevel@tonic-gate 				return;
7107c478bd9Sstevel@tonic-gate 			}
7117c478bd9Sstevel@tonic-gate 			if (i >= fbsz)
7127c478bd9Sstevel@tonic-gate 				goto notc;
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 		if (ccom() == 0)
7157c478bd9Sstevel@tonic-gate 			goto notc;
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate check:
7187c478bd9Sstevel@tonic-gate 	if (lookup(c) == 1) {
7197c478bd9Sstevel@tonic-gate 		while ((ch = fbuf[i]) != ';' && ch != '{') {
7207c478bd9Sstevel@tonic-gate 			if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
7217c478bd9Sstevel@tonic-gate 				len = 1;
7227c478bd9Sstevel@tonic-gate 			i += len;
7237c478bd9Sstevel@tonic-gate 			if (i >= fbsz)
7247c478bd9Sstevel@tonic-gate 				goto notc;
7257c478bd9Sstevel@tonic-gate 		}
7267c478bd9Sstevel@tonic-gate 		(void) printf(gettext("c program text"));
7277c478bd9Sstevel@tonic-gate 		goto outa;
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 	nl = 0;
7307c478bd9Sstevel@tonic-gate 	while (fbuf[i] != '(') {
7317c478bd9Sstevel@tonic-gate 		if (fbuf[i] <= 0)
7327c478bd9Sstevel@tonic-gate 			goto notas;
7337c478bd9Sstevel@tonic-gate 		if (fbuf[i] == ';') {
7347c478bd9Sstevel@tonic-gate 			i++;
7357c478bd9Sstevel@tonic-gate 			goto check;
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 		if (fbuf[i++] == '\n')
7387c478bd9Sstevel@tonic-gate 			if (nl++ > 6)
7397c478bd9Sstevel@tonic-gate 				goto notc;
7407c478bd9Sstevel@tonic-gate 		if (i >= fbsz)
7417c478bd9Sstevel@tonic-gate 			goto notc;
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 	while (fbuf[i] != ')') {
7447c478bd9Sstevel@tonic-gate 		if (fbuf[i++] == '\n')
7457c478bd9Sstevel@tonic-gate 			if (nl++ > 6)
7467c478bd9Sstevel@tonic-gate 				goto notc;
7477c478bd9Sstevel@tonic-gate 		if (i >= fbsz)
7487c478bd9Sstevel@tonic-gate 			goto notc;
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 	while (fbuf[i] != '{') {
7517c478bd9Sstevel@tonic-gate 		if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
7527c478bd9Sstevel@tonic-gate 			len = 1;
7537c478bd9Sstevel@tonic-gate 		if (fbuf[i] == '\n')
7547c478bd9Sstevel@tonic-gate 			if (nl++ > 6)
7557c478bd9Sstevel@tonic-gate 				goto notc;
7567c478bd9Sstevel@tonic-gate 		i += len;
7577c478bd9Sstevel@tonic-gate 		if (i >= fbsz)
7587c478bd9Sstevel@tonic-gate 			goto notc;
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 	(void) printf(gettext("c program text"));
7617c478bd9Sstevel@tonic-gate 	goto outa;
7627c478bd9Sstevel@tonic-gate notc:
7637c478bd9Sstevel@tonic-gate 	i = 0;			/* reset to begining of file again */
7647c478bd9Sstevel@tonic-gate 	while (fbuf[i] == 'c' || fbuf[i] == 'C'|| fbuf[i] == '!' ||
7657c478bd9Sstevel@tonic-gate 	    fbuf[i] == '*' || fbuf[i] == '\n') {
7667c478bd9Sstevel@tonic-gate 		while (fbuf[i++] != '\n')
7677c478bd9Sstevel@tonic-gate 			if (i >= fbsz)
7687c478bd9Sstevel@tonic-gate 				goto notfort;
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 	if (lookup(fort) == 1) {
7717c478bd9Sstevel@tonic-gate 		(void) printf(gettext("fortran program text"));
7727c478bd9Sstevel@tonic-gate 		goto outa;
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate notfort:			/* looking for assembler program */
7757c478bd9Sstevel@tonic-gate 	i = 0;			/* reset to beginning of file again */
7767c478bd9Sstevel@tonic-gate 	if (ccom() == 0)	/* assembler programs may contain */
7777c478bd9Sstevel@tonic-gate 				/* c-style comments */
7787c478bd9Sstevel@tonic-gate 		goto notas;
7797c478bd9Sstevel@tonic-gate 	if (ascom() == 0)
7807c478bd9Sstevel@tonic-gate 		goto notas;
7817c478bd9Sstevel@tonic-gate 	j = i - 1;
7827c478bd9Sstevel@tonic-gate 	if (fbuf[i] == '.') {
7837c478bd9Sstevel@tonic-gate 		i++;
7847c478bd9Sstevel@tonic-gate 		if (lookup(as) == 1) {
7857c478bd9Sstevel@tonic-gate 			(void) printf(gettext("assembler program text"));
7867c478bd9Sstevel@tonic-gate 			goto outa;
7877c478bd9Sstevel@tonic-gate 		} else if (j != -1 && fbuf[j] == '\n' && isalpha(fbuf[j + 2])) {
7887c478bd9Sstevel@tonic-gate 			(void) printf(
7897c478bd9Sstevel@tonic-gate 			    gettext("[nt]roff, tbl, or eqn input text"));
7907c478bd9Sstevel@tonic-gate 			goto outa;
7917c478bd9Sstevel@tonic-gate 		}
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 	while (lookup(asc) == 0) {
7947c478bd9Sstevel@tonic-gate 		if (ccom() == 0)
7957c478bd9Sstevel@tonic-gate 			goto notas;
7967c478bd9Sstevel@tonic-gate 		if (ascom() == 0)
7977c478bd9Sstevel@tonic-gate 			goto notas;
7987c478bd9Sstevel@tonic-gate 		while (fbuf[i] != '\n' && fbuf[i++] != ':') {
7997c478bd9Sstevel@tonic-gate 			if (i >= fbsz)
8007c478bd9Sstevel@tonic-gate 				goto notas;
8017c478bd9Sstevel@tonic-gate 		}
8027c478bd9Sstevel@tonic-gate 		while (fbuf[i] == '\n' || fbuf[i] == ' ' || fbuf[i] == '\t')
8037c478bd9Sstevel@tonic-gate 			if (i++ >= fbsz)
8047c478bd9Sstevel@tonic-gate 				goto notas;
8057c478bd9Sstevel@tonic-gate 		j = i - 1;
8067c478bd9Sstevel@tonic-gate 		if (fbuf[i] == '.') {
8077c478bd9Sstevel@tonic-gate 			i++;
8087c478bd9Sstevel@tonic-gate 			if (lookup(as) == 1) {
8097c478bd9Sstevel@tonic-gate 				(void) printf(
8107c478bd9Sstevel@tonic-gate 				    gettext("assembler program text"));
8117c478bd9Sstevel@tonic-gate 				goto outa;
8127c478bd9Sstevel@tonic-gate 			} else if (fbuf[j] == '\n' && isalpha(fbuf[j+2])) {
8137c478bd9Sstevel@tonic-gate 				(void) printf(
8147c478bd9Sstevel@tonic-gate 				    gettext("[nt]roff, tbl, or eqn input "
8157c478bd9Sstevel@tonic-gate 				    "text"));
8167c478bd9Sstevel@tonic-gate 				goto outa;
8177c478bd9Sstevel@tonic-gate 			}
8187c478bd9Sstevel@tonic-gate 		}
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 	(void) printf(gettext("assembler program text"));
8217c478bd9Sstevel@tonic-gate 	goto outa;
8227c478bd9Sstevel@tonic-gate notas:
8237c478bd9Sstevel@tonic-gate 	/* start modification for multibyte env */
8247c478bd9Sstevel@tonic-gate 	IS_ascii = 1;
8257c478bd9Sstevel@tonic-gate 	if (fbsz < FBSZ)
8267c478bd9Sstevel@tonic-gate 		Max = fbsz;
8277c478bd9Sstevel@tonic-gate 	else
8287c478bd9Sstevel@tonic-gate 		Max = FBSZ - MB_LEN_MAX; /* prevent cut of wchar read */
8297c478bd9Sstevel@tonic-gate 	/* end modification for multibyte env */
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	for (i = 0; i < Max; /* null */)
8327c478bd9Sstevel@tonic-gate 		if (fbuf[i] & 0200) {
8337c478bd9Sstevel@tonic-gate 			IS_ascii = 0;
8347c478bd9Sstevel@tonic-gate 			if (fbuf[0] == '\100' && fbuf[1] == '\357') {
8357c478bd9Sstevel@tonic-gate 				(void) printf(gettext("troff output\n"));
8367c478bd9Sstevel@tonic-gate 				return;
8377c478bd9Sstevel@tonic-gate 			}
8387c478bd9Sstevel@tonic-gate 		/* start modification for multibyte env */
8397c478bd9Sstevel@tonic-gate 			if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX))
8407c478bd9Sstevel@tonic-gate 			    <= 0 || !iswprint(wchar)) {
8417c478bd9Sstevel@tonic-gate 				(void) printf(gettext("data\n"));
8427c478bd9Sstevel@tonic-gate 				return;
8437c478bd9Sstevel@tonic-gate 			}
8447c478bd9Sstevel@tonic-gate 			i += length;
8457c478bd9Sstevel@tonic-gate 		}
8467c478bd9Sstevel@tonic-gate 		else
8477c478bd9Sstevel@tonic-gate 			i++;
8487c478bd9Sstevel@tonic-gate 	i = fbsz;
8497c478bd9Sstevel@tonic-gate 		/* end modification for multibyte env */
8507c478bd9Sstevel@tonic-gate 	if (mbuf.st_mode&(S_IXUSR|S_IXGRP|S_IXOTH))
8517c478bd9Sstevel@tonic-gate 		(void) printf(gettext("commands text"));
8527c478bd9Sstevel@tonic-gate 	else if (troffint(fbuf, fbsz))
8537c478bd9Sstevel@tonic-gate 		(void) printf(gettext("troff intermediate output text"));
8547c478bd9Sstevel@tonic-gate 	else if (english(fbuf, fbsz))
8557c478bd9Sstevel@tonic-gate 		(void) printf(gettext("English text"));
8567c478bd9Sstevel@tonic-gate 	else if (IS_ascii)
8577c478bd9Sstevel@tonic-gate 		(void) printf(gettext("ascii text"));
8587c478bd9Sstevel@tonic-gate 	else
8597c478bd9Sstevel@tonic-gate 		(void) printf(gettext("text")); /* for multibyte env */
8607c478bd9Sstevel@tonic-gate outa:
8617c478bd9Sstevel@tonic-gate 	/*
8627c478bd9Sstevel@tonic-gate 	 * This code is to make sure that no MB char is cut in half
8637c478bd9Sstevel@tonic-gate 	 * while still being used.
8647c478bd9Sstevel@tonic-gate 	 */
8657c478bd9Sstevel@tonic-gate 	fbsz = (fbsz < FBSZ ? fbsz : fbsz - MB_CUR_MAX + 1);
8667c478bd9Sstevel@tonic-gate 	while (i < fbsz) {
8677c478bd9Sstevel@tonic-gate 		if (isascii(fbuf[i])) {
8687c478bd9Sstevel@tonic-gate 			i++;
8697c478bd9Sstevel@tonic-gate 			continue;
8707c478bd9Sstevel@tonic-gate 		} else {
8717c478bd9Sstevel@tonic-gate 			if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX))
8727c478bd9Sstevel@tonic-gate 			    <= 0 || !iswprint(wchar)) {
8737c478bd9Sstevel@tonic-gate 				(void) printf(gettext(" with garbage\n"));
8747c478bd9Sstevel@tonic-gate 				return;
8757c478bd9Sstevel@tonic-gate 			}
8767c478bd9Sstevel@tonic-gate 			i = i + length;
8777c478bd9Sstevel@tonic-gate 		}
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 	(void) printf("\n");
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate static int
8837c478bd9Sstevel@tonic-gate troffint(char *bp, int n)
8847c478bd9Sstevel@tonic-gate {
8857c478bd9Sstevel@tonic-gate 	int k;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	i = 0;
8887c478bd9Sstevel@tonic-gate 	for (k = 0; k < 6; k++) {
8897c478bd9Sstevel@tonic-gate 		if (lookup(troff) == 0)
8907c478bd9Sstevel@tonic-gate 			return (0);
8917c478bd9Sstevel@tonic-gate 		if (lookup(troff) == 0)
8927c478bd9Sstevel@tonic-gate 			return (0);
8937c478bd9Sstevel@tonic-gate 		while (i < n && bp[i] != '\n')
8947c478bd9Sstevel@tonic-gate 			i++;
8957c478bd9Sstevel@tonic-gate 		if (i++ >= n)
8967c478bd9Sstevel@tonic-gate 			return (0);
8977c478bd9Sstevel@tonic-gate 	}
8987c478bd9Sstevel@tonic-gate 	return (1);
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate /*
9027c478bd9Sstevel@tonic-gate  * Determine if the passed descriptor describes an ELF file.
9037c478bd9Sstevel@tonic-gate  * If so, return the Elf handle.
9047c478bd9Sstevel@tonic-gate  */
9057c478bd9Sstevel@tonic-gate static Elf *
9067c478bd9Sstevel@tonic-gate is_elf_file(int elffd)
9077c478bd9Sstevel@tonic-gate {
9087c478bd9Sstevel@tonic-gate 	Elf *elf;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	elf = elf_begin(elffd, ELF_C_READ, (Elf *)0);
9117c478bd9Sstevel@tonic-gate 	switch (elf_kind(elf)) {
9127c478bd9Sstevel@tonic-gate 	case ELF_K_ELF:
9137c478bd9Sstevel@tonic-gate 		break;
9147c478bd9Sstevel@tonic-gate 	default:
9157c478bd9Sstevel@tonic-gate 		(void) elf_end(elf);
9167c478bd9Sstevel@tonic-gate 		elf = NULL;
9177c478bd9Sstevel@tonic-gate 		break;
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 	return (elf);
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate static void
9237c478bd9Sstevel@tonic-gate ar_coff_or_aout(int elffd)
9247c478bd9Sstevel@tonic-gate {
9257c478bd9Sstevel@tonic-gate 	Elf *elf;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	/*
9287c478bd9Sstevel@tonic-gate 	 * Get the files elf descriptor and process it as an elf or
9297c478bd9Sstevel@tonic-gate 	 * a.out (4.x) file.
9307c478bd9Sstevel@tonic-gate 	 */
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	elf = elf_begin(elffd, ELF_C_READ, (Elf *)0);
9337c478bd9Sstevel@tonic-gate 	switch (elf_kind(elf)) {
9347c478bd9Sstevel@tonic-gate 		case ELF_K_AR :
9357c478bd9Sstevel@tonic-gate 			(void) printf(gettext(", not a dynamic executable "
9367c478bd9Sstevel@tonic-gate 			    "or shared object"));
9377c478bd9Sstevel@tonic-gate 			break;
9387c478bd9Sstevel@tonic-gate 		case ELF_K_COFF:
9397c478bd9Sstevel@tonic-gate 			(void) printf(gettext(", unsupported or unknown "
9407c478bd9Sstevel@tonic-gate 			    "file type"));
9417c478bd9Sstevel@tonic-gate 			break;
9427c478bd9Sstevel@tonic-gate 		default:
9437c478bd9Sstevel@tonic-gate 			/*
9447c478bd9Sstevel@tonic-gate 			 * This is either an unknown file or an aout format
9457c478bd9Sstevel@tonic-gate 			 * At this time, we don't print dynamic/stripped
9467c478bd9Sstevel@tonic-gate 			 * info. on a.out or non-Elf binaries.
9477c478bd9Sstevel@tonic-gate 			 */
9487c478bd9Sstevel@tonic-gate 			break;
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 	(void) elf_end(elf);
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate static void
9557c478bd9Sstevel@tonic-gate print_elf_type(Elf *elf, GElf_Ehdr *ehdr, int format)
9567c478bd9Sstevel@tonic-gate {
9577c478bd9Sstevel@tonic-gate 	switch (ehdr->e_type) {
9587c478bd9Sstevel@tonic-gate 	case ET_NONE:
9597c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("unknown type"));
9607c478bd9Sstevel@tonic-gate 		break;
9617c478bd9Sstevel@tonic-gate 	case ET_REL:
9627c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("relocatable"));
9637c478bd9Sstevel@tonic-gate 		break;
9647c478bd9Sstevel@tonic-gate 	case ET_EXEC:
9657c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("executable"));
9667c478bd9Sstevel@tonic-gate 		break;
9677c478bd9Sstevel@tonic-gate 	case ET_DYN:
9687c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("dynamic lib"));
9697c478bd9Sstevel@tonic-gate 		break;
9707c478bd9Sstevel@tonic-gate 	case ET_CORE:
9717c478bd9Sstevel@tonic-gate 		if (old_core(elf, ehdr, format))
9727c478bd9Sstevel@tonic-gate 			(void) printf(" %s", gettext("pre-2.6 core file"));
9737c478bd9Sstevel@tonic-gate 		else
9747c478bd9Sstevel@tonic-gate 			(void) printf(" %s", gettext("core file"));
9757c478bd9Sstevel@tonic-gate 		break;
9767c478bd9Sstevel@tonic-gate 	default:
9777c478bd9Sstevel@tonic-gate 		break;
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate static void
9827c478bd9Sstevel@tonic-gate print_elf_machine(int machine)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	switch (machine) {
9857c478bd9Sstevel@tonic-gate 	case EM_NONE:
9867c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("unknown machine"));
9877c478bd9Sstevel@tonic-gate 		break;
9887c478bd9Sstevel@tonic-gate 	case EM_M32:
9897c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("WE32100"));
9907c478bd9Sstevel@tonic-gate 		break;
9917c478bd9Sstevel@tonic-gate 	case EM_SPARC:
9927c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("SPARC"));
9937c478bd9Sstevel@tonic-gate 		break;
9947c478bd9Sstevel@tonic-gate 	case EM_386:
9957c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("80386"));
9967c478bd9Sstevel@tonic-gate 		break;
9977c478bd9Sstevel@tonic-gate 	case EM_68K:
9987c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("M68000"));
9997c478bd9Sstevel@tonic-gate 		break;
10007c478bd9Sstevel@tonic-gate 	case EM_88K:
10017c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("M88000"));
10027c478bd9Sstevel@tonic-gate 		break;
10037c478bd9Sstevel@tonic-gate 	case EM_486:
10047c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("80486"));
10057c478bd9Sstevel@tonic-gate 		break;
10067c478bd9Sstevel@tonic-gate 	case EM_860:
10077c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("i860"));
10087c478bd9Sstevel@tonic-gate 		break;
10097c478bd9Sstevel@tonic-gate 	case EM_MIPS:
10107c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("MIPS RS3000 Big-Endian"));
10117c478bd9Sstevel@tonic-gate 		break;
10127c478bd9Sstevel@tonic-gate 	case EM_MIPS_RS3_LE:
10137c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("MIPS RS3000 Little-Endian"));
10147c478bd9Sstevel@tonic-gate 		break;
10157c478bd9Sstevel@tonic-gate 	case EM_RS6000:
10167c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("MIPS RS6000"));
10177c478bd9Sstevel@tonic-gate 		break;
10187c478bd9Sstevel@tonic-gate 	case EM_PA_RISC:
10197c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("PA-RISC"));
10207c478bd9Sstevel@tonic-gate 		break;
10217c478bd9Sstevel@tonic-gate 	case EM_nCUBE:
10227c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("nCUBE"));
10237c478bd9Sstevel@tonic-gate 		break;
10247c478bd9Sstevel@tonic-gate 	case EM_VPP500:
10257c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("VPP500"));
10267c478bd9Sstevel@tonic-gate 		break;
10277c478bd9Sstevel@tonic-gate 	case EM_SPARC32PLUS:
10287c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("SPARC32PLUS"));
10297c478bd9Sstevel@tonic-gate 		break;
10307c478bd9Sstevel@tonic-gate 	case EM_PPC:
10317c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("PowerPC"));
10327c478bd9Sstevel@tonic-gate 		break;
10337c478bd9Sstevel@tonic-gate 	case EM_SPARCV9:
10347c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("SPARCV9"));
10357c478bd9Sstevel@tonic-gate 		break;
10367c478bd9Sstevel@tonic-gate 	case EM_IA_64:
10377c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("IA64"));
10387c478bd9Sstevel@tonic-gate 		break;
10397c478bd9Sstevel@tonic-gate 	case EM_AMD64:
10407c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("AMD64"));
10417c478bd9Sstevel@tonic-gate 		break;
10427c478bd9Sstevel@tonic-gate 	default:
10437c478bd9Sstevel@tonic-gate 		break;
10447c478bd9Sstevel@tonic-gate 	}
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate static void
10487c478bd9Sstevel@tonic-gate print_elf_datatype(int datatype)
10497c478bd9Sstevel@tonic-gate {
10507c478bd9Sstevel@tonic-gate 	switch (datatype) {
10517c478bd9Sstevel@tonic-gate 	case ELFDATA2LSB:
10527c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("LSB"));
10537c478bd9Sstevel@tonic-gate 		break;
10547c478bd9Sstevel@tonic-gate 	case ELFDATA2MSB:
10557c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("MSB"));
10567c478bd9Sstevel@tonic-gate 		break;
10577c478bd9Sstevel@tonic-gate 	default:
10587c478bd9Sstevel@tonic-gate 		break;
10597c478bd9Sstevel@tonic-gate 	}
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate static void
10637c478bd9Sstevel@tonic-gate print_elf_class(int class)
10647c478bd9Sstevel@tonic-gate {
10657c478bd9Sstevel@tonic-gate 	switch (class) {
10667c478bd9Sstevel@tonic-gate 	case ELFCLASS32:
10677c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("32-bit"));
10687c478bd9Sstevel@tonic-gate 		break;
10697c478bd9Sstevel@tonic-gate 	case ELFCLASS64:
10707c478bd9Sstevel@tonic-gate 		(void) printf(" %s", gettext("64-bit"));
10717c478bd9Sstevel@tonic-gate 		break;
10727c478bd9Sstevel@tonic-gate 	default:
10737c478bd9Sstevel@tonic-gate 		break;
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate }
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate static void
10787c478bd9Sstevel@tonic-gate print_elf_flags(int machine, unsigned int flags)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	switch (machine) {
10817c478bd9Sstevel@tonic-gate 	case EM_SPARCV9:
10827c478bd9Sstevel@tonic-gate 		if (flags & EF_SPARC_EXT_MASK) {
10837c478bd9Sstevel@tonic-gate 			if (flags & EF_SPARC_SUN_US3) {
10847c478bd9Sstevel@tonic-gate 				(void) printf("%s", gettext(
10857c478bd9Sstevel@tonic-gate 				    ", UltraSPARC3 Extensions Required"));
10867c478bd9Sstevel@tonic-gate 			} else if (flags & EF_SPARC_SUN_US1) {
10877c478bd9Sstevel@tonic-gate 				(void) printf("%s", gettext(
10887c478bd9Sstevel@tonic-gate 				    ", UltraSPARC1 Extensions Required"));
10897c478bd9Sstevel@tonic-gate 			}
10907c478bd9Sstevel@tonic-gate 			if (flags & EF_SPARC_HAL_R1)
10917c478bd9Sstevel@tonic-gate 				(void) printf("%s", gettext(
10927c478bd9Sstevel@tonic-gate 				    ", HaL R1 Extensions Required"));
10937c478bd9Sstevel@tonic-gate 		}
10947c478bd9Sstevel@tonic-gate 		break;
10957c478bd9Sstevel@tonic-gate 	case EM_SPARC32PLUS:
10967c478bd9Sstevel@tonic-gate 		if (flags & EF_SPARC_32PLUS)
10977c478bd9Sstevel@tonic-gate 			(void) printf("%s", gettext(", V8+ Required"));
10987c478bd9Sstevel@tonic-gate 		if (flags & EF_SPARC_SUN_US3) {
10997c478bd9Sstevel@tonic-gate 			(void) printf("%s",
11007c478bd9Sstevel@tonic-gate 				gettext(", UltraSPARC3 Extensions Required"));
11017c478bd9Sstevel@tonic-gate 		} else if (flags & EF_SPARC_SUN_US1) {
11027c478bd9Sstevel@tonic-gate 			(void) printf("%s",
11037c478bd9Sstevel@tonic-gate 				gettext(", UltraSPARC1 Extensions Required"));
11047c478bd9Sstevel@tonic-gate 		}
11057c478bd9Sstevel@tonic-gate 		if (flags & EF_SPARC_HAL_R1)
11067c478bd9Sstevel@tonic-gate 			(void) printf("%s",
11077c478bd9Sstevel@tonic-gate 				gettext(", HaL R1 Extensions Required"));
11087c478bd9Sstevel@tonic-gate 		break;
11097c478bd9Sstevel@tonic-gate 	default:
11107c478bd9Sstevel@tonic-gate 		break;
11117c478bd9Sstevel@tonic-gate 	}
11127c478bd9Sstevel@tonic-gate }
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate static int
11157c478bd9Sstevel@tonic-gate print_cap(Elf *elf, GElf_Ehdr *ehdr)
11167c478bd9Sstevel@tonic-gate {
11177c478bd9Sstevel@tonic-gate 	Elf_Scn	*scn = 0;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	/*
11207c478bd9Sstevel@tonic-gate 	 * Traverse the files sections to see if any software/hardware
11217c478bd9Sstevel@tonic-gate 	 * capabilities are available.
11227c478bd9Sstevel@tonic-gate 	 */
11237c478bd9Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf, scn)) != 0) {
11247c478bd9Sstevel@tonic-gate 		GElf_Word	ndx, capn;
11257c478bd9Sstevel@tonic-gate 		GElf_Shdr	shdr;
11267c478bd9Sstevel@tonic-gate 		Elf_Data	*data;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr) == 0) {
11297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
11307c478bd9Sstevel@tonic-gate 			    gettext("can't read ELF section header\n"));
11317c478bd9Sstevel@tonic-gate 			return (1);
11327c478bd9Sstevel@tonic-gate 		}
11337c478bd9Sstevel@tonic-gate 		if (shdr.sh_type != SHT_SUNW_cap)
11347c478bd9Sstevel@tonic-gate 			continue;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 		/*
11377c478bd9Sstevel@tonic-gate 		 * Get the data associated with the .cap section.
11387c478bd9Sstevel@tonic-gate 		 */
11397c478bd9Sstevel@tonic-gate 		if ((data = elf_getdata(scn, 0)) == 0) {
11407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
11417c478bd9Sstevel@tonic-gate 				gettext("can't read ELF section data\n"));
11427c478bd9Sstevel@tonic-gate 			return (1);
11437c478bd9Sstevel@tonic-gate 		}
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 		capn = (GElf_Word)(shdr.sh_size / shdr.sh_entsize);
11467c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < capn; ndx++) {
11477c478bd9Sstevel@tonic-gate 			char		str[100];
11487c478bd9Sstevel@tonic-gate 			GElf_Cap	cap;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 			if (gelf_getcap(data, ndx, &cap) == NULL) {
11517c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11527c478bd9Sstevel@tonic-gate 				    gettext("can't read capabilities data\n"));
11537c478bd9Sstevel@tonic-gate 				return (1);
11547c478bd9Sstevel@tonic-gate 			}
11557c478bd9Sstevel@tonic-gate 			if (cap.c_tag != CA_SUNW_NULL) {
11567c478bd9Sstevel@tonic-gate 				(void) cap_val2str(cap.c_tag, cap.c_un.c_val,
11577c478bd9Sstevel@tonic-gate 				    str, sizeof (str), 0, ehdr->e_machine);
11587c478bd9Sstevel@tonic-gate 				(void) printf(" [%s]", str);
11597c478bd9Sstevel@tonic-gate 			}
11607c478bd9Sstevel@tonic-gate 		}
11617c478bd9Sstevel@tonic-gate 	}
11627c478bd9Sstevel@tonic-gate 	return (0);
11637c478bd9Sstevel@tonic-gate }
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate static int
11667c478bd9Sstevel@tonic-gate elf_check(Elf *elf)
11677c478bd9Sstevel@tonic-gate {
11687c478bd9Sstevel@tonic-gate 	GElf_Ehdr	ehdr;
11697c478bd9Sstevel@tonic-gate 	GElf_Phdr	phdr;
11707c478bd9Sstevel@tonic-gate 	int		dynamic, cnt;
11717c478bd9Sstevel@tonic-gate 	char	*ident;
11727c478bd9Sstevel@tonic-gate 	size_t	size;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	/*
11757c478bd9Sstevel@tonic-gate 	 * verify information in file header
11767c478bd9Sstevel@tonic-gate 	 */
11777c478bd9Sstevel@tonic-gate 	if (gelf_getehdr(elf, &ehdr) == (GElf_Ehdr *)0) {
11787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("can't read ELF header\n"));
11797c478bd9Sstevel@tonic-gate 		return (1);
11807c478bd9Sstevel@tonic-gate 	}
11817c478bd9Sstevel@tonic-gate 	ident = elf_getident(elf, &size);
11827c478bd9Sstevel@tonic-gate 	(void) printf("%s", gettext("ELF"));
11837c478bd9Sstevel@tonic-gate 	print_elf_class(ident[EI_CLASS]);
11847c478bd9Sstevel@tonic-gate 	print_elf_datatype(ident[EI_DATA]);
11857c478bd9Sstevel@tonic-gate 	print_elf_type(elf, &ehdr, ident[EI_DATA]);
11867c478bd9Sstevel@tonic-gate 	print_elf_machine(ehdr.e_machine);
11877c478bd9Sstevel@tonic-gate 	if (ehdr.e_version == 1)
11887c478bd9Sstevel@tonic-gate 		(void) printf(" %s %d",
11897c478bd9Sstevel@tonic-gate 		    gettext("Version"), (int)ehdr.e_version);
11907c478bd9Sstevel@tonic-gate 	print_elf_flags(ehdr.e_machine, ehdr.e_flags);
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	if (core(elf, &ehdr, ident[EI_DATA]))	/* check for core file */
11937c478bd9Sstevel@tonic-gate 		return (0);
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	if (print_cap(elf, &ehdr))
11967c478bd9Sstevel@tonic-gate 		return (1);
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	/*
11997c478bd9Sstevel@tonic-gate 	 * check type
12007c478bd9Sstevel@tonic-gate 	 */
12017c478bd9Sstevel@tonic-gate 	if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN))
12027c478bd9Sstevel@tonic-gate 		return (1);
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	/*
12057c478bd9Sstevel@tonic-gate 	 * read program header and check for dynamic section
12067c478bd9Sstevel@tonic-gate 	 */
12077c478bd9Sstevel@tonic-gate 	if (ehdr.e_phnum == 0) {
12087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("can't read program header\n"));
12097c478bd9Sstevel@tonic-gate 		return (1);
12107c478bd9Sstevel@tonic-gate 	}
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	for (dynamic = 0, cnt = 0; cnt < (int)ehdr.e_phnum; cnt++) {
12137c478bd9Sstevel@tonic-gate 		if (gelf_getphdr(elf, cnt, &phdr) == NULL) {
12147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12157c478bd9Sstevel@tonic-gate 				gettext("can't read program header\n"));
12167c478bd9Sstevel@tonic-gate 			return (1);
12177c478bd9Sstevel@tonic-gate 		}
12187c478bd9Sstevel@tonic-gate 		if (phdr.p_type == PT_DYNAMIC) {
12197c478bd9Sstevel@tonic-gate 			dynamic = 1;
12207c478bd9Sstevel@tonic-gate 			break;
12217c478bd9Sstevel@tonic-gate 		}
12227c478bd9Sstevel@tonic-gate 	}
12237c478bd9Sstevel@tonic-gate 	if (dynamic)
12247c478bd9Sstevel@tonic-gate 		(void) printf(gettext(", dynamically linked"));
12257c478bd9Sstevel@tonic-gate 	else
12267c478bd9Sstevel@tonic-gate 		(void) printf(gettext(", statically linked"));
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	is_stripped(elf);
12297c478bd9Sstevel@tonic-gate 	return (0);
12307c478bd9Sstevel@tonic-gate }
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate /*
12337c478bd9Sstevel@tonic-gate  * is_stripped prints information on whether the executable has
12347c478bd9Sstevel@tonic-gate  * been stripped.
12357c478bd9Sstevel@tonic-gate  */
12367c478bd9Sstevel@tonic-gate static void
12377c478bd9Sstevel@tonic-gate is_stripped(Elf *elf)
12387c478bd9Sstevel@tonic-gate {
12397c478bd9Sstevel@tonic-gate 	GElf_Shdr	shdr;
12407c478bd9Sstevel@tonic-gate 	GElf_Ehdr	ehdr;
12417c478bd9Sstevel@tonic-gate 	Elf_Scn		*scn, *nextscn;
12427c478bd9Sstevel@tonic-gate 	char		*section_name;
12437c478bd9Sstevel@tonic-gate 	int		symtab = 0;
12447c478bd9Sstevel@tonic-gate 	int		debuginfo = 0;
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	if (gelf_getehdr(elf, &ehdr) == NULL) {
12487c478bd9Sstevel@tonic-gate 		return;
12497c478bd9Sstevel@tonic-gate 	}
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	/*
12527c478bd9Sstevel@tonic-gate 	 * Definition time:
12537c478bd9Sstevel@tonic-gate 	 *	- "not stripped" means that an executable file
12547c478bd9Sstevel@tonic-gate 	 *	contains a Symbol Table (.symtab)
12557c478bd9Sstevel@tonic-gate 	 *	- "stripped" means that an executable file
12567c478bd9Sstevel@tonic-gate 	 *	does not contain a Symbol Table.
12577c478bd9Sstevel@tonic-gate 	 * When strip -l or strip -x is run, it strips the
12587c478bd9Sstevel@tonic-gate 	 * debugging information (.line section name (strip -l),
12597c478bd9Sstevel@tonic-gate 	 * .line, .debug*, .stabs*, .dwarf* section names
12607c478bd9Sstevel@tonic-gate 	 * and SHT_SUNW_DEBUGSTR and SHT_SUNW_DEBUG
12617c478bd9Sstevel@tonic-gate 	 * section types (strip -x), however the Symbol
12627c478bd9Sstevel@tonic-gate 	 * Table will still be present.
12637c478bd9Sstevel@tonic-gate 	 * Therefore, if
12647c478bd9Sstevel@tonic-gate 	 *	- No Symbol Table present, then report
12657c478bd9Sstevel@tonic-gate 	 *		"stripped"
12667c478bd9Sstevel@tonic-gate 	 *	- Symbol Table present with debugging
12677c478bd9Sstevel@tonic-gate 	 *	information (line number or debug section names,
12687c478bd9Sstevel@tonic-gate 	 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
12697c478bd9Sstevel@tonic-gate 	 *	types) then report:
12707c478bd9Sstevel@tonic-gate 	 *		"not stripped"
12717c478bd9Sstevel@tonic-gate 	 *	- Symbol Table present with no debugging
12727c478bd9Sstevel@tonic-gate 	 *	information (line number or debug section names,
12737c478bd9Sstevel@tonic-gate 	 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
12747c478bd9Sstevel@tonic-gate 	 *	types) then report:
12757c478bd9Sstevel@tonic-gate 	 *		"not stripped, no debugging information
12767c478bd9Sstevel@tonic-gate 	 *		available"
12777c478bd9Sstevel@tonic-gate 	 */
12787c478bd9Sstevel@tonic-gate 	scn = NULL;
12797c478bd9Sstevel@tonic-gate 	while ((nextscn = elf_nextscn(elf, scn)) != NULL) {
12807c478bd9Sstevel@tonic-gate 		if (symtab && debuginfo) {
12817c478bd9Sstevel@tonic-gate 			break;
12827c478bd9Sstevel@tonic-gate 		}
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 		scn = nextscn;
12857c478bd9Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr) == NULL) {
12867c478bd9Sstevel@tonic-gate 			continue;
12877c478bd9Sstevel@tonic-gate 		}
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 		if (!symtab && (shdr.sh_type == SHT_SYMTAB)) {
12907c478bd9Sstevel@tonic-gate 			symtab++;
12917c478bd9Sstevel@tonic-gate 			continue;
12927c478bd9Sstevel@tonic-gate 		}
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 		if (!debuginfo &&
12957c478bd9Sstevel@tonic-gate 		    ((shdr.sh_type == SHT_SUNW_DEBUG) ||
12967c478bd9Sstevel@tonic-gate 		    (shdr.sh_type == SHT_SUNW_DEBUGSTR) ||
12977c478bd9Sstevel@tonic-gate 		    (((section_name = elf_strptr(elf, ehdr.e_shstrndx,
12987c478bd9Sstevel@tonic-gate 		    (size_t)shdr.sh_name)) != NULL) &&
12997c478bd9Sstevel@tonic-gate 		    (is_in_list(debug_sections, section_name))))) {
13007c478bd9Sstevel@tonic-gate 			debuginfo++;
13017c478bd9Sstevel@tonic-gate 		}
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	/*
13057c478bd9Sstevel@tonic-gate 	 * Now that we've scanned all sections, print out the appropriate
13067c478bd9Sstevel@tonic-gate 	 * diagnostic.
13077c478bd9Sstevel@tonic-gate 	 */
13087c478bd9Sstevel@tonic-gate 	if (symtab) {
13097c478bd9Sstevel@tonic-gate 		(void) printf(gettext(", not stripped"));
13107c478bd9Sstevel@tonic-gate 		if (!debuginfo) {
13117c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
13127c478bd9Sstevel@tonic-gate 			    ", no debugging information available"));
13137c478bd9Sstevel@tonic-gate 		}
13147c478bd9Sstevel@tonic-gate 	} else {
13157c478bd9Sstevel@tonic-gate 		(void) printf(gettext(", stripped"));
13167c478bd9Sstevel@tonic-gate 	}
13177c478bd9Sstevel@tonic-gate }
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate /*
1320*c13de8f6Sab196087  * is_rtld_config - If file is a runtime linker config file, prints
1321*c13de8f6Sab196087  * the description and returns True (1). Otherwise, silently returns
1322*c13de8f6Sab196087  * False (0).
1323*c13de8f6Sab196087  */
1324*c13de8f6Sab196087 int
1325*c13de8f6Sab196087 is_rtld_config(void)
1326*c13de8f6Sab196087 {
1327*c13de8f6Sab196087 	Rtc_id *id;
1328*c13de8f6Sab196087 
1329*c13de8f6Sab196087 	if ((fbsz >= sizeof (*id)) && RTC_ID_TEST(fbuf)) {
1330*c13de8f6Sab196087 		(void) printf(gettext("Runtime Linking Configuration"));
1331*c13de8f6Sab196087 		/* LINTED: pointer cast may result in improper alignment */
1332*c13de8f6Sab196087 		id = (Rtc_id *) fbuf;
1333*c13de8f6Sab196087 		print_elf_class(id->id_class);
1334*c13de8f6Sab196087 		print_elf_datatype(id->id_data);
1335*c13de8f6Sab196087 		print_elf_machine(id->id_machine);
1336*c13de8f6Sab196087 		(void) printf("\n");
1337*c13de8f6Sab196087 		return (1);
1338*c13de8f6Sab196087 	}
1339*c13de8f6Sab196087 
1340*c13de8f6Sab196087 	return (0);
1341*c13de8f6Sab196087 }
1342*c13de8f6Sab196087 
1343*c13de8f6Sab196087 /*
13447c478bd9Sstevel@tonic-gate  * lookup -
13457c478bd9Sstevel@tonic-gate  * Attempts to match one of the strings from a list, 'tab',
13467c478bd9Sstevel@tonic-gate  * with what is in the file, starting at the current index position 'i'.
13477c478bd9Sstevel@tonic-gate  * Looks past any initial whitespace and expects whitespace or other
13487c478bd9Sstevel@tonic-gate  * delimiting characters to follow the matched string.
13497c478bd9Sstevel@tonic-gate  * A match identifies the file as being 'assembler', 'fortran', 'c', etc.
13507c478bd9Sstevel@tonic-gate  * Returns 1 for a successful match, 0 otherwise.
13517c478bd9Sstevel@tonic-gate  */
13527c478bd9Sstevel@tonic-gate static int
13537c478bd9Sstevel@tonic-gate lookup(char **tab)
13547c478bd9Sstevel@tonic-gate {
13557c478bd9Sstevel@tonic-gate 	register char	r;
13567c478bd9Sstevel@tonic-gate 	register int	k, j, l;
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	while (fbuf[i] == ' ' || fbuf[i] == '\t' || fbuf[i] == '\n')
13597c478bd9Sstevel@tonic-gate 		i++;
13607c478bd9Sstevel@tonic-gate 	for (j = 0; tab[j] != 0; j++) {
13617c478bd9Sstevel@tonic-gate 		l = 0;
13627c478bd9Sstevel@tonic-gate 		for (k = i; ((r = tab[j][l++]) == fbuf[k] && r != '\0'); k++);
13637c478bd9Sstevel@tonic-gate 		if (r == '\0')
13647c478bd9Sstevel@tonic-gate 			if (fbuf[k] == ' ' || fbuf[k] == '\n' ||
13657c478bd9Sstevel@tonic-gate 			    fbuf[k] == '\t' || fbuf[k] == '{' ||
13667c478bd9Sstevel@tonic-gate 			    fbuf[k] == '/') {
13677c478bd9Sstevel@tonic-gate 				i = k;
13687c478bd9Sstevel@tonic-gate 				return (1);
13697c478bd9Sstevel@tonic-gate 			}
13707c478bd9Sstevel@tonic-gate 	}
13717c478bd9Sstevel@tonic-gate 	return (0);
13727c478bd9Sstevel@tonic-gate }
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate /*
13757c478bd9Sstevel@tonic-gate  * ccom -
13767c478bd9Sstevel@tonic-gate  * Increments the current index 'i' into the file buffer 'fbuf' past any
13777c478bd9Sstevel@tonic-gate  * whitespace lines and C-style comments found, starting at the current
13787c478bd9Sstevel@tonic-gate  * position of 'i'.  Returns 1 as long as we don't increment i past the
13797c478bd9Sstevel@tonic-gate  * size of fbuf (fbsz).  Otherwise, returns 0.
13807c478bd9Sstevel@tonic-gate  */
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate static int
13837c478bd9Sstevel@tonic-gate ccom(void)
13847c478bd9Sstevel@tonic-gate {
13857c478bd9Sstevel@tonic-gate 	register char	cc;
13867c478bd9Sstevel@tonic-gate 	int		len;
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	while ((cc = fbuf[i]) == ' ' || cc == '\t' || cc == '\n')
13897c478bd9Sstevel@tonic-gate 		if (i++ >= fbsz)
13907c478bd9Sstevel@tonic-gate 			return (0);
13917c478bd9Sstevel@tonic-gate 	if (fbuf[i] == '/' && fbuf[i+1] == '*') {
13927c478bd9Sstevel@tonic-gate 		i += 2;
13937c478bd9Sstevel@tonic-gate 		while (fbuf[i] != '*' || fbuf[i+1] != '/') {
13947c478bd9Sstevel@tonic-gate 			if (fbuf[i] == '\\')
13957c478bd9Sstevel@tonic-gate 				i++;
13967c478bd9Sstevel@tonic-gate 			if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
13977c478bd9Sstevel@tonic-gate 				len = 1;
13987c478bd9Sstevel@tonic-gate 			i += len;
13997c478bd9Sstevel@tonic-gate 			if (i >= fbsz)
14007c478bd9Sstevel@tonic-gate 				return (0);
14017c478bd9Sstevel@tonic-gate 		}
14027c478bd9Sstevel@tonic-gate 		if ((i += 2) >= fbsz)
14037c478bd9Sstevel@tonic-gate 			return (0);
14047c478bd9Sstevel@tonic-gate 	}
14057c478bd9Sstevel@tonic-gate 	if (fbuf[i] == '\n')
14067c478bd9Sstevel@tonic-gate 		if (ccom() == 0)
14077c478bd9Sstevel@tonic-gate 			return (0);
14087c478bd9Sstevel@tonic-gate 	return (1);
14097c478bd9Sstevel@tonic-gate }
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate /*
14127c478bd9Sstevel@tonic-gate  * ascom -
14137c478bd9Sstevel@tonic-gate  * Increments the current index 'i' into the file buffer 'fbuf' past
14147c478bd9Sstevel@tonic-gate  * consecutive assembler program comment lines starting with ASCOMCHAR,
14157c478bd9Sstevel@tonic-gate  * starting at the current position of 'i'.
14167c478bd9Sstevel@tonic-gate  * Returns 1 as long as we don't increment i past the
14177c478bd9Sstevel@tonic-gate  * size of fbuf (fbsz).  Otherwise returns 0.
14187c478bd9Sstevel@tonic-gate  */
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate static int
14217c478bd9Sstevel@tonic-gate ascom(void)
14227c478bd9Sstevel@tonic-gate {
14237c478bd9Sstevel@tonic-gate 	while (fbuf[i] == ASCOMCHAR) {
14247c478bd9Sstevel@tonic-gate 		i++;
14257c478bd9Sstevel@tonic-gate 		while (fbuf[i++] != '\n')
14267c478bd9Sstevel@tonic-gate 			if (i >= fbsz)
14277c478bd9Sstevel@tonic-gate 				return (0);
14287c478bd9Sstevel@tonic-gate 		while (fbuf[i] == '\n')
14297c478bd9Sstevel@tonic-gate 			if (i++ >= fbsz)
14307c478bd9Sstevel@tonic-gate 				return (0);
14317c478bd9Sstevel@tonic-gate 	}
14327c478bd9Sstevel@tonic-gate 	return (1);
14337c478bd9Sstevel@tonic-gate }
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate static int
14367c478bd9Sstevel@tonic-gate sccs(void)
14377c478bd9Sstevel@tonic-gate {				/* look for "1hddddd" where d is a digit */
14387c478bd9Sstevel@tonic-gate 	register int j;
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	if (fbuf[0] == 1 && fbuf[1] == 'h') {
14417c478bd9Sstevel@tonic-gate 		for (j = 2; j <= 6; j++) {
14427c478bd9Sstevel@tonic-gate 			if (isdigit(fbuf[j]))
14437c478bd9Sstevel@tonic-gate 				continue;
14447c478bd9Sstevel@tonic-gate 			else
14457c478bd9Sstevel@tonic-gate 				return (0);
14467c478bd9Sstevel@tonic-gate 		}
14477c478bd9Sstevel@tonic-gate 	} else {
14487c478bd9Sstevel@tonic-gate 		return (0);
14497c478bd9Sstevel@tonic-gate 	}
14507c478bd9Sstevel@tonic-gate 	return (1);
14517c478bd9Sstevel@tonic-gate }
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate static int
14547c478bd9Sstevel@tonic-gate english(char *bp, int n)
14557c478bd9Sstevel@tonic-gate {
14567c478bd9Sstevel@tonic-gate #define	NASC 128		/* number of ascii char ?? */
14577c478bd9Sstevel@tonic-gate 	register int	j, vow, freq, rare, len;
14587c478bd9Sstevel@tonic-gate 	register int	badpun = 0, punct = 0;
14597c478bd9Sstevel@tonic-gate 	int	ct[NASC];
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	if (n < 50)
14627c478bd9Sstevel@tonic-gate 		return (0); /* no point in statistics on squibs */
14637c478bd9Sstevel@tonic-gate 	for (j = 0; j < NASC; j++)
14647c478bd9Sstevel@tonic-gate 		ct[j] = 0;
14657c478bd9Sstevel@tonic-gate 	for (j = 0; j < n; j += len) {
14667c478bd9Sstevel@tonic-gate 		if ((unsigned char)bp[j] < NASC)
14677c478bd9Sstevel@tonic-gate 			ct[bp[j]|040]++;
14687c478bd9Sstevel@tonic-gate 		switch (bp[j]) {
14697c478bd9Sstevel@tonic-gate 		case '.':
14707c478bd9Sstevel@tonic-gate 		case ',':
14717c478bd9Sstevel@tonic-gate 		case ')':
14727c478bd9Sstevel@tonic-gate 		case '%':
14737c478bd9Sstevel@tonic-gate 		case ';':
14747c478bd9Sstevel@tonic-gate 		case ':':
14757c478bd9Sstevel@tonic-gate 		case '?':
14767c478bd9Sstevel@tonic-gate 			punct++;
14777c478bd9Sstevel@tonic-gate 			if (j < n-1 && bp[j+1] != ' ' && bp[j+1] != '\n')
14787c478bd9Sstevel@tonic-gate 				badpun++;
14797c478bd9Sstevel@tonic-gate 		}
14807c478bd9Sstevel@tonic-gate 		if ((len = mblen(&bp[j], MB_CUR_MAX)) <= 0)
14817c478bd9Sstevel@tonic-gate 			len = 1;
14827c478bd9Sstevel@tonic-gate 	}
14837c478bd9Sstevel@tonic-gate 	if (badpun*5 > punct)
14847c478bd9Sstevel@tonic-gate 		return (0);
14857c478bd9Sstevel@tonic-gate 	vow = ct['a'] + ct['e'] + ct['i'] + ct['o'] + ct['u'];
14867c478bd9Sstevel@tonic-gate 	freq = ct['e'] + ct['t'] + ct['a'] + ct['i'] + ct['o'] + ct['n'];
14877c478bd9Sstevel@tonic-gate 	rare = ct['v'] + ct['j'] + ct['k'] + ct['q'] + ct['x'] + ct['z'];
14887c478bd9Sstevel@tonic-gate 	if (2*ct[';'] > ct['e'])
14897c478bd9Sstevel@tonic-gate 		return (0);
14907c478bd9Sstevel@tonic-gate 	if ((ct['>'] + ct['<'] + ct['/']) > ct['e'])
14917c478bd9Sstevel@tonic-gate 		return (0);	/* shell file test */
14927c478bd9Sstevel@tonic-gate 	return (vow * 5 >= n - ct[' '] && freq >= 10 * rare);
14937c478bd9Sstevel@tonic-gate }
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate /*
14967c478bd9Sstevel@tonic-gate  * Convert a word from an elf file to native format.
14977c478bd9Sstevel@tonic-gate  * This is needed because there's no elf routine to
14987c478bd9Sstevel@tonic-gate  * get and decode a Note section header.
14997c478bd9Sstevel@tonic-gate  */
15007c478bd9Sstevel@tonic-gate static void
15017c478bd9Sstevel@tonic-gate convert_gelf_word(Elf *elf, GElf_Word *data, int version, int format)
15027c478bd9Sstevel@tonic-gate {
15037c478bd9Sstevel@tonic-gate 	Elf_Data src, dst;
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	dst.d_buf = data;
15067c478bd9Sstevel@tonic-gate 	dst.d_version = version;
15077c478bd9Sstevel@tonic-gate 	dst.d_size = sizeof (GElf_Word);
15087c478bd9Sstevel@tonic-gate 	dst.d_type = ELF_T_WORD;
15097c478bd9Sstevel@tonic-gate 	src.d_buf = data;
15107c478bd9Sstevel@tonic-gate 	src.d_version = version;
15117c478bd9Sstevel@tonic-gate 	src.d_size = sizeof (GElf_Word);
15127c478bd9Sstevel@tonic-gate 	src.d_type = ELF_T_WORD;
15137c478bd9Sstevel@tonic-gate 	(void) gelf_xlatetom(elf, &dst, &src, format);
15147c478bd9Sstevel@tonic-gate }
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate static void
15177c478bd9Sstevel@tonic-gate convert_gelf_nhdr(Elf *elf, GElf_Nhdr *nhdr, GElf_Word version, int format)
15187c478bd9Sstevel@tonic-gate {
15197c478bd9Sstevel@tonic-gate 	convert_gelf_word(elf, &nhdr->n_namesz, version, format);
15207c478bd9Sstevel@tonic-gate 	convert_gelf_word(elf, &nhdr->n_descsz, version, format);
15217c478bd9Sstevel@tonic-gate 	convert_gelf_word(elf, &nhdr->n_type, version, format);
15227c478bd9Sstevel@tonic-gate }
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate /*
15257c478bd9Sstevel@tonic-gate  * Return true if it is an old (pre-restructured /proc) core file.
15267c478bd9Sstevel@tonic-gate  */
15277c478bd9Sstevel@tonic-gate static int
15287c478bd9Sstevel@tonic-gate old_core(Elf *elf, GElf_Ehdr *ehdr, int format)
15297c478bd9Sstevel@tonic-gate {
15307c478bd9Sstevel@tonic-gate 	register int inx;
15317c478bd9Sstevel@tonic-gate 	GElf_Phdr phdr;
15327c478bd9Sstevel@tonic-gate 	GElf_Phdr nphdr;
15337c478bd9Sstevel@tonic-gate 	GElf_Nhdr nhdr;
15347c478bd9Sstevel@tonic-gate 	off_t offset;
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 	if (ehdr->e_type != ET_CORE)
15377c478bd9Sstevel@tonic-gate 		return (0);
15387c478bd9Sstevel@tonic-gate 	for (inx = 0; inx < (int)ehdr->e_phnum; inx++) {
15397c478bd9Sstevel@tonic-gate 		if (gelf_getphdr(elf, inx, &phdr) == NULL) {
15407c478bd9Sstevel@tonic-gate 			return (0);
15417c478bd9Sstevel@tonic-gate 		}
15427c478bd9Sstevel@tonic-gate 		if (phdr.p_type == PT_NOTE) {
15437c478bd9Sstevel@tonic-gate 			/*
15447c478bd9Sstevel@tonic-gate 			 * If the next segment is also a note, use it instead.
15457c478bd9Sstevel@tonic-gate 			 */
15467c478bd9Sstevel@tonic-gate 			if (gelf_getphdr(elf, inx+1, &nphdr) == NULL) {
15477c478bd9Sstevel@tonic-gate 				return (0);
15487c478bd9Sstevel@tonic-gate 			}
15497c478bd9Sstevel@tonic-gate 			if (nphdr.p_type == PT_NOTE)
15507c478bd9Sstevel@tonic-gate 				phdr = nphdr;
15517c478bd9Sstevel@tonic-gate 			offset = (off_t)phdr.p_offset;
15527c478bd9Sstevel@tonic-gate 			(void) pread(ifd, &nhdr, sizeof (GElf_Nhdr), offset);
15537c478bd9Sstevel@tonic-gate 			convert_gelf_nhdr(elf, &nhdr, ehdr->e_version, format);
15547c478bd9Sstevel@tonic-gate 			/*
15557c478bd9Sstevel@tonic-gate 			 * Old core files have type NT_PRPSINFO.
15567c478bd9Sstevel@tonic-gate 			 */
15577c478bd9Sstevel@tonic-gate 			if (nhdr.n_type == NT_PRPSINFO)
15587c478bd9Sstevel@tonic-gate 				return (1);
15597c478bd9Sstevel@tonic-gate 			return (0);
15607c478bd9Sstevel@tonic-gate 		}
15617c478bd9Sstevel@tonic-gate 	}
15627c478bd9Sstevel@tonic-gate 	return (0);
15637c478bd9Sstevel@tonic-gate }
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate /*
15667c478bd9Sstevel@tonic-gate  * If it's a core file, print out the name of the file that dumped core.
15677c478bd9Sstevel@tonic-gate  */
15687c478bd9Sstevel@tonic-gate static int
15697c478bd9Sstevel@tonic-gate core(Elf *elf, GElf_Ehdr *ehdr, int format)
15707c478bd9Sstevel@tonic-gate {
15717c478bd9Sstevel@tonic-gate 	register int inx;
15727c478bd9Sstevel@tonic-gate 	char *psinfo;
15737c478bd9Sstevel@tonic-gate 	GElf_Phdr phdr;
15747c478bd9Sstevel@tonic-gate 	GElf_Phdr nphdr;
15757c478bd9Sstevel@tonic-gate 	GElf_Nhdr nhdr;
15767c478bd9Sstevel@tonic-gate 	off_t offset;
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	if (ehdr->e_type != ET_CORE)
15797c478bd9Sstevel@tonic-gate 		return (0);
15807c478bd9Sstevel@tonic-gate 	for (inx = 0; inx < (int)ehdr->e_phnum; inx++) {
15817c478bd9Sstevel@tonic-gate 		if (gelf_getphdr(elf, inx, &phdr) == NULL) {
15827c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
15837c478bd9Sstevel@tonic-gate 				gettext("can't read program header\n"));
15847c478bd9Sstevel@tonic-gate 			return (0);
15857c478bd9Sstevel@tonic-gate 		}
15867c478bd9Sstevel@tonic-gate 		if (phdr.p_type == PT_NOTE) {
15877c478bd9Sstevel@tonic-gate 			char *fname;
15887c478bd9Sstevel@tonic-gate 			size_t size;
15897c478bd9Sstevel@tonic-gate 			/*
15907c478bd9Sstevel@tonic-gate 			 * If the next segment is also a note, use it instead.
15917c478bd9Sstevel@tonic-gate 			 */
15927c478bd9Sstevel@tonic-gate 			if (gelf_getphdr(elf, inx+1, &nphdr) == NULL) {
15937c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
15947c478bd9Sstevel@tonic-gate 				    gettext("can't read program header\n"));
15957c478bd9Sstevel@tonic-gate 				return (0);
15967c478bd9Sstevel@tonic-gate 			}
15977c478bd9Sstevel@tonic-gate 			if (nphdr.p_type == PT_NOTE)
15987c478bd9Sstevel@tonic-gate 				phdr = nphdr;
15997c478bd9Sstevel@tonic-gate 			offset = (off_t)phdr.p_offset;
16007c478bd9Sstevel@tonic-gate 			(void) pread(ifd, &nhdr, sizeof (GElf_Nhdr), offset);
16017c478bd9Sstevel@tonic-gate 			convert_gelf_nhdr(elf, &nhdr, ehdr->e_version, format);
16027c478bd9Sstevel@tonic-gate 			/*
16037c478bd9Sstevel@tonic-gate 			 * Note: the ABI states that n_namesz must
16047c478bd9Sstevel@tonic-gate 			 * be rounded up to a 4 byte boundary.
16057c478bd9Sstevel@tonic-gate 			 */
16067c478bd9Sstevel@tonic-gate 			offset += sizeof (GElf_Nhdr) +
16077c478bd9Sstevel@tonic-gate 			    ((nhdr.n_namesz + 0x03) & ~0x3);
16087c478bd9Sstevel@tonic-gate 			size = nhdr.n_descsz;
16097c478bd9Sstevel@tonic-gate 			psinfo = malloc(size);
16107c478bd9Sstevel@tonic-gate 			(void) pread(ifd, psinfo, size, offset);
16117c478bd9Sstevel@tonic-gate 			/*
16127c478bd9Sstevel@tonic-gate 			 * We want to print the string contained
16137c478bd9Sstevel@tonic-gate 			 * in psinfo->pr_fname[], where 'psinfo'
16147c478bd9Sstevel@tonic-gate 			 * is either an old NT_PRPSINFO structure
16157c478bd9Sstevel@tonic-gate 			 * or a new NT_PSINFO structure.
16167c478bd9Sstevel@tonic-gate 			 *
16177c478bd9Sstevel@tonic-gate 			 * Old core files have only type NT_PRPSINFO.
16187c478bd9Sstevel@tonic-gate 			 * New core files have type NT_PSINFO.
16197c478bd9Sstevel@tonic-gate 			 *
16207c478bd9Sstevel@tonic-gate 			 * These structures are also different by
16217c478bd9Sstevel@tonic-gate 			 * virtue of being contained in a core file
16227c478bd9Sstevel@tonic-gate 			 * of either 32-bit or 64-bit type.
16237c478bd9Sstevel@tonic-gate 			 *
16247c478bd9Sstevel@tonic-gate 			 * To further complicate matters, we ourself
16257c478bd9Sstevel@tonic-gate 			 * might be compiled either 32-bit or 64-bit.
16267c478bd9Sstevel@tonic-gate 			 *
16277c478bd9Sstevel@tonic-gate 			 * For these reason, we just *know* the offsets of
16287c478bd9Sstevel@tonic-gate 			 * pr_fname[] into the four different structures
16297c478bd9Sstevel@tonic-gate 			 * here, regardless of how we are compiled.
16307c478bd9Sstevel@tonic-gate 			 */
16317c478bd9Sstevel@tonic-gate 			if (gelf_getclass(elf) == ELFCLASS32) {
16327c478bd9Sstevel@tonic-gate 				/* 32-bit core file, 32-bit structures */
16337c478bd9Sstevel@tonic-gate 				if (nhdr.n_type == NT_PSINFO)
16347c478bd9Sstevel@tonic-gate 					fname = psinfo + 88;
16357c478bd9Sstevel@tonic-gate 				else	/* old: NT_PRPSINFO */
16367c478bd9Sstevel@tonic-gate 					fname = psinfo + 84;
16377c478bd9Sstevel@tonic-gate 			} else if (gelf_getclass(elf) == ELFCLASS64) {
16387c478bd9Sstevel@tonic-gate 				/* 64-bit core file, 64-bit structures */
16397c478bd9Sstevel@tonic-gate 				if (nhdr.n_type == NT_PSINFO)
16407c478bd9Sstevel@tonic-gate 					fname = psinfo + 136;
16417c478bd9Sstevel@tonic-gate 				else	/* old: NT_PRPSINFO */
16427c478bd9Sstevel@tonic-gate 					fname = psinfo + 120;
16437c478bd9Sstevel@tonic-gate 			} else {
16447c478bd9Sstevel@tonic-gate 				free(psinfo);
16457c478bd9Sstevel@tonic-gate 				break;
16467c478bd9Sstevel@tonic-gate 			}
16477c478bd9Sstevel@tonic-gate 			(void) printf(gettext(", from '%s'"), fname);
16487c478bd9Sstevel@tonic-gate 			free(psinfo);
16497c478bd9Sstevel@tonic-gate 			break;
16507c478bd9Sstevel@tonic-gate 		}
16517c478bd9Sstevel@tonic-gate 	}
16527c478bd9Sstevel@tonic-gate 	return (1);
16537c478bd9Sstevel@tonic-gate }
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate static int
16567c478bd9Sstevel@tonic-gate shellscript(char buf[], struct stat64 *sb)
16577c478bd9Sstevel@tonic-gate {
16587c478bd9Sstevel@tonic-gate 	char *tp, *cp, *xp, *up, *gp;
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 	cp = strchr(buf, '\n');
16617c478bd9Sstevel@tonic-gate 	if (cp == NULL || cp - fbuf > fbsz)
16627c478bd9Sstevel@tonic-gate 		return (0);
16637c478bd9Sstevel@tonic-gate 	for (tp = buf; tp != cp && isspace((unsigned char)*tp); tp++)
16647c478bd9Sstevel@tonic-gate 		if (!isascii(*tp))
16657c478bd9Sstevel@tonic-gate 			return (0);
16667c478bd9Sstevel@tonic-gate 	for (xp = tp; tp != cp && !isspace((unsigned char)*tp); tp++)
16677c478bd9Sstevel@tonic-gate 		if (!isascii(*tp))
16687c478bd9Sstevel@tonic-gate 			return (0);
16697c478bd9Sstevel@tonic-gate 	if (tp == xp)
16707c478bd9Sstevel@tonic-gate 		return (0);
16717c478bd9Sstevel@tonic-gate 	if (sb->st_mode & S_ISUID)
16727c478bd9Sstevel@tonic-gate 		up = gettext("set-uid ");
16737c478bd9Sstevel@tonic-gate 	else
16747c478bd9Sstevel@tonic-gate 		up = "";
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	if (sb->st_mode & S_ISGID)
16777c478bd9Sstevel@tonic-gate 		gp = gettext("set-gid ");
16787c478bd9Sstevel@tonic-gate 	else
16797c478bd9Sstevel@tonic-gate 		gp = "";
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 	if (strncmp(xp, "/bin/sh", tp - xp) == 0)
16827c478bd9Sstevel@tonic-gate 		xp = gettext("shell");
16837c478bd9Sstevel@tonic-gate 	else if (strncmp(xp, "/bin/csh", tp - xp) == 0)
16847c478bd9Sstevel@tonic-gate 		xp = gettext("c-shell");
16857c478bd9Sstevel@tonic-gate 	else if (strncmp(xp, "/usr/sbin/dtrace", tp - xp) == 0)
16867c478bd9Sstevel@tonic-gate 		xp = gettext("DTrace");
16877c478bd9Sstevel@tonic-gate 	else
16887c478bd9Sstevel@tonic-gate 		*tp = '\0';
16897c478bd9Sstevel@tonic-gate 	/*
16907c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
16917c478bd9Sstevel@tonic-gate 	 * This message is printed by file command for shell scripts.
16927c478bd9Sstevel@tonic-gate 	 * The first %s is for the translation for "set-uid " (if the script
16937c478bd9Sstevel@tonic-gate 	 *   has the set-uid bit set), or is for an empty string (if the
16947c478bd9Sstevel@tonic-gate 	 *   script does not have the set-uid bit set).
16957c478bd9Sstevel@tonic-gate 	 * Similarly, the second %s is for the translation for "set-gid ",
16967c478bd9Sstevel@tonic-gate 	 *   or is for an empty string.
16977c478bd9Sstevel@tonic-gate 	 * The third %s is for the translation for either: "shell", "c-shell",
16987c478bd9Sstevel@tonic-gate 	 *   or "DTrace", or is for the pathname of the program the script
16997c478bd9Sstevel@tonic-gate 	 *   executes.
17007c478bd9Sstevel@tonic-gate 	 */
17017c478bd9Sstevel@tonic-gate 	(void) printf(gettext("%s%sexecutable %s script\n"), up, gp, xp);
17027c478bd9Sstevel@tonic-gate 	return (1);
17037c478bd9Sstevel@tonic-gate }
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate static int
17067c478bd9Sstevel@tonic-gate get_door_target(char *file, char *buf, size_t bufsize)
17077c478bd9Sstevel@tonic-gate {
17087c478bd9Sstevel@tonic-gate 	int fd;
17097c478bd9Sstevel@tonic-gate 	door_info_t di;
17107c478bd9Sstevel@tonic-gate 	psinfo_t psinfo;
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	if ((fd = open64(file, O_RDONLY)) < 0 ||
17137c478bd9Sstevel@tonic-gate 	    door_info(fd, &di) != 0) {
17147c478bd9Sstevel@tonic-gate 		if (fd >= 0)
17157c478bd9Sstevel@tonic-gate 			(void) close(fd);
17167c478bd9Sstevel@tonic-gate 		return (-1);
17177c478bd9Sstevel@tonic-gate 	}
17187c478bd9Sstevel@tonic-gate 	(void) close(fd);
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "/proc/%ld/psinfo", di.di_target);
17217c478bd9Sstevel@tonic-gate 	if ((fd = open64(buf, O_RDONLY)) < 0 ||
17227c478bd9Sstevel@tonic-gate 	    read(fd, &psinfo, sizeof (psinfo)) != sizeof (psinfo)) {
17237c478bd9Sstevel@tonic-gate 		if (fd >= 0)
17247c478bd9Sstevel@tonic-gate 			(void) close(fd);
17257c478bd9Sstevel@tonic-gate 		return (-1);
17267c478bd9Sstevel@tonic-gate 	}
17277c478bd9Sstevel@tonic-gate 	(void) close(fd);
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, bufsize, "%s[%ld]", psinfo.pr_fname, di.di_target);
17307c478bd9Sstevel@tonic-gate 	return (0);
17317c478bd9Sstevel@tonic-gate }
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate /*
17347c478bd9Sstevel@tonic-gate  * ZIP file header information
17357c478bd9Sstevel@tonic-gate  */
17367c478bd9Sstevel@tonic-gate #define	SIGSIZ		4
17377c478bd9Sstevel@tonic-gate #define	LOCSIG		"PK\003\004"
17387c478bd9Sstevel@tonic-gate #define	LOCHDRSIZ	30
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate #define	CH(b, n)	(((unsigned char *)(b))[n])
17417c478bd9Sstevel@tonic-gate #define	SH(b, n)	(CH(b, n) | (CH(b, n+1) << 8))
17427c478bd9Sstevel@tonic-gate #define	LG(b, n)	(SH(b, n) | (SH(b, n+2) << 16))
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate #define	LOCNAM(b)	(SH(b, 26))	/* filename size */
17457c478bd9Sstevel@tonic-gate #define	LOCEXT(b)	(SH(b, 28))	/* extra field size */
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate #define	XFHSIZ		4		/* header id, data size */
17487c478bd9Sstevel@tonic-gate #define	XFHID(b)	(SH(b, 0))	/* extract field header id */
17497c478bd9Sstevel@tonic-gate #define	XFDATASIZ(b)	(SH(b, 2))	/* extract field data size */
17507c478bd9Sstevel@tonic-gate #define	XFJAVASIG	0xcafe		/* java executables */
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate static int
17537c478bd9Sstevel@tonic-gate zipfile(char *fbuf, int fd)
17547c478bd9Sstevel@tonic-gate {
17557c478bd9Sstevel@tonic-gate 	off_t xoff, xoff_end;
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	if (strncmp(fbuf, LOCSIG, SIGSIZ) != 0)
17587c478bd9Sstevel@tonic-gate 		return (0);
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	xoff = LOCHDRSIZ + LOCNAM(fbuf);
17617c478bd9Sstevel@tonic-gate 	xoff_end = xoff + LOCEXT(fbuf);
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	while (xoff < xoff_end) {
17647c478bd9Sstevel@tonic-gate 		char xfhdr[XFHSIZ];
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 		if (pread(fd, xfhdr, XFHSIZ, xoff) != XFHSIZ)
17677c478bd9Sstevel@tonic-gate 			break;
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 		if (XFHID(xfhdr) == XFJAVASIG) {
17707c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", gettext("java program"));
17717c478bd9Sstevel@tonic-gate 			return (1);
17727c478bd9Sstevel@tonic-gate 		}
17737c478bd9Sstevel@tonic-gate 		xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr);
17747c478bd9Sstevel@tonic-gate 	}
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	/*
17777c478bd9Sstevel@tonic-gate 	 * We could just print "ZIP archive" here.
17787c478bd9Sstevel@tonic-gate 	 *
17797c478bd9Sstevel@tonic-gate 	 * However, customers may be using their own entries in
17807c478bd9Sstevel@tonic-gate 	 * /etc/magic to distinguish one kind of ZIP file from another, so
17817c478bd9Sstevel@tonic-gate 	 * let's defer the printing of "ZIP archive" to there.
17827c478bd9Sstevel@tonic-gate 	 */
17837c478bd9Sstevel@tonic-gate 	return (0);
17847c478bd9Sstevel@tonic-gate }
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate static int
17877c478bd9Sstevel@tonic-gate is_crash_dump(const char *buf, int fd)
17887c478bd9Sstevel@tonic-gate {
17897c478bd9Sstevel@tonic-gate 	/* LINTED: pointer cast may result in improper alignment */
17907c478bd9Sstevel@tonic-gate 	const dumphdr_t *dhp = (const dumphdr_t *)buf;
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	/*
17937c478bd9Sstevel@tonic-gate 	 * The current DUMP_MAGIC string covers Solaris 7 and later releases.
17947c478bd9Sstevel@tonic-gate 	 * The utsname struct is only present in dumphdr_t's with dump_version
17957c478bd9Sstevel@tonic-gate 	 * greater than or equal to 9.
17967c478bd9Sstevel@tonic-gate 	 */
17977c478bd9Sstevel@tonic-gate 	if (dhp->dump_magic == DUMP_MAGIC) {
17987c478bd9Sstevel@tonic-gate 		print_dumphdr(fd, dhp, return_uint32, NATIVE_ISA);
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	} else if (dhp->dump_magic == swap_uint32(DUMP_MAGIC)) {
18017c478bd9Sstevel@tonic-gate 		print_dumphdr(fd, dhp, swap_uint32, OTHER_ISA);
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	} else if (dhp->dump_magic == OLD_DUMP_MAGIC ||
18047c478bd9Sstevel@tonic-gate 	    dhp->dump_magic == swap_uint32(OLD_DUMP_MAGIC)) {
18057c478bd9Sstevel@tonic-gate 		char *isa = (dhp->dump_magic == OLD_DUMP_MAGIC ?
18067c478bd9Sstevel@tonic-gate 		    NATIVE_ISA : OTHER_ISA);
18077c478bd9Sstevel@tonic-gate 		(void) printf(gettext("SunOS 32-bit %s crash dump\n"), isa);
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	} else {
18107c478bd9Sstevel@tonic-gate 		return (0);
18117c478bd9Sstevel@tonic-gate 	}
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 	return (1);
18147c478bd9Sstevel@tonic-gate }
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate static void
18177c478bd9Sstevel@tonic-gate print_dumphdr(const int fd, const dumphdr_t *dhp, uint32_t (*swap)(uint32_t),
18187c478bd9Sstevel@tonic-gate     const char *isa)
18197c478bd9Sstevel@tonic-gate {
18207c478bd9Sstevel@tonic-gate 	dumphdr_t dh;
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	/*
18237c478bd9Sstevel@tonic-gate 	 * A dumphdr_t is bigger than FBSZ, so we have to manually read the
18247c478bd9Sstevel@tonic-gate 	 * rest of it.
18257c478bd9Sstevel@tonic-gate 	 */
18267c478bd9Sstevel@tonic-gate 	if (swap(dhp->dump_version) > 8 && pread(fd, &dh, sizeof (dumphdr_t),
18277c478bd9Sstevel@tonic-gate 	    (off_t)0) == sizeof (dumphdr_t)) {
18287c478bd9Sstevel@tonic-gate 		(void) printf(gettext(
18297c478bd9Sstevel@tonic-gate 		    "%s %s %s %u-bit %s crash dump from '%s'\n"),
18307c478bd9Sstevel@tonic-gate 		    dh.dump_utsname.sysname, dh.dump_utsname.release,
18317c478bd9Sstevel@tonic-gate 		    dh.dump_utsname.version, swap(dh.dump_wordsize), isa,
18327c478bd9Sstevel@tonic-gate 		    dh.dump_utsname.nodename);
18337c478bd9Sstevel@tonic-gate 	} else {
18347c478bd9Sstevel@tonic-gate 		(void) printf(gettext("SunOS %u-bit %s crash dump\n"),
18357c478bd9Sstevel@tonic-gate 		    swap(dhp->dump_wordsize), isa);
18367c478bd9Sstevel@tonic-gate 	}
18377c478bd9Sstevel@tonic-gate }
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate static void
18407c478bd9Sstevel@tonic-gate usage(void)
18417c478bd9Sstevel@tonic-gate {
18427c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
18437c478bd9Sstevel@tonic-gate 		"usage: file [-dh] [-M mfile] [-m mfile] [-f ffile] file ...\n"
18447c478bd9Sstevel@tonic-gate 		"       file [-dh] [-M mfile] [-m mfile] -f ffile\n"
18457c478bd9Sstevel@tonic-gate 		"       file -i [-h] [-f ffile] file ...\n"
18467c478bd9Sstevel@tonic-gate 		"       file -i [-h] -f ffile\n"
18477c478bd9Sstevel@tonic-gate 		"       file -c [-d] [-M mfile] [-m mfile]\n"));
18487c478bd9Sstevel@tonic-gate 	exit(2);
18497c478bd9Sstevel@tonic-gate }
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate static uint32_t
18527c478bd9Sstevel@tonic-gate swap_uint32(uint32_t in)
18537c478bd9Sstevel@tonic-gate {
18547c478bd9Sstevel@tonic-gate 	uint32_t out;
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 	out = (in & 0x000000ff) << 24;
18577c478bd9Sstevel@tonic-gate 	out |= (in & 0x0000ff00) << 8; /* >> 8 << 16 */
18587c478bd9Sstevel@tonic-gate 	out |= (in & 0x00ff0000) >> 8; /* >> 16 << 8 */
18597c478bd9Sstevel@tonic-gate 	out |= (in & 0xff000000) >> 24;
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 	return (out);
18627c478bd9Sstevel@tonic-gate }
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate static uint32_t
18657c478bd9Sstevel@tonic-gate return_uint32(uint32_t in)
18667c478bd9Sstevel@tonic-gate {
18677c478bd9Sstevel@tonic-gate 	return (in);
18687c478bd9Sstevel@tonic-gate }
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate /*
18717c478bd9Sstevel@tonic-gate  * Check if str is in the string list str_list.
18727c478bd9Sstevel@tonic-gate  */
18737c478bd9Sstevel@tonic-gate static int
18747c478bd9Sstevel@tonic-gate is_in_list(char *str_list[], char *str)
18757c478bd9Sstevel@tonic-gate {
18767c478bd9Sstevel@tonic-gate 	int i;
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	/*
18797c478bd9Sstevel@tonic-gate 	 * Only need to compare the strlen(str_list[i]) bytes.
18807c478bd9Sstevel@tonic-gate 	 * That way .stab will match on .stab* sections, and
18817c478bd9Sstevel@tonic-gate 	 * .debug will match on .debug* sections.
18827c478bd9Sstevel@tonic-gate 	 */
18837c478bd9Sstevel@tonic-gate 	for (i = 0; str_list[i] != NULL; i++) {
18847c478bd9Sstevel@tonic-gate 		if (strncmp(str_list[i], str, strlen(str_list[i])) == 0) {
18857c478bd9Sstevel@tonic-gate 			return (1);
18867c478bd9Sstevel@tonic-gate 		}
18877c478bd9Sstevel@tonic-gate 	}
18887c478bd9Sstevel@tonic-gate 	return (0);
18897c478bd9Sstevel@tonic-gate }
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate /*
18927c478bd9Sstevel@tonic-gate  * default_magic -
18937c478bd9Sstevel@tonic-gate  *	allocate space for and create the default magic file
18947c478bd9Sstevel@tonic-gate  *	name string.
18957c478bd9Sstevel@tonic-gate  */
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate static void
18987c478bd9Sstevel@tonic-gate default_magic(void)
18997c478bd9Sstevel@tonic-gate {
19007c478bd9Sstevel@tonic-gate 	const char *msg_locale = setlocale(LC_MESSAGES, NULL);
19017c478bd9Sstevel@tonic-gate 	struct stat	statbuf;
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	if ((dfile = (char *)malloc(strlen(msg_locale) + 35)) == NULL) {
19047c478bd9Sstevel@tonic-gate 		perror("file");
19057c478bd9Sstevel@tonic-gate 		exit(2);
19067c478bd9Sstevel@tonic-gate 	}
19077c478bd9Sstevel@tonic-gate 	(void) snprintf(dfile, strlen(msg_locale) + 35,
19087c478bd9Sstevel@tonic-gate 	    "/usr/lib/locale/%s/LC_MESSAGES/magic", msg_locale);
19097c478bd9Sstevel@tonic-gate 	if (stat(dfile, &statbuf) != 0) {
19107c478bd9Sstevel@tonic-gate 		(void) strcpy(dfile, "/etc/magic");
19117c478bd9Sstevel@tonic-gate 	}
19127c478bd9Sstevel@tonic-gate }
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate /*
19157c478bd9Sstevel@tonic-gate  * add_to_mlist -
19167c478bd9Sstevel@tonic-gate  *	Add the given magic_file filename string to the list of magic
19177c478bd9Sstevel@tonic-gate  *	files (mlist).  This list of files will later be examined, and
19187c478bd9Sstevel@tonic-gate  *	each magic file's entries will be added in order to
19197c478bd9Sstevel@tonic-gate  *	the mtab table.
19207c478bd9Sstevel@tonic-gate  *
19217c478bd9Sstevel@tonic-gate  *	The first flag is set to 1 to add to the first list, mlist1.
19227c478bd9Sstevel@tonic-gate  *	The first flag is set to 0 to add to the second list, mlist2.
19237c478bd9Sstevel@tonic-gate  */
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate static void
19267c478bd9Sstevel@tonic-gate add_to_mlist(char *magic_file, int first)
19277c478bd9Sstevel@tonic-gate {
19287c478bd9Sstevel@tonic-gate 	char	**mlist;	/* ordered list of magic files */
19297c478bd9Sstevel@tonic-gate 	size_t	mlist_sz;	/* number of pointers allocated  for mlist */
19307c478bd9Sstevel@tonic-gate 	char	**mlistp;	/* next entry in mlist */
19317c478bd9Sstevel@tonic-gate 	size_t mlistp_off;
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	if (first) {
19347c478bd9Sstevel@tonic-gate 		mlist = mlist1;
19357c478bd9Sstevel@tonic-gate 		mlist_sz = mlist1_sz;
19367c478bd9Sstevel@tonic-gate 		mlistp = mlist1p;
19377c478bd9Sstevel@tonic-gate 	} else {
19387c478bd9Sstevel@tonic-gate 		mlist = mlist2;
19397c478bd9Sstevel@tonic-gate 		mlist_sz = mlist2_sz;
19407c478bd9Sstevel@tonic-gate 		mlistp = mlist2p;
19417c478bd9Sstevel@tonic-gate 	}
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	if (mlist == NULL) {	/* initial mlist allocation */
19447c478bd9Sstevel@tonic-gate 		if ((mlist = (char **)calloc(MLIST_SZ, sizeof (char *)))
19457c478bd9Sstevel@tonic-gate 		    == NULL) {
19467c478bd9Sstevel@tonic-gate 			perror("file");
19477c478bd9Sstevel@tonic-gate 			exit(2);
19487c478bd9Sstevel@tonic-gate 		}
19497c478bd9Sstevel@tonic-gate 		mlist_sz = MLIST_SZ;
19507c478bd9Sstevel@tonic-gate 		mlistp = mlist;
19517c478bd9Sstevel@tonic-gate 	}
19527c478bd9Sstevel@tonic-gate 	if ((mlistp - mlist) >= mlist_sz) {
19537c478bd9Sstevel@tonic-gate 		mlistp_off = mlistp - mlist;
19547c478bd9Sstevel@tonic-gate 		mlist_sz *= 2;
19557c478bd9Sstevel@tonic-gate 		if ((mlist = (char **)realloc(mlist,
19567c478bd9Sstevel@tonic-gate 		    mlist_sz * sizeof (char *))) == NULL) {
19577c478bd9Sstevel@tonic-gate 			perror("file");
19587c478bd9Sstevel@tonic-gate 			exit(2);
19597c478bd9Sstevel@tonic-gate 		}
19607c478bd9Sstevel@tonic-gate 		mlistp = mlist + mlistp_off;
19617c478bd9Sstevel@tonic-gate 	}
19627c478bd9Sstevel@tonic-gate 	/*
19637c478bd9Sstevel@tonic-gate 	 * now allocate memory for and copy the
19647c478bd9Sstevel@tonic-gate 	 * magic file name string
19657c478bd9Sstevel@tonic-gate 	 */
19667c478bd9Sstevel@tonic-gate 	if ((*mlistp = malloc(strlen(magic_file) + 1)) == NULL) {
19677c478bd9Sstevel@tonic-gate 		perror("file");
19687c478bd9Sstevel@tonic-gate 		exit(2);
19697c478bd9Sstevel@tonic-gate 	}
19707c478bd9Sstevel@tonic-gate 	(void) strlcpy(*mlistp, magic_file, strlen(magic_file) + 1);
19717c478bd9Sstevel@tonic-gate 	mlistp++;
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	if (first) {
19747c478bd9Sstevel@tonic-gate 		mlist1 = mlist;
19757c478bd9Sstevel@tonic-gate 		mlist1_sz = mlist_sz;
19767c478bd9Sstevel@tonic-gate 		mlist1p = mlistp;
19777c478bd9Sstevel@tonic-gate 	} else {
19787c478bd9Sstevel@tonic-gate 		mlist2 = mlist;
19797c478bd9Sstevel@tonic-gate 		mlist2_sz = mlist_sz;
19807c478bd9Sstevel@tonic-gate 		mlist2p = mlistp;
19817c478bd9Sstevel@tonic-gate 	}
19827c478bd9Sstevel@tonic-gate }
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate static void
19857c478bd9Sstevel@tonic-gate fd_cleanup(void)
19867c478bd9Sstevel@tonic-gate {
19877c478bd9Sstevel@tonic-gate 	if (ifd != -1) {
19887c478bd9Sstevel@tonic-gate 		(void) close(ifd);
19897c478bd9Sstevel@tonic-gate 		ifd = -1;
19907c478bd9Sstevel@tonic-gate 	}
19917c478bd9Sstevel@tonic-gate 	if (elffd != -1) {
19927c478bd9Sstevel@tonic-gate 		(void) close(elffd);
19937c478bd9Sstevel@tonic-gate 		elffd = -1;
19947c478bd9Sstevel@tonic-gate 	}
19957c478bd9Sstevel@tonic-gate }
1996