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