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