xref: /onnv-gate/usr/src/cmd/file/file.c (revision 0)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
27*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
31*0Sstevel@tonic-gate  * Use is subject to license terms.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #define	_LARGEFILE64_SOURCE
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include <ctype.h>
39*0Sstevel@tonic-gate #include <unistd.h>
40*0Sstevel@tonic-gate #include <fcntl.h>
41*0Sstevel@tonic-gate #include <signal.h>
42*0Sstevel@tonic-gate #include <stdio.h>
43*0Sstevel@tonic-gate #include <libelf.h>
44*0Sstevel@tonic-gate #include <stdlib.h>
45*0Sstevel@tonic-gate #include <limits.h>
46*0Sstevel@tonic-gate #include <locale.h>
47*0Sstevel@tonic-gate #include <wctype.h>
48*0Sstevel@tonic-gate #include <string.h>
49*0Sstevel@tonic-gate #include <errno.h>
50*0Sstevel@tonic-gate #include <door.h>
51*0Sstevel@tonic-gate #include <sys/param.h>
52*0Sstevel@tonic-gate #include <sys/types.h>
53*0Sstevel@tonic-gate #include <sys/mkdev.h>
54*0Sstevel@tonic-gate #include <sys/stat.h>
55*0Sstevel@tonic-gate #include <sys/elf.h>
56*0Sstevel@tonic-gate #include <sys/elf_M32.h>
57*0Sstevel@tonic-gate #include <sys/elf_SPARC.h>
58*0Sstevel@tonic-gate #include <procfs.h>
59*0Sstevel@tonic-gate #include <sys/core.h>
60*0Sstevel@tonic-gate #include <sys/dumphdr.h>
61*0Sstevel@tonic-gate #include <netinet/in.h>
62*0Sstevel@tonic-gate #include <gelf.h>
63*0Sstevel@tonic-gate #include <elfcap.h>
64*0Sstevel@tonic-gate #include "file.h"
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate typedef Elf64_Nhdr	GElf_Nhdr;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  *	Misc
70*0Sstevel@tonic-gate  */
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate #define	FBSZ		512
73*0Sstevel@tonic-gate #define	MLIST_SZ	12
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /*
76*0Sstevel@tonic-gate  * The 0x8FCA0102 magic string was used in crash dumps generated by releases
77*0Sstevel@tonic-gate  * prior to Solaris 7.
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate #define	OLD_DUMP_MAGIC	0x8FCA0102
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate #if defined(__sparc)
82*0Sstevel@tonic-gate #define	NATIVE_ISA	"SPARC"
83*0Sstevel@tonic-gate #define	OTHER_ISA	"Intel"
84*0Sstevel@tonic-gate #else
85*0Sstevel@tonic-gate #define	NATIVE_ISA	"Intel"
86*0Sstevel@tonic-gate #define	OTHER_ISA	"SPARC"
87*0Sstevel@tonic-gate #endif
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate /* Assembly language comment char */
90*0Sstevel@tonic-gate #ifdef pdp11
91*0Sstevel@tonic-gate #define	ASCOMCHAR '/'
92*0Sstevel@tonic-gate #else
93*0Sstevel@tonic-gate #define	ASCOMCHAR '!'
94*0Sstevel@tonic-gate #endif
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate #pragma	align	16(fbuf)
97*0Sstevel@tonic-gate static char	fbuf[FBSZ];
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate /*
100*0Sstevel@tonic-gate  * Magic file variables
101*0Sstevel@tonic-gate  */
102*0Sstevel@tonic-gate static intmax_t maxmagicoffset;
103*0Sstevel@tonic-gate static intmax_t tmpmax;
104*0Sstevel@tonic-gate static char	*magicbuf;
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate static char	*dfile;
107*0Sstevel@tonic-gate static char	*troff[] = {	/* new troff intermediate lang */
108*0Sstevel@tonic-gate 		"x", "T", "res", "init", "font", "202", "V0", "p1", 0};
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate static char	*fort[] = {			/* FORTRAN */
111*0Sstevel@tonic-gate 		"function", "subroutine", "common", "dimension", "block",
112*0Sstevel@tonic-gate 		"integer", "real", "data", "double",
113*0Sstevel@tonic-gate 		"FUNCTION", "SUBROUTINE", "COMMON", "DIMENSION", "BLOCK",
114*0Sstevel@tonic-gate 		"INTEGER", "REAL", "DATA", "DOUBLE", 0};
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate static char	*asc[] = {		/* Assembler Commands */
117*0Sstevel@tonic-gate 		"sys", "mov", "tst", "clr", "jmp", "cmp", "set", "inc",
118*0Sstevel@tonic-gate 		"dec", 0};
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate static char	*c[] = {			/* C Language */
121*0Sstevel@tonic-gate 		"int", "char", "float", "double", "short", "long", "unsigned",
122*0Sstevel@tonic-gate 		"register", "static", "struct", "extern", 0};
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate static char	*as[] = {	/* Assembler Pseudo Ops, prepended with '.' */
125*0Sstevel@tonic-gate 		"globl", "global", "ident", "file", "byte", "even",
126*0Sstevel@tonic-gate 		"text", "data", "bss", "comm", 0};
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate /*
129*0Sstevel@tonic-gate  * The line and debug section names are used by the strip command.
130*0Sstevel@tonic-gate  * Any changes in the strip implementation need to be reflected here.
131*0Sstevel@tonic-gate  */
132*0Sstevel@tonic-gate static char	*debug_sections[] = { /* Debug sections in a ELF file */
133*0Sstevel@tonic-gate 		".debug", ".stab", ".dwarf", ".line", NULL};
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate /* start for MB env */
137*0Sstevel@tonic-gate static wchar_t wchar;
138*0Sstevel@tonic-gate static int	length;
139*0Sstevel@tonic-gate static int	IS_ascii;
140*0Sstevel@tonic-gate static int	Max;
141*0Sstevel@tonic-gate /* end for MB env */
142*0Sstevel@tonic-gate static int	i;	/* global index into first 'fbsz' bytes of file */
143*0Sstevel@tonic-gate static int	fbsz;
144*0Sstevel@tonic-gate static int	ifd = -1;
145*0Sstevel@tonic-gate static int	elffd = -1;
146*0Sstevel@tonic-gate static int	tret;
147*0Sstevel@tonic-gate static int	hflg;
148*0Sstevel@tonic-gate static int	dflg;
149*0Sstevel@tonic-gate static int	mflg;
150*0Sstevel@tonic-gate static int	M_flg;
151*0Sstevel@tonic-gate static int	iflg;
152*0Sstevel@tonic-gate static struct stat64	mbuf;
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate static char	**mlist1;	/* 1st ordered list of magic files */
155*0Sstevel@tonic-gate static char	**mlist2;	/* 2nd ordered list of magic files */
156*0Sstevel@tonic-gate static size_t	mlist1_sz;	/* number of ptrs allocated for mlist1 */
157*0Sstevel@tonic-gate static size_t	mlist2_sz;	/* number of ptrs allocated for mlist2 */
158*0Sstevel@tonic-gate static char	**mlist1p;	/* next entry in mlist1 */
159*0Sstevel@tonic-gate static char	**mlist2p;	/* next entry in mlist2 */
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate static ssize_t	mread;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate static void is_stripped(Elf *elf);
164*0Sstevel@tonic-gate static Elf *is_elf_file(int elffd);
165*0Sstevel@tonic-gate static void ar_coff_or_aout(int ifd);
166*0Sstevel@tonic-gate static int type(char *file);
167*0Sstevel@tonic-gate static int def_position_tests(void);
168*0Sstevel@tonic-gate static void def_context_tests(void);
169*0Sstevel@tonic-gate static int troffint(char *bp, int n);
170*0Sstevel@tonic-gate static int lookup(char **tab);
171*0Sstevel@tonic-gate static int ccom(void);
172*0Sstevel@tonic-gate static int ascom(void);
173*0Sstevel@tonic-gate static int sccs(void);
174*0Sstevel@tonic-gate static int english(char *bp, int n);
175*0Sstevel@tonic-gate static int old_core(Elf *elf, GElf_Ehdr *ehdr, int format);
176*0Sstevel@tonic-gate static int core(Elf *elf, GElf_Ehdr *ehdr, int format);
177*0Sstevel@tonic-gate static int shellscript(char buf[], struct stat64 *sb);
178*0Sstevel@tonic-gate static int elf_check(Elf *elf);
179*0Sstevel@tonic-gate static int get_door_target(char *, char *, size_t);
180*0Sstevel@tonic-gate static int zipfile(char *, int);
181*0Sstevel@tonic-gate static int is_crash_dump(const char *, int);
182*0Sstevel@tonic-gate static void print_dumphdr(const int, const dumphdr_t *, uint32_t (*)(uint32_t),
183*0Sstevel@tonic-gate     const char *);
184*0Sstevel@tonic-gate static uint32_t swap_uint32(uint32_t);
185*0Sstevel@tonic-gate static uint32_t return_uint32(uint32_t);
186*0Sstevel@tonic-gate static int is_in_list(char *[], char *);
187*0Sstevel@tonic-gate static void usage(void);
188*0Sstevel@tonic-gate static void default_magic(void);
189*0Sstevel@tonic-gate static void add_to_mlist(char *, int);
190*0Sstevel@tonic-gate static void fd_cleanup(void);
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate #ifdef XPG4
193*0Sstevel@tonic-gate 	/* SUSv3 requires a single <space> after the colon */
194*0Sstevel@tonic-gate #define	prf(x)	(void) printf("%s: ", x);
195*0Sstevel@tonic-gate #else	/* !XPG4 */
196*0Sstevel@tonic-gate #define	prf(x)	(void) printf("%s:%s", x, (int)strlen(x) > 6 ? "\t" : "\t\t");
197*0Sstevel@tonic-gate #endif	/* XPG4 */
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate int
200*0Sstevel@tonic-gate main(int argc, char **argv)
201*0Sstevel@tonic-gate {
202*0Sstevel@tonic-gate 	char	*p;
203*0Sstevel@tonic-gate 	int	ch;
204*0Sstevel@tonic-gate 	FILE	*fl;
205*0Sstevel@tonic-gate 	int	cflg = 0;
206*0Sstevel@tonic-gate 	int	eflg = 0;
207*0Sstevel@tonic-gate 	int	fflg = 0;
208*0Sstevel@tonic-gate 	char	*ap = NULL;
209*0Sstevel@tonic-gate 	int	pathlen;
210*0Sstevel@tonic-gate 	char	**filep;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
213*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
214*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
215*0Sstevel@tonic-gate #endif
216*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "M:cdf:him:")) != EOF) {
219*0Sstevel@tonic-gate 		switch (ch) {
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 		case 'M':
222*0Sstevel@tonic-gate 			add_to_mlist(optarg, !dflg);
223*0Sstevel@tonic-gate 			M_flg++;
224*0Sstevel@tonic-gate 			break;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 		case 'c':
227*0Sstevel@tonic-gate 			cflg++;
228*0Sstevel@tonic-gate 			break;
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 		case 'd':
231*0Sstevel@tonic-gate 			if (!dflg) {
232*0Sstevel@tonic-gate 				default_magic();
233*0Sstevel@tonic-gate 				add_to_mlist(dfile, 0);
234*0Sstevel@tonic-gate 				dflg++;
235*0Sstevel@tonic-gate 			}
236*0Sstevel@tonic-gate 			break;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 		case 'f':
239*0Sstevel@tonic-gate 			fflg++;
240*0Sstevel@tonic-gate 			if ((fl = fopen(optarg, "r")) == NULL) {
241*0Sstevel@tonic-gate 				(void) fprintf(stderr,
242*0Sstevel@tonic-gate 					gettext("cannot open %s\n"), optarg);
243*0Sstevel@tonic-gate 				usage();
244*0Sstevel@tonic-gate 			}
245*0Sstevel@tonic-gate 			pathlen = pathconf("/", _PC_PATH_MAX);
246*0Sstevel@tonic-gate 			if (pathlen == -1) {
247*0Sstevel@tonic-gate 				(void) fprintf(stderr,
248*0Sstevel@tonic-gate 				    gettext("pathconf: cannot determine "
249*0Sstevel@tonic-gate 					"maximum path length\n"));
250*0Sstevel@tonic-gate 				exit(1);
251*0Sstevel@tonic-gate 			}
252*0Sstevel@tonic-gate 			pathlen += 2; /* for null and newline in fgets */
253*0Sstevel@tonic-gate 			ap = malloc(pathlen * sizeof (char));
254*0Sstevel@tonic-gate 			if (ap == NULL) {
255*0Sstevel@tonic-gate 				perror("malloc");
256*0Sstevel@tonic-gate 				exit(1);
257*0Sstevel@tonic-gate 			}
258*0Sstevel@tonic-gate 			break;
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 		case 'h':
261*0Sstevel@tonic-gate 			hflg++;
262*0Sstevel@tonic-gate 			break;
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 		case 'i':
265*0Sstevel@tonic-gate 			iflg++;
266*0Sstevel@tonic-gate 			break;
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 		case 'm':
269*0Sstevel@tonic-gate 			add_to_mlist(optarg, !dflg);
270*0Sstevel@tonic-gate 			mflg++;
271*0Sstevel@tonic-gate 			break;
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 		case '?':
274*0Sstevel@tonic-gate 			eflg++;
275*0Sstevel@tonic-gate 			break;
276*0Sstevel@tonic-gate 		}
277*0Sstevel@tonic-gate 	}
278*0Sstevel@tonic-gate 	if (!cflg && !fflg && (eflg || optind == argc))
279*0Sstevel@tonic-gate 		usage();
280*0Sstevel@tonic-gate 	if (iflg && (dflg || mflg || M_flg)) {
281*0Sstevel@tonic-gate 		usage();
282*0Sstevel@tonic-gate 	}
283*0Sstevel@tonic-gate 	if (iflg && cflg) {
284*0Sstevel@tonic-gate 		usage();
285*0Sstevel@tonic-gate 	}
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	if (!dflg && !mflg && !M_flg && !iflg) {
288*0Sstevel@tonic-gate 	/* no -d, -m, nor -M option; also -i option doesn't need magic  */
289*0Sstevel@tonic-gate 		default_magic();
290*0Sstevel@tonic-gate 		if (f_mkmtab(dfile, cflg, 0) == -1) {
291*0Sstevel@tonic-gate 			exit(2);
292*0Sstevel@tonic-gate 		}
293*0Sstevel@tonic-gate 	}
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	else if (mflg && !M_flg && !dflg) {
296*0Sstevel@tonic-gate 	/* -m specified without -d nor -M */
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate #ifdef XPG4	/* For SUSv3 only */
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 		/*
301*0Sstevel@tonic-gate 		 * The default position-dependent magic file tests
302*0Sstevel@tonic-gate 		 * in /etc/magic will follow all the -m magic tests.
303*0Sstevel@tonic-gate 		 */
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 		for (filep = mlist1; filep < mlist1p; filep++) {
306*0Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 1) == -1) {
307*0Sstevel@tonic-gate 				exit(2);
308*0Sstevel@tonic-gate 			}
309*0Sstevel@tonic-gate 		}
310*0Sstevel@tonic-gate 		default_magic();
311*0Sstevel@tonic-gate 		if (f_mkmtab(dfile, cflg, 0) == -1) {
312*0Sstevel@tonic-gate 			exit(2);
313*0Sstevel@tonic-gate 		}
314*0Sstevel@tonic-gate #else	/* !XPG4 */
315*0Sstevel@tonic-gate 		/*
316*0Sstevel@tonic-gate 		 * Retain Solaris file behavior for -m before SUSv3,
317*0Sstevel@tonic-gate 		 * when the new -d and -M options are not specified.
318*0Sstevel@tonic-gate 		 * Use the -m file specified in place of the default
319*0Sstevel@tonic-gate 		 * /etc/magic file.  Solaris file will
320*0Sstevel@tonic-gate 		 * now allow more than one magic file to be specified
321*0Sstevel@tonic-gate 		 * with multiple -m options, for consistency with
322*0Sstevel@tonic-gate 		 * other behavior.
323*0Sstevel@tonic-gate 		 *
324*0Sstevel@tonic-gate 		 * Put the magic table(s) specified by -m into
325*0Sstevel@tonic-gate 		 * the second magic table instead of the first
326*0Sstevel@tonic-gate 		 * (as indicated by the last argument to f_mkmtab()),
327*0Sstevel@tonic-gate 		 * since they replace the /etc/magic tests and
328*0Sstevel@tonic-gate 		 * must be executed alongside the default
329*0Sstevel@tonic-gate 		 * position-sensitive tests.
330*0Sstevel@tonic-gate 		 */
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 		for (filep = mlist1; filep < mlist1p; filep++) {
333*0Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 0) == -1) {
334*0Sstevel@tonic-gate 				exit(2);
335*0Sstevel@tonic-gate 			}
336*0Sstevel@tonic-gate 		}
337*0Sstevel@tonic-gate #endif /* XPG4 */
338*0Sstevel@tonic-gate 	} else {
339*0Sstevel@tonic-gate 		/*
340*0Sstevel@tonic-gate 		 * For any other combination of -d, -m, and -M,
341*0Sstevel@tonic-gate 		 * use the magic files in command-line order.
342*0Sstevel@tonic-gate 		 * Store the entries from the two separate lists of magic
343*0Sstevel@tonic-gate 		 * files, if any, into two separate magic file tables.
344*0Sstevel@tonic-gate 		 * mlist1: magic tests executed before default magic tests
345*0Sstevel@tonic-gate 		 * mlist2: default magic tests and after
346*0Sstevel@tonic-gate 		 */
347*0Sstevel@tonic-gate 		for (filep = mlist1; filep && (filep < mlist1p); filep++) {
348*0Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 1) == -1) {
349*0Sstevel@tonic-gate 				exit(2);
350*0Sstevel@tonic-gate 			}
351*0Sstevel@tonic-gate 		}
352*0Sstevel@tonic-gate 		for (filep = mlist2; filep && (filep < mlist2p); filep++) {
353*0Sstevel@tonic-gate 			if (f_mkmtab(*filep, cflg, 0) == -1) {
354*0Sstevel@tonic-gate 				exit(2);
355*0Sstevel@tonic-gate 			}
356*0Sstevel@tonic-gate 		}
357*0Sstevel@tonic-gate 	}
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	/* Initialize the magic file variables; check both magic tables */
360*0Sstevel@tonic-gate 	tmpmax = f_getmaxoffset(1);
361*0Sstevel@tonic-gate 	maxmagicoffset = f_getmaxoffset(0);
362*0Sstevel@tonic-gate 	if (maxmagicoffset < tmpmax) {
363*0Sstevel@tonic-gate 		maxmagicoffset = tmpmax;
364*0Sstevel@tonic-gate 	}
365*0Sstevel@tonic-gate 	if (maxmagicoffset < (intmax_t)FBSZ)
366*0Sstevel@tonic-gate 		maxmagicoffset = (intmax_t)FBSZ;
367*0Sstevel@tonic-gate 	if ((magicbuf = (char *)malloc(maxmagicoffset)) == NULL) {
368*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("malloc failed\n"));
369*0Sstevel@tonic-gate 		exit(2);
370*0Sstevel@tonic-gate 	}
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	if (cflg) {
373*0Sstevel@tonic-gate 		f_prtmtab();
374*0Sstevel@tonic-gate 		if (ferror(stdout) != 0) {
375*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("file: error writing to "
376*0Sstevel@tonic-gate 			    "stdout\n"));
377*0Sstevel@tonic-gate 			exit(1);
378*0Sstevel@tonic-gate 		}
379*0Sstevel@tonic-gate 		if (fclose(stdout) != 0) {
380*0Sstevel@tonic-gate 			perror(gettext("file: fclose failed"));
381*0Sstevel@tonic-gate 			exit(1);
382*0Sstevel@tonic-gate 		}
383*0Sstevel@tonic-gate 		exit(0);
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate 	for (; fflg || optind < argc; optind += !fflg) {
386*0Sstevel@tonic-gate 		register int	l;
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 		if (fflg) {
389*0Sstevel@tonic-gate 			if ((p = fgets(ap, pathlen, fl)) == NULL) {
390*0Sstevel@tonic-gate 				fflg = 0;
391*0Sstevel@tonic-gate 				optind--;
392*0Sstevel@tonic-gate 				continue;
393*0Sstevel@tonic-gate 			}
394*0Sstevel@tonic-gate 			l = strlen(p);
395*0Sstevel@tonic-gate 			if (l > 0)
396*0Sstevel@tonic-gate 				p[l - 1] = '\0';
397*0Sstevel@tonic-gate 		} else
398*0Sstevel@tonic-gate 			p = argv[optind];
399*0Sstevel@tonic-gate 		prf(p);				/* print "file_name:<tab>" */
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 		if (type(p))
402*0Sstevel@tonic-gate 			tret = 1;
403*0Sstevel@tonic-gate 	}
404*0Sstevel@tonic-gate 	if (ap != NULL)
405*0Sstevel@tonic-gate 		free(ap);
406*0Sstevel@tonic-gate 	if (tret != 0) {
407*0Sstevel@tonic-gate 		exit(tret);
408*0Sstevel@tonic-gate 	}
409*0Sstevel@tonic-gate 	if (ferror(stdout) != 0) {
410*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("file: error writing to "
411*0Sstevel@tonic-gate 		    "stdout\n"));
412*0Sstevel@tonic-gate 		exit(1);
413*0Sstevel@tonic-gate 	}
414*0Sstevel@tonic-gate 	if (fclose(stdout) != 0) {
415*0Sstevel@tonic-gate 		perror(gettext("file: fclose failed"));
416*0Sstevel@tonic-gate 		exit(1);
417*0Sstevel@tonic-gate 	}
418*0Sstevel@tonic-gate 	return (0);
419*0Sstevel@tonic-gate }
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate static int
422*0Sstevel@tonic-gate type(char *file)
423*0Sstevel@tonic-gate {
424*0Sstevel@tonic-gate 	int	cc;
425*0Sstevel@tonic-gate 	char	buf[BUFSIZ];
426*0Sstevel@tonic-gate 	int	(*statf)() = hflg ? lstat64 : stat64;
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate 	i = 0;		/* reset index to beginning of file */
429*0Sstevel@tonic-gate 	ifd = -1;
430*0Sstevel@tonic-gate 	if ((*statf)(file, &mbuf) < 0) {
431*0Sstevel@tonic-gate 		if (statf == lstat64 || lstat64(file, &mbuf) < 0) {
432*0Sstevel@tonic-gate 			(void) printf(gettext("cannot open: %s\n"),
433*0Sstevel@tonic-gate 			    strerror(errno));
434*0Sstevel@tonic-gate 			return (0);		/* POSIX.2 */
435*0Sstevel@tonic-gate 		}
436*0Sstevel@tonic-gate 	}
437*0Sstevel@tonic-gate 	switch (mbuf.st_mode & S_IFMT) {
438*0Sstevel@tonic-gate 	case S_IFREG:
439*0Sstevel@tonic-gate 		if (iflg) {
440*0Sstevel@tonic-gate 			(void) printf(gettext("regular file\n"));
441*0Sstevel@tonic-gate 			return (0);
442*0Sstevel@tonic-gate 		}
443*0Sstevel@tonic-gate 		break;
444*0Sstevel@tonic-gate 	case S_IFCHR:
445*0Sstevel@tonic-gate 		(void) printf(gettext("character"));
446*0Sstevel@tonic-gate 		goto spcl;
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	case S_IFDIR:
449*0Sstevel@tonic-gate 		(void) printf(gettext("directory\n"));
450*0Sstevel@tonic-gate 		return (0);
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	case S_IFIFO:
453*0Sstevel@tonic-gate 		(void) printf(gettext("fifo\n"));
454*0Sstevel@tonic-gate 		return (0);
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	case S_IFLNK:
457*0Sstevel@tonic-gate 		if ((cc = readlink(file, buf, BUFSIZ)) < 0) {
458*0Sstevel@tonic-gate 			(void) printf(gettext("readlink error: %s\n"),
459*0Sstevel@tonic-gate 				strerror(errno));
460*0Sstevel@tonic-gate 			return (1);
461*0Sstevel@tonic-gate 		}
462*0Sstevel@tonic-gate 		buf[cc] = '\0';
463*0Sstevel@tonic-gate 		(void) printf(gettext("symbolic link to %s\n"), buf);
464*0Sstevel@tonic-gate 		return (0);
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 	case S_IFBLK:
467*0Sstevel@tonic-gate 		(void) printf(gettext("block"));
468*0Sstevel@tonic-gate 					/* major and minor, see sys/mkdev.h */
469*0Sstevel@tonic-gate spcl:
470*0Sstevel@tonic-gate 		(void) printf(gettext(" special (%d/%d)\n"),
471*0Sstevel@tonic-gate 		    major(mbuf.st_rdev), minor(mbuf.st_rdev));
472*0Sstevel@tonic-gate 		return (0);
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate 	case S_IFSOCK:
475*0Sstevel@tonic-gate 		(void) printf("socket\n");
476*0Sstevel@tonic-gate 		/* FIXME, should open and try to getsockname. */
477*0Sstevel@tonic-gate 		return (0);
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 	case S_IFDOOR:
480*0Sstevel@tonic-gate 		if (get_door_target(file, buf, sizeof (buf)) == 0)
481*0Sstevel@tonic-gate 			(void) printf(gettext("door to %s\n"), buf);
482*0Sstevel@tonic-gate 		else
483*0Sstevel@tonic-gate 			(void) printf(gettext("door\n"));
484*0Sstevel@tonic-gate 		return (0);
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 	}
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
489*0Sstevel@tonic-gate 		(void) printf(gettext("libelf is out of date\n"));
490*0Sstevel@tonic-gate 		return (1);
491*0Sstevel@tonic-gate 	}
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 	ifd = open64(file, O_RDONLY);
494*0Sstevel@tonic-gate 	if (ifd < 0) {
495*0Sstevel@tonic-gate 		(void) printf(gettext("cannot open: %s\n"), strerror(errno));
496*0Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
497*0Sstevel@tonic-gate 	}
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 	/* need another fd for elf, since we might want to read the file too */
500*0Sstevel@tonic-gate 	elffd = open64(file, O_RDONLY);
501*0Sstevel@tonic-gate 	if (elffd < 0) {
502*0Sstevel@tonic-gate 		(void) printf(gettext("cannot open: %s\n"), strerror(errno));
503*0Sstevel@tonic-gate 		(void) close(ifd);
504*0Sstevel@tonic-gate 		ifd = -1;
505*0Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
506*0Sstevel@tonic-gate 	}
507*0Sstevel@tonic-gate 	if ((fbsz = read(ifd, fbuf, FBSZ)) == -1) {
508*0Sstevel@tonic-gate 		(void) printf(gettext("cannot read: %s\n"), strerror(errno));
509*0Sstevel@tonic-gate 		(void) close(ifd);
510*0Sstevel@tonic-gate 		ifd = -1;
511*0Sstevel@tonic-gate 		return (0);			/* POSIX.2 */
512*0Sstevel@tonic-gate 	}
513*0Sstevel@tonic-gate 	if (fbsz == 0) {
514*0Sstevel@tonic-gate 		(void) printf(gettext("empty file\n"));
515*0Sstevel@tonic-gate 		fd_cleanup();
516*0Sstevel@tonic-gate 		return (0);
517*0Sstevel@tonic-gate 	}
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate 	/*
520*0Sstevel@tonic-gate 	 * First try user-specified position-dependent magic tests, if any,
521*0Sstevel@tonic-gate 	 * which need to execute before the default tests.
522*0Sstevel@tonic-gate 	 */
523*0Sstevel@tonic-gate 	if ((mread = pread(ifd, (void*)magicbuf, (size_t)maxmagicoffset,
524*0Sstevel@tonic-gate 			(off_t)0)) == -1) {
525*0Sstevel@tonic-gate 		(void) printf(gettext("cannot read: %s\n"), strerror(errno));
526*0Sstevel@tonic-gate 		fd_cleanup();
527*0Sstevel@tonic-gate 		return (0);
528*0Sstevel@tonic-gate 	}
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate 	/*
531*0Sstevel@tonic-gate 	 * ChecK against Magic Table entries.
532*0Sstevel@tonic-gate 	 * Check first magic table for magic tests to be applied
533*0Sstevel@tonic-gate 	 * before default tests.
534*0Sstevel@tonic-gate 	 * If no default tests are to be applied, all magic tests
535*0Sstevel@tonic-gate 	 * should occur in this magic table.
536*0Sstevel@tonic-gate 	 */
537*0Sstevel@tonic-gate 	switch (f_ckmtab(magicbuf, mread, 1)) {
538*0Sstevel@tonic-gate 		case -1:	/* Error */
539*0Sstevel@tonic-gate 			exit(2);
540*0Sstevel@tonic-gate 			break;
541*0Sstevel@tonic-gate 		case 0:		/* Not magic */
542*0Sstevel@tonic-gate 			break;
543*0Sstevel@tonic-gate 		default:	/* Switch is magic index */
544*0Sstevel@tonic-gate 			(void) putchar('\n');
545*0Sstevel@tonic-gate 			fd_cleanup();
546*0Sstevel@tonic-gate 			return (0);
547*0Sstevel@tonic-gate 			/* NOTREACHED */
548*0Sstevel@tonic-gate 			break;
549*0Sstevel@tonic-gate 	}
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate 	if (dflg || !M_flg) {
552*0Sstevel@tonic-gate 		/*
553*0Sstevel@tonic-gate 		 * default position-dependent tests,
554*0Sstevel@tonic-gate 		 * plus non-default magic tests, if any
555*0Sstevel@tonic-gate 		 */
556*0Sstevel@tonic-gate 		switch (def_position_tests()) {
557*0Sstevel@tonic-gate 			case -1:	/* error */
558*0Sstevel@tonic-gate 				fd_cleanup();
559*0Sstevel@tonic-gate 				return (1);
560*0Sstevel@tonic-gate 			case 1:	/* matching type found */
561*0Sstevel@tonic-gate 				fd_cleanup();
562*0Sstevel@tonic-gate 				return (0);
563*0Sstevel@tonic-gate 				/* NOTREACHED */
564*0Sstevel@tonic-gate 				break;
565*0Sstevel@tonic-gate 			case 0:		/* no matching type found */
566*0Sstevel@tonic-gate 				break;
567*0Sstevel@tonic-gate 		}
568*0Sstevel@tonic-gate 		/* default context-sensitive tests */
569*0Sstevel@tonic-gate 		def_context_tests();
570*0Sstevel@tonic-gate 	} else {
571*0Sstevel@tonic-gate 		/* no more tests to apply; no match was found */
572*0Sstevel@tonic-gate 		(void) printf(gettext("data\n"));
573*0Sstevel@tonic-gate 	}
574*0Sstevel@tonic-gate 	fd_cleanup();
575*0Sstevel@tonic-gate 	return (0);
576*0Sstevel@tonic-gate }
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate /*
579*0Sstevel@tonic-gate  * def_position_tests() - applies default position-sensitive tests,
580*0Sstevel@tonic-gate  *	looking for values in specific positions in the file.
581*0Sstevel@tonic-gate  *	These are followed by default (followed by possibly some
582*0Sstevel@tonic-gate  *	non-default) magic file tests.
583*0Sstevel@tonic-gate  *
584*0Sstevel@tonic-gate  *	All position-sensitive tests, default or otherwise, must
585*0Sstevel@tonic-gate  *	be applied before context-sensitive tests, to avoid
586*0Sstevel@tonic-gate  *	false context-sensitive matches.
587*0Sstevel@tonic-gate  *
588*0Sstevel@tonic-gate  * 	Returns -1 on error which should result in error (non-zero)
589*0Sstevel@tonic-gate  *	exit status for the file utility.
590*0Sstevel@tonic-gate  *	Returns 0 if no matching file type found.
591*0Sstevel@tonic-gate  *	Returns 1 if matching file type found.
592*0Sstevel@tonic-gate  */
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate static int
595*0Sstevel@tonic-gate def_position_tests(void)
596*0Sstevel@tonic-gate {
597*0Sstevel@tonic-gate 	Elf	*elf;
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	if (sccs()) {	/* look for "1hddddd" where d is a digit */
600*0Sstevel@tonic-gate 		(void) printf("sccs \n");
601*0Sstevel@tonic-gate 		return (1);
602*0Sstevel@tonic-gate 	}
603*0Sstevel@tonic-gate 	if (fbuf[0] == '#' && fbuf[1] == '!' && shellscript(fbuf+2, &mbuf))
604*0Sstevel@tonic-gate 		return (1);
605*0Sstevel@tonic-gate 	if ((elf = is_elf_file(elffd)) != NULL) {
606*0Sstevel@tonic-gate 		(void) elf_check(elf);
607*0Sstevel@tonic-gate 		(void) elf_end(elf);
608*0Sstevel@tonic-gate 		(void) putchar('\n');
609*0Sstevel@tonic-gate 		return (1);
610*0Sstevel@tonic-gate 
611*0Sstevel@tonic-gate 	/* LINTED: pointer cast may result in improper alignment */
612*0Sstevel@tonic-gate 	} else if (*(int *)fbuf == CORE_MAGIC) {
613*0Sstevel@tonic-gate 		/* LINTED: pointer cast may result in improper alignment */
614*0Sstevel@tonic-gate 		struct core *corep = (struct core *)fbuf;
615*0Sstevel@tonic-gate 
616*0Sstevel@tonic-gate 		(void) printf("a.out core file");
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 		if (*(corep->c_cmdname) != '\0')
619*0Sstevel@tonic-gate 			(void) printf(" from '%s'", corep->c_cmdname);
620*0Sstevel@tonic-gate 		(void) putchar('\n');
621*0Sstevel@tonic-gate 		return (1);
622*0Sstevel@tonic-gate 	}
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate 	/*
625*0Sstevel@tonic-gate 	 * ZIP files, JAR files, and Java executables
626*0Sstevel@tonic-gate 	 */
627*0Sstevel@tonic-gate 	if (zipfile(fbuf, ifd))
628*0Sstevel@tonic-gate 		return (1);
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate 	if (is_crash_dump(fbuf, ifd))
631*0Sstevel@tonic-gate 		return (1);
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate 	/*
634*0Sstevel@tonic-gate 	 * ChecK against Magic Table entries.
635*0Sstevel@tonic-gate 	 * The magic entries checked here always start with default
636*0Sstevel@tonic-gate 	 * magic tests and may be followed by other, non-default magic
637*0Sstevel@tonic-gate 	 * tests.  If no default tests are to be executed, all the
638*0Sstevel@tonic-gate 	 * magic tests should have been in the first magic table.
639*0Sstevel@tonic-gate 	 */
640*0Sstevel@tonic-gate 	switch (f_ckmtab(magicbuf, mread, 0)) {
641*0Sstevel@tonic-gate 		case -1:	/* Error */
642*0Sstevel@tonic-gate 			exit(2);
643*0Sstevel@tonic-gate 			break;
644*0Sstevel@tonic-gate 		case 0:		/* Not magic */
645*0Sstevel@tonic-gate 			return (0);
646*0Sstevel@tonic-gate 			/* NOTREACHED */
647*0Sstevel@tonic-gate 			break;
648*0Sstevel@tonic-gate 		default:	/* Switch is magic index */
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate 			/*
651*0Sstevel@tonic-gate 			 * f_ckmtab recognizes file type,
652*0Sstevel@tonic-gate 			 * check if it is PostScript.
653*0Sstevel@tonic-gate 			 * if not, check if elf or a.out
654*0Sstevel@tonic-gate 			 */
655*0Sstevel@tonic-gate 			if (magicbuf[0] == '%' && magicbuf[1] == '!') {
656*0Sstevel@tonic-gate 				(void) putchar('\n');
657*0Sstevel@tonic-gate 			} else {
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate 				/*
660*0Sstevel@tonic-gate 				 * Check that the file is executable (dynamic
661*0Sstevel@tonic-gate 				 * objects must be executable to be exec'ed,
662*0Sstevel@tonic-gate 				 * shared objects need not be, but by convention
663*0Sstevel@tonic-gate 				 * should be executable).
664*0Sstevel@tonic-gate 				 *
665*0Sstevel@tonic-gate 				 * Note that we should already have processed
666*0Sstevel@tonic-gate 				 * the file if it was an ELF file.
667*0Sstevel@tonic-gate 				 */
668*0Sstevel@tonic-gate 				ar_coff_or_aout(elffd);
669*0Sstevel@tonic-gate 				(void) putchar('\n');
670*0Sstevel@tonic-gate 			}
671*0Sstevel@tonic-gate 			return (1);
672*0Sstevel@tonic-gate 			/* NOTREACHED */
673*0Sstevel@tonic-gate 			break;
674*0Sstevel@tonic-gate 	}
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate 	return (0);	/* file was not identified */
677*0Sstevel@tonic-gate }
678*0Sstevel@tonic-gate 
679*0Sstevel@tonic-gate /*
680*0Sstevel@tonic-gate  * def_context_tests() - default context-sensitive tests.
681*0Sstevel@tonic-gate  *	These are the last tests to be applied.
682*0Sstevel@tonic-gate  *	If no match is found, prints out "data".
683*0Sstevel@tonic-gate  */
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate static void
686*0Sstevel@tonic-gate def_context_tests(void)
687*0Sstevel@tonic-gate {
688*0Sstevel@tonic-gate 	int	j;
689*0Sstevel@tonic-gate 	int	nl;
690*0Sstevel@tonic-gate 	char	ch;
691*0Sstevel@tonic-gate 	int	len;
692*0Sstevel@tonic-gate 
693*0Sstevel@tonic-gate 	if (ccom() == 0)
694*0Sstevel@tonic-gate 		goto notc;
695*0Sstevel@tonic-gate 	while (fbuf[i] == '#') {
696*0Sstevel@tonic-gate 		j = i;
697*0Sstevel@tonic-gate 		while (fbuf[i++] != '\n') {
698*0Sstevel@tonic-gate 			if (i - j > 255) {
699*0Sstevel@tonic-gate 				(void) printf(gettext("data\n"));
700*0Sstevel@tonic-gate 				return;
701*0Sstevel@tonic-gate 			}
702*0Sstevel@tonic-gate 			if (i >= fbsz)
703*0Sstevel@tonic-gate 				goto notc;
704*0Sstevel@tonic-gate 		}
705*0Sstevel@tonic-gate 		if (ccom() == 0)
706*0Sstevel@tonic-gate 			goto notc;
707*0Sstevel@tonic-gate 	}
708*0Sstevel@tonic-gate check:
709*0Sstevel@tonic-gate 	if (lookup(c) == 1) {
710*0Sstevel@tonic-gate 		while ((ch = fbuf[i]) != ';' && ch != '{') {
711*0Sstevel@tonic-gate 			if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
712*0Sstevel@tonic-gate 				len = 1;
713*0Sstevel@tonic-gate 			i += len;
714*0Sstevel@tonic-gate 			if (i >= fbsz)
715*0Sstevel@tonic-gate 				goto notc;
716*0Sstevel@tonic-gate 		}
717*0Sstevel@tonic-gate 		(void) printf(gettext("c program text"));
718*0Sstevel@tonic-gate 		goto outa;
719*0Sstevel@tonic-gate 	}
720*0Sstevel@tonic-gate 	nl = 0;
721*0Sstevel@tonic-gate 	while (fbuf[i] != '(') {
722*0Sstevel@tonic-gate 		if (fbuf[i] <= 0)
723*0Sstevel@tonic-gate 			goto notas;
724*0Sstevel@tonic-gate 		if (fbuf[i] == ';') {
725*0Sstevel@tonic-gate 			i++;
726*0Sstevel@tonic-gate 			goto check;
727*0Sstevel@tonic-gate 		}
728*0Sstevel@tonic-gate 		if (fbuf[i++] == '\n')
729*0Sstevel@tonic-gate 			if (nl++ > 6)
730*0Sstevel@tonic-gate 				goto notc;
731*0Sstevel@tonic-gate 		if (i >= fbsz)
732*0Sstevel@tonic-gate 			goto notc;
733*0Sstevel@tonic-gate 	}
734*0Sstevel@tonic-gate 	while (fbuf[i] != ')') {
735*0Sstevel@tonic-gate 		if (fbuf[i++] == '\n')
736*0Sstevel@tonic-gate 			if (nl++ > 6)
737*0Sstevel@tonic-gate 				goto notc;
738*0Sstevel@tonic-gate 		if (i >= fbsz)
739*0Sstevel@tonic-gate 			goto notc;
740*0Sstevel@tonic-gate 	}
741*0Sstevel@tonic-gate 	while (fbuf[i] != '{') {
742*0Sstevel@tonic-gate 		if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
743*0Sstevel@tonic-gate 			len = 1;
744*0Sstevel@tonic-gate 		if (fbuf[i] == '\n')
745*0Sstevel@tonic-gate 			if (nl++ > 6)
746*0Sstevel@tonic-gate 				goto notc;
747*0Sstevel@tonic-gate 		i += len;
748*0Sstevel@tonic-gate 		if (i >= fbsz)
749*0Sstevel@tonic-gate 			goto notc;
750*0Sstevel@tonic-gate 	}
751*0Sstevel@tonic-gate 	(void) printf(gettext("c program text"));
752*0Sstevel@tonic-gate 	goto outa;
753*0Sstevel@tonic-gate notc:
754*0Sstevel@tonic-gate 	i = 0;			/* reset to begining of file again */
755*0Sstevel@tonic-gate 	while (fbuf[i] == 'c' || fbuf[i] == 'C'|| fbuf[i] == '!' ||
756*0Sstevel@tonic-gate 	    fbuf[i] == '*' || fbuf[i] == '\n') {
757*0Sstevel@tonic-gate 		while (fbuf[i++] != '\n')
758*0Sstevel@tonic-gate 			if (i >= fbsz)
759*0Sstevel@tonic-gate 				goto notfort;
760*0Sstevel@tonic-gate 	}
761*0Sstevel@tonic-gate 	if (lookup(fort) == 1) {
762*0Sstevel@tonic-gate 		(void) printf(gettext("fortran program text"));
763*0Sstevel@tonic-gate 		goto outa;
764*0Sstevel@tonic-gate 	}
765*0Sstevel@tonic-gate notfort:			/* looking for assembler program */
766*0Sstevel@tonic-gate 	i = 0;			/* reset to beginning of file again */
767*0Sstevel@tonic-gate 	if (ccom() == 0)	/* assembler programs may contain */
768*0Sstevel@tonic-gate 				/* c-style comments */
769*0Sstevel@tonic-gate 		goto notas;
770*0Sstevel@tonic-gate 	if (ascom() == 0)
771*0Sstevel@tonic-gate 		goto notas;
772*0Sstevel@tonic-gate 	j = i - 1;
773*0Sstevel@tonic-gate 	if (fbuf[i] == '.') {
774*0Sstevel@tonic-gate 		i++;
775*0Sstevel@tonic-gate 		if (lookup(as) == 1) {
776*0Sstevel@tonic-gate 			(void) printf(gettext("assembler program text"));
777*0Sstevel@tonic-gate 			goto outa;
778*0Sstevel@tonic-gate 		} else if (j != -1 && fbuf[j] == '\n' && isalpha(fbuf[j + 2])) {
779*0Sstevel@tonic-gate 			(void) printf(
780*0Sstevel@tonic-gate 			    gettext("[nt]roff, tbl, or eqn input text"));
781*0Sstevel@tonic-gate 			goto outa;
782*0Sstevel@tonic-gate 		}
783*0Sstevel@tonic-gate 	}
784*0Sstevel@tonic-gate 	while (lookup(asc) == 0) {
785*0Sstevel@tonic-gate 		if (ccom() == 0)
786*0Sstevel@tonic-gate 			goto notas;
787*0Sstevel@tonic-gate 		if (ascom() == 0)
788*0Sstevel@tonic-gate 			goto notas;
789*0Sstevel@tonic-gate 		while (fbuf[i] != '\n' && fbuf[i++] != ':') {
790*0Sstevel@tonic-gate 			if (i >= fbsz)
791*0Sstevel@tonic-gate 				goto notas;
792*0Sstevel@tonic-gate 		}
793*0Sstevel@tonic-gate 		while (fbuf[i] == '\n' || fbuf[i] == ' ' || fbuf[i] == '\t')
794*0Sstevel@tonic-gate 			if (i++ >= fbsz)
795*0Sstevel@tonic-gate 				goto notas;
796*0Sstevel@tonic-gate 		j = i - 1;
797*0Sstevel@tonic-gate 		if (fbuf[i] == '.') {
798*0Sstevel@tonic-gate 			i++;
799*0Sstevel@tonic-gate 			if (lookup(as) == 1) {
800*0Sstevel@tonic-gate 				(void) printf(
801*0Sstevel@tonic-gate 				    gettext("assembler program text"));
802*0Sstevel@tonic-gate 				goto outa;
803*0Sstevel@tonic-gate 			} else if (fbuf[j] == '\n' && isalpha(fbuf[j+2])) {
804*0Sstevel@tonic-gate 				(void) printf(
805*0Sstevel@tonic-gate 				    gettext("[nt]roff, tbl, or eqn input "
806*0Sstevel@tonic-gate 				    "text"));
807*0Sstevel@tonic-gate 				goto outa;
808*0Sstevel@tonic-gate 			}
809*0Sstevel@tonic-gate 		}
810*0Sstevel@tonic-gate 	}
811*0Sstevel@tonic-gate 	(void) printf(gettext("assembler program text"));
812*0Sstevel@tonic-gate 	goto outa;
813*0Sstevel@tonic-gate notas:
814*0Sstevel@tonic-gate 	/* start modification for multibyte env */
815*0Sstevel@tonic-gate 	IS_ascii = 1;
816*0Sstevel@tonic-gate 	if (fbsz < FBSZ)
817*0Sstevel@tonic-gate 		Max = fbsz;
818*0Sstevel@tonic-gate 	else
819*0Sstevel@tonic-gate 		Max = FBSZ - MB_LEN_MAX; /* prevent cut of wchar read */
820*0Sstevel@tonic-gate 	/* end modification for multibyte env */
821*0Sstevel@tonic-gate 
822*0Sstevel@tonic-gate 	for (i = 0; i < Max; /* null */)
823*0Sstevel@tonic-gate 		if (fbuf[i] & 0200) {
824*0Sstevel@tonic-gate 			IS_ascii = 0;
825*0Sstevel@tonic-gate 			if (fbuf[0] == '\100' && fbuf[1] == '\357') {
826*0Sstevel@tonic-gate 				(void) printf(gettext("troff output\n"));
827*0Sstevel@tonic-gate 				return;
828*0Sstevel@tonic-gate 			}
829*0Sstevel@tonic-gate 		/* start modification for multibyte env */
830*0Sstevel@tonic-gate 			if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX))
831*0Sstevel@tonic-gate 			    <= 0 || !iswprint(wchar)) {
832*0Sstevel@tonic-gate 				(void) printf(gettext("data\n"));
833*0Sstevel@tonic-gate 				return;
834*0Sstevel@tonic-gate 			}
835*0Sstevel@tonic-gate 			i += length;
836*0Sstevel@tonic-gate 		}
837*0Sstevel@tonic-gate 		else
838*0Sstevel@tonic-gate 			i++;
839*0Sstevel@tonic-gate 	i = fbsz;
840*0Sstevel@tonic-gate 		/* end modification for multibyte env */
841*0Sstevel@tonic-gate 	if (mbuf.st_mode&(S_IXUSR|S_IXGRP|S_IXOTH))
842*0Sstevel@tonic-gate 		(void) printf(gettext("commands text"));
843*0Sstevel@tonic-gate 	else if (troffint(fbuf, fbsz))
844*0Sstevel@tonic-gate 		(void) printf(gettext("troff intermediate output text"));
845*0Sstevel@tonic-gate 	else if (english(fbuf, fbsz))
846*0Sstevel@tonic-gate 		(void) printf(gettext("English text"));
847*0Sstevel@tonic-gate 	else if (IS_ascii)
848*0Sstevel@tonic-gate 		(void) printf(gettext("ascii text"));
849*0Sstevel@tonic-gate 	else
850*0Sstevel@tonic-gate 		(void) printf(gettext("text")); /* for multibyte env */
851*0Sstevel@tonic-gate outa:
852*0Sstevel@tonic-gate 	/*
853*0Sstevel@tonic-gate 	 * This code is to make sure that no MB char is cut in half
854*0Sstevel@tonic-gate 	 * while still being used.
855*0Sstevel@tonic-gate 	 */
856*0Sstevel@tonic-gate 	fbsz = (fbsz < FBSZ ? fbsz : fbsz - MB_CUR_MAX + 1);
857*0Sstevel@tonic-gate 	while (i < fbsz) {
858*0Sstevel@tonic-gate 		if (isascii(fbuf[i])) {
859*0Sstevel@tonic-gate 			i++;
860*0Sstevel@tonic-gate 			continue;
861*0Sstevel@tonic-gate 		} else {
862*0Sstevel@tonic-gate 			if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX))
863*0Sstevel@tonic-gate 			    <= 0 || !iswprint(wchar)) {
864*0Sstevel@tonic-gate 				(void) printf(gettext(" with garbage\n"));
865*0Sstevel@tonic-gate 				return;
866*0Sstevel@tonic-gate 			}
867*0Sstevel@tonic-gate 			i = i + length;
868*0Sstevel@tonic-gate 		}
869*0Sstevel@tonic-gate 	}
870*0Sstevel@tonic-gate 	(void) printf("\n");
871*0Sstevel@tonic-gate }
872*0Sstevel@tonic-gate 
873*0Sstevel@tonic-gate static int
874*0Sstevel@tonic-gate troffint(char *bp, int n)
875*0Sstevel@tonic-gate {
876*0Sstevel@tonic-gate 	int k;
877*0Sstevel@tonic-gate 
878*0Sstevel@tonic-gate 	i = 0;
879*0Sstevel@tonic-gate 	for (k = 0; k < 6; k++) {
880*0Sstevel@tonic-gate 		if (lookup(troff) == 0)
881*0Sstevel@tonic-gate 			return (0);
882*0Sstevel@tonic-gate 		if (lookup(troff) == 0)
883*0Sstevel@tonic-gate 			return (0);
884*0Sstevel@tonic-gate 		while (i < n && bp[i] != '\n')
885*0Sstevel@tonic-gate 			i++;
886*0Sstevel@tonic-gate 		if (i++ >= n)
887*0Sstevel@tonic-gate 			return (0);
888*0Sstevel@tonic-gate 	}
889*0Sstevel@tonic-gate 	return (1);
890*0Sstevel@tonic-gate }
891*0Sstevel@tonic-gate 
892*0Sstevel@tonic-gate /*
893*0Sstevel@tonic-gate  * Determine if the passed descriptor describes an ELF file.
894*0Sstevel@tonic-gate  * If so, return the Elf handle.
895*0Sstevel@tonic-gate  */
896*0Sstevel@tonic-gate static Elf *
897*0Sstevel@tonic-gate is_elf_file(int elffd)
898*0Sstevel@tonic-gate {
899*0Sstevel@tonic-gate 	Elf *elf;
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate 	elf = elf_begin(elffd, ELF_C_READ, (Elf *)0);
902*0Sstevel@tonic-gate 	switch (elf_kind(elf)) {
903*0Sstevel@tonic-gate 	case ELF_K_ELF:
904*0Sstevel@tonic-gate 		break;
905*0Sstevel@tonic-gate 	default:
906*0Sstevel@tonic-gate 		(void) elf_end(elf);
907*0Sstevel@tonic-gate 		elf = NULL;
908*0Sstevel@tonic-gate 		break;
909*0Sstevel@tonic-gate 	}
910*0Sstevel@tonic-gate 	return (elf);
911*0Sstevel@tonic-gate }
912*0Sstevel@tonic-gate 
913*0Sstevel@tonic-gate static void
914*0Sstevel@tonic-gate ar_coff_or_aout(int elffd)
915*0Sstevel@tonic-gate {
916*0Sstevel@tonic-gate 	Elf *elf;
917*0Sstevel@tonic-gate 
918*0Sstevel@tonic-gate 	/*
919*0Sstevel@tonic-gate 	 * Get the files elf descriptor and process it as an elf or
920*0Sstevel@tonic-gate 	 * a.out (4.x) file.
921*0Sstevel@tonic-gate 	 */
922*0Sstevel@tonic-gate 
923*0Sstevel@tonic-gate 	elf = elf_begin(elffd, ELF_C_READ, (Elf *)0);
924*0Sstevel@tonic-gate 	switch (elf_kind(elf)) {
925*0Sstevel@tonic-gate 		case ELF_K_AR :
926*0Sstevel@tonic-gate 			(void) printf(gettext(", not a dynamic executable "
927*0Sstevel@tonic-gate 			    "or shared object"));
928*0Sstevel@tonic-gate 			break;
929*0Sstevel@tonic-gate 		case ELF_K_COFF:
930*0Sstevel@tonic-gate 			(void) printf(gettext(", unsupported or unknown "
931*0Sstevel@tonic-gate 			    "file type"));
932*0Sstevel@tonic-gate 			break;
933*0Sstevel@tonic-gate 		default:
934*0Sstevel@tonic-gate 			/*
935*0Sstevel@tonic-gate 			 * This is either an unknown file or an aout format
936*0Sstevel@tonic-gate 			 * At this time, we don't print dynamic/stripped
937*0Sstevel@tonic-gate 			 * info. on a.out or non-Elf binaries.
938*0Sstevel@tonic-gate 			 */
939*0Sstevel@tonic-gate 			break;
940*0Sstevel@tonic-gate 	}
941*0Sstevel@tonic-gate 	(void) elf_end(elf);
942*0Sstevel@tonic-gate }
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate 
945*0Sstevel@tonic-gate static void
946*0Sstevel@tonic-gate print_elf_type(Elf *elf, GElf_Ehdr *ehdr, int format)
947*0Sstevel@tonic-gate {
948*0Sstevel@tonic-gate 	switch (ehdr->e_type) {
949*0Sstevel@tonic-gate 	case ET_NONE:
950*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("unknown type"));
951*0Sstevel@tonic-gate 		break;
952*0Sstevel@tonic-gate 	case ET_REL:
953*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("relocatable"));
954*0Sstevel@tonic-gate 		break;
955*0Sstevel@tonic-gate 	case ET_EXEC:
956*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("executable"));
957*0Sstevel@tonic-gate 		break;
958*0Sstevel@tonic-gate 	case ET_DYN:
959*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("dynamic lib"));
960*0Sstevel@tonic-gate 		break;
961*0Sstevel@tonic-gate 	case ET_CORE:
962*0Sstevel@tonic-gate 		if (old_core(elf, ehdr, format))
963*0Sstevel@tonic-gate 			(void) printf(" %s", gettext("pre-2.6 core file"));
964*0Sstevel@tonic-gate 		else
965*0Sstevel@tonic-gate 			(void) printf(" %s", gettext("core file"));
966*0Sstevel@tonic-gate 		break;
967*0Sstevel@tonic-gate 	default:
968*0Sstevel@tonic-gate 		break;
969*0Sstevel@tonic-gate 	}
970*0Sstevel@tonic-gate }
971*0Sstevel@tonic-gate 
972*0Sstevel@tonic-gate static void
973*0Sstevel@tonic-gate print_elf_machine(int machine)
974*0Sstevel@tonic-gate {
975*0Sstevel@tonic-gate 	switch (machine) {
976*0Sstevel@tonic-gate 	case EM_NONE:
977*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("unknown machine"));
978*0Sstevel@tonic-gate 		break;
979*0Sstevel@tonic-gate 	case EM_M32:
980*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("WE32100"));
981*0Sstevel@tonic-gate 		break;
982*0Sstevel@tonic-gate 	case EM_SPARC:
983*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("SPARC"));
984*0Sstevel@tonic-gate 		break;
985*0Sstevel@tonic-gate 	case EM_386:
986*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("80386"));
987*0Sstevel@tonic-gate 		break;
988*0Sstevel@tonic-gate 	case EM_68K:
989*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("M68000"));
990*0Sstevel@tonic-gate 		break;
991*0Sstevel@tonic-gate 	case EM_88K:
992*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("M88000"));
993*0Sstevel@tonic-gate 		break;
994*0Sstevel@tonic-gate 	case EM_486:
995*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("80486"));
996*0Sstevel@tonic-gate 		break;
997*0Sstevel@tonic-gate 	case EM_860:
998*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("i860"));
999*0Sstevel@tonic-gate 		break;
1000*0Sstevel@tonic-gate 	case EM_MIPS:
1001*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("MIPS RS3000 Big-Endian"));
1002*0Sstevel@tonic-gate 		break;
1003*0Sstevel@tonic-gate 	case EM_MIPS_RS3_LE:
1004*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("MIPS RS3000 Little-Endian"));
1005*0Sstevel@tonic-gate 		break;
1006*0Sstevel@tonic-gate 	case EM_RS6000:
1007*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("MIPS RS6000"));
1008*0Sstevel@tonic-gate 		break;
1009*0Sstevel@tonic-gate 	case EM_PA_RISC:
1010*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("PA-RISC"));
1011*0Sstevel@tonic-gate 		break;
1012*0Sstevel@tonic-gate 	case EM_nCUBE:
1013*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("nCUBE"));
1014*0Sstevel@tonic-gate 		break;
1015*0Sstevel@tonic-gate 	case EM_VPP500:
1016*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("VPP500"));
1017*0Sstevel@tonic-gate 		break;
1018*0Sstevel@tonic-gate 	case EM_SPARC32PLUS:
1019*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("SPARC32PLUS"));
1020*0Sstevel@tonic-gate 		break;
1021*0Sstevel@tonic-gate 	case EM_PPC:
1022*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("PowerPC"));
1023*0Sstevel@tonic-gate 		break;
1024*0Sstevel@tonic-gate 	case EM_SPARCV9:
1025*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("SPARCV9"));
1026*0Sstevel@tonic-gate 		break;
1027*0Sstevel@tonic-gate 	case EM_IA_64:
1028*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("IA64"));
1029*0Sstevel@tonic-gate 		break;
1030*0Sstevel@tonic-gate 	case EM_AMD64:
1031*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("AMD64"));
1032*0Sstevel@tonic-gate 		break;
1033*0Sstevel@tonic-gate 	default:
1034*0Sstevel@tonic-gate 		break;
1035*0Sstevel@tonic-gate 	}
1036*0Sstevel@tonic-gate }
1037*0Sstevel@tonic-gate 
1038*0Sstevel@tonic-gate static void
1039*0Sstevel@tonic-gate print_elf_datatype(int datatype)
1040*0Sstevel@tonic-gate {
1041*0Sstevel@tonic-gate 	switch (datatype) {
1042*0Sstevel@tonic-gate 	case ELFDATA2LSB:
1043*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("LSB"));
1044*0Sstevel@tonic-gate 		break;
1045*0Sstevel@tonic-gate 	case ELFDATA2MSB:
1046*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("MSB"));
1047*0Sstevel@tonic-gate 		break;
1048*0Sstevel@tonic-gate 	default:
1049*0Sstevel@tonic-gate 		break;
1050*0Sstevel@tonic-gate 	}
1051*0Sstevel@tonic-gate }
1052*0Sstevel@tonic-gate 
1053*0Sstevel@tonic-gate static void
1054*0Sstevel@tonic-gate print_elf_class(int class)
1055*0Sstevel@tonic-gate {
1056*0Sstevel@tonic-gate 	switch (class) {
1057*0Sstevel@tonic-gate 	case ELFCLASS32:
1058*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("32-bit"));
1059*0Sstevel@tonic-gate 		break;
1060*0Sstevel@tonic-gate 	case ELFCLASS64:
1061*0Sstevel@tonic-gate 		(void) printf(" %s", gettext("64-bit"));
1062*0Sstevel@tonic-gate 		break;
1063*0Sstevel@tonic-gate 	default:
1064*0Sstevel@tonic-gate 		break;
1065*0Sstevel@tonic-gate 	}
1066*0Sstevel@tonic-gate }
1067*0Sstevel@tonic-gate 
1068*0Sstevel@tonic-gate static void
1069*0Sstevel@tonic-gate print_elf_flags(int machine, unsigned int flags)
1070*0Sstevel@tonic-gate {
1071*0Sstevel@tonic-gate 	switch (machine) {
1072*0Sstevel@tonic-gate 	case EM_M32:
1073*0Sstevel@tonic-gate 		if (flags & EF_M32_MAU)
1074*0Sstevel@tonic-gate 			(void) printf("%s", gettext(", MAU Required"));
1075*0Sstevel@tonic-gate 		break;
1076*0Sstevel@tonic-gate 	case EM_SPARCV9:
1077*0Sstevel@tonic-gate 		if (flags & EF_SPARC_EXT_MASK) {
1078*0Sstevel@tonic-gate 			if (flags & EF_SPARC_SUN_US3) {
1079*0Sstevel@tonic-gate 				(void) printf("%s", gettext(
1080*0Sstevel@tonic-gate 				    ", UltraSPARC3 Extensions Required"));
1081*0Sstevel@tonic-gate 			} else if (flags & EF_SPARC_SUN_US1) {
1082*0Sstevel@tonic-gate 				(void) printf("%s", gettext(
1083*0Sstevel@tonic-gate 				    ", UltraSPARC1 Extensions Required"));
1084*0Sstevel@tonic-gate 			}
1085*0Sstevel@tonic-gate 			if (flags & EF_SPARC_HAL_R1)
1086*0Sstevel@tonic-gate 				(void) printf("%s", gettext(
1087*0Sstevel@tonic-gate 				    ", HaL R1 Extensions Required"));
1088*0Sstevel@tonic-gate 		}
1089*0Sstevel@tonic-gate 		break;
1090*0Sstevel@tonic-gate 	case EM_SPARC32PLUS:
1091*0Sstevel@tonic-gate 		if (flags & EF_SPARC_32PLUS)
1092*0Sstevel@tonic-gate 			(void) printf("%s", gettext(", V8+ Required"));
1093*0Sstevel@tonic-gate 		if (flags & EF_SPARC_SUN_US3) {
1094*0Sstevel@tonic-gate 			(void) printf("%s",
1095*0Sstevel@tonic-gate 				gettext(", UltraSPARC3 Extensions Required"));
1096*0Sstevel@tonic-gate 		} else if (flags & EF_SPARC_SUN_US1) {
1097*0Sstevel@tonic-gate 			(void) printf("%s",
1098*0Sstevel@tonic-gate 				gettext(", UltraSPARC1 Extensions Required"));
1099*0Sstevel@tonic-gate 		}
1100*0Sstevel@tonic-gate 		if (flags & EF_SPARC_HAL_R1)
1101*0Sstevel@tonic-gate 			(void) printf("%s",
1102*0Sstevel@tonic-gate 				gettext(", HaL R1 Extensions Required"));
1103*0Sstevel@tonic-gate 		break;
1104*0Sstevel@tonic-gate 	default:
1105*0Sstevel@tonic-gate 		break;
1106*0Sstevel@tonic-gate 	}
1107*0Sstevel@tonic-gate }
1108*0Sstevel@tonic-gate 
1109*0Sstevel@tonic-gate static int
1110*0Sstevel@tonic-gate print_cap(Elf *elf, GElf_Ehdr *ehdr)
1111*0Sstevel@tonic-gate {
1112*0Sstevel@tonic-gate 	Elf_Scn	*scn = 0;
1113*0Sstevel@tonic-gate 
1114*0Sstevel@tonic-gate 	/*
1115*0Sstevel@tonic-gate 	 * Traverse the files sections to see if any software/hardware
1116*0Sstevel@tonic-gate 	 * capabilities are available.
1117*0Sstevel@tonic-gate 	 */
1118*0Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf, scn)) != 0) {
1119*0Sstevel@tonic-gate 		GElf_Word	ndx, capn;
1120*0Sstevel@tonic-gate 		GElf_Shdr	shdr;
1121*0Sstevel@tonic-gate 		Elf_Data	*data;
1122*0Sstevel@tonic-gate 
1123*0Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr) == 0) {
1124*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1125*0Sstevel@tonic-gate 			    gettext("can't read ELF section header\n"));
1126*0Sstevel@tonic-gate 			return (1);
1127*0Sstevel@tonic-gate 		}
1128*0Sstevel@tonic-gate 		if (shdr.sh_type != SHT_SUNW_cap)
1129*0Sstevel@tonic-gate 			continue;
1130*0Sstevel@tonic-gate 
1131*0Sstevel@tonic-gate 		/*
1132*0Sstevel@tonic-gate 		 * Get the data associated with the .cap section.
1133*0Sstevel@tonic-gate 		 */
1134*0Sstevel@tonic-gate 		if ((data = elf_getdata(scn, 0)) == 0) {
1135*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1136*0Sstevel@tonic-gate 				gettext("can't read ELF section data\n"));
1137*0Sstevel@tonic-gate 			return (1);
1138*0Sstevel@tonic-gate 		}
1139*0Sstevel@tonic-gate 
1140*0Sstevel@tonic-gate 		capn = (GElf_Word)(shdr.sh_size / shdr.sh_entsize);
1141*0Sstevel@tonic-gate 		for (ndx = 0; ndx < capn; ndx++) {
1142*0Sstevel@tonic-gate 			char		str[100];
1143*0Sstevel@tonic-gate 			GElf_Cap	cap;
1144*0Sstevel@tonic-gate 
1145*0Sstevel@tonic-gate 			if (gelf_getcap(data, ndx, &cap) == NULL) {
1146*0Sstevel@tonic-gate 				(void) fprintf(stderr,
1147*0Sstevel@tonic-gate 				    gettext("can't read capabilities data\n"));
1148*0Sstevel@tonic-gate 				return (1);
1149*0Sstevel@tonic-gate 			}
1150*0Sstevel@tonic-gate 			if (cap.c_tag != CA_SUNW_NULL) {
1151*0Sstevel@tonic-gate 				(void) cap_val2str(cap.c_tag, cap.c_un.c_val,
1152*0Sstevel@tonic-gate 				    str, sizeof (str), 0, ehdr->e_machine);
1153*0Sstevel@tonic-gate 				(void) printf(" [%s]", str);
1154*0Sstevel@tonic-gate 			}
1155*0Sstevel@tonic-gate 		}
1156*0Sstevel@tonic-gate 	}
1157*0Sstevel@tonic-gate 	return (0);
1158*0Sstevel@tonic-gate }
1159*0Sstevel@tonic-gate 
1160*0Sstevel@tonic-gate static int
1161*0Sstevel@tonic-gate elf_check(Elf *elf)
1162*0Sstevel@tonic-gate {
1163*0Sstevel@tonic-gate 	GElf_Ehdr	ehdr;
1164*0Sstevel@tonic-gate 	GElf_Phdr	phdr;
1165*0Sstevel@tonic-gate 	int		dynamic, cnt;
1166*0Sstevel@tonic-gate 	char	*ident;
1167*0Sstevel@tonic-gate 	size_t	size;
1168*0Sstevel@tonic-gate 
1169*0Sstevel@tonic-gate 	/*
1170*0Sstevel@tonic-gate 	 * verify information in file header
1171*0Sstevel@tonic-gate 	 */
1172*0Sstevel@tonic-gate 	if (gelf_getehdr(elf, &ehdr) == (GElf_Ehdr *)0) {
1173*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("can't read ELF header\n"));
1174*0Sstevel@tonic-gate 		return (1);
1175*0Sstevel@tonic-gate 	}
1176*0Sstevel@tonic-gate 	ident = elf_getident(elf, &size);
1177*0Sstevel@tonic-gate 	(void) printf("%s", gettext("ELF"));
1178*0Sstevel@tonic-gate 	print_elf_class(ident[EI_CLASS]);
1179*0Sstevel@tonic-gate 	print_elf_datatype(ident[EI_DATA]);
1180*0Sstevel@tonic-gate 	print_elf_type(elf, &ehdr, ident[EI_DATA]);
1181*0Sstevel@tonic-gate 	print_elf_machine(ehdr.e_machine);
1182*0Sstevel@tonic-gate 	if (ehdr.e_version == 1)
1183*0Sstevel@tonic-gate 		(void) printf(" %s %d",
1184*0Sstevel@tonic-gate 		    gettext("Version"), (int)ehdr.e_version);
1185*0Sstevel@tonic-gate 	print_elf_flags(ehdr.e_machine, ehdr.e_flags);
1186*0Sstevel@tonic-gate 
1187*0Sstevel@tonic-gate 	if (core(elf, &ehdr, ident[EI_DATA]))	/* check for core file */
1188*0Sstevel@tonic-gate 		return (0);
1189*0Sstevel@tonic-gate 
1190*0Sstevel@tonic-gate 	if (print_cap(elf, &ehdr))
1191*0Sstevel@tonic-gate 		return (1);
1192*0Sstevel@tonic-gate 
1193*0Sstevel@tonic-gate 	/*
1194*0Sstevel@tonic-gate 	 * check type
1195*0Sstevel@tonic-gate 	 */
1196*0Sstevel@tonic-gate 	if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN))
1197*0Sstevel@tonic-gate 		return (1);
1198*0Sstevel@tonic-gate 
1199*0Sstevel@tonic-gate 	/*
1200*0Sstevel@tonic-gate 	 * read program header and check for dynamic section
1201*0Sstevel@tonic-gate 	 */
1202*0Sstevel@tonic-gate 	if (ehdr.e_phnum == 0) {
1203*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("can't read program header\n"));
1204*0Sstevel@tonic-gate 		return (1);
1205*0Sstevel@tonic-gate 	}
1206*0Sstevel@tonic-gate 
1207*0Sstevel@tonic-gate 	for (dynamic = 0, cnt = 0; cnt < (int)ehdr.e_phnum; cnt++) {
1208*0Sstevel@tonic-gate 		if (gelf_getphdr(elf, cnt, &phdr) == NULL) {
1209*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1210*0Sstevel@tonic-gate 				gettext("can't read program header\n"));
1211*0Sstevel@tonic-gate 			return (1);
1212*0Sstevel@tonic-gate 		}
1213*0Sstevel@tonic-gate 		if (phdr.p_type == PT_DYNAMIC) {
1214*0Sstevel@tonic-gate 			dynamic = 1;
1215*0Sstevel@tonic-gate 			break;
1216*0Sstevel@tonic-gate 		}
1217*0Sstevel@tonic-gate 	}
1218*0Sstevel@tonic-gate 	if (dynamic)
1219*0Sstevel@tonic-gate 		(void) printf(gettext(", dynamically linked"));
1220*0Sstevel@tonic-gate 	else
1221*0Sstevel@tonic-gate 		(void) printf(gettext(", statically linked"));
1222*0Sstevel@tonic-gate 
1223*0Sstevel@tonic-gate 	is_stripped(elf);
1224*0Sstevel@tonic-gate 	return (0);
1225*0Sstevel@tonic-gate }
1226*0Sstevel@tonic-gate 
1227*0Sstevel@tonic-gate /*
1228*0Sstevel@tonic-gate  * is_stripped prints information on whether the executable has
1229*0Sstevel@tonic-gate  * been stripped.
1230*0Sstevel@tonic-gate  */
1231*0Sstevel@tonic-gate static void
1232*0Sstevel@tonic-gate is_stripped(Elf *elf)
1233*0Sstevel@tonic-gate {
1234*0Sstevel@tonic-gate 	GElf_Shdr	shdr;
1235*0Sstevel@tonic-gate 	GElf_Ehdr	ehdr;
1236*0Sstevel@tonic-gate 	Elf_Scn		*scn, *nextscn;
1237*0Sstevel@tonic-gate 	char		*section_name;
1238*0Sstevel@tonic-gate 	int		symtab = 0;
1239*0Sstevel@tonic-gate 	int		debuginfo = 0;
1240*0Sstevel@tonic-gate 
1241*0Sstevel@tonic-gate 
1242*0Sstevel@tonic-gate 	if (gelf_getehdr(elf, &ehdr) == NULL) {
1243*0Sstevel@tonic-gate 		return;
1244*0Sstevel@tonic-gate 	}
1245*0Sstevel@tonic-gate 
1246*0Sstevel@tonic-gate 	/*
1247*0Sstevel@tonic-gate 	 * Definition time:
1248*0Sstevel@tonic-gate 	 *	- "not stripped" means that an executable file
1249*0Sstevel@tonic-gate 	 *	contains a Symbol Table (.symtab)
1250*0Sstevel@tonic-gate 	 *	- "stripped" means that an executable file
1251*0Sstevel@tonic-gate 	 *	does not contain a Symbol Table.
1252*0Sstevel@tonic-gate 	 * When strip -l or strip -x is run, it strips the
1253*0Sstevel@tonic-gate 	 * debugging information (.line section name (strip -l),
1254*0Sstevel@tonic-gate 	 * .line, .debug*, .stabs*, .dwarf* section names
1255*0Sstevel@tonic-gate 	 * and SHT_SUNW_DEBUGSTR and SHT_SUNW_DEBUG
1256*0Sstevel@tonic-gate 	 * section types (strip -x), however the Symbol
1257*0Sstevel@tonic-gate 	 * Table will still be present.
1258*0Sstevel@tonic-gate 	 * Therefore, if
1259*0Sstevel@tonic-gate 	 *	- No Symbol Table present, then report
1260*0Sstevel@tonic-gate 	 *		"stripped"
1261*0Sstevel@tonic-gate 	 *	- Symbol Table present with debugging
1262*0Sstevel@tonic-gate 	 *	information (line number or debug section names,
1263*0Sstevel@tonic-gate 	 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
1264*0Sstevel@tonic-gate 	 *	types) then report:
1265*0Sstevel@tonic-gate 	 *		"not stripped"
1266*0Sstevel@tonic-gate 	 *	- Symbol Table present with no debugging
1267*0Sstevel@tonic-gate 	 *	information (line number or debug section names,
1268*0Sstevel@tonic-gate 	 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
1269*0Sstevel@tonic-gate 	 *	types) then report:
1270*0Sstevel@tonic-gate 	 *		"not stripped, no debugging information
1271*0Sstevel@tonic-gate 	 *		available"
1272*0Sstevel@tonic-gate 	 */
1273*0Sstevel@tonic-gate 	scn = NULL;
1274*0Sstevel@tonic-gate 	while ((nextscn = elf_nextscn(elf, scn)) != NULL) {
1275*0Sstevel@tonic-gate 		if (symtab && debuginfo) {
1276*0Sstevel@tonic-gate 			break;
1277*0Sstevel@tonic-gate 		}
1278*0Sstevel@tonic-gate 
1279*0Sstevel@tonic-gate 		scn = nextscn;
1280*0Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr) == NULL) {
1281*0Sstevel@tonic-gate 			continue;
1282*0Sstevel@tonic-gate 		}
1283*0Sstevel@tonic-gate 
1284*0Sstevel@tonic-gate 		if (!symtab && (shdr.sh_type == SHT_SYMTAB)) {
1285*0Sstevel@tonic-gate 			symtab++;
1286*0Sstevel@tonic-gate 			continue;
1287*0Sstevel@tonic-gate 		}
1288*0Sstevel@tonic-gate 
1289*0Sstevel@tonic-gate 		if (!debuginfo &&
1290*0Sstevel@tonic-gate 		    ((shdr.sh_type == SHT_SUNW_DEBUG) ||
1291*0Sstevel@tonic-gate 		    (shdr.sh_type == SHT_SUNW_DEBUGSTR) ||
1292*0Sstevel@tonic-gate 		    (((section_name = elf_strptr(elf, ehdr.e_shstrndx,
1293*0Sstevel@tonic-gate 		    (size_t)shdr.sh_name)) != NULL) &&
1294*0Sstevel@tonic-gate 		    (is_in_list(debug_sections, section_name))))) {
1295*0Sstevel@tonic-gate 			debuginfo++;
1296*0Sstevel@tonic-gate 		}
1297*0Sstevel@tonic-gate 	}
1298*0Sstevel@tonic-gate 
1299*0Sstevel@tonic-gate 	/*
1300*0Sstevel@tonic-gate 	 * Now that we've scanned all sections, print out the appropriate
1301*0Sstevel@tonic-gate 	 * diagnostic.
1302*0Sstevel@tonic-gate 	 */
1303*0Sstevel@tonic-gate 	if (symtab) {
1304*0Sstevel@tonic-gate 		(void) printf(gettext(", not stripped"));
1305*0Sstevel@tonic-gate 		if (!debuginfo) {
1306*0Sstevel@tonic-gate 			(void) printf(gettext(
1307*0Sstevel@tonic-gate 			    ", no debugging information available"));
1308*0Sstevel@tonic-gate 		}
1309*0Sstevel@tonic-gate 	} else {
1310*0Sstevel@tonic-gate 		(void) printf(gettext(", stripped"));
1311*0Sstevel@tonic-gate 	}
1312*0Sstevel@tonic-gate }
1313*0Sstevel@tonic-gate 
1314*0Sstevel@tonic-gate /*
1315*0Sstevel@tonic-gate  * lookup -
1316*0Sstevel@tonic-gate  * Attempts to match one of the strings from a list, 'tab',
1317*0Sstevel@tonic-gate  * with what is in the file, starting at the current index position 'i'.
1318*0Sstevel@tonic-gate  * Looks past any initial whitespace and expects whitespace or other
1319*0Sstevel@tonic-gate  * delimiting characters to follow the matched string.
1320*0Sstevel@tonic-gate  * A match identifies the file as being 'assembler', 'fortran', 'c', etc.
1321*0Sstevel@tonic-gate  * Returns 1 for a successful match, 0 otherwise.
1322*0Sstevel@tonic-gate  */
1323*0Sstevel@tonic-gate static int
1324*0Sstevel@tonic-gate lookup(char **tab)
1325*0Sstevel@tonic-gate {
1326*0Sstevel@tonic-gate 	register char	r;
1327*0Sstevel@tonic-gate 	register int	k, j, l;
1328*0Sstevel@tonic-gate 
1329*0Sstevel@tonic-gate 	while (fbuf[i] == ' ' || fbuf[i] == '\t' || fbuf[i] == '\n')
1330*0Sstevel@tonic-gate 		i++;
1331*0Sstevel@tonic-gate 	for (j = 0; tab[j] != 0; j++) {
1332*0Sstevel@tonic-gate 		l = 0;
1333*0Sstevel@tonic-gate 		for (k = i; ((r = tab[j][l++]) == fbuf[k] && r != '\0'); k++);
1334*0Sstevel@tonic-gate 		if (r == '\0')
1335*0Sstevel@tonic-gate 			if (fbuf[k] == ' ' || fbuf[k] == '\n' ||
1336*0Sstevel@tonic-gate 			    fbuf[k] == '\t' || fbuf[k] == '{' ||
1337*0Sstevel@tonic-gate 			    fbuf[k] == '/') {
1338*0Sstevel@tonic-gate 				i = k;
1339*0Sstevel@tonic-gate 				return (1);
1340*0Sstevel@tonic-gate 			}
1341*0Sstevel@tonic-gate 	}
1342*0Sstevel@tonic-gate 	return (0);
1343*0Sstevel@tonic-gate }
1344*0Sstevel@tonic-gate 
1345*0Sstevel@tonic-gate /*
1346*0Sstevel@tonic-gate  * ccom -
1347*0Sstevel@tonic-gate  * Increments the current index 'i' into the file buffer 'fbuf' past any
1348*0Sstevel@tonic-gate  * whitespace lines and C-style comments found, starting at the current
1349*0Sstevel@tonic-gate  * position of 'i'.  Returns 1 as long as we don't increment i past the
1350*0Sstevel@tonic-gate  * size of fbuf (fbsz).  Otherwise, returns 0.
1351*0Sstevel@tonic-gate  */
1352*0Sstevel@tonic-gate 
1353*0Sstevel@tonic-gate static int
1354*0Sstevel@tonic-gate ccom(void)
1355*0Sstevel@tonic-gate {
1356*0Sstevel@tonic-gate 	register char	cc;
1357*0Sstevel@tonic-gate 	int		len;
1358*0Sstevel@tonic-gate 
1359*0Sstevel@tonic-gate 	while ((cc = fbuf[i]) == ' ' || cc == '\t' || cc == '\n')
1360*0Sstevel@tonic-gate 		if (i++ >= fbsz)
1361*0Sstevel@tonic-gate 			return (0);
1362*0Sstevel@tonic-gate 	if (fbuf[i] == '/' && fbuf[i+1] == '*') {
1363*0Sstevel@tonic-gate 		i += 2;
1364*0Sstevel@tonic-gate 		while (fbuf[i] != '*' || fbuf[i+1] != '/') {
1365*0Sstevel@tonic-gate 			if (fbuf[i] == '\\')
1366*0Sstevel@tonic-gate 				i++;
1367*0Sstevel@tonic-gate 			if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
1368*0Sstevel@tonic-gate 				len = 1;
1369*0Sstevel@tonic-gate 			i += len;
1370*0Sstevel@tonic-gate 			if (i >= fbsz)
1371*0Sstevel@tonic-gate 				return (0);
1372*0Sstevel@tonic-gate 		}
1373*0Sstevel@tonic-gate 		if ((i += 2) >= fbsz)
1374*0Sstevel@tonic-gate 			return (0);
1375*0Sstevel@tonic-gate 	}
1376*0Sstevel@tonic-gate 	if (fbuf[i] == '\n')
1377*0Sstevel@tonic-gate 		if (ccom() == 0)
1378*0Sstevel@tonic-gate 			return (0);
1379*0Sstevel@tonic-gate 	return (1);
1380*0Sstevel@tonic-gate }
1381*0Sstevel@tonic-gate 
1382*0Sstevel@tonic-gate /*
1383*0Sstevel@tonic-gate  * ascom -
1384*0Sstevel@tonic-gate  * Increments the current index 'i' into the file buffer 'fbuf' past
1385*0Sstevel@tonic-gate  * consecutive assembler program comment lines starting with ASCOMCHAR,
1386*0Sstevel@tonic-gate  * starting at the current position of 'i'.
1387*0Sstevel@tonic-gate  * Returns 1 as long as we don't increment i past the
1388*0Sstevel@tonic-gate  * size of fbuf (fbsz).  Otherwise returns 0.
1389*0Sstevel@tonic-gate  */
1390*0Sstevel@tonic-gate 
1391*0Sstevel@tonic-gate static int
1392*0Sstevel@tonic-gate ascom(void)
1393*0Sstevel@tonic-gate {
1394*0Sstevel@tonic-gate 	while (fbuf[i] == ASCOMCHAR) {
1395*0Sstevel@tonic-gate 		i++;
1396*0Sstevel@tonic-gate 		while (fbuf[i++] != '\n')
1397*0Sstevel@tonic-gate 			if (i >= fbsz)
1398*0Sstevel@tonic-gate 				return (0);
1399*0Sstevel@tonic-gate 		while (fbuf[i] == '\n')
1400*0Sstevel@tonic-gate 			if (i++ >= fbsz)
1401*0Sstevel@tonic-gate 				return (0);
1402*0Sstevel@tonic-gate 	}
1403*0Sstevel@tonic-gate 	return (1);
1404*0Sstevel@tonic-gate }
1405*0Sstevel@tonic-gate 
1406*0Sstevel@tonic-gate static int
1407*0Sstevel@tonic-gate sccs(void)
1408*0Sstevel@tonic-gate {				/* look for "1hddddd" where d is a digit */
1409*0Sstevel@tonic-gate 	register int j;
1410*0Sstevel@tonic-gate 
1411*0Sstevel@tonic-gate 	if (fbuf[0] == 1 && fbuf[1] == 'h') {
1412*0Sstevel@tonic-gate 		for (j = 2; j <= 6; j++) {
1413*0Sstevel@tonic-gate 			if (isdigit(fbuf[j]))
1414*0Sstevel@tonic-gate 				continue;
1415*0Sstevel@tonic-gate 			else
1416*0Sstevel@tonic-gate 				return (0);
1417*0Sstevel@tonic-gate 		}
1418*0Sstevel@tonic-gate 	} else {
1419*0Sstevel@tonic-gate 		return (0);
1420*0Sstevel@tonic-gate 	}
1421*0Sstevel@tonic-gate 	return (1);
1422*0Sstevel@tonic-gate }
1423*0Sstevel@tonic-gate 
1424*0Sstevel@tonic-gate static int
1425*0Sstevel@tonic-gate english(char *bp, int n)
1426*0Sstevel@tonic-gate {
1427*0Sstevel@tonic-gate #define	NASC 128		/* number of ascii char ?? */
1428*0Sstevel@tonic-gate 	register int	j, vow, freq, rare, len;
1429*0Sstevel@tonic-gate 	register int	badpun = 0, punct = 0;
1430*0Sstevel@tonic-gate 	int	ct[NASC];
1431*0Sstevel@tonic-gate 
1432*0Sstevel@tonic-gate 	if (n < 50)
1433*0Sstevel@tonic-gate 		return (0); /* no point in statistics on squibs */
1434*0Sstevel@tonic-gate 	for (j = 0; j < NASC; j++)
1435*0Sstevel@tonic-gate 		ct[j] = 0;
1436*0Sstevel@tonic-gate 	for (j = 0; j < n; j += len) {
1437*0Sstevel@tonic-gate 		if ((unsigned char)bp[j] < NASC)
1438*0Sstevel@tonic-gate 			ct[bp[j]|040]++;
1439*0Sstevel@tonic-gate 		switch (bp[j]) {
1440*0Sstevel@tonic-gate 		case '.':
1441*0Sstevel@tonic-gate 		case ',':
1442*0Sstevel@tonic-gate 		case ')':
1443*0Sstevel@tonic-gate 		case '%':
1444*0Sstevel@tonic-gate 		case ';':
1445*0Sstevel@tonic-gate 		case ':':
1446*0Sstevel@tonic-gate 		case '?':
1447*0Sstevel@tonic-gate 			punct++;
1448*0Sstevel@tonic-gate 			if (j < n-1 && bp[j+1] != ' ' && bp[j+1] != '\n')
1449*0Sstevel@tonic-gate 				badpun++;
1450*0Sstevel@tonic-gate 		}
1451*0Sstevel@tonic-gate 		if ((len = mblen(&bp[j], MB_CUR_MAX)) <= 0)
1452*0Sstevel@tonic-gate 			len = 1;
1453*0Sstevel@tonic-gate 	}
1454*0Sstevel@tonic-gate 	if (badpun*5 > punct)
1455*0Sstevel@tonic-gate 		return (0);
1456*0Sstevel@tonic-gate 	vow = ct['a'] + ct['e'] + ct['i'] + ct['o'] + ct['u'];
1457*0Sstevel@tonic-gate 	freq = ct['e'] + ct['t'] + ct['a'] + ct['i'] + ct['o'] + ct['n'];
1458*0Sstevel@tonic-gate 	rare = ct['v'] + ct['j'] + ct['k'] + ct['q'] + ct['x'] + ct['z'];
1459*0Sstevel@tonic-gate 	if (2*ct[';'] > ct['e'])
1460*0Sstevel@tonic-gate 		return (0);
1461*0Sstevel@tonic-gate 	if ((ct['>'] + ct['<'] + ct['/']) > ct['e'])
1462*0Sstevel@tonic-gate 		return (0);	/* shell file test */
1463*0Sstevel@tonic-gate 	return (vow * 5 >= n - ct[' '] && freq >= 10 * rare);
1464*0Sstevel@tonic-gate }
1465*0Sstevel@tonic-gate 
1466*0Sstevel@tonic-gate /*
1467*0Sstevel@tonic-gate  * Convert a word from an elf file to native format.
1468*0Sstevel@tonic-gate  * This is needed because there's no elf routine to
1469*0Sstevel@tonic-gate  * get and decode a Note section header.
1470*0Sstevel@tonic-gate  */
1471*0Sstevel@tonic-gate static void
1472*0Sstevel@tonic-gate convert_gelf_word(Elf *elf, GElf_Word *data, int version, int format)
1473*0Sstevel@tonic-gate {
1474*0Sstevel@tonic-gate 	Elf_Data src, dst;
1475*0Sstevel@tonic-gate 
1476*0Sstevel@tonic-gate 	dst.d_buf = data;
1477*0Sstevel@tonic-gate 	dst.d_version = version;
1478*0Sstevel@tonic-gate 	dst.d_size = sizeof (GElf_Word);
1479*0Sstevel@tonic-gate 	dst.d_type = ELF_T_WORD;
1480*0Sstevel@tonic-gate 	src.d_buf = data;
1481*0Sstevel@tonic-gate 	src.d_version = version;
1482*0Sstevel@tonic-gate 	src.d_size = sizeof (GElf_Word);
1483*0Sstevel@tonic-gate 	src.d_type = ELF_T_WORD;
1484*0Sstevel@tonic-gate 	(void) gelf_xlatetom(elf, &dst, &src, format);
1485*0Sstevel@tonic-gate }
1486*0Sstevel@tonic-gate 
1487*0Sstevel@tonic-gate static void
1488*0Sstevel@tonic-gate convert_gelf_nhdr(Elf *elf, GElf_Nhdr *nhdr, GElf_Word version, int format)
1489*0Sstevel@tonic-gate {
1490*0Sstevel@tonic-gate 	convert_gelf_word(elf, &nhdr->n_namesz, version, format);
1491*0Sstevel@tonic-gate 	convert_gelf_word(elf, &nhdr->n_descsz, version, format);
1492*0Sstevel@tonic-gate 	convert_gelf_word(elf, &nhdr->n_type, version, format);
1493*0Sstevel@tonic-gate }
1494*0Sstevel@tonic-gate 
1495*0Sstevel@tonic-gate /*
1496*0Sstevel@tonic-gate  * Return true if it is an old (pre-restructured /proc) core file.
1497*0Sstevel@tonic-gate  */
1498*0Sstevel@tonic-gate static int
1499*0Sstevel@tonic-gate old_core(Elf *elf, GElf_Ehdr *ehdr, int format)
1500*0Sstevel@tonic-gate {
1501*0Sstevel@tonic-gate 	register int inx;
1502*0Sstevel@tonic-gate 	GElf_Phdr phdr;
1503*0Sstevel@tonic-gate 	GElf_Phdr nphdr;
1504*0Sstevel@tonic-gate 	GElf_Nhdr nhdr;
1505*0Sstevel@tonic-gate 	off_t offset;
1506*0Sstevel@tonic-gate 
1507*0Sstevel@tonic-gate 	if (ehdr->e_type != ET_CORE)
1508*0Sstevel@tonic-gate 		return (0);
1509*0Sstevel@tonic-gate 	for (inx = 0; inx < (int)ehdr->e_phnum; inx++) {
1510*0Sstevel@tonic-gate 		if (gelf_getphdr(elf, inx, &phdr) == NULL) {
1511*0Sstevel@tonic-gate 			return (0);
1512*0Sstevel@tonic-gate 		}
1513*0Sstevel@tonic-gate 		if (phdr.p_type == PT_NOTE) {
1514*0Sstevel@tonic-gate 			/*
1515*0Sstevel@tonic-gate 			 * If the next segment is also a note, use it instead.
1516*0Sstevel@tonic-gate 			 */
1517*0Sstevel@tonic-gate 			if (gelf_getphdr(elf, inx+1, &nphdr) == NULL) {
1518*0Sstevel@tonic-gate 				return (0);
1519*0Sstevel@tonic-gate 			}
1520*0Sstevel@tonic-gate 			if (nphdr.p_type == PT_NOTE)
1521*0Sstevel@tonic-gate 				phdr = nphdr;
1522*0Sstevel@tonic-gate 			offset = (off_t)phdr.p_offset;
1523*0Sstevel@tonic-gate 			(void) pread(ifd, &nhdr, sizeof (GElf_Nhdr), offset);
1524*0Sstevel@tonic-gate 			convert_gelf_nhdr(elf, &nhdr, ehdr->e_version, format);
1525*0Sstevel@tonic-gate 			/*
1526*0Sstevel@tonic-gate 			 * Old core files have type NT_PRPSINFO.
1527*0Sstevel@tonic-gate 			 */
1528*0Sstevel@tonic-gate 			if (nhdr.n_type == NT_PRPSINFO)
1529*0Sstevel@tonic-gate 				return (1);
1530*0Sstevel@tonic-gate 			return (0);
1531*0Sstevel@tonic-gate 		}
1532*0Sstevel@tonic-gate 	}
1533*0Sstevel@tonic-gate 	return (0);
1534*0Sstevel@tonic-gate }
1535*0Sstevel@tonic-gate 
1536*0Sstevel@tonic-gate /*
1537*0Sstevel@tonic-gate  * If it's a core file, print out the name of the file that dumped core.
1538*0Sstevel@tonic-gate  */
1539*0Sstevel@tonic-gate static int
1540*0Sstevel@tonic-gate core(Elf *elf, GElf_Ehdr *ehdr, int format)
1541*0Sstevel@tonic-gate {
1542*0Sstevel@tonic-gate 	register int inx;
1543*0Sstevel@tonic-gate 	char *psinfo;
1544*0Sstevel@tonic-gate 	GElf_Phdr phdr;
1545*0Sstevel@tonic-gate 	GElf_Phdr nphdr;
1546*0Sstevel@tonic-gate 	GElf_Nhdr nhdr;
1547*0Sstevel@tonic-gate 	off_t offset;
1548*0Sstevel@tonic-gate 
1549*0Sstevel@tonic-gate 	if (ehdr->e_type != ET_CORE)
1550*0Sstevel@tonic-gate 		return (0);
1551*0Sstevel@tonic-gate 	for (inx = 0; inx < (int)ehdr->e_phnum; inx++) {
1552*0Sstevel@tonic-gate 		if (gelf_getphdr(elf, inx, &phdr) == NULL) {
1553*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1554*0Sstevel@tonic-gate 				gettext("can't read program header\n"));
1555*0Sstevel@tonic-gate 			return (0);
1556*0Sstevel@tonic-gate 		}
1557*0Sstevel@tonic-gate 		if (phdr.p_type == PT_NOTE) {
1558*0Sstevel@tonic-gate 			char *fname;
1559*0Sstevel@tonic-gate 			size_t size;
1560*0Sstevel@tonic-gate 			/*
1561*0Sstevel@tonic-gate 			 * If the next segment is also a note, use it instead.
1562*0Sstevel@tonic-gate 			 */
1563*0Sstevel@tonic-gate 			if (gelf_getphdr(elf, inx+1, &nphdr) == NULL) {
1564*0Sstevel@tonic-gate 				(void) fprintf(stderr,
1565*0Sstevel@tonic-gate 				    gettext("can't read program header\n"));
1566*0Sstevel@tonic-gate 				return (0);
1567*0Sstevel@tonic-gate 			}
1568*0Sstevel@tonic-gate 			if (nphdr.p_type == PT_NOTE)
1569*0Sstevel@tonic-gate 				phdr = nphdr;
1570*0Sstevel@tonic-gate 			offset = (off_t)phdr.p_offset;
1571*0Sstevel@tonic-gate 			(void) pread(ifd, &nhdr, sizeof (GElf_Nhdr), offset);
1572*0Sstevel@tonic-gate 			convert_gelf_nhdr(elf, &nhdr, ehdr->e_version, format);
1573*0Sstevel@tonic-gate 			/*
1574*0Sstevel@tonic-gate 			 * Note: the ABI states that n_namesz must
1575*0Sstevel@tonic-gate 			 * be rounded up to a 4 byte boundary.
1576*0Sstevel@tonic-gate 			 */
1577*0Sstevel@tonic-gate 			offset += sizeof (GElf_Nhdr) +
1578*0Sstevel@tonic-gate 			    ((nhdr.n_namesz + 0x03) & ~0x3);
1579*0Sstevel@tonic-gate 			size = nhdr.n_descsz;
1580*0Sstevel@tonic-gate 			psinfo = malloc(size);
1581*0Sstevel@tonic-gate 			(void) pread(ifd, psinfo, size, offset);
1582*0Sstevel@tonic-gate 			/*
1583*0Sstevel@tonic-gate 			 * We want to print the string contained
1584*0Sstevel@tonic-gate 			 * in psinfo->pr_fname[], where 'psinfo'
1585*0Sstevel@tonic-gate 			 * is either an old NT_PRPSINFO structure
1586*0Sstevel@tonic-gate 			 * or a new NT_PSINFO structure.
1587*0Sstevel@tonic-gate 			 *
1588*0Sstevel@tonic-gate 			 * Old core files have only type NT_PRPSINFO.
1589*0Sstevel@tonic-gate 			 * New core files have type NT_PSINFO.
1590*0Sstevel@tonic-gate 			 *
1591*0Sstevel@tonic-gate 			 * These structures are also different by
1592*0Sstevel@tonic-gate 			 * virtue of being contained in a core file
1593*0Sstevel@tonic-gate 			 * of either 32-bit or 64-bit type.
1594*0Sstevel@tonic-gate 			 *
1595*0Sstevel@tonic-gate 			 * To further complicate matters, we ourself
1596*0Sstevel@tonic-gate 			 * might be compiled either 32-bit or 64-bit.
1597*0Sstevel@tonic-gate 			 *
1598*0Sstevel@tonic-gate 			 * For these reason, we just *know* the offsets of
1599*0Sstevel@tonic-gate 			 * pr_fname[] into the four different structures
1600*0Sstevel@tonic-gate 			 * here, regardless of how we are compiled.
1601*0Sstevel@tonic-gate 			 */
1602*0Sstevel@tonic-gate 			if (gelf_getclass(elf) == ELFCLASS32) {
1603*0Sstevel@tonic-gate 				/* 32-bit core file, 32-bit structures */
1604*0Sstevel@tonic-gate 				if (nhdr.n_type == NT_PSINFO)
1605*0Sstevel@tonic-gate 					fname = psinfo + 88;
1606*0Sstevel@tonic-gate 				else	/* old: NT_PRPSINFO */
1607*0Sstevel@tonic-gate 					fname = psinfo + 84;
1608*0Sstevel@tonic-gate 			} else if (gelf_getclass(elf) == ELFCLASS64) {
1609*0Sstevel@tonic-gate 				/* 64-bit core file, 64-bit structures */
1610*0Sstevel@tonic-gate 				if (nhdr.n_type == NT_PSINFO)
1611*0Sstevel@tonic-gate 					fname = psinfo + 136;
1612*0Sstevel@tonic-gate 				else	/* old: NT_PRPSINFO */
1613*0Sstevel@tonic-gate 					fname = psinfo + 120;
1614*0Sstevel@tonic-gate 			} else {
1615*0Sstevel@tonic-gate 				free(psinfo);
1616*0Sstevel@tonic-gate 				break;
1617*0Sstevel@tonic-gate 			}
1618*0Sstevel@tonic-gate 			(void) printf(gettext(", from '%s'"), fname);
1619*0Sstevel@tonic-gate 			free(psinfo);
1620*0Sstevel@tonic-gate 			break;
1621*0Sstevel@tonic-gate 		}
1622*0Sstevel@tonic-gate 	}
1623*0Sstevel@tonic-gate 	return (1);
1624*0Sstevel@tonic-gate }
1625*0Sstevel@tonic-gate 
1626*0Sstevel@tonic-gate static int
1627*0Sstevel@tonic-gate shellscript(char buf[], struct stat64 *sb)
1628*0Sstevel@tonic-gate {
1629*0Sstevel@tonic-gate 	char *tp, *cp, *xp, *up, *gp;
1630*0Sstevel@tonic-gate 
1631*0Sstevel@tonic-gate 	cp = strchr(buf, '\n');
1632*0Sstevel@tonic-gate 	if (cp == NULL || cp - fbuf > fbsz)
1633*0Sstevel@tonic-gate 		return (0);
1634*0Sstevel@tonic-gate 	for (tp = buf; tp != cp && isspace((unsigned char)*tp); tp++)
1635*0Sstevel@tonic-gate 		if (!isascii(*tp))
1636*0Sstevel@tonic-gate 			return (0);
1637*0Sstevel@tonic-gate 	for (xp = tp; tp != cp && !isspace((unsigned char)*tp); tp++)
1638*0Sstevel@tonic-gate 		if (!isascii(*tp))
1639*0Sstevel@tonic-gate 			return (0);
1640*0Sstevel@tonic-gate 	if (tp == xp)
1641*0Sstevel@tonic-gate 		return (0);
1642*0Sstevel@tonic-gate 	if (sb->st_mode & S_ISUID)
1643*0Sstevel@tonic-gate 		up = gettext("set-uid ");
1644*0Sstevel@tonic-gate 	else
1645*0Sstevel@tonic-gate 		up = "";
1646*0Sstevel@tonic-gate 
1647*0Sstevel@tonic-gate 	if (sb->st_mode & S_ISGID)
1648*0Sstevel@tonic-gate 		gp = gettext("set-gid ");
1649*0Sstevel@tonic-gate 	else
1650*0Sstevel@tonic-gate 		gp = "";
1651*0Sstevel@tonic-gate 
1652*0Sstevel@tonic-gate 	if (strncmp(xp, "/bin/sh", tp - xp) == 0)
1653*0Sstevel@tonic-gate 		xp = gettext("shell");
1654*0Sstevel@tonic-gate 	else if (strncmp(xp, "/bin/csh", tp - xp) == 0)
1655*0Sstevel@tonic-gate 		xp = gettext("c-shell");
1656*0Sstevel@tonic-gate 	else if (strncmp(xp, "/usr/sbin/dtrace", tp - xp) == 0)
1657*0Sstevel@tonic-gate 		xp = gettext("DTrace");
1658*0Sstevel@tonic-gate 	else
1659*0Sstevel@tonic-gate 		*tp = '\0';
1660*0Sstevel@tonic-gate 	/*
1661*0Sstevel@tonic-gate 	 * TRANSLATION_NOTE
1662*0Sstevel@tonic-gate 	 * This message is printed by file command for shell scripts.
1663*0Sstevel@tonic-gate 	 * The first %s is for the translation for "set-uid " (if the script
1664*0Sstevel@tonic-gate 	 *   has the set-uid bit set), or is for an empty string (if the
1665*0Sstevel@tonic-gate 	 *   script does not have the set-uid bit set).
1666*0Sstevel@tonic-gate 	 * Similarly, the second %s is for the translation for "set-gid ",
1667*0Sstevel@tonic-gate 	 *   or is for an empty string.
1668*0Sstevel@tonic-gate 	 * The third %s is for the translation for either: "shell", "c-shell",
1669*0Sstevel@tonic-gate 	 *   or "DTrace", or is for the pathname of the program the script
1670*0Sstevel@tonic-gate 	 *   executes.
1671*0Sstevel@tonic-gate 	 */
1672*0Sstevel@tonic-gate 	(void) printf(gettext("%s%sexecutable %s script\n"), up, gp, xp);
1673*0Sstevel@tonic-gate 	return (1);
1674*0Sstevel@tonic-gate }
1675*0Sstevel@tonic-gate 
1676*0Sstevel@tonic-gate static int
1677*0Sstevel@tonic-gate get_door_target(char *file, char *buf, size_t bufsize)
1678*0Sstevel@tonic-gate {
1679*0Sstevel@tonic-gate 	int fd;
1680*0Sstevel@tonic-gate 	door_info_t di;
1681*0Sstevel@tonic-gate 	psinfo_t psinfo;
1682*0Sstevel@tonic-gate 
1683*0Sstevel@tonic-gate 	if ((fd = open64(file, O_RDONLY)) < 0 ||
1684*0Sstevel@tonic-gate 	    door_info(fd, &di) != 0) {
1685*0Sstevel@tonic-gate 		if (fd >= 0)
1686*0Sstevel@tonic-gate 			(void) close(fd);
1687*0Sstevel@tonic-gate 		return (-1);
1688*0Sstevel@tonic-gate 	}
1689*0Sstevel@tonic-gate 	(void) close(fd);
1690*0Sstevel@tonic-gate 
1691*0Sstevel@tonic-gate 	(void) sprintf(buf, "/proc/%ld/psinfo", di.di_target);
1692*0Sstevel@tonic-gate 	if ((fd = open64(buf, O_RDONLY)) < 0 ||
1693*0Sstevel@tonic-gate 	    read(fd, &psinfo, sizeof (psinfo)) != sizeof (psinfo)) {
1694*0Sstevel@tonic-gate 		if (fd >= 0)
1695*0Sstevel@tonic-gate 			(void) close(fd);
1696*0Sstevel@tonic-gate 		return (-1);
1697*0Sstevel@tonic-gate 	}
1698*0Sstevel@tonic-gate 	(void) close(fd);
1699*0Sstevel@tonic-gate 
1700*0Sstevel@tonic-gate 	(void) snprintf(buf, bufsize, "%s[%ld]", psinfo.pr_fname, di.di_target);
1701*0Sstevel@tonic-gate 	return (0);
1702*0Sstevel@tonic-gate }
1703*0Sstevel@tonic-gate 
1704*0Sstevel@tonic-gate /*
1705*0Sstevel@tonic-gate  * ZIP file header information
1706*0Sstevel@tonic-gate  */
1707*0Sstevel@tonic-gate #define	SIGSIZ		4
1708*0Sstevel@tonic-gate #define	LOCSIG		"PK\003\004"
1709*0Sstevel@tonic-gate #define	LOCHDRSIZ	30
1710*0Sstevel@tonic-gate 
1711*0Sstevel@tonic-gate #define	CH(b, n)	(((unsigned char *)(b))[n])
1712*0Sstevel@tonic-gate #define	SH(b, n)	(CH(b, n) | (CH(b, n+1) << 8))
1713*0Sstevel@tonic-gate #define	LG(b, n)	(SH(b, n) | (SH(b, n+2) << 16))
1714*0Sstevel@tonic-gate 
1715*0Sstevel@tonic-gate #define	LOCNAM(b)	(SH(b, 26))	/* filename size */
1716*0Sstevel@tonic-gate #define	LOCEXT(b)	(SH(b, 28))	/* extra field size */
1717*0Sstevel@tonic-gate 
1718*0Sstevel@tonic-gate #define	XFHSIZ		4		/* header id, data size */
1719*0Sstevel@tonic-gate #define	XFHID(b)	(SH(b, 0))	/* extract field header id */
1720*0Sstevel@tonic-gate #define	XFDATASIZ(b)	(SH(b, 2))	/* extract field data size */
1721*0Sstevel@tonic-gate #define	XFJAVASIG	0xcafe		/* java executables */
1722*0Sstevel@tonic-gate 
1723*0Sstevel@tonic-gate static int
1724*0Sstevel@tonic-gate zipfile(char *fbuf, int fd)
1725*0Sstevel@tonic-gate {
1726*0Sstevel@tonic-gate 	off_t xoff, xoff_end;
1727*0Sstevel@tonic-gate 
1728*0Sstevel@tonic-gate 	if (strncmp(fbuf, LOCSIG, SIGSIZ) != 0)
1729*0Sstevel@tonic-gate 		return (0);
1730*0Sstevel@tonic-gate 
1731*0Sstevel@tonic-gate 	xoff = LOCHDRSIZ + LOCNAM(fbuf);
1732*0Sstevel@tonic-gate 	xoff_end = xoff + LOCEXT(fbuf);
1733*0Sstevel@tonic-gate 
1734*0Sstevel@tonic-gate 	while (xoff < xoff_end) {
1735*0Sstevel@tonic-gate 		char xfhdr[XFHSIZ];
1736*0Sstevel@tonic-gate 
1737*0Sstevel@tonic-gate 		if (pread(fd, xfhdr, XFHSIZ, xoff) != XFHSIZ)
1738*0Sstevel@tonic-gate 			break;
1739*0Sstevel@tonic-gate 
1740*0Sstevel@tonic-gate 		if (XFHID(xfhdr) == XFJAVASIG) {
1741*0Sstevel@tonic-gate 			(void) printf("%s\n", gettext("java program"));
1742*0Sstevel@tonic-gate 			return (1);
1743*0Sstevel@tonic-gate 		}
1744*0Sstevel@tonic-gate 		xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr);
1745*0Sstevel@tonic-gate 	}
1746*0Sstevel@tonic-gate 
1747*0Sstevel@tonic-gate 	/*
1748*0Sstevel@tonic-gate 	 * We could just print "ZIP archive" here.
1749*0Sstevel@tonic-gate 	 *
1750*0Sstevel@tonic-gate 	 * However, customers may be using their own entries in
1751*0Sstevel@tonic-gate 	 * /etc/magic to distinguish one kind of ZIP file from another, so
1752*0Sstevel@tonic-gate 	 * let's defer the printing of "ZIP archive" to there.
1753*0Sstevel@tonic-gate 	 */
1754*0Sstevel@tonic-gate 	return (0);
1755*0Sstevel@tonic-gate }
1756*0Sstevel@tonic-gate 
1757*0Sstevel@tonic-gate static int
1758*0Sstevel@tonic-gate is_crash_dump(const char *buf, int fd)
1759*0Sstevel@tonic-gate {
1760*0Sstevel@tonic-gate 	/* LINTED: pointer cast may result in improper alignment */
1761*0Sstevel@tonic-gate 	const dumphdr_t *dhp = (const dumphdr_t *)buf;
1762*0Sstevel@tonic-gate 
1763*0Sstevel@tonic-gate 	/*
1764*0Sstevel@tonic-gate 	 * The current DUMP_MAGIC string covers Solaris 7 and later releases.
1765*0Sstevel@tonic-gate 	 * The utsname struct is only present in dumphdr_t's with dump_version
1766*0Sstevel@tonic-gate 	 * greater than or equal to 9.
1767*0Sstevel@tonic-gate 	 */
1768*0Sstevel@tonic-gate 	if (dhp->dump_magic == DUMP_MAGIC) {
1769*0Sstevel@tonic-gate 		print_dumphdr(fd, dhp, return_uint32, NATIVE_ISA);
1770*0Sstevel@tonic-gate 
1771*0Sstevel@tonic-gate 	} else if (dhp->dump_magic == swap_uint32(DUMP_MAGIC)) {
1772*0Sstevel@tonic-gate 		print_dumphdr(fd, dhp, swap_uint32, OTHER_ISA);
1773*0Sstevel@tonic-gate 
1774*0Sstevel@tonic-gate 	} else if (dhp->dump_magic == OLD_DUMP_MAGIC ||
1775*0Sstevel@tonic-gate 	    dhp->dump_magic == swap_uint32(OLD_DUMP_MAGIC)) {
1776*0Sstevel@tonic-gate 		char *isa = (dhp->dump_magic == OLD_DUMP_MAGIC ?
1777*0Sstevel@tonic-gate 		    NATIVE_ISA : OTHER_ISA);
1778*0Sstevel@tonic-gate 		(void) printf(gettext("SunOS 32-bit %s crash dump\n"), isa);
1779*0Sstevel@tonic-gate 
1780*0Sstevel@tonic-gate 	} else {
1781*0Sstevel@tonic-gate 		return (0);
1782*0Sstevel@tonic-gate 	}
1783*0Sstevel@tonic-gate 
1784*0Sstevel@tonic-gate 	return (1);
1785*0Sstevel@tonic-gate }
1786*0Sstevel@tonic-gate 
1787*0Sstevel@tonic-gate static void
1788*0Sstevel@tonic-gate print_dumphdr(const int fd, const dumphdr_t *dhp, uint32_t (*swap)(uint32_t),
1789*0Sstevel@tonic-gate     const char *isa)
1790*0Sstevel@tonic-gate {
1791*0Sstevel@tonic-gate 	dumphdr_t dh;
1792*0Sstevel@tonic-gate 
1793*0Sstevel@tonic-gate 	/*
1794*0Sstevel@tonic-gate 	 * A dumphdr_t is bigger than FBSZ, so we have to manually read the
1795*0Sstevel@tonic-gate 	 * rest of it.
1796*0Sstevel@tonic-gate 	 */
1797*0Sstevel@tonic-gate 	if (swap(dhp->dump_version) > 8 && pread(fd, &dh, sizeof (dumphdr_t),
1798*0Sstevel@tonic-gate 	    (off_t)0) == sizeof (dumphdr_t)) {
1799*0Sstevel@tonic-gate 		(void) printf(gettext(
1800*0Sstevel@tonic-gate 		    "%s %s %s %u-bit %s crash dump from '%s'\n"),
1801*0Sstevel@tonic-gate 		    dh.dump_utsname.sysname, dh.dump_utsname.release,
1802*0Sstevel@tonic-gate 		    dh.dump_utsname.version, swap(dh.dump_wordsize), isa,
1803*0Sstevel@tonic-gate 		    dh.dump_utsname.nodename);
1804*0Sstevel@tonic-gate 	} else {
1805*0Sstevel@tonic-gate 		(void) printf(gettext("SunOS %u-bit %s crash dump\n"),
1806*0Sstevel@tonic-gate 		    swap(dhp->dump_wordsize), isa);
1807*0Sstevel@tonic-gate 	}
1808*0Sstevel@tonic-gate }
1809*0Sstevel@tonic-gate 
1810*0Sstevel@tonic-gate static void
1811*0Sstevel@tonic-gate usage(void)
1812*0Sstevel@tonic-gate {
1813*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
1814*0Sstevel@tonic-gate 		"usage: file [-dh] [-M mfile] [-m mfile] [-f ffile] file ...\n"
1815*0Sstevel@tonic-gate 		"       file [-dh] [-M mfile] [-m mfile] -f ffile\n"
1816*0Sstevel@tonic-gate 		"       file -i [-h] [-f ffile] file ...\n"
1817*0Sstevel@tonic-gate 		"       file -i [-h] -f ffile\n"
1818*0Sstevel@tonic-gate 		"       file -c [-d] [-M mfile] [-m mfile]\n"));
1819*0Sstevel@tonic-gate 	exit(2);
1820*0Sstevel@tonic-gate }
1821*0Sstevel@tonic-gate 
1822*0Sstevel@tonic-gate static uint32_t
1823*0Sstevel@tonic-gate swap_uint32(uint32_t in)
1824*0Sstevel@tonic-gate {
1825*0Sstevel@tonic-gate 	uint32_t out;
1826*0Sstevel@tonic-gate 
1827*0Sstevel@tonic-gate 	out = (in & 0x000000ff) << 24;
1828*0Sstevel@tonic-gate 	out |= (in & 0x0000ff00) << 8; /* >> 8 << 16 */
1829*0Sstevel@tonic-gate 	out |= (in & 0x00ff0000) >> 8; /* >> 16 << 8 */
1830*0Sstevel@tonic-gate 	out |= (in & 0xff000000) >> 24;
1831*0Sstevel@tonic-gate 
1832*0Sstevel@tonic-gate 	return (out);
1833*0Sstevel@tonic-gate }
1834*0Sstevel@tonic-gate 
1835*0Sstevel@tonic-gate static uint32_t
1836*0Sstevel@tonic-gate return_uint32(uint32_t in)
1837*0Sstevel@tonic-gate {
1838*0Sstevel@tonic-gate 	return (in);
1839*0Sstevel@tonic-gate }
1840*0Sstevel@tonic-gate 
1841*0Sstevel@tonic-gate /*
1842*0Sstevel@tonic-gate  * Check if str is in the string list str_list.
1843*0Sstevel@tonic-gate  */
1844*0Sstevel@tonic-gate static int
1845*0Sstevel@tonic-gate is_in_list(char *str_list[], char *str)
1846*0Sstevel@tonic-gate {
1847*0Sstevel@tonic-gate 	int i;
1848*0Sstevel@tonic-gate 
1849*0Sstevel@tonic-gate 	/*
1850*0Sstevel@tonic-gate 	 * Only need to compare the strlen(str_list[i]) bytes.
1851*0Sstevel@tonic-gate 	 * That way .stab will match on .stab* sections, and
1852*0Sstevel@tonic-gate 	 * .debug will match on .debug* sections.
1853*0Sstevel@tonic-gate 	 */
1854*0Sstevel@tonic-gate 	for (i = 0; str_list[i] != NULL; i++) {
1855*0Sstevel@tonic-gate 		if (strncmp(str_list[i], str, strlen(str_list[i])) == 0) {
1856*0Sstevel@tonic-gate 			return (1);
1857*0Sstevel@tonic-gate 		}
1858*0Sstevel@tonic-gate 	}
1859*0Sstevel@tonic-gate 	return (0);
1860*0Sstevel@tonic-gate }
1861*0Sstevel@tonic-gate 
1862*0Sstevel@tonic-gate /*
1863*0Sstevel@tonic-gate  * default_magic -
1864*0Sstevel@tonic-gate  *	allocate space for and create the default magic file
1865*0Sstevel@tonic-gate  *	name string.
1866*0Sstevel@tonic-gate  */
1867*0Sstevel@tonic-gate 
1868*0Sstevel@tonic-gate static void
1869*0Sstevel@tonic-gate default_magic(void)
1870*0Sstevel@tonic-gate {
1871*0Sstevel@tonic-gate 	const char *msg_locale = setlocale(LC_MESSAGES, NULL);
1872*0Sstevel@tonic-gate 	struct stat	statbuf;
1873*0Sstevel@tonic-gate 
1874*0Sstevel@tonic-gate 	if ((dfile = (char *)malloc(strlen(msg_locale) + 35)) == NULL) {
1875*0Sstevel@tonic-gate 		perror("file");
1876*0Sstevel@tonic-gate 		exit(2);
1877*0Sstevel@tonic-gate 	}
1878*0Sstevel@tonic-gate 	(void) snprintf(dfile, strlen(msg_locale) + 35,
1879*0Sstevel@tonic-gate 	    "/usr/lib/locale/%s/LC_MESSAGES/magic", msg_locale);
1880*0Sstevel@tonic-gate 	if (stat(dfile, &statbuf) != 0) {
1881*0Sstevel@tonic-gate 		(void) strcpy(dfile, "/etc/magic");
1882*0Sstevel@tonic-gate 	}
1883*0Sstevel@tonic-gate }
1884*0Sstevel@tonic-gate 
1885*0Sstevel@tonic-gate /*
1886*0Sstevel@tonic-gate  * add_to_mlist -
1887*0Sstevel@tonic-gate  *	Add the given magic_file filename string to the list of magic
1888*0Sstevel@tonic-gate  *	files (mlist).  This list of files will later be examined, and
1889*0Sstevel@tonic-gate  *	each magic file's entries will be added in order to
1890*0Sstevel@tonic-gate  *	the mtab table.
1891*0Sstevel@tonic-gate  *
1892*0Sstevel@tonic-gate  *	The first flag is set to 1 to add to the first list, mlist1.
1893*0Sstevel@tonic-gate  *	The first flag is set to 0 to add to the second list, mlist2.
1894*0Sstevel@tonic-gate  */
1895*0Sstevel@tonic-gate 
1896*0Sstevel@tonic-gate static void
1897*0Sstevel@tonic-gate add_to_mlist(char *magic_file, int first)
1898*0Sstevel@tonic-gate {
1899*0Sstevel@tonic-gate 	char	**mlist;	/* ordered list of magic files */
1900*0Sstevel@tonic-gate 	size_t	mlist_sz;	/* number of pointers allocated  for mlist */
1901*0Sstevel@tonic-gate 	char	**mlistp;	/* next entry in mlist */
1902*0Sstevel@tonic-gate 	size_t mlistp_off;
1903*0Sstevel@tonic-gate 
1904*0Sstevel@tonic-gate 	if (first) {
1905*0Sstevel@tonic-gate 		mlist = mlist1;
1906*0Sstevel@tonic-gate 		mlist_sz = mlist1_sz;
1907*0Sstevel@tonic-gate 		mlistp = mlist1p;
1908*0Sstevel@tonic-gate 	} else {
1909*0Sstevel@tonic-gate 		mlist = mlist2;
1910*0Sstevel@tonic-gate 		mlist_sz = mlist2_sz;
1911*0Sstevel@tonic-gate 		mlistp = mlist2p;
1912*0Sstevel@tonic-gate 	}
1913*0Sstevel@tonic-gate 
1914*0Sstevel@tonic-gate 	if (mlist == NULL) {	/* initial mlist allocation */
1915*0Sstevel@tonic-gate 		if ((mlist = (char **)calloc(MLIST_SZ, sizeof (char *)))
1916*0Sstevel@tonic-gate 		    == NULL) {
1917*0Sstevel@tonic-gate 			perror("file");
1918*0Sstevel@tonic-gate 			exit(2);
1919*0Sstevel@tonic-gate 		}
1920*0Sstevel@tonic-gate 		mlist_sz = MLIST_SZ;
1921*0Sstevel@tonic-gate 		mlistp = mlist;
1922*0Sstevel@tonic-gate 	}
1923*0Sstevel@tonic-gate 	if ((mlistp - mlist) >= mlist_sz) {
1924*0Sstevel@tonic-gate 		mlistp_off = mlistp - mlist;
1925*0Sstevel@tonic-gate 		mlist_sz *= 2;
1926*0Sstevel@tonic-gate 		if ((mlist = (char **)realloc(mlist,
1927*0Sstevel@tonic-gate 		    mlist_sz * sizeof (char *))) == NULL) {
1928*0Sstevel@tonic-gate 			perror("file");
1929*0Sstevel@tonic-gate 			exit(2);
1930*0Sstevel@tonic-gate 		}
1931*0Sstevel@tonic-gate 		mlistp = mlist + mlistp_off;
1932*0Sstevel@tonic-gate 	}
1933*0Sstevel@tonic-gate 	/*
1934*0Sstevel@tonic-gate 	 * now allocate memory for and copy the
1935*0Sstevel@tonic-gate 	 * magic file name string
1936*0Sstevel@tonic-gate 	 */
1937*0Sstevel@tonic-gate 	if ((*mlistp = malloc(strlen(magic_file) + 1)) == NULL) {
1938*0Sstevel@tonic-gate 		perror("file");
1939*0Sstevel@tonic-gate 		exit(2);
1940*0Sstevel@tonic-gate 	}
1941*0Sstevel@tonic-gate 	(void) strlcpy(*mlistp, magic_file, strlen(magic_file) + 1);
1942*0Sstevel@tonic-gate 	mlistp++;
1943*0Sstevel@tonic-gate 
1944*0Sstevel@tonic-gate 	if (first) {
1945*0Sstevel@tonic-gate 		mlist1 = mlist;
1946*0Sstevel@tonic-gate 		mlist1_sz = mlist_sz;
1947*0Sstevel@tonic-gate 		mlist1p = mlistp;
1948*0Sstevel@tonic-gate 	} else {
1949*0Sstevel@tonic-gate 		mlist2 = mlist;
1950*0Sstevel@tonic-gate 		mlist2_sz = mlist_sz;
1951*0Sstevel@tonic-gate 		mlist2p = mlistp;
1952*0Sstevel@tonic-gate 	}
1953*0Sstevel@tonic-gate }
1954*0Sstevel@tonic-gate 
1955*0Sstevel@tonic-gate static void
1956*0Sstevel@tonic-gate fd_cleanup(void)
1957*0Sstevel@tonic-gate {
1958*0Sstevel@tonic-gate 	if (ifd != -1) {
1959*0Sstevel@tonic-gate 		(void) close(ifd);
1960*0Sstevel@tonic-gate 		ifd = -1;
1961*0Sstevel@tonic-gate 	}
1962*0Sstevel@tonic-gate 	if (elffd != -1) {
1963*0Sstevel@tonic-gate 		(void) close(elffd);
1964*0Sstevel@tonic-gate 		elffd = -1;
1965*0Sstevel@tonic-gate 	}
1966*0Sstevel@tonic-gate }
1967