17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5c13de8f6Sab196087 * Common Development and Distribution License (the "License"). 6c13de8f6Sab196087 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 227c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */ 267c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 29*ca3e8d88SDave Plauger * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 307c478bd9Sstevel@tonic-gate * Use is subject to license terms. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #define _LARGEFILE64_SOURCE 347c478bd9Sstevel@tonic-gate 35c13de8f6Sab196087 /* Get definitions for the relocation types supported. */ 36c13de8f6Sab196087 #define ELF_TARGET_ALL 37c13de8f6Sab196087 387c478bd9Sstevel@tonic-gate #include <ctype.h> 397c478bd9Sstevel@tonic-gate #include <unistd.h> 407c478bd9Sstevel@tonic-gate #include <fcntl.h> 417c478bd9Sstevel@tonic-gate #include <signal.h> 427c478bd9Sstevel@tonic-gate #include <stdio.h> 437c478bd9Sstevel@tonic-gate #include <libelf.h> 447c478bd9Sstevel@tonic-gate #include <stdlib.h> 457c478bd9Sstevel@tonic-gate #include <limits.h> 467c478bd9Sstevel@tonic-gate #include <locale.h> 477c478bd9Sstevel@tonic-gate #include <wctype.h> 487c478bd9Sstevel@tonic-gate #include <string.h> 497c478bd9Sstevel@tonic-gate #include <errno.h> 507c478bd9Sstevel@tonic-gate #include <door.h> 517c478bd9Sstevel@tonic-gate #include <sys/param.h> 527c478bd9Sstevel@tonic-gate #include <sys/types.h> 537c478bd9Sstevel@tonic-gate #include <sys/mkdev.h> 547c478bd9Sstevel@tonic-gate #include <sys/stat.h> 557c478bd9Sstevel@tonic-gate #include <sys/elf.h> 567c478bd9Sstevel@tonic-gate #include <procfs.h> 577c478bd9Sstevel@tonic-gate #include <sys/core.h> 587c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h> 597c478bd9Sstevel@tonic-gate #include <netinet/in.h> 607c478bd9Sstevel@tonic-gate #include <gelf.h> 617c478bd9Sstevel@tonic-gate #include <elfcap.h> 62c13de8f6Sab196087 #include <sgsrtcid.h> 637c478bd9Sstevel@tonic-gate #include "file.h" 64c2c65e21Sny155746 #include "elf_read.h" 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate /* 677c478bd9Sstevel@tonic-gate * Misc 687c478bd9Sstevel@tonic-gate */ 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate #define FBSZ 512 717c478bd9Sstevel@tonic-gate #define MLIST_SZ 12 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate /* 747c478bd9Sstevel@tonic-gate * The 0x8FCA0102 magic string was used in crash dumps generated by releases 757c478bd9Sstevel@tonic-gate * prior to Solaris 7. 767c478bd9Sstevel@tonic-gate */ 777c478bd9Sstevel@tonic-gate #define OLD_DUMP_MAGIC 0x8FCA0102 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate #if defined(__sparc) 807c478bd9Sstevel@tonic-gate #define NATIVE_ISA "SPARC" 817c478bd9Sstevel@tonic-gate #define OTHER_ISA "Intel" 827c478bd9Sstevel@tonic-gate #else 837c478bd9Sstevel@tonic-gate #define NATIVE_ISA "Intel" 847c478bd9Sstevel@tonic-gate #define OTHER_ISA "SPARC" 857c478bd9Sstevel@tonic-gate #endif 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate /* Assembly language comment char */ 887c478bd9Sstevel@tonic-gate #ifdef pdp11 897c478bd9Sstevel@tonic-gate #define ASCOMCHAR '/' 907c478bd9Sstevel@tonic-gate #else 917c478bd9Sstevel@tonic-gate #define ASCOMCHAR '!' 927c478bd9Sstevel@tonic-gate #endif 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate #pragma align 16(fbuf) 957c478bd9Sstevel@tonic-gate static char fbuf[FBSZ]; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* 987c478bd9Sstevel@tonic-gate * Magic file variables 997c478bd9Sstevel@tonic-gate */ 1007c478bd9Sstevel@tonic-gate static intmax_t maxmagicoffset; 1017c478bd9Sstevel@tonic-gate static intmax_t tmpmax; 1027c478bd9Sstevel@tonic-gate static char *magicbuf; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate static char *dfile; 1057c478bd9Sstevel@tonic-gate static char *troff[] = { /* new troff intermediate lang */ 1067c478bd9Sstevel@tonic-gate "x", "T", "res", "init", "font", "202", "V0", "p1", 0}; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate static char *fort[] = { /* FORTRAN */ 1097c478bd9Sstevel@tonic-gate "function", "subroutine", "common", "dimension", "block", 1107c478bd9Sstevel@tonic-gate "integer", "real", "data", "double", 1117c478bd9Sstevel@tonic-gate "FUNCTION", "SUBROUTINE", "COMMON", "DIMENSION", "BLOCK", 1127c478bd9Sstevel@tonic-gate "INTEGER", "REAL", "DATA", "DOUBLE", 0}; 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate static char *asc[] = { /* Assembler Commands */ 1157c478bd9Sstevel@tonic-gate "sys", "mov", "tst", "clr", "jmp", "cmp", "set", "inc", 1167c478bd9Sstevel@tonic-gate "dec", 0}; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate static char *c[] = { /* C Language */ 1197c478bd9Sstevel@tonic-gate "int", "char", "float", "double", "short", "long", "unsigned", 1207c478bd9Sstevel@tonic-gate "register", "static", "struct", "extern", 0}; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate static char *as[] = { /* Assembler Pseudo Ops, prepended with '.' */ 1237c478bd9Sstevel@tonic-gate "globl", "global", "ident", "file", "byte", "even", 1247c478bd9Sstevel@tonic-gate "text", "data", "bss", "comm", 0}; 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate /* 1277c478bd9Sstevel@tonic-gate * The line and debug section names are used by the strip command. 1287c478bd9Sstevel@tonic-gate * Any changes in the strip implementation need to be reflected here. 1297c478bd9Sstevel@tonic-gate */ 1307c478bd9Sstevel@tonic-gate static char *debug_sections[] = { /* Debug sections in a ELF file */ 1317c478bd9Sstevel@tonic-gate ".debug", ".stab", ".dwarf", ".line", NULL}; 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate /* start for MB env */ 1347c478bd9Sstevel@tonic-gate static wchar_t wchar; 1357c478bd9Sstevel@tonic-gate static int length; 1367c478bd9Sstevel@tonic-gate static int IS_ascii; 1377c478bd9Sstevel@tonic-gate static int Max; 1387c478bd9Sstevel@tonic-gate /* end for MB env */ 1397c478bd9Sstevel@tonic-gate static int i; /* global index into first 'fbsz' bytes of file */ 1407c478bd9Sstevel@tonic-gate static int fbsz; 1417c478bd9Sstevel@tonic-gate static int ifd = -1; 1427c478bd9Sstevel@tonic-gate static int elffd = -1; 1437c478bd9Sstevel@tonic-gate static int tret; 1447c478bd9Sstevel@tonic-gate static int hflg; 1457c478bd9Sstevel@tonic-gate static int dflg; 1467c478bd9Sstevel@tonic-gate static int mflg; 1477c478bd9Sstevel@tonic-gate static int M_flg; 1487c478bd9Sstevel@tonic-gate static int iflg; 1497c478bd9Sstevel@tonic-gate static struct stat64 mbuf; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate static char **mlist1; /* 1st ordered list of magic files */ 1527c478bd9Sstevel@tonic-gate static char **mlist2; /* 2nd ordered list of magic files */ 1537c478bd9Sstevel@tonic-gate static size_t mlist1_sz; /* number of ptrs allocated for mlist1 */ 1547c478bd9Sstevel@tonic-gate static size_t mlist2_sz; /* number of ptrs allocated for mlist2 */ 1557c478bd9Sstevel@tonic-gate static char **mlist1p; /* next entry in mlist1 */ 1567c478bd9Sstevel@tonic-gate static char **mlist2p; /* next entry in mlist2 */ 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate static ssize_t mread; 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate static void ar_coff_or_aout(int ifd); 1617c478bd9Sstevel@tonic-gate static int type(char *file); 1629a411307Srie static int def_position_tests(char *file); 1637c478bd9Sstevel@tonic-gate static void def_context_tests(void); 1647c478bd9Sstevel@tonic-gate static int troffint(char *bp, int n); 1657c478bd9Sstevel@tonic-gate static int lookup(char **tab); 1667c478bd9Sstevel@tonic-gate static int ccom(void); 1677c478bd9Sstevel@tonic-gate static int ascom(void); 1687c478bd9Sstevel@tonic-gate static int sccs(void); 1697c478bd9Sstevel@tonic-gate static int english(char *bp, int n); 1707c478bd9Sstevel@tonic-gate static int shellscript(char buf[], struct stat64 *sb); 171c2c65e21Sny155746 static int elf_check(char *file); 1727c478bd9Sstevel@tonic-gate static int get_door_target(char *, char *, size_t); 1737c478bd9Sstevel@tonic-gate static int zipfile(char *, int); 1747c478bd9Sstevel@tonic-gate static int is_crash_dump(const char *, int); 1757c478bd9Sstevel@tonic-gate static void print_dumphdr(const int, const dumphdr_t *, uint32_t (*)(uint32_t), 1767c478bd9Sstevel@tonic-gate const char *); 1777c478bd9Sstevel@tonic-gate static uint32_t swap_uint32(uint32_t); 1787c478bd9Sstevel@tonic-gate static uint32_t return_uint32(uint32_t); 1797c478bd9Sstevel@tonic-gate static void usage(void); 1807c478bd9Sstevel@tonic-gate static void default_magic(void); 1817c478bd9Sstevel@tonic-gate static void add_to_mlist(char *, int); 1827c478bd9Sstevel@tonic-gate static void fd_cleanup(void); 183c13de8f6Sab196087 static int is_rtld_config(void); 1847c478bd9Sstevel@tonic-gate 185c2c65e21Sny155746 /* from elf_read.c */ 186c2c65e21Sny155746 int elf_read32(int elffd, Elf_Info *EInfo); 187c2c65e21Sny155746 int elf_read64(int elffd, Elf_Info *EInfo); 188c2c65e21Sny155746 1897c478bd9Sstevel@tonic-gate #ifdef XPG4 1907c478bd9Sstevel@tonic-gate /* SUSv3 requires a single <space> after the colon */ 1917c478bd9Sstevel@tonic-gate #define prf(x) (void) printf("%s: ", x); 1927c478bd9Sstevel@tonic-gate #else /* !XPG4 */ 1937c478bd9Sstevel@tonic-gate #define prf(x) (void) printf("%s:%s", x, (int)strlen(x) > 6 ? "\t" : "\t\t"); 1947c478bd9Sstevel@tonic-gate #endif /* XPG4 */ 1957c478bd9Sstevel@tonic-gate 1969a411307Srie /* 1979a411307Srie * Static program identifier - used to prevent localization of the name "file" 1989a411307Srie * within individual error messages. 1999a411307Srie */ 2009a411307Srie const char *File = "file"; 2019a411307Srie 2027c478bd9Sstevel@tonic-gate int 2037c478bd9Sstevel@tonic-gate main(int argc, char **argv) 2047c478bd9Sstevel@tonic-gate { 2057c478bd9Sstevel@tonic-gate char *p; 2067c478bd9Sstevel@tonic-gate int ch; 2077c478bd9Sstevel@tonic-gate FILE *fl; 2087c478bd9Sstevel@tonic-gate int cflg = 0; 2097c478bd9Sstevel@tonic-gate int eflg = 0; 2107c478bd9Sstevel@tonic-gate int fflg = 0; 2117c478bd9Sstevel@tonic-gate char *ap = NULL; 2127c478bd9Sstevel@tonic-gate int pathlen; 2137c478bd9Sstevel@tonic-gate char **filep; 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 2167c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 2177c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 2187c478bd9Sstevel@tonic-gate #endif 2197c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate while ((ch = getopt(argc, argv, "M:cdf:him:")) != EOF) { 2227c478bd9Sstevel@tonic-gate switch (ch) { 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate case 'M': 2257c478bd9Sstevel@tonic-gate add_to_mlist(optarg, !dflg); 2267c478bd9Sstevel@tonic-gate M_flg++; 2277c478bd9Sstevel@tonic-gate break; 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate case 'c': 2307c478bd9Sstevel@tonic-gate cflg++; 2317c478bd9Sstevel@tonic-gate break; 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate case 'd': 2347c478bd9Sstevel@tonic-gate if (!dflg) { 2357c478bd9Sstevel@tonic-gate default_magic(); 2367c478bd9Sstevel@tonic-gate add_to_mlist(dfile, 0); 2377c478bd9Sstevel@tonic-gate dflg++; 2387c478bd9Sstevel@tonic-gate } 2397c478bd9Sstevel@tonic-gate break; 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate case 'f': 2427c478bd9Sstevel@tonic-gate fflg++; 2439a411307Srie errno = 0; 2447c478bd9Sstevel@tonic-gate if ((fl = fopen(optarg, "r")) == NULL) { 2459a411307Srie int err = errno; 2469a411307Srie (void) fprintf(stderr, gettext("%s: cannot " 2479a411307Srie "open file %s: %s\n"), File, optarg, 2489a411307Srie err ? strerror(err) : ""); 2497c478bd9Sstevel@tonic-gate usage(); 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate pathlen = pathconf("/", _PC_PATH_MAX); 2527c478bd9Sstevel@tonic-gate if (pathlen == -1) { 2539a411307Srie int err = errno; 2549a411307Srie (void) fprintf(stderr, gettext("%s: cannot " 2559a411307Srie "determine maximum path length: %s\n"), 2569a411307Srie File, strerror(err)); 2577c478bd9Sstevel@tonic-gate exit(1); 2587c478bd9Sstevel@tonic-gate } 2597c478bd9Sstevel@tonic-gate pathlen += 2; /* for null and newline in fgets */ 2609a411307Srie if ((ap = malloc(pathlen * sizeof (char))) == NULL) { 2619a411307Srie int err = errno; 2629a411307Srie (void) fprintf(stderr, gettext("%s: malloc " 2639a411307Srie "failed: %s\n"), File, strerror(err)); 2649a411307Srie exit(2); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate break; 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate case 'h': 2697c478bd9Sstevel@tonic-gate hflg++; 2707c478bd9Sstevel@tonic-gate break; 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate case 'i': 2737c478bd9Sstevel@tonic-gate iflg++; 2747c478bd9Sstevel@tonic-gate break; 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate case 'm': 2777c478bd9Sstevel@tonic-gate add_to_mlist(optarg, !dflg); 2787c478bd9Sstevel@tonic-gate mflg++; 2797c478bd9Sstevel@tonic-gate break; 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate case '?': 2827c478bd9Sstevel@tonic-gate eflg++; 2837c478bd9Sstevel@tonic-gate break; 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate if (!cflg && !fflg && (eflg || optind == argc)) 2877c478bd9Sstevel@tonic-gate usage(); 2887c478bd9Sstevel@tonic-gate if (iflg && (dflg || mflg || M_flg)) { 2897c478bd9Sstevel@tonic-gate usage(); 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate if (iflg && cflg) { 2927c478bd9Sstevel@tonic-gate usage(); 2937c478bd9Sstevel@tonic-gate } 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate if (!dflg && !mflg && !M_flg && !iflg) { 2967c478bd9Sstevel@tonic-gate /* no -d, -m, nor -M option; also -i option doesn't need magic */ 2977c478bd9Sstevel@tonic-gate default_magic(); 2987c478bd9Sstevel@tonic-gate if (f_mkmtab(dfile, cflg, 0) == -1) { 2997c478bd9Sstevel@tonic-gate exit(2); 3007c478bd9Sstevel@tonic-gate } 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate else if (mflg && !M_flg && !dflg) { 3047c478bd9Sstevel@tonic-gate /* -m specified without -d nor -M */ 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate #ifdef XPG4 /* For SUSv3 only */ 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate /* 3097c478bd9Sstevel@tonic-gate * The default position-dependent magic file tests 3107c478bd9Sstevel@tonic-gate * in /etc/magic will follow all the -m magic tests. 3117c478bd9Sstevel@tonic-gate */ 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate for (filep = mlist1; filep < mlist1p; filep++) { 3147c478bd9Sstevel@tonic-gate if (f_mkmtab(*filep, cflg, 1) == -1) { 3157c478bd9Sstevel@tonic-gate exit(2); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate default_magic(); 3197c478bd9Sstevel@tonic-gate if (f_mkmtab(dfile, cflg, 0) == -1) { 3207c478bd9Sstevel@tonic-gate exit(2); 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate #else /* !XPG4 */ 3237c478bd9Sstevel@tonic-gate /* 3247c478bd9Sstevel@tonic-gate * Retain Solaris file behavior for -m before SUSv3, 3257c478bd9Sstevel@tonic-gate * when the new -d and -M options are not specified. 3267c478bd9Sstevel@tonic-gate * Use the -m file specified in place of the default 3277c478bd9Sstevel@tonic-gate * /etc/magic file. Solaris file will 3287c478bd9Sstevel@tonic-gate * now allow more than one magic file to be specified 3297c478bd9Sstevel@tonic-gate * with multiple -m options, for consistency with 3307c478bd9Sstevel@tonic-gate * other behavior. 3317c478bd9Sstevel@tonic-gate * 3327c478bd9Sstevel@tonic-gate * Put the magic table(s) specified by -m into 3337c478bd9Sstevel@tonic-gate * the second magic table instead of the first 3347c478bd9Sstevel@tonic-gate * (as indicated by the last argument to f_mkmtab()), 3357c478bd9Sstevel@tonic-gate * since they replace the /etc/magic tests and 3367c478bd9Sstevel@tonic-gate * must be executed alongside the default 3377c478bd9Sstevel@tonic-gate * position-sensitive tests. 3387c478bd9Sstevel@tonic-gate */ 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate for (filep = mlist1; filep < mlist1p; filep++) { 3417c478bd9Sstevel@tonic-gate if (f_mkmtab(*filep, cflg, 0) == -1) { 3427c478bd9Sstevel@tonic-gate exit(2); 3437c478bd9Sstevel@tonic-gate } 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate #endif /* XPG4 */ 3467c478bd9Sstevel@tonic-gate } else { 3477c478bd9Sstevel@tonic-gate /* 3487c478bd9Sstevel@tonic-gate * For any other combination of -d, -m, and -M, 3497c478bd9Sstevel@tonic-gate * use the magic files in command-line order. 3507c478bd9Sstevel@tonic-gate * Store the entries from the two separate lists of magic 3517c478bd9Sstevel@tonic-gate * files, if any, into two separate magic file tables. 3527c478bd9Sstevel@tonic-gate * mlist1: magic tests executed before default magic tests 3537c478bd9Sstevel@tonic-gate * mlist2: default magic tests and after 3547c478bd9Sstevel@tonic-gate */ 3557c478bd9Sstevel@tonic-gate for (filep = mlist1; filep && (filep < mlist1p); filep++) { 3567c478bd9Sstevel@tonic-gate if (f_mkmtab(*filep, cflg, 1) == -1) { 3577c478bd9Sstevel@tonic-gate exit(2); 3587c478bd9Sstevel@tonic-gate } 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate for (filep = mlist2; filep && (filep < mlist2p); filep++) { 3617c478bd9Sstevel@tonic-gate if (f_mkmtab(*filep, cflg, 0) == -1) { 3627c478bd9Sstevel@tonic-gate exit(2); 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate /* Initialize the magic file variables; check both magic tables */ 3687c478bd9Sstevel@tonic-gate tmpmax = f_getmaxoffset(1); 3697c478bd9Sstevel@tonic-gate maxmagicoffset = f_getmaxoffset(0); 3707c478bd9Sstevel@tonic-gate if (maxmagicoffset < tmpmax) { 3717c478bd9Sstevel@tonic-gate maxmagicoffset = tmpmax; 3727c478bd9Sstevel@tonic-gate } 3737c478bd9Sstevel@tonic-gate if (maxmagicoffset < (intmax_t)FBSZ) 3747c478bd9Sstevel@tonic-gate maxmagicoffset = (intmax_t)FBSZ; 3759a411307Srie if ((magicbuf = malloc(maxmagicoffset)) == NULL) { 3769a411307Srie int err = errno; 3779a411307Srie (void) fprintf(stderr, gettext("%s: malloc failed: %s\n"), 3789a411307Srie File, strerror(err)); 3797c478bd9Sstevel@tonic-gate exit(2); 3807c478bd9Sstevel@tonic-gate } 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate if (cflg) { 3837c478bd9Sstevel@tonic-gate f_prtmtab(); 3847c478bd9Sstevel@tonic-gate if (ferror(stdout) != 0) { 3859a411307Srie (void) fprintf(stderr, gettext("%s: error writing to " 3869a411307Srie "stdout\n"), File); 3877c478bd9Sstevel@tonic-gate exit(1); 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate if (fclose(stdout) != 0) { 3909a411307Srie int err = errno; 3919a411307Srie (void) fprintf(stderr, gettext("%s: fclose " 3929a411307Srie "failed: %s\n"), File, strerror(err)); 3937c478bd9Sstevel@tonic-gate exit(1); 3947c478bd9Sstevel@tonic-gate } 3957c478bd9Sstevel@tonic-gate exit(0); 3967c478bd9Sstevel@tonic-gate } 3979a411307Srie 3987c478bd9Sstevel@tonic-gate for (; fflg || optind < argc; optind += !fflg) { 3997c478bd9Sstevel@tonic-gate register int l; 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate if (fflg) { 4027c478bd9Sstevel@tonic-gate if ((p = fgets(ap, pathlen, fl)) == NULL) { 4037c478bd9Sstevel@tonic-gate fflg = 0; 4047c478bd9Sstevel@tonic-gate optind--; 4057c478bd9Sstevel@tonic-gate continue; 4067c478bd9Sstevel@tonic-gate } 4077c478bd9Sstevel@tonic-gate l = strlen(p); 4087c478bd9Sstevel@tonic-gate if (l > 0) 4097c478bd9Sstevel@tonic-gate p[l - 1] = '\0'; 4107c478bd9Sstevel@tonic-gate } else 4117c478bd9Sstevel@tonic-gate p = argv[optind]; 4127c478bd9Sstevel@tonic-gate prf(p); /* print "file_name:<tab>" */ 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate if (type(p)) 4157c478bd9Sstevel@tonic-gate tret = 1; 4167c478bd9Sstevel@tonic-gate } 4177c478bd9Sstevel@tonic-gate if (ap != NULL) 4187c478bd9Sstevel@tonic-gate free(ap); 4199a411307Srie if (tret != 0) 4207c478bd9Sstevel@tonic-gate exit(tret); 4219a411307Srie 4227c478bd9Sstevel@tonic-gate if (ferror(stdout) != 0) { 4239a411307Srie (void) fprintf(stderr, gettext("%s: error writing to " 4249a411307Srie "stdout\n"), File); 4257c478bd9Sstevel@tonic-gate exit(1); 4267c478bd9Sstevel@tonic-gate } 4277c478bd9Sstevel@tonic-gate if (fclose(stdout) != 0) { 4289a411307Srie int err = errno; 4299a411307Srie (void) fprintf(stderr, gettext("%s: fclose failed: %s\n"), 4309a411307Srie File, strerror(err)); 4317c478bd9Sstevel@tonic-gate exit(1); 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate return (0); 4347c478bd9Sstevel@tonic-gate } 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate static int 4377c478bd9Sstevel@tonic-gate type(char *file) 4387c478bd9Sstevel@tonic-gate { 4397c478bd9Sstevel@tonic-gate int cc; 4407c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 4417c478bd9Sstevel@tonic-gate int (*statf)() = hflg ? lstat64 : stat64; 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate i = 0; /* reset index to beginning of file */ 4447c478bd9Sstevel@tonic-gate ifd = -1; 4457c478bd9Sstevel@tonic-gate if ((*statf)(file, &mbuf) < 0) { 4467c478bd9Sstevel@tonic-gate if (statf == lstat64 || lstat64(file, &mbuf) < 0) { 4479a411307Srie int err = errno; 4487c478bd9Sstevel@tonic-gate (void) printf(gettext("cannot open: %s\n"), 4499a411307Srie strerror(err)); 4507c478bd9Sstevel@tonic-gate return (0); /* POSIX.2 */ 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate } 4537c478bd9Sstevel@tonic-gate switch (mbuf.st_mode & S_IFMT) { 4547c478bd9Sstevel@tonic-gate case S_IFREG: 4557c478bd9Sstevel@tonic-gate if (iflg) { 4567c478bd9Sstevel@tonic-gate (void) printf(gettext("regular file\n")); 4577c478bd9Sstevel@tonic-gate return (0); 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate break; 4607c478bd9Sstevel@tonic-gate case S_IFCHR: 4617c478bd9Sstevel@tonic-gate (void) printf(gettext("character")); 4627c478bd9Sstevel@tonic-gate goto spcl; 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate case S_IFDIR: 4657c478bd9Sstevel@tonic-gate (void) printf(gettext("directory\n")); 4667c478bd9Sstevel@tonic-gate return (0); 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate case S_IFIFO: 4697c478bd9Sstevel@tonic-gate (void) printf(gettext("fifo\n")); 4707c478bd9Sstevel@tonic-gate return (0); 4717c478bd9Sstevel@tonic-gate 4727c478bd9Sstevel@tonic-gate case S_IFLNK: 4737c478bd9Sstevel@tonic-gate if ((cc = readlink(file, buf, BUFSIZ)) < 0) { 4749a411307Srie int err = errno; 4757c478bd9Sstevel@tonic-gate (void) printf(gettext("readlink error: %s\n"), 4769a411307Srie strerror(err)); 4777c478bd9Sstevel@tonic-gate return (1); 4787c478bd9Sstevel@tonic-gate } 4797c478bd9Sstevel@tonic-gate buf[cc] = '\0'; 4807c478bd9Sstevel@tonic-gate (void) printf(gettext("symbolic link to %s\n"), buf); 4817c478bd9Sstevel@tonic-gate return (0); 4827c478bd9Sstevel@tonic-gate 4837c478bd9Sstevel@tonic-gate case S_IFBLK: 4847c478bd9Sstevel@tonic-gate (void) printf(gettext("block")); 4857c478bd9Sstevel@tonic-gate /* major and minor, see sys/mkdev.h */ 4867c478bd9Sstevel@tonic-gate spcl: 4877c478bd9Sstevel@tonic-gate (void) printf(gettext(" special (%d/%d)\n"), 4887c478bd9Sstevel@tonic-gate major(mbuf.st_rdev), minor(mbuf.st_rdev)); 4897c478bd9Sstevel@tonic-gate return (0); 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate case S_IFSOCK: 4927c478bd9Sstevel@tonic-gate (void) printf("socket\n"); 4937c478bd9Sstevel@tonic-gate /* FIXME, should open and try to getsockname. */ 4947c478bd9Sstevel@tonic-gate return (0); 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate case S_IFDOOR: 4977c478bd9Sstevel@tonic-gate if (get_door_target(file, buf, sizeof (buf)) == 0) 4987c478bd9Sstevel@tonic-gate (void) printf(gettext("door to %s\n"), buf); 4997c478bd9Sstevel@tonic-gate else 5007c478bd9Sstevel@tonic-gate (void) printf(gettext("door\n")); 5017c478bd9Sstevel@tonic-gate return (0); 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate } 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) { 5067c478bd9Sstevel@tonic-gate (void) printf(gettext("libelf is out of date\n")); 5077c478bd9Sstevel@tonic-gate return (1); 5087c478bd9Sstevel@tonic-gate } 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate ifd = open64(file, O_RDONLY); 5117c478bd9Sstevel@tonic-gate if (ifd < 0) { 5129a411307Srie int err = errno; 5139a411307Srie (void) printf(gettext("cannot open: %s\n"), strerror(err)); 5147c478bd9Sstevel@tonic-gate return (0); /* POSIX.2 */ 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate /* need another fd for elf, since we might want to read the file too */ 5187c478bd9Sstevel@tonic-gate elffd = open64(file, O_RDONLY); 5197c478bd9Sstevel@tonic-gate if (elffd < 0) { 5209a411307Srie int err = errno; 5219a411307Srie (void) printf(gettext("cannot open: %s\n"), strerror(err)); 5227c478bd9Sstevel@tonic-gate (void) close(ifd); 5237c478bd9Sstevel@tonic-gate ifd = -1; 5247c478bd9Sstevel@tonic-gate return (0); /* POSIX.2 */ 5257c478bd9Sstevel@tonic-gate } 5267c478bd9Sstevel@tonic-gate if ((fbsz = read(ifd, fbuf, FBSZ)) == -1) { 5279a411307Srie int err = errno; 5289a411307Srie (void) printf(gettext("cannot read: %s\n"), strerror(err)); 5297c478bd9Sstevel@tonic-gate (void) close(ifd); 5307c478bd9Sstevel@tonic-gate ifd = -1; 5317c478bd9Sstevel@tonic-gate return (0); /* POSIX.2 */ 5327c478bd9Sstevel@tonic-gate } 5337c478bd9Sstevel@tonic-gate if (fbsz == 0) { 5347c478bd9Sstevel@tonic-gate (void) printf(gettext("empty file\n")); 5357c478bd9Sstevel@tonic-gate fd_cleanup(); 5367c478bd9Sstevel@tonic-gate return (0); 5377c478bd9Sstevel@tonic-gate } 5387c478bd9Sstevel@tonic-gate 5397c478bd9Sstevel@tonic-gate /* 5407c478bd9Sstevel@tonic-gate * First try user-specified position-dependent magic tests, if any, 5417c478bd9Sstevel@tonic-gate * which need to execute before the default tests. 5427c478bd9Sstevel@tonic-gate */ 5437c478bd9Sstevel@tonic-gate if ((mread = pread(ifd, (void*)magicbuf, (size_t)maxmagicoffset, 5447c478bd9Sstevel@tonic-gate (off_t)0)) == -1) { 5459a411307Srie int err = errno; 5469a411307Srie (void) printf(gettext("cannot read: %s\n"), strerror(err)); 5477c478bd9Sstevel@tonic-gate fd_cleanup(); 5487c478bd9Sstevel@tonic-gate return (0); 5497c478bd9Sstevel@tonic-gate } 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate /* 5527c478bd9Sstevel@tonic-gate * ChecK against Magic Table entries. 5537c478bd9Sstevel@tonic-gate * Check first magic table for magic tests to be applied 5547c478bd9Sstevel@tonic-gate * before default tests. 5557c478bd9Sstevel@tonic-gate * If no default tests are to be applied, all magic tests 5567c478bd9Sstevel@tonic-gate * should occur in this magic table. 5577c478bd9Sstevel@tonic-gate */ 5587c478bd9Sstevel@tonic-gate switch (f_ckmtab(magicbuf, mread, 1)) { 5597c478bd9Sstevel@tonic-gate case -1: /* Error */ 5607c478bd9Sstevel@tonic-gate exit(2); 5617c478bd9Sstevel@tonic-gate break; 5627c478bd9Sstevel@tonic-gate case 0: /* Not magic */ 5637c478bd9Sstevel@tonic-gate break; 5647c478bd9Sstevel@tonic-gate default: /* Switch is magic index */ 5657c478bd9Sstevel@tonic-gate (void) putchar('\n'); 5667c478bd9Sstevel@tonic-gate fd_cleanup(); 5677c478bd9Sstevel@tonic-gate return (0); 5687c478bd9Sstevel@tonic-gate /* NOTREACHED */ 5697c478bd9Sstevel@tonic-gate break; 5707c478bd9Sstevel@tonic-gate } 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate if (dflg || !M_flg) { 5737c478bd9Sstevel@tonic-gate /* 5747c478bd9Sstevel@tonic-gate * default position-dependent tests, 5757c478bd9Sstevel@tonic-gate * plus non-default magic tests, if any 5767c478bd9Sstevel@tonic-gate */ 5779a411307Srie switch (def_position_tests(file)) { 5787c478bd9Sstevel@tonic-gate case -1: /* error */ 5797c478bd9Sstevel@tonic-gate fd_cleanup(); 5807c478bd9Sstevel@tonic-gate return (1); 5817c478bd9Sstevel@tonic-gate case 1: /* matching type found */ 5827c478bd9Sstevel@tonic-gate fd_cleanup(); 5837c478bd9Sstevel@tonic-gate return (0); 5847c478bd9Sstevel@tonic-gate /* NOTREACHED */ 5857c478bd9Sstevel@tonic-gate break; 5867c478bd9Sstevel@tonic-gate case 0: /* no matching type found */ 5877c478bd9Sstevel@tonic-gate break; 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate /* default context-sensitive tests */ 5907c478bd9Sstevel@tonic-gate def_context_tests(); 5917c478bd9Sstevel@tonic-gate } else { 5927c478bd9Sstevel@tonic-gate /* no more tests to apply; no match was found */ 5937c478bd9Sstevel@tonic-gate (void) printf(gettext("data\n")); 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate fd_cleanup(); 5967c478bd9Sstevel@tonic-gate return (0); 5977c478bd9Sstevel@tonic-gate } 5987c478bd9Sstevel@tonic-gate 5997c478bd9Sstevel@tonic-gate /* 6007c478bd9Sstevel@tonic-gate * def_position_tests() - applies default position-sensitive tests, 6017c478bd9Sstevel@tonic-gate * looking for values in specific positions in the file. 6027c478bd9Sstevel@tonic-gate * These are followed by default (followed by possibly some 6037c478bd9Sstevel@tonic-gate * non-default) magic file tests. 6047c478bd9Sstevel@tonic-gate * 6057c478bd9Sstevel@tonic-gate * All position-sensitive tests, default or otherwise, must 6067c478bd9Sstevel@tonic-gate * be applied before context-sensitive tests, to avoid 6077c478bd9Sstevel@tonic-gate * false context-sensitive matches. 6087c478bd9Sstevel@tonic-gate * 6097c478bd9Sstevel@tonic-gate * Returns -1 on error which should result in error (non-zero) 6107c478bd9Sstevel@tonic-gate * exit status for the file utility. 6117c478bd9Sstevel@tonic-gate * Returns 0 if no matching file type found. 6127c478bd9Sstevel@tonic-gate * Returns 1 if matching file type found. 6137c478bd9Sstevel@tonic-gate */ 6147c478bd9Sstevel@tonic-gate 6157c478bd9Sstevel@tonic-gate static int 6169a411307Srie def_position_tests(char *file) 6177c478bd9Sstevel@tonic-gate { 6187c478bd9Sstevel@tonic-gate if (sccs()) { /* look for "1hddddd" where d is a digit */ 6197c478bd9Sstevel@tonic-gate (void) printf("sccs \n"); 6207c478bd9Sstevel@tonic-gate return (1); 6217c478bd9Sstevel@tonic-gate } 6227c478bd9Sstevel@tonic-gate if (fbuf[0] == '#' && fbuf[1] == '!' && shellscript(fbuf+2, &mbuf)) 6237c478bd9Sstevel@tonic-gate return (1); 624c2c65e21Sny155746 625c2c65e21Sny155746 if (elf_check(file) == 0) { 6267c478bd9Sstevel@tonic-gate (void) putchar('\n'); 6277c478bd9Sstevel@tonic-gate return (1); 6287c478bd9Sstevel@tonic-gate /* LINTED: pointer cast may result in improper alignment */ 6297c478bd9Sstevel@tonic-gate } else if (*(int *)fbuf == CORE_MAGIC) { 6307c478bd9Sstevel@tonic-gate /* LINTED: pointer cast may result in improper alignment */ 6317c478bd9Sstevel@tonic-gate struct core *corep = (struct core *)fbuf; 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate (void) printf("a.out core file"); 6347c478bd9Sstevel@tonic-gate 6357c478bd9Sstevel@tonic-gate if (*(corep->c_cmdname) != '\0') 6367c478bd9Sstevel@tonic-gate (void) printf(" from '%s'", corep->c_cmdname); 6377c478bd9Sstevel@tonic-gate (void) putchar('\n'); 6387c478bd9Sstevel@tonic-gate return (1); 6397c478bd9Sstevel@tonic-gate } 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate /* 642c13de8f6Sab196087 * Runtime linker (ld.so.1) configuration file. 643c13de8f6Sab196087 */ 644c13de8f6Sab196087 if (is_rtld_config()) 645c13de8f6Sab196087 return (1); 646c13de8f6Sab196087 647c13de8f6Sab196087 /* 6487c478bd9Sstevel@tonic-gate * ZIP files, JAR files, and Java executables 6497c478bd9Sstevel@tonic-gate */ 6507c478bd9Sstevel@tonic-gate if (zipfile(fbuf, ifd)) 6517c478bd9Sstevel@tonic-gate return (1); 6527c478bd9Sstevel@tonic-gate 6537c478bd9Sstevel@tonic-gate if (is_crash_dump(fbuf, ifd)) 6547c478bd9Sstevel@tonic-gate return (1); 6557c478bd9Sstevel@tonic-gate 6567c478bd9Sstevel@tonic-gate /* 6577c478bd9Sstevel@tonic-gate * ChecK against Magic Table entries. 6587c478bd9Sstevel@tonic-gate * The magic entries checked here always start with default 6597c478bd9Sstevel@tonic-gate * magic tests and may be followed by other, non-default magic 6607c478bd9Sstevel@tonic-gate * tests. If no default tests are to be executed, all the 6617c478bd9Sstevel@tonic-gate * magic tests should have been in the first magic table. 6627c478bd9Sstevel@tonic-gate */ 6637c478bd9Sstevel@tonic-gate switch (f_ckmtab(magicbuf, mread, 0)) { 6647c478bd9Sstevel@tonic-gate case -1: /* Error */ 6657c478bd9Sstevel@tonic-gate exit(2); 6667c478bd9Sstevel@tonic-gate break; 6677c478bd9Sstevel@tonic-gate case 0: /* Not magic */ 6687c478bd9Sstevel@tonic-gate return (0); 6697c478bd9Sstevel@tonic-gate /* NOTREACHED */ 6707c478bd9Sstevel@tonic-gate break; 6717c478bd9Sstevel@tonic-gate default: /* Switch is magic index */ 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate /* 6747c478bd9Sstevel@tonic-gate * f_ckmtab recognizes file type, 6757c478bd9Sstevel@tonic-gate * check if it is PostScript. 6767c478bd9Sstevel@tonic-gate * if not, check if elf or a.out 6777c478bd9Sstevel@tonic-gate */ 6787c478bd9Sstevel@tonic-gate if (magicbuf[0] == '%' && magicbuf[1] == '!') { 6797c478bd9Sstevel@tonic-gate (void) putchar('\n'); 6807c478bd9Sstevel@tonic-gate } else { 6817c478bd9Sstevel@tonic-gate 6827c478bd9Sstevel@tonic-gate /* 6837c478bd9Sstevel@tonic-gate * Check that the file is executable (dynamic 6847c478bd9Sstevel@tonic-gate * objects must be executable to be exec'ed, 6857c478bd9Sstevel@tonic-gate * shared objects need not be, but by convention 6867c478bd9Sstevel@tonic-gate * should be executable). 6877c478bd9Sstevel@tonic-gate * 6887c478bd9Sstevel@tonic-gate * Note that we should already have processed 6897c478bd9Sstevel@tonic-gate * the file if it was an ELF file. 6907c478bd9Sstevel@tonic-gate */ 6917c478bd9Sstevel@tonic-gate ar_coff_or_aout(elffd); 6927c478bd9Sstevel@tonic-gate (void) putchar('\n'); 6937c478bd9Sstevel@tonic-gate } 6947c478bd9Sstevel@tonic-gate return (1); 6957c478bd9Sstevel@tonic-gate /* NOTREACHED */ 6967c478bd9Sstevel@tonic-gate break; 6977c478bd9Sstevel@tonic-gate } 6987c478bd9Sstevel@tonic-gate 6997c478bd9Sstevel@tonic-gate return (0); /* file was not identified */ 7007c478bd9Sstevel@tonic-gate } 7017c478bd9Sstevel@tonic-gate 7027c478bd9Sstevel@tonic-gate /* 7037c478bd9Sstevel@tonic-gate * def_context_tests() - default context-sensitive tests. 7047c478bd9Sstevel@tonic-gate * These are the last tests to be applied. 7057c478bd9Sstevel@tonic-gate * If no match is found, prints out "data". 7067c478bd9Sstevel@tonic-gate */ 7077c478bd9Sstevel@tonic-gate 7087c478bd9Sstevel@tonic-gate static void 7097c478bd9Sstevel@tonic-gate def_context_tests(void) 7107c478bd9Sstevel@tonic-gate { 7117c478bd9Sstevel@tonic-gate int j; 7127c478bd9Sstevel@tonic-gate int nl; 7137c478bd9Sstevel@tonic-gate char ch; 7147c478bd9Sstevel@tonic-gate int len; 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate if (ccom() == 0) 7177c478bd9Sstevel@tonic-gate goto notc; 7187c478bd9Sstevel@tonic-gate while (fbuf[i] == '#') { 7197c478bd9Sstevel@tonic-gate j = i; 7207c478bd9Sstevel@tonic-gate while (fbuf[i++] != '\n') { 7217c478bd9Sstevel@tonic-gate if (i - j > 255) { 7227c478bd9Sstevel@tonic-gate (void) printf(gettext("data\n")); 7237c478bd9Sstevel@tonic-gate return; 7247c478bd9Sstevel@tonic-gate } 7257c478bd9Sstevel@tonic-gate if (i >= fbsz) 7267c478bd9Sstevel@tonic-gate goto notc; 7277c478bd9Sstevel@tonic-gate } 7287c478bd9Sstevel@tonic-gate if (ccom() == 0) 7297c478bd9Sstevel@tonic-gate goto notc; 7307c478bd9Sstevel@tonic-gate } 7317c478bd9Sstevel@tonic-gate check: 7327c478bd9Sstevel@tonic-gate if (lookup(c) == 1) { 7337c478bd9Sstevel@tonic-gate while ((ch = fbuf[i]) != ';' && ch != '{') { 7347c478bd9Sstevel@tonic-gate if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0) 7357c478bd9Sstevel@tonic-gate len = 1; 7367c478bd9Sstevel@tonic-gate i += len; 7377c478bd9Sstevel@tonic-gate if (i >= fbsz) 7387c478bd9Sstevel@tonic-gate goto notc; 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate (void) printf(gettext("c program text")); 7417c478bd9Sstevel@tonic-gate goto outa; 7427c478bd9Sstevel@tonic-gate } 7437c478bd9Sstevel@tonic-gate nl = 0; 7447c478bd9Sstevel@tonic-gate while (fbuf[i] != '(') { 7457c478bd9Sstevel@tonic-gate if (fbuf[i] <= 0) 7467c478bd9Sstevel@tonic-gate goto notas; 7477c478bd9Sstevel@tonic-gate if (fbuf[i] == ';') { 7487c478bd9Sstevel@tonic-gate i++; 7497c478bd9Sstevel@tonic-gate goto check; 7507c478bd9Sstevel@tonic-gate } 7517c478bd9Sstevel@tonic-gate if (fbuf[i++] == '\n') 7527c478bd9Sstevel@tonic-gate if (nl++ > 6) 7537c478bd9Sstevel@tonic-gate goto notc; 7547c478bd9Sstevel@tonic-gate if (i >= fbsz) 7557c478bd9Sstevel@tonic-gate goto notc; 7567c478bd9Sstevel@tonic-gate } 7577c478bd9Sstevel@tonic-gate while (fbuf[i] != ')') { 7587c478bd9Sstevel@tonic-gate if (fbuf[i++] == '\n') 7597c478bd9Sstevel@tonic-gate if (nl++ > 6) 7607c478bd9Sstevel@tonic-gate goto notc; 7617c478bd9Sstevel@tonic-gate if (i >= fbsz) 7627c478bd9Sstevel@tonic-gate goto notc; 7637c478bd9Sstevel@tonic-gate } 7647c478bd9Sstevel@tonic-gate while (fbuf[i] != '{') { 7657c478bd9Sstevel@tonic-gate if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0) 7667c478bd9Sstevel@tonic-gate len = 1; 7677c478bd9Sstevel@tonic-gate if (fbuf[i] == '\n') 7687c478bd9Sstevel@tonic-gate if (nl++ > 6) 7697c478bd9Sstevel@tonic-gate goto notc; 7707c478bd9Sstevel@tonic-gate i += len; 7717c478bd9Sstevel@tonic-gate if (i >= fbsz) 7727c478bd9Sstevel@tonic-gate goto notc; 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate (void) printf(gettext("c program text")); 7757c478bd9Sstevel@tonic-gate goto outa; 7767c478bd9Sstevel@tonic-gate notc: 7777c478bd9Sstevel@tonic-gate i = 0; /* reset to begining of file again */ 7787c478bd9Sstevel@tonic-gate while (fbuf[i] == 'c' || fbuf[i] == 'C'|| fbuf[i] == '!' || 7797c478bd9Sstevel@tonic-gate fbuf[i] == '*' || fbuf[i] == '\n') { 7807c478bd9Sstevel@tonic-gate while (fbuf[i++] != '\n') 7817c478bd9Sstevel@tonic-gate if (i >= fbsz) 7827c478bd9Sstevel@tonic-gate goto notfort; 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate if (lookup(fort) == 1) { 7857c478bd9Sstevel@tonic-gate (void) printf(gettext("fortran program text")); 7867c478bd9Sstevel@tonic-gate goto outa; 7877c478bd9Sstevel@tonic-gate } 7887c478bd9Sstevel@tonic-gate notfort: /* looking for assembler program */ 7897c478bd9Sstevel@tonic-gate i = 0; /* reset to beginning of file again */ 7907c478bd9Sstevel@tonic-gate if (ccom() == 0) /* assembler programs may contain */ 7917c478bd9Sstevel@tonic-gate /* c-style comments */ 7927c478bd9Sstevel@tonic-gate goto notas; 7937c478bd9Sstevel@tonic-gate if (ascom() == 0) 7947c478bd9Sstevel@tonic-gate goto notas; 7957c478bd9Sstevel@tonic-gate j = i - 1; 7967c478bd9Sstevel@tonic-gate if (fbuf[i] == '.') { 7977c478bd9Sstevel@tonic-gate i++; 7987c478bd9Sstevel@tonic-gate if (lookup(as) == 1) { 7997c478bd9Sstevel@tonic-gate (void) printf(gettext("assembler program text")); 8007c478bd9Sstevel@tonic-gate goto outa; 8017c478bd9Sstevel@tonic-gate } else if (j != -1 && fbuf[j] == '\n' && isalpha(fbuf[j + 2])) { 8027c478bd9Sstevel@tonic-gate (void) printf( 8037c478bd9Sstevel@tonic-gate gettext("[nt]roff, tbl, or eqn input text")); 8047c478bd9Sstevel@tonic-gate goto outa; 8057c478bd9Sstevel@tonic-gate } 8067c478bd9Sstevel@tonic-gate } 8077c478bd9Sstevel@tonic-gate while (lookup(asc) == 0) { 8087c478bd9Sstevel@tonic-gate if (ccom() == 0) 8097c478bd9Sstevel@tonic-gate goto notas; 8107c478bd9Sstevel@tonic-gate if (ascom() == 0) 8117c478bd9Sstevel@tonic-gate goto notas; 8127c478bd9Sstevel@tonic-gate while (fbuf[i] != '\n' && fbuf[i++] != ':') { 8137c478bd9Sstevel@tonic-gate if (i >= fbsz) 8147c478bd9Sstevel@tonic-gate goto notas; 8157c478bd9Sstevel@tonic-gate } 8167c478bd9Sstevel@tonic-gate while (fbuf[i] == '\n' || fbuf[i] == ' ' || fbuf[i] == '\t') 8177c478bd9Sstevel@tonic-gate if (i++ >= fbsz) 8187c478bd9Sstevel@tonic-gate goto notas; 8197c478bd9Sstevel@tonic-gate j = i - 1; 8207c478bd9Sstevel@tonic-gate if (fbuf[i] == '.') { 8217c478bd9Sstevel@tonic-gate i++; 8227c478bd9Sstevel@tonic-gate if (lookup(as) == 1) { 8237c478bd9Sstevel@tonic-gate (void) printf( 8247c478bd9Sstevel@tonic-gate gettext("assembler program text")); 8257c478bd9Sstevel@tonic-gate goto outa; 8267c478bd9Sstevel@tonic-gate } else if (fbuf[j] == '\n' && isalpha(fbuf[j+2])) { 8277c478bd9Sstevel@tonic-gate (void) printf( 8287c478bd9Sstevel@tonic-gate gettext("[nt]roff, tbl, or eqn input " 8297c478bd9Sstevel@tonic-gate "text")); 8307c478bd9Sstevel@tonic-gate goto outa; 8317c478bd9Sstevel@tonic-gate } 8327c478bd9Sstevel@tonic-gate } 8337c478bd9Sstevel@tonic-gate } 8347c478bd9Sstevel@tonic-gate (void) printf(gettext("assembler program text")); 8357c478bd9Sstevel@tonic-gate goto outa; 8367c478bd9Sstevel@tonic-gate notas: 8377c478bd9Sstevel@tonic-gate /* start modification for multibyte env */ 8387c478bd9Sstevel@tonic-gate IS_ascii = 1; 8397c478bd9Sstevel@tonic-gate if (fbsz < FBSZ) 8407c478bd9Sstevel@tonic-gate Max = fbsz; 8417c478bd9Sstevel@tonic-gate else 8427c478bd9Sstevel@tonic-gate Max = FBSZ - MB_LEN_MAX; /* prevent cut of wchar read */ 8437c478bd9Sstevel@tonic-gate /* end modification for multibyte env */ 8447c478bd9Sstevel@tonic-gate 8457c478bd9Sstevel@tonic-gate for (i = 0; i < Max; /* null */) 8467c478bd9Sstevel@tonic-gate if (fbuf[i] & 0200) { 8477c478bd9Sstevel@tonic-gate IS_ascii = 0; 8487c478bd9Sstevel@tonic-gate if (fbuf[0] == '\100' && fbuf[1] == '\357') { 8497c478bd9Sstevel@tonic-gate (void) printf(gettext("troff output\n")); 8507c478bd9Sstevel@tonic-gate return; 8517c478bd9Sstevel@tonic-gate } 8527c478bd9Sstevel@tonic-gate /* start modification for multibyte env */ 8537c478bd9Sstevel@tonic-gate if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX)) 8547c478bd9Sstevel@tonic-gate <= 0 || !iswprint(wchar)) { 8557c478bd9Sstevel@tonic-gate (void) printf(gettext("data\n")); 8567c478bd9Sstevel@tonic-gate return; 8577c478bd9Sstevel@tonic-gate } 8587c478bd9Sstevel@tonic-gate i += length; 8597c478bd9Sstevel@tonic-gate } 8607c478bd9Sstevel@tonic-gate else 8617c478bd9Sstevel@tonic-gate i++; 8627c478bd9Sstevel@tonic-gate i = fbsz; 8637c478bd9Sstevel@tonic-gate /* end modification for multibyte env */ 8647c478bd9Sstevel@tonic-gate if (mbuf.st_mode&(S_IXUSR|S_IXGRP|S_IXOTH)) 8657c478bd9Sstevel@tonic-gate (void) printf(gettext("commands text")); 8667c478bd9Sstevel@tonic-gate else if (troffint(fbuf, fbsz)) 8677c478bd9Sstevel@tonic-gate (void) printf(gettext("troff intermediate output text")); 8687c478bd9Sstevel@tonic-gate else if (english(fbuf, fbsz)) 8697c478bd9Sstevel@tonic-gate (void) printf(gettext("English text")); 8707c478bd9Sstevel@tonic-gate else if (IS_ascii) 8717c478bd9Sstevel@tonic-gate (void) printf(gettext("ascii text")); 8727c478bd9Sstevel@tonic-gate else 8737c478bd9Sstevel@tonic-gate (void) printf(gettext("text")); /* for multibyte env */ 8747c478bd9Sstevel@tonic-gate outa: 8757c478bd9Sstevel@tonic-gate /* 8767c478bd9Sstevel@tonic-gate * This code is to make sure that no MB char is cut in half 8777c478bd9Sstevel@tonic-gate * while still being used. 8787c478bd9Sstevel@tonic-gate */ 8797c478bd9Sstevel@tonic-gate fbsz = (fbsz < FBSZ ? fbsz : fbsz - MB_CUR_MAX + 1); 8807c478bd9Sstevel@tonic-gate while (i < fbsz) { 8817c478bd9Sstevel@tonic-gate if (isascii(fbuf[i])) { 8827c478bd9Sstevel@tonic-gate i++; 8837c478bd9Sstevel@tonic-gate continue; 8847c478bd9Sstevel@tonic-gate } else { 8857c478bd9Sstevel@tonic-gate if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX)) 8867c478bd9Sstevel@tonic-gate <= 0 || !iswprint(wchar)) { 8877c478bd9Sstevel@tonic-gate (void) printf(gettext(" with garbage\n")); 8887c478bd9Sstevel@tonic-gate return; 8897c478bd9Sstevel@tonic-gate } 8907c478bd9Sstevel@tonic-gate i = i + length; 8917c478bd9Sstevel@tonic-gate } 8927c478bd9Sstevel@tonic-gate } 8937c478bd9Sstevel@tonic-gate (void) printf("\n"); 8947c478bd9Sstevel@tonic-gate } 8957c478bd9Sstevel@tonic-gate 8967c478bd9Sstevel@tonic-gate static int 8977c478bd9Sstevel@tonic-gate troffint(char *bp, int n) 8987c478bd9Sstevel@tonic-gate { 8997c478bd9Sstevel@tonic-gate int k; 9007c478bd9Sstevel@tonic-gate 9017c478bd9Sstevel@tonic-gate i = 0; 9027c478bd9Sstevel@tonic-gate for (k = 0; k < 6; k++) { 9037c478bd9Sstevel@tonic-gate if (lookup(troff) == 0) 9047c478bd9Sstevel@tonic-gate return (0); 9057c478bd9Sstevel@tonic-gate if (lookup(troff) == 0) 9067c478bd9Sstevel@tonic-gate return (0); 9077c478bd9Sstevel@tonic-gate while (i < n && bp[i] != '\n') 9087c478bd9Sstevel@tonic-gate i++; 9097c478bd9Sstevel@tonic-gate if (i++ >= n) 9107c478bd9Sstevel@tonic-gate return (0); 9117c478bd9Sstevel@tonic-gate } 9127c478bd9Sstevel@tonic-gate return (1); 9137c478bd9Sstevel@tonic-gate } 9147c478bd9Sstevel@tonic-gate 9157c478bd9Sstevel@tonic-gate static void 9167c478bd9Sstevel@tonic-gate ar_coff_or_aout(int elffd) 9177c478bd9Sstevel@tonic-gate { 9187c478bd9Sstevel@tonic-gate Elf *elf; 9197c478bd9Sstevel@tonic-gate 9207c478bd9Sstevel@tonic-gate /* 9217c478bd9Sstevel@tonic-gate * Get the files elf descriptor and process it as an elf or 9227c478bd9Sstevel@tonic-gate * a.out (4.x) file. 9237c478bd9Sstevel@tonic-gate */ 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate elf = elf_begin(elffd, ELF_C_READ, (Elf *)0); 9267c478bd9Sstevel@tonic-gate switch (elf_kind(elf)) { 9277c478bd9Sstevel@tonic-gate case ELF_K_AR : 9287c478bd9Sstevel@tonic-gate (void) printf(gettext(", not a dynamic executable " 9297c478bd9Sstevel@tonic-gate "or shared object")); 9307c478bd9Sstevel@tonic-gate break; 9317c478bd9Sstevel@tonic-gate case ELF_K_COFF: 9327c478bd9Sstevel@tonic-gate (void) printf(gettext(", unsupported or unknown " 9337c478bd9Sstevel@tonic-gate "file type")); 9347c478bd9Sstevel@tonic-gate break; 9357c478bd9Sstevel@tonic-gate default: 9367c478bd9Sstevel@tonic-gate /* 9377c478bd9Sstevel@tonic-gate * This is either an unknown file or an aout format 9387c478bd9Sstevel@tonic-gate * At this time, we don't print dynamic/stripped 9397c478bd9Sstevel@tonic-gate * info. on a.out or non-Elf binaries. 9407c478bd9Sstevel@tonic-gate */ 9417c478bd9Sstevel@tonic-gate break; 9427c478bd9Sstevel@tonic-gate } 9437c478bd9Sstevel@tonic-gate (void) elf_end(elf); 9447c478bd9Sstevel@tonic-gate } 9457c478bd9Sstevel@tonic-gate 9467c478bd9Sstevel@tonic-gate 9477c478bd9Sstevel@tonic-gate static void 948c2c65e21Sny155746 print_elf_type(Elf_Info EI) 9497c478bd9Sstevel@tonic-gate { 950c2c65e21Sny155746 switch (EI.type) { 9517c478bd9Sstevel@tonic-gate case ET_NONE: 9527c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("unknown type")); 9537c478bd9Sstevel@tonic-gate break; 9547c478bd9Sstevel@tonic-gate case ET_REL: 9557c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("relocatable")); 9567c478bd9Sstevel@tonic-gate break; 9577c478bd9Sstevel@tonic-gate case ET_EXEC: 9587c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("executable")); 9597c478bd9Sstevel@tonic-gate break; 9607c478bd9Sstevel@tonic-gate case ET_DYN: 9617c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("dynamic lib")); 9627c478bd9Sstevel@tonic-gate break; 9637c478bd9Sstevel@tonic-gate default: 9647c478bd9Sstevel@tonic-gate break; 9657c478bd9Sstevel@tonic-gate } 9667c478bd9Sstevel@tonic-gate } 9677c478bd9Sstevel@tonic-gate 9687c478bd9Sstevel@tonic-gate static void 9697c478bd9Sstevel@tonic-gate print_elf_machine(int machine) 9707c478bd9Sstevel@tonic-gate { 971ca1e0a81Sab196087 /* 972ca1e0a81Sab196087 * This table must be kept in sync with the EM_ constants 973ca1e0a81Sab196087 * in /usr/include/sys/elf.h. 974ca1e0a81Sab196087 */ 975ca1e0a81Sab196087 static const char *mach_str[EM_NUM] = { 976ca1e0a81Sab196087 "unknown machine", /* 0 - EM_NONE */ 977ca1e0a81Sab196087 "WE32100", /* 1 - EM_M32 */ 978ca1e0a81Sab196087 "SPARC", /* 2 - EM_SPARC */ 979ca1e0a81Sab196087 "80386", /* 3 - EM_386 */ 980ca1e0a81Sab196087 "M68000", /* 4 - EM_68K */ 981ca1e0a81Sab196087 "M88000", /* 5 - EM_88K */ 982ca1e0a81Sab196087 "80486", /* 6 - EM_486 */ 983ca1e0a81Sab196087 "i860", /* 7 - EM_860 */ 984ca1e0a81Sab196087 "MIPS RS3000 Big-Endian", /* 8 - EM_MIPS */ 985ca1e0a81Sab196087 "S/370", /* 9 - EM_S370 */ 986ca1e0a81Sab196087 "MIPS RS3000 Little-Endian", /* 10 - EM_MIPS_RS3_LE */ 987ca1e0a81Sab196087 "MIPS RS6000", /* 11 - EM_RS6000 */ 988ca1e0a81Sab196087 NULL, /* 12 - EM_UNKNOWN12 */ 989ca1e0a81Sab196087 NULL, /* 13 - EM_UNKNOWN13 */ 990ca1e0a81Sab196087 NULL, /* 14 - EM_UNKNOWN14 */ 991ca1e0a81Sab196087 "PA-RISC", /* 15 - EM_PA_RISC */ 992ca1e0a81Sab196087 "nCUBE", /* 16 - EM_nCUBE */ 993ca1e0a81Sab196087 "VPP500", /* 17 - EM_VPP500 */ 994ca1e0a81Sab196087 "SPARC32PLUS", /* 18 - EM_SPARC32PLUS */ 995ca1e0a81Sab196087 "i960", /* 19 - EM_960 */ 996ca1e0a81Sab196087 "PowerPC", /* 20 - EM_PPC */ 997ca1e0a81Sab196087 "PowerPC64", /* 21 - EM_PPC64 */ 9981638af81Sab196087 "S/390", /* 22 - EM_S390 */ 999ca1e0a81Sab196087 NULL, /* 23 - EM_UNKNOWN23 */ 1000ca1e0a81Sab196087 NULL, /* 24 - EM_UNKNOWN24 */ 1001ca1e0a81Sab196087 NULL, /* 25 - EM_UNKNOWN25 */ 1002ca1e0a81Sab196087 NULL, /* 26 - EM_UNKNOWN26 */ 1003ca1e0a81Sab196087 NULL, /* 27 - EM_UNKNOWN27 */ 1004ca1e0a81Sab196087 NULL, /* 28 - EM_UNKNOWN28 */ 1005ca1e0a81Sab196087 NULL, /* 29 - EM_UNKNOWN29 */ 1006ca1e0a81Sab196087 NULL, /* 30 - EM_UNKNOWN30 */ 1007ca1e0a81Sab196087 NULL, /* 31 - EM_UNKNOWN31 */ 1008ca1e0a81Sab196087 NULL, /* 32 - EM_UNKNOWN32 */ 1009ca1e0a81Sab196087 NULL, /* 33 - EM_UNKNOWN33 */ 1010ca1e0a81Sab196087 NULL, /* 34 - EM_UNKNOWN34 */ 1011ca1e0a81Sab196087 NULL, /* 35 - EM_UNKNOWN35 */ 1012ca1e0a81Sab196087 "V800", /* 36 - EM_V800 */ 1013ca1e0a81Sab196087 "FR20", /* 37 - EM_FR20 */ 1014ca1e0a81Sab196087 "RH32", /* 38 - EM_RH32 */ 1015ca1e0a81Sab196087 "RCE", /* 39 - EM_RCE */ 1016ca1e0a81Sab196087 "ARM", /* 40 - EM_ARM */ 1017ca1e0a81Sab196087 "Alpha", /* 41 - EM_ALPHA */ 1018ca1e0a81Sab196087 "S/390", /* 42 - EM_SH */ 1019ca1e0a81Sab196087 "SPARCV9", /* 43 - EM_SPARCV9 */ 1020ca1e0a81Sab196087 "Tricore", /* 44 - EM_TRICORE */ 1021ca1e0a81Sab196087 "ARC", /* 45 - EM_ARC */ 1022ca1e0a81Sab196087 "H8/300", /* 46 - EM_H8_300 */ 1023ca1e0a81Sab196087 "H8/300H", /* 47 - EM_H8_300H */ 1024ca1e0a81Sab196087 "H8S", /* 48 - EM_H8S */ 1025ca1e0a81Sab196087 "H8/500", /* 49 - EM_H8_500 */ 1026ca1e0a81Sab196087 "IA64", /* 50 - EM_IA_64 */ 1027ca1e0a81Sab196087 "MIPS-X", /* 51 - EM_MIPS_X */ 1028ca1e0a81Sab196087 "Coldfire", /* 52 - EM_COLDFIRE */ 1029ca1e0a81Sab196087 "M68HC12", /* 53 - EM_68HC12 */ 1030ca1e0a81Sab196087 "MMA", /* 54 - EM_MMA */ 1031ca1e0a81Sab196087 "PCP", /* 55 - EM_PCP */ 1032ca1e0a81Sab196087 "nCPU", /* 56 - EM_NCPU */ 1033ca1e0a81Sab196087 "NDR1", /* 57 - EM_NDR1 */ 1034ca1e0a81Sab196087 "Starcore", /* 58 - EM_STARCORE */ 1035ca1e0a81Sab196087 "ME16", /* 59 - EM_ME16 */ 1036ca1e0a81Sab196087 "ST100", /* 60 - EM_ST100 */ 1037ca1e0a81Sab196087 "TINYJ", /* 61 - EM_TINYJ */ 1038ca1e0a81Sab196087 "AMD64", /* 62 - EM_AMD64 */ 1039ca1e0a81Sab196087 "PDSP", /* 63 - EM_PDSP */ 1040ca1e0a81Sab196087 NULL, /* 64 - EM_UNKNOWN64 */ 1041ca1e0a81Sab196087 NULL, /* 65 - EM_UNKNOWN65 */ 1042ca1e0a81Sab196087 "FX66", /* 66 - EM_FX66 */ 1043ca1e0a81Sab196087 "ST9 PLUS", /* 67 - EM_ST9PLUS */ 1044ca1e0a81Sab196087 "ST7", /* 68 - EM_ST7 */ 1045ca1e0a81Sab196087 "68HC16", /* 69 - EM_68HC16 */ 1046ca1e0a81Sab196087 "68HC11", /* 70 - EM_68HC11 */ 1047ca1e0a81Sab196087 "68H08", /* 71 - EM_68HC08 */ 1048ca1e0a81Sab196087 "68HC05", /* 72 - EM_68HC05 */ 1049ca1e0a81Sab196087 "SVX", /* 73 - EM_SVX */ 1050ca1e0a81Sab196087 "ST19", /* 74 - EM_ST19 */ 1051ca1e0a81Sab196087 "VAX", /* 75 - EM_VAX */ 1052ca1e0a81Sab196087 "CRIS", /* 76 - EM_CRIS */ 1053ca1e0a81Sab196087 "Javelin", /* 77 - EM_JAVELIN */ 1054ca1e0a81Sab196087 "Firepath", /* 78 - EM_FIREPATH */ 1055ca1e0a81Sab196087 "ZSP", /* 79 - EM_ZSP */ 1056ca1e0a81Sab196087 "MMIX", /* 80 - EM_MMIX */ 1057ca1e0a81Sab196087 "HUANY", /* 81 - EM_HUANY */ 1058ca1e0a81Sab196087 "Prism", /* 82 - EM_PRISM */ 1059ca1e0a81Sab196087 "AVR", /* 83 - EM_AVR */ 1060ca1e0a81Sab196087 "FR30", /* 84 - EM_FR30 */ 1061ca1e0a81Sab196087 "D10V", /* 85 - EM_D10V */ 1062ca1e0a81Sab196087 "D30V", /* 86 - EM_D30V */ 1063ca1e0a81Sab196087 "V850", /* 87 - EM_V850 */ 1064ca1e0a81Sab196087 "M32R", /* 88 - EM_M32R */ 1065ca1e0a81Sab196087 "MN10300", /* 89 - EM_MN10300 */ 1066ca1e0a81Sab196087 "MN10200", /* 90 - EM_MN10200 */ 1067ca1e0a81Sab196087 "picoJava", /* 91 - EM_PJ */ 1068ca1e0a81Sab196087 "OpenRISC", /* 92 - EM_OPENRISC */ 1069ca1e0a81Sab196087 "Tangent-A5", /* 93 - EM_ARC_A5 */ 1070ca1e0a81Sab196087 "Xtensa" /* 94 - EM_XTENSA */ 1071ca1e0a81Sab196087 }; 1072ca1e0a81Sab196087 /* If new machine is added, refuse to compile until we're updated */ 1073ca1e0a81Sab196087 #if EM_NUM != 95 1074ca1e0a81Sab196087 #error "Number of known ELF machine constants has changed" 1075ca1e0a81Sab196087 #endif 1076ca1e0a81Sab196087 1077ca1e0a81Sab196087 const char *str; 1078ca1e0a81Sab196087 1079ca1e0a81Sab196087 if ((machine < EM_NONE) || (machine >= EM_NUM)) 1080ca1e0a81Sab196087 machine = EM_NONE; 1081ca1e0a81Sab196087 1082ca1e0a81Sab196087 str = mach_str[machine]; 1083ca1e0a81Sab196087 if (str) 1084ca1e0a81Sab196087 (void) printf(" %s", str); 10857c478bd9Sstevel@tonic-gate } 10867c478bd9Sstevel@tonic-gate 10877c478bd9Sstevel@tonic-gate static void 10887c478bd9Sstevel@tonic-gate print_elf_datatype(int datatype) 10897c478bd9Sstevel@tonic-gate { 10907c478bd9Sstevel@tonic-gate switch (datatype) { 10917c478bd9Sstevel@tonic-gate case ELFDATA2LSB: 1092ca1e0a81Sab196087 (void) printf(" LSB"); 10937c478bd9Sstevel@tonic-gate break; 10947c478bd9Sstevel@tonic-gate case ELFDATA2MSB: 1095ca1e0a81Sab196087 (void) printf(" MSB"); 10967c478bd9Sstevel@tonic-gate break; 10977c478bd9Sstevel@tonic-gate default: 10987c478bd9Sstevel@tonic-gate break; 10997c478bd9Sstevel@tonic-gate } 11007c478bd9Sstevel@tonic-gate } 11017c478bd9Sstevel@tonic-gate 11027c478bd9Sstevel@tonic-gate static void 11037c478bd9Sstevel@tonic-gate print_elf_class(int class) 11047c478bd9Sstevel@tonic-gate { 11057c478bd9Sstevel@tonic-gate switch (class) { 11067c478bd9Sstevel@tonic-gate case ELFCLASS32: 11077c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("32-bit")); 11087c478bd9Sstevel@tonic-gate break; 11097c478bd9Sstevel@tonic-gate case ELFCLASS64: 11107c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("64-bit")); 11117c478bd9Sstevel@tonic-gate break; 11127c478bd9Sstevel@tonic-gate default: 11137c478bd9Sstevel@tonic-gate break; 11147c478bd9Sstevel@tonic-gate } 11157c478bd9Sstevel@tonic-gate } 11167c478bd9Sstevel@tonic-gate 11177c478bd9Sstevel@tonic-gate static void 1118c2c65e21Sny155746 print_elf_flags(Elf_Info EI) 11197c478bd9Sstevel@tonic-gate { 1120c2c65e21Sny155746 unsigned int flags; 1121c2c65e21Sny155746 1122c2c65e21Sny155746 flags = EI.flags; 1123c2c65e21Sny155746 switch (EI.machine) { 11247c478bd9Sstevel@tonic-gate case EM_SPARCV9: 11257c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_EXT_MASK) { 11267c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_SUN_US3) { 11277c478bd9Sstevel@tonic-gate (void) printf("%s", gettext( 11287c478bd9Sstevel@tonic-gate ", UltraSPARC3 Extensions Required")); 11297c478bd9Sstevel@tonic-gate } else if (flags & EF_SPARC_SUN_US1) { 11307c478bd9Sstevel@tonic-gate (void) printf("%s", gettext( 11317c478bd9Sstevel@tonic-gate ", UltraSPARC1 Extensions Required")); 11327c478bd9Sstevel@tonic-gate } 11337c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_HAL_R1) 11347c478bd9Sstevel@tonic-gate (void) printf("%s", gettext( 11357c478bd9Sstevel@tonic-gate ", HaL R1 Extensions Required")); 11367c478bd9Sstevel@tonic-gate } 11377c478bd9Sstevel@tonic-gate break; 11387c478bd9Sstevel@tonic-gate case EM_SPARC32PLUS: 11397c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_32PLUS) 11407c478bd9Sstevel@tonic-gate (void) printf("%s", gettext(", V8+ Required")); 11417c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_SUN_US3) { 11427c478bd9Sstevel@tonic-gate (void) printf("%s", 11437c478bd9Sstevel@tonic-gate gettext(", UltraSPARC3 Extensions Required")); 11447c478bd9Sstevel@tonic-gate } else if (flags & EF_SPARC_SUN_US1) { 11457c478bd9Sstevel@tonic-gate (void) printf("%s", 11467c478bd9Sstevel@tonic-gate gettext(", UltraSPARC1 Extensions Required")); 11477c478bd9Sstevel@tonic-gate } 11487c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_HAL_R1) 11497c478bd9Sstevel@tonic-gate (void) printf("%s", 11507c478bd9Sstevel@tonic-gate gettext(", HaL R1 Extensions Required")); 11517c478bd9Sstevel@tonic-gate break; 11527c478bd9Sstevel@tonic-gate default: 11537c478bd9Sstevel@tonic-gate break; 11547c478bd9Sstevel@tonic-gate } 11557c478bd9Sstevel@tonic-gate } 11567c478bd9Sstevel@tonic-gate 1157c2c65e21Sny155746 /* 1158c2c65e21Sny155746 * check_ident: checks the ident field of the presumeably 1159c2c65e21Sny155746 * elf file. If check fails, this is not an 1160c2c65e21Sny155746 * elf file. 1161c2c65e21Sny155746 */ 11627c478bd9Sstevel@tonic-gate static int 1163c2c65e21Sny155746 check_ident(unsigned char *ident, int fd) 11647c478bd9Sstevel@tonic-gate { 1165c2c65e21Sny155746 int class; 1166c2c65e21Sny155746 if (pread64(fd, ident, EI_NIDENT, 0) != EI_NIDENT) 1167c2c65e21Sny155746 return (ELF_READ_FAIL); 1168c2c65e21Sny155746 class = ident[EI_CLASS]; 1169c2c65e21Sny155746 if (class != ELFCLASS32 && class != ELFCLASS64) 1170c2c65e21Sny155746 return (ELF_READ_FAIL); 1171c2c65e21Sny155746 if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 || 1172c2c65e21Sny155746 ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3) 1173c2c65e21Sny155746 return (ELF_READ_FAIL); 1174c2c65e21Sny155746 1175c2c65e21Sny155746 return (ELF_READ_OKAY); 1176c2c65e21Sny155746 } 1177c2c65e21Sny155746 1178c2c65e21Sny155746 static int 1179c2c65e21Sny155746 elf_check(char *file) 1180c2c65e21Sny155746 { 1181c2c65e21Sny155746 Elf_Info EInfo; 1182c2c65e21Sny155746 int class, version, format; 1183c2c65e21Sny155746 unsigned char ident[EI_NIDENT]; 1184c2c65e21Sny155746 1185c2c65e21Sny155746 (void) memset(&EInfo, 0, sizeof (Elf_Info)); 1186c2c65e21Sny155746 EInfo.file = file; 11877c478bd9Sstevel@tonic-gate 11887c478bd9Sstevel@tonic-gate /* 1189c2c65e21Sny155746 * Verify information in file indentifier. 1190c2c65e21Sny155746 * Return quietly if not elf; Different type of file. 11917c478bd9Sstevel@tonic-gate */ 1192c2c65e21Sny155746 if (check_ident(ident, elffd) == ELF_READ_FAIL) 11937c478bd9Sstevel@tonic-gate return (1); 11947c478bd9Sstevel@tonic-gate 11957c478bd9Sstevel@tonic-gate /* 1196c2c65e21Sny155746 * Read the elf headers for processing and get the 1197c2c65e21Sny155746 * get the needed information in Elf_Info struct. 11987c478bd9Sstevel@tonic-gate */ 1199c2c65e21Sny155746 class = ident[EI_CLASS]; 1200c2c65e21Sny155746 if (class == ELFCLASS32) { 1201c2c65e21Sny155746 if (elf_read32(elffd, &EInfo) == ELF_READ_FAIL) { 12029a411307Srie (void) fprintf(stderr, gettext("%s: %s: can't " 1203c2c65e21Sny155746 "read ELF header\n"), File, file); 12047c478bd9Sstevel@tonic-gate return (1); 12057c478bd9Sstevel@tonic-gate } 1206c2c65e21Sny155746 } else if (class == ELFCLASS64) { 1207c2c65e21Sny155746 if (elf_read64(elffd, &EInfo) == ELF_READ_FAIL) { 1208c2c65e21Sny155746 (void) fprintf(stderr, gettext("%s: %s: can't " 1209c2c65e21Sny155746 "read ELF header\n"), File, file); 12107c478bd9Sstevel@tonic-gate return (1); 12117c478bd9Sstevel@tonic-gate } 1212c2c65e21Sny155746 } else { 1213c2c65e21Sny155746 /* something wrong */ 1214c2c65e21Sny155746 return (1); 1215c2c65e21Sny155746 } 1216c2c65e21Sny155746 1217c2c65e21Sny155746 /* version not in ident then 1 */ 1218c2c65e21Sny155746 version = ident[EI_VERSION] ? ident[EI_VERSION] : 1; 1219c2c65e21Sny155746 1220c2c65e21Sny155746 format = ident[EI_DATA]; 12217c478bd9Sstevel@tonic-gate (void) printf("%s", gettext("ELF")); 1222c2c65e21Sny155746 print_elf_class(class); 1223c2c65e21Sny155746 print_elf_datatype(format); 1224c2c65e21Sny155746 print_elf_type(EInfo); 12257c478bd9Sstevel@tonic-gate 1226c2c65e21Sny155746 if (EInfo.core_type != EC_NOTCORE) { 1227c2c65e21Sny155746 /* Print what kind of core is this */ 1228c2c65e21Sny155746 if (EInfo.core_type == EC_OLDCORE) 1229c2c65e21Sny155746 (void) printf(" %s", gettext("pre-2.6 core file")); 1230c2c65e21Sny155746 else 1231c2c65e21Sny155746 (void) printf(" %s", gettext("core file")); 1232c2c65e21Sny155746 } 1233c2c65e21Sny155746 1234c2c65e21Sny155746 /* Print machine info */ 1235c2c65e21Sny155746 print_elf_machine(EInfo.machine); 1236c2c65e21Sny155746 1237c2c65e21Sny155746 /* Print Version */ 1238c2c65e21Sny155746 if (version == 1) 1239c2c65e21Sny155746 (void) printf(" %s %d", gettext("Version"), version); 1240c2c65e21Sny155746 1241c2c65e21Sny155746 /* Print Flags */ 1242c2c65e21Sny155746 print_elf_flags(EInfo); 1243c2c65e21Sny155746 1244c2c65e21Sny155746 /* Last bit, if it is a core */ 1245c2c65e21Sny155746 if (EInfo.core_type != EC_NOTCORE) { 1246c2c65e21Sny155746 /* Print the program name that dumped this core */ 1247c2c65e21Sny155746 (void) printf(gettext(", from '%s'"), EInfo.fname); 1248c2c65e21Sny155746 return (0); 1249c2c65e21Sny155746 } 1250c2c65e21Sny155746 1251c2c65e21Sny155746 /* Print Capabilities */ 1252c2c65e21Sny155746 if (EInfo.cap_str[0] != '\0') 1253c2c65e21Sny155746 (void) printf(" [%s]", EInfo.cap_str); 1254c2c65e21Sny155746 1255c2c65e21Sny155746 if ((EInfo.type != ET_EXEC) && (EInfo.type != ET_DYN)) 12567c478bd9Sstevel@tonic-gate return (0); 12577c478bd9Sstevel@tonic-gate 1258c2c65e21Sny155746 /* Print if it is dynamically linked */ 1259c2c65e21Sny155746 if (EInfo.dynamic) 12607c478bd9Sstevel@tonic-gate (void) printf(gettext(", dynamically linked")); 12617c478bd9Sstevel@tonic-gate else 12627c478bd9Sstevel@tonic-gate (void) printf(gettext(", statically linked")); 12637c478bd9Sstevel@tonic-gate 1264c2c65e21Sny155746 /* Printf it it is stripped */ 1265c2c65e21Sny155746 if (EInfo.stripped & E_SYMTAB) { 12667c478bd9Sstevel@tonic-gate (void) printf(gettext(", not stripped")); 1267c2c65e21Sny155746 if (!(EInfo.stripped & E_DBGINF)) { 12687c478bd9Sstevel@tonic-gate (void) printf(gettext( 12697c478bd9Sstevel@tonic-gate ", no debugging information available")); 12707c478bd9Sstevel@tonic-gate } 12717c478bd9Sstevel@tonic-gate } else { 12727c478bd9Sstevel@tonic-gate (void) printf(gettext(", stripped")); 12737c478bd9Sstevel@tonic-gate } 1274c2c65e21Sny155746 1275c2c65e21Sny155746 return (0); 12767c478bd9Sstevel@tonic-gate } 12777c478bd9Sstevel@tonic-gate 12787c478bd9Sstevel@tonic-gate /* 1279c13de8f6Sab196087 * is_rtld_config - If file is a runtime linker config file, prints 1280c13de8f6Sab196087 * the description and returns True (1). Otherwise, silently returns 1281c13de8f6Sab196087 * False (0). 1282c13de8f6Sab196087 */ 1283c13de8f6Sab196087 int 1284c13de8f6Sab196087 is_rtld_config(void) 1285c13de8f6Sab196087 { 1286c13de8f6Sab196087 Rtc_id *id; 1287c13de8f6Sab196087 1288c13de8f6Sab196087 if ((fbsz >= sizeof (*id)) && RTC_ID_TEST(fbuf)) { 1289c13de8f6Sab196087 (void) printf(gettext("Runtime Linking Configuration")); 1290c13de8f6Sab196087 id = (Rtc_id *) fbuf; 1291c13de8f6Sab196087 print_elf_class(id->id_class); 1292c13de8f6Sab196087 print_elf_datatype(id->id_data); 1293c13de8f6Sab196087 print_elf_machine(id->id_machine); 1294c13de8f6Sab196087 (void) printf("\n"); 1295c13de8f6Sab196087 return (1); 1296c13de8f6Sab196087 } 1297c13de8f6Sab196087 1298c13de8f6Sab196087 return (0); 1299c13de8f6Sab196087 } 1300c13de8f6Sab196087 1301c13de8f6Sab196087 /* 13027c478bd9Sstevel@tonic-gate * lookup - 13037c478bd9Sstevel@tonic-gate * Attempts to match one of the strings from a list, 'tab', 13047c478bd9Sstevel@tonic-gate * with what is in the file, starting at the current index position 'i'. 13057c478bd9Sstevel@tonic-gate * Looks past any initial whitespace and expects whitespace or other 13067c478bd9Sstevel@tonic-gate * delimiting characters to follow the matched string. 13077c478bd9Sstevel@tonic-gate * A match identifies the file as being 'assembler', 'fortran', 'c', etc. 13087c478bd9Sstevel@tonic-gate * Returns 1 for a successful match, 0 otherwise. 13097c478bd9Sstevel@tonic-gate */ 13107c478bd9Sstevel@tonic-gate static int 13117c478bd9Sstevel@tonic-gate lookup(char **tab) 13127c478bd9Sstevel@tonic-gate { 13137c478bd9Sstevel@tonic-gate register char r; 13147c478bd9Sstevel@tonic-gate register int k, j, l; 13157c478bd9Sstevel@tonic-gate 13167c478bd9Sstevel@tonic-gate while (fbuf[i] == ' ' || fbuf[i] == '\t' || fbuf[i] == '\n') 13177c478bd9Sstevel@tonic-gate i++; 13187c478bd9Sstevel@tonic-gate for (j = 0; tab[j] != 0; j++) { 13197c478bd9Sstevel@tonic-gate l = 0; 1320*ca3e8d88SDave Plauger for (k = i; ((r = tab[j][l++]) == fbuf[k] && r != '\0'); k++) 1321*ca3e8d88SDave Plauger ; 13227c478bd9Sstevel@tonic-gate if (r == '\0') 13237c478bd9Sstevel@tonic-gate if (fbuf[k] == ' ' || fbuf[k] == '\n' || 13247c478bd9Sstevel@tonic-gate fbuf[k] == '\t' || fbuf[k] == '{' || 13257c478bd9Sstevel@tonic-gate fbuf[k] == '/') { 13267c478bd9Sstevel@tonic-gate i = k; 13277c478bd9Sstevel@tonic-gate return (1); 13287c478bd9Sstevel@tonic-gate } 13297c478bd9Sstevel@tonic-gate } 13307c478bd9Sstevel@tonic-gate return (0); 13317c478bd9Sstevel@tonic-gate } 13327c478bd9Sstevel@tonic-gate 13337c478bd9Sstevel@tonic-gate /* 13347c478bd9Sstevel@tonic-gate * ccom - 13357c478bd9Sstevel@tonic-gate * Increments the current index 'i' into the file buffer 'fbuf' past any 13367c478bd9Sstevel@tonic-gate * whitespace lines and C-style comments found, starting at the current 13377c478bd9Sstevel@tonic-gate * position of 'i'. Returns 1 as long as we don't increment i past the 13387c478bd9Sstevel@tonic-gate * size of fbuf (fbsz). Otherwise, returns 0. 13397c478bd9Sstevel@tonic-gate */ 13407c478bd9Sstevel@tonic-gate 13417c478bd9Sstevel@tonic-gate static int 13427c478bd9Sstevel@tonic-gate ccom(void) 13437c478bd9Sstevel@tonic-gate { 13447c478bd9Sstevel@tonic-gate register char cc; 13457c478bd9Sstevel@tonic-gate int len; 13467c478bd9Sstevel@tonic-gate 13477c478bd9Sstevel@tonic-gate while ((cc = fbuf[i]) == ' ' || cc == '\t' || cc == '\n') 13487c478bd9Sstevel@tonic-gate if (i++ >= fbsz) 13497c478bd9Sstevel@tonic-gate return (0); 13507c478bd9Sstevel@tonic-gate if (fbuf[i] == '/' && fbuf[i+1] == '*') { 13517c478bd9Sstevel@tonic-gate i += 2; 13527c478bd9Sstevel@tonic-gate while (fbuf[i] != '*' || fbuf[i+1] != '/') { 13537c478bd9Sstevel@tonic-gate if (fbuf[i] == '\\') 13547c478bd9Sstevel@tonic-gate i++; 13557c478bd9Sstevel@tonic-gate if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0) 13567c478bd9Sstevel@tonic-gate len = 1; 13577c478bd9Sstevel@tonic-gate i += len; 13587c478bd9Sstevel@tonic-gate if (i >= fbsz) 13597c478bd9Sstevel@tonic-gate return (0); 13607c478bd9Sstevel@tonic-gate } 13617c478bd9Sstevel@tonic-gate if ((i += 2) >= fbsz) 13627c478bd9Sstevel@tonic-gate return (0); 13637c478bd9Sstevel@tonic-gate } 13647c478bd9Sstevel@tonic-gate if (fbuf[i] == '\n') 13657c478bd9Sstevel@tonic-gate if (ccom() == 0) 13667c478bd9Sstevel@tonic-gate return (0); 13677c478bd9Sstevel@tonic-gate return (1); 13687c478bd9Sstevel@tonic-gate } 13697c478bd9Sstevel@tonic-gate 13707c478bd9Sstevel@tonic-gate /* 13717c478bd9Sstevel@tonic-gate * ascom - 13727c478bd9Sstevel@tonic-gate * Increments the current index 'i' into the file buffer 'fbuf' past 13737c478bd9Sstevel@tonic-gate * consecutive assembler program comment lines starting with ASCOMCHAR, 13747c478bd9Sstevel@tonic-gate * starting at the current position of 'i'. 13757c478bd9Sstevel@tonic-gate * Returns 1 as long as we don't increment i past the 13767c478bd9Sstevel@tonic-gate * size of fbuf (fbsz). Otherwise returns 0. 13777c478bd9Sstevel@tonic-gate */ 13787c478bd9Sstevel@tonic-gate 13797c478bd9Sstevel@tonic-gate static int 13807c478bd9Sstevel@tonic-gate ascom(void) 13817c478bd9Sstevel@tonic-gate { 13827c478bd9Sstevel@tonic-gate while (fbuf[i] == ASCOMCHAR) { 13837c478bd9Sstevel@tonic-gate i++; 13847c478bd9Sstevel@tonic-gate while (fbuf[i++] != '\n') 13857c478bd9Sstevel@tonic-gate if (i >= fbsz) 13867c478bd9Sstevel@tonic-gate return (0); 13877c478bd9Sstevel@tonic-gate while (fbuf[i] == '\n') 13887c478bd9Sstevel@tonic-gate if (i++ >= fbsz) 13897c478bd9Sstevel@tonic-gate return (0); 13907c478bd9Sstevel@tonic-gate } 13917c478bd9Sstevel@tonic-gate return (1); 13927c478bd9Sstevel@tonic-gate } 13937c478bd9Sstevel@tonic-gate 13947c478bd9Sstevel@tonic-gate static int 13957c478bd9Sstevel@tonic-gate sccs(void) 13967c478bd9Sstevel@tonic-gate { /* look for "1hddddd" where d is a digit */ 13977c478bd9Sstevel@tonic-gate register int j; 13987c478bd9Sstevel@tonic-gate 13997c478bd9Sstevel@tonic-gate if (fbuf[0] == 1 && fbuf[1] == 'h') { 14007c478bd9Sstevel@tonic-gate for (j = 2; j <= 6; j++) { 14017c478bd9Sstevel@tonic-gate if (isdigit(fbuf[j])) 14027c478bd9Sstevel@tonic-gate continue; 14037c478bd9Sstevel@tonic-gate else 14047c478bd9Sstevel@tonic-gate return (0); 14057c478bd9Sstevel@tonic-gate } 14067c478bd9Sstevel@tonic-gate } else { 14077c478bd9Sstevel@tonic-gate return (0); 14087c478bd9Sstevel@tonic-gate } 14097c478bd9Sstevel@tonic-gate return (1); 14107c478bd9Sstevel@tonic-gate } 14117c478bd9Sstevel@tonic-gate 14127c478bd9Sstevel@tonic-gate static int 14137c478bd9Sstevel@tonic-gate english(char *bp, int n) 14147c478bd9Sstevel@tonic-gate { 14157c478bd9Sstevel@tonic-gate #define NASC 128 /* number of ascii char ?? */ 14167c478bd9Sstevel@tonic-gate register int j, vow, freq, rare, len; 14177c478bd9Sstevel@tonic-gate register int badpun = 0, punct = 0; 14187c478bd9Sstevel@tonic-gate int ct[NASC]; 14197c478bd9Sstevel@tonic-gate 14207c478bd9Sstevel@tonic-gate if (n < 50) 14217c478bd9Sstevel@tonic-gate return (0); /* no point in statistics on squibs */ 14227c478bd9Sstevel@tonic-gate for (j = 0; j < NASC; j++) 14237c478bd9Sstevel@tonic-gate ct[j] = 0; 14247c478bd9Sstevel@tonic-gate for (j = 0; j < n; j += len) { 14257c478bd9Sstevel@tonic-gate if ((unsigned char)bp[j] < NASC) 14267c478bd9Sstevel@tonic-gate ct[bp[j]|040]++; 14277c478bd9Sstevel@tonic-gate switch (bp[j]) { 14287c478bd9Sstevel@tonic-gate case '.': 14297c478bd9Sstevel@tonic-gate case ',': 14307c478bd9Sstevel@tonic-gate case ')': 14317c478bd9Sstevel@tonic-gate case '%': 14327c478bd9Sstevel@tonic-gate case ';': 14337c478bd9Sstevel@tonic-gate case ':': 14347c478bd9Sstevel@tonic-gate case '?': 14357c478bd9Sstevel@tonic-gate punct++; 14367c478bd9Sstevel@tonic-gate if (j < n-1 && bp[j+1] != ' ' && bp[j+1] != '\n') 14377c478bd9Sstevel@tonic-gate badpun++; 14387c478bd9Sstevel@tonic-gate } 14397c478bd9Sstevel@tonic-gate if ((len = mblen(&bp[j], MB_CUR_MAX)) <= 0) 14407c478bd9Sstevel@tonic-gate len = 1; 14417c478bd9Sstevel@tonic-gate } 14427c478bd9Sstevel@tonic-gate if (badpun*5 > punct) 14437c478bd9Sstevel@tonic-gate return (0); 14447c478bd9Sstevel@tonic-gate vow = ct['a'] + ct['e'] + ct['i'] + ct['o'] + ct['u']; 14457c478bd9Sstevel@tonic-gate freq = ct['e'] + ct['t'] + ct['a'] + ct['i'] + ct['o'] + ct['n']; 14467c478bd9Sstevel@tonic-gate rare = ct['v'] + ct['j'] + ct['k'] + ct['q'] + ct['x'] + ct['z']; 14477c478bd9Sstevel@tonic-gate if (2*ct[';'] > ct['e']) 14487c478bd9Sstevel@tonic-gate return (0); 14497c478bd9Sstevel@tonic-gate if ((ct['>'] + ct['<'] + ct['/']) > ct['e']) 14507c478bd9Sstevel@tonic-gate return (0); /* shell file test */ 14517c478bd9Sstevel@tonic-gate return (vow * 5 >= n - ct[' '] && freq >= 10 * rare); 14527c478bd9Sstevel@tonic-gate } 14537c478bd9Sstevel@tonic-gate 14547c478bd9Sstevel@tonic-gate 14557c478bd9Sstevel@tonic-gate static int 14567c478bd9Sstevel@tonic-gate shellscript(char buf[], struct stat64 *sb) 14577c478bd9Sstevel@tonic-gate { 14587c478bd9Sstevel@tonic-gate char *tp, *cp, *xp, *up, *gp; 14597c478bd9Sstevel@tonic-gate 14607c478bd9Sstevel@tonic-gate cp = strchr(buf, '\n'); 14617c478bd9Sstevel@tonic-gate if (cp == NULL || cp - fbuf > fbsz) 14627c478bd9Sstevel@tonic-gate return (0); 14637c478bd9Sstevel@tonic-gate for (tp = buf; tp != cp && isspace((unsigned char)*tp); tp++) 14647c478bd9Sstevel@tonic-gate if (!isascii(*tp)) 14657c478bd9Sstevel@tonic-gate return (0); 14667c478bd9Sstevel@tonic-gate for (xp = tp; tp != cp && !isspace((unsigned char)*tp); tp++) 14677c478bd9Sstevel@tonic-gate if (!isascii(*tp)) 14687c478bd9Sstevel@tonic-gate return (0); 14697c478bd9Sstevel@tonic-gate if (tp == xp) 14707c478bd9Sstevel@tonic-gate return (0); 14717c478bd9Sstevel@tonic-gate if (sb->st_mode & S_ISUID) 14727c478bd9Sstevel@tonic-gate up = gettext("set-uid "); 14737c478bd9Sstevel@tonic-gate else 14747c478bd9Sstevel@tonic-gate up = ""; 14757c478bd9Sstevel@tonic-gate 14767c478bd9Sstevel@tonic-gate if (sb->st_mode & S_ISGID) 14777c478bd9Sstevel@tonic-gate gp = gettext("set-gid "); 14787c478bd9Sstevel@tonic-gate else 14797c478bd9Sstevel@tonic-gate gp = ""; 14807c478bd9Sstevel@tonic-gate 14817c478bd9Sstevel@tonic-gate if (strncmp(xp, "/bin/sh", tp - xp) == 0) 14827c478bd9Sstevel@tonic-gate xp = gettext("shell"); 14837c478bd9Sstevel@tonic-gate else if (strncmp(xp, "/bin/csh", tp - xp) == 0) 14847c478bd9Sstevel@tonic-gate xp = gettext("c-shell"); 14857c478bd9Sstevel@tonic-gate else if (strncmp(xp, "/usr/sbin/dtrace", tp - xp) == 0) 14867c478bd9Sstevel@tonic-gate xp = gettext("DTrace"); 14877c478bd9Sstevel@tonic-gate else 14887c478bd9Sstevel@tonic-gate *tp = '\0'; 14897c478bd9Sstevel@tonic-gate /* 14907c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 14917c478bd9Sstevel@tonic-gate * This message is printed by file command for shell scripts. 14927c478bd9Sstevel@tonic-gate * The first %s is for the translation for "set-uid " (if the script 14937c478bd9Sstevel@tonic-gate * has the set-uid bit set), or is for an empty string (if the 14947c478bd9Sstevel@tonic-gate * script does not have the set-uid bit set). 14957c478bd9Sstevel@tonic-gate * Similarly, the second %s is for the translation for "set-gid ", 14967c478bd9Sstevel@tonic-gate * or is for an empty string. 14977c478bd9Sstevel@tonic-gate * The third %s is for the translation for either: "shell", "c-shell", 14987c478bd9Sstevel@tonic-gate * or "DTrace", or is for the pathname of the program the script 14997c478bd9Sstevel@tonic-gate * executes. 15007c478bd9Sstevel@tonic-gate */ 15017c478bd9Sstevel@tonic-gate (void) printf(gettext("%s%sexecutable %s script\n"), up, gp, xp); 15027c478bd9Sstevel@tonic-gate return (1); 15037c478bd9Sstevel@tonic-gate } 15047c478bd9Sstevel@tonic-gate 15057c478bd9Sstevel@tonic-gate static int 15067c478bd9Sstevel@tonic-gate get_door_target(char *file, char *buf, size_t bufsize) 15077c478bd9Sstevel@tonic-gate { 15087c478bd9Sstevel@tonic-gate int fd; 15097c478bd9Sstevel@tonic-gate door_info_t di; 15107c478bd9Sstevel@tonic-gate psinfo_t psinfo; 15117c478bd9Sstevel@tonic-gate 15127c478bd9Sstevel@tonic-gate if ((fd = open64(file, O_RDONLY)) < 0 || 15137c478bd9Sstevel@tonic-gate door_info(fd, &di) != 0) { 15147c478bd9Sstevel@tonic-gate if (fd >= 0) 15157c478bd9Sstevel@tonic-gate (void) close(fd); 15167c478bd9Sstevel@tonic-gate return (-1); 15177c478bd9Sstevel@tonic-gate } 15187c478bd9Sstevel@tonic-gate (void) close(fd); 15197c478bd9Sstevel@tonic-gate 15207c478bd9Sstevel@tonic-gate (void) sprintf(buf, "/proc/%ld/psinfo", di.di_target); 15217c478bd9Sstevel@tonic-gate if ((fd = open64(buf, O_RDONLY)) < 0 || 15227c478bd9Sstevel@tonic-gate read(fd, &psinfo, sizeof (psinfo)) != sizeof (psinfo)) { 15237c478bd9Sstevel@tonic-gate if (fd >= 0) 15247c478bd9Sstevel@tonic-gate (void) close(fd); 15257c478bd9Sstevel@tonic-gate return (-1); 15267c478bd9Sstevel@tonic-gate } 15277c478bd9Sstevel@tonic-gate (void) close(fd); 15287c478bd9Sstevel@tonic-gate 15297c478bd9Sstevel@tonic-gate (void) snprintf(buf, bufsize, "%s[%ld]", psinfo.pr_fname, di.di_target); 15307c478bd9Sstevel@tonic-gate return (0); 15317c478bd9Sstevel@tonic-gate } 15327c478bd9Sstevel@tonic-gate 15337c478bd9Sstevel@tonic-gate /* 15347c478bd9Sstevel@tonic-gate * ZIP file header information 15357c478bd9Sstevel@tonic-gate */ 15367c478bd9Sstevel@tonic-gate #define SIGSIZ 4 15377c478bd9Sstevel@tonic-gate #define LOCSIG "PK\003\004" 15387c478bd9Sstevel@tonic-gate #define LOCHDRSIZ 30 15397c478bd9Sstevel@tonic-gate 15407c478bd9Sstevel@tonic-gate #define CH(b, n) (((unsigned char *)(b))[n]) 15417c478bd9Sstevel@tonic-gate #define SH(b, n) (CH(b, n) | (CH(b, n+1) << 8)) 15427c478bd9Sstevel@tonic-gate #define LG(b, n) (SH(b, n) | (SH(b, n+2) << 16)) 15437c478bd9Sstevel@tonic-gate 15447c478bd9Sstevel@tonic-gate #define LOCNAM(b) (SH(b, 26)) /* filename size */ 15457c478bd9Sstevel@tonic-gate #define LOCEXT(b) (SH(b, 28)) /* extra field size */ 15467c478bd9Sstevel@tonic-gate 15477c478bd9Sstevel@tonic-gate #define XFHSIZ 4 /* header id, data size */ 15487c478bd9Sstevel@tonic-gate #define XFHID(b) (SH(b, 0)) /* extract field header id */ 15497c478bd9Sstevel@tonic-gate #define XFDATASIZ(b) (SH(b, 2)) /* extract field data size */ 15507c478bd9Sstevel@tonic-gate #define XFJAVASIG 0xcafe /* java executables */ 15517c478bd9Sstevel@tonic-gate 15527c478bd9Sstevel@tonic-gate static int 15537c478bd9Sstevel@tonic-gate zipfile(char *fbuf, int fd) 15547c478bd9Sstevel@tonic-gate { 15557c478bd9Sstevel@tonic-gate off_t xoff, xoff_end; 15567c478bd9Sstevel@tonic-gate 15577c478bd9Sstevel@tonic-gate if (strncmp(fbuf, LOCSIG, SIGSIZ) != 0) 15587c478bd9Sstevel@tonic-gate return (0); 15597c478bd9Sstevel@tonic-gate 15607c478bd9Sstevel@tonic-gate xoff = LOCHDRSIZ + LOCNAM(fbuf); 15617c478bd9Sstevel@tonic-gate xoff_end = xoff + LOCEXT(fbuf); 15627c478bd9Sstevel@tonic-gate 15637c478bd9Sstevel@tonic-gate while (xoff < xoff_end) { 15647c478bd9Sstevel@tonic-gate char xfhdr[XFHSIZ]; 15657c478bd9Sstevel@tonic-gate 15667c478bd9Sstevel@tonic-gate if (pread(fd, xfhdr, XFHSIZ, xoff) != XFHSIZ) 15677c478bd9Sstevel@tonic-gate break; 15687c478bd9Sstevel@tonic-gate 15697c478bd9Sstevel@tonic-gate if (XFHID(xfhdr) == XFJAVASIG) { 1570ea51a530Sny155746 (void) printf("%s\n", gettext("java archive file")); 15717c478bd9Sstevel@tonic-gate return (1); 15727c478bd9Sstevel@tonic-gate } 15737c478bd9Sstevel@tonic-gate xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr); 15747c478bd9Sstevel@tonic-gate } 15757c478bd9Sstevel@tonic-gate 15767c478bd9Sstevel@tonic-gate /* 15777c478bd9Sstevel@tonic-gate * We could just print "ZIP archive" here. 15787c478bd9Sstevel@tonic-gate * 15797c478bd9Sstevel@tonic-gate * However, customers may be using their own entries in 15807c478bd9Sstevel@tonic-gate * /etc/magic to distinguish one kind of ZIP file from another, so 15817c478bd9Sstevel@tonic-gate * let's defer the printing of "ZIP archive" to there. 15827c478bd9Sstevel@tonic-gate */ 15837c478bd9Sstevel@tonic-gate return (0); 15847c478bd9Sstevel@tonic-gate } 15857c478bd9Sstevel@tonic-gate 15867c478bd9Sstevel@tonic-gate static int 15877c478bd9Sstevel@tonic-gate is_crash_dump(const char *buf, int fd) 15887c478bd9Sstevel@tonic-gate { 15897c478bd9Sstevel@tonic-gate /* LINTED: pointer cast may result in improper alignment */ 15907c478bd9Sstevel@tonic-gate const dumphdr_t *dhp = (const dumphdr_t *)buf; 15917c478bd9Sstevel@tonic-gate 15927c478bd9Sstevel@tonic-gate /* 15937c478bd9Sstevel@tonic-gate * The current DUMP_MAGIC string covers Solaris 7 and later releases. 15947c478bd9Sstevel@tonic-gate * The utsname struct is only present in dumphdr_t's with dump_version 15957c478bd9Sstevel@tonic-gate * greater than or equal to 9. 15967c478bd9Sstevel@tonic-gate */ 15977c478bd9Sstevel@tonic-gate if (dhp->dump_magic == DUMP_MAGIC) { 15987c478bd9Sstevel@tonic-gate print_dumphdr(fd, dhp, return_uint32, NATIVE_ISA); 15997c478bd9Sstevel@tonic-gate 16007c478bd9Sstevel@tonic-gate } else if (dhp->dump_magic == swap_uint32(DUMP_MAGIC)) { 16017c478bd9Sstevel@tonic-gate print_dumphdr(fd, dhp, swap_uint32, OTHER_ISA); 16027c478bd9Sstevel@tonic-gate 16037c478bd9Sstevel@tonic-gate } else if (dhp->dump_magic == OLD_DUMP_MAGIC || 16047c478bd9Sstevel@tonic-gate dhp->dump_magic == swap_uint32(OLD_DUMP_MAGIC)) { 16057c478bd9Sstevel@tonic-gate char *isa = (dhp->dump_magic == OLD_DUMP_MAGIC ? 16067c478bd9Sstevel@tonic-gate NATIVE_ISA : OTHER_ISA); 16077c478bd9Sstevel@tonic-gate (void) printf(gettext("SunOS 32-bit %s crash dump\n"), isa); 16087c478bd9Sstevel@tonic-gate 16097c478bd9Sstevel@tonic-gate } else { 16107c478bd9Sstevel@tonic-gate return (0); 16117c478bd9Sstevel@tonic-gate } 16127c478bd9Sstevel@tonic-gate 16137c478bd9Sstevel@tonic-gate return (1); 16147c478bd9Sstevel@tonic-gate } 16157c478bd9Sstevel@tonic-gate 16167c478bd9Sstevel@tonic-gate static void 16177c478bd9Sstevel@tonic-gate print_dumphdr(const int fd, const dumphdr_t *dhp, uint32_t (*swap)(uint32_t), 16187c478bd9Sstevel@tonic-gate const char *isa) 16197c478bd9Sstevel@tonic-gate { 16207c478bd9Sstevel@tonic-gate dumphdr_t dh; 16217c478bd9Sstevel@tonic-gate 16227c478bd9Sstevel@tonic-gate /* 16237c478bd9Sstevel@tonic-gate * A dumphdr_t is bigger than FBSZ, so we have to manually read the 16247c478bd9Sstevel@tonic-gate * rest of it. 16257c478bd9Sstevel@tonic-gate */ 16267c478bd9Sstevel@tonic-gate if (swap(dhp->dump_version) > 8 && pread(fd, &dh, sizeof (dumphdr_t), 16277c478bd9Sstevel@tonic-gate (off_t)0) == sizeof (dumphdr_t)) { 1628*ca3e8d88SDave Plauger const char *c = swap(dh.dump_flags) & DF_COMPRESSED ? 1629*ca3e8d88SDave Plauger "compressed " : ""; 1630*ca3e8d88SDave Plauger const char *l = swap(dh.dump_flags) & DF_LIVE ? 1631*ca3e8d88SDave Plauger "live" : "crash"; 1632*ca3e8d88SDave Plauger 16337c478bd9Sstevel@tonic-gate (void) printf(gettext( 1634*ca3e8d88SDave Plauger "%s %s %s %u-bit %s %s%s dump from '%s'\n"), 16357c478bd9Sstevel@tonic-gate dh.dump_utsname.sysname, dh.dump_utsname.release, 16367c478bd9Sstevel@tonic-gate dh.dump_utsname.version, swap(dh.dump_wordsize), isa, 1637*ca3e8d88SDave Plauger c, l, dh.dump_utsname.nodename); 16387c478bd9Sstevel@tonic-gate } else { 16397c478bd9Sstevel@tonic-gate (void) printf(gettext("SunOS %u-bit %s crash dump\n"), 16407c478bd9Sstevel@tonic-gate swap(dhp->dump_wordsize), isa); 16417c478bd9Sstevel@tonic-gate } 16427c478bd9Sstevel@tonic-gate } 16437c478bd9Sstevel@tonic-gate 16447c478bd9Sstevel@tonic-gate static void 16457c478bd9Sstevel@tonic-gate usage(void) 16467c478bd9Sstevel@tonic-gate { 16477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 16487c478bd9Sstevel@tonic-gate "usage: file [-dh] [-M mfile] [-m mfile] [-f ffile] file ...\n" 16497c478bd9Sstevel@tonic-gate " file [-dh] [-M mfile] [-m mfile] -f ffile\n" 16507c478bd9Sstevel@tonic-gate " file -i [-h] [-f ffile] file ...\n" 16517c478bd9Sstevel@tonic-gate " file -i [-h] -f ffile\n" 16527c478bd9Sstevel@tonic-gate " file -c [-d] [-M mfile] [-m mfile]\n")); 16537c478bd9Sstevel@tonic-gate exit(2); 16547c478bd9Sstevel@tonic-gate } 16557c478bd9Sstevel@tonic-gate 16567c478bd9Sstevel@tonic-gate static uint32_t 16577c478bd9Sstevel@tonic-gate swap_uint32(uint32_t in) 16587c478bd9Sstevel@tonic-gate { 16597c478bd9Sstevel@tonic-gate uint32_t out; 16607c478bd9Sstevel@tonic-gate 16617c478bd9Sstevel@tonic-gate out = (in & 0x000000ff) << 24; 16627c478bd9Sstevel@tonic-gate out |= (in & 0x0000ff00) << 8; /* >> 8 << 16 */ 16637c478bd9Sstevel@tonic-gate out |= (in & 0x00ff0000) >> 8; /* >> 16 << 8 */ 16647c478bd9Sstevel@tonic-gate out |= (in & 0xff000000) >> 24; 16657c478bd9Sstevel@tonic-gate 16667c478bd9Sstevel@tonic-gate return (out); 16677c478bd9Sstevel@tonic-gate } 16687c478bd9Sstevel@tonic-gate 16697c478bd9Sstevel@tonic-gate static uint32_t 16707c478bd9Sstevel@tonic-gate return_uint32(uint32_t in) 16717c478bd9Sstevel@tonic-gate { 16727c478bd9Sstevel@tonic-gate return (in); 16737c478bd9Sstevel@tonic-gate } 16747c478bd9Sstevel@tonic-gate 16757c478bd9Sstevel@tonic-gate /* 16767c478bd9Sstevel@tonic-gate * Check if str is in the string list str_list. 16777c478bd9Sstevel@tonic-gate */ 1678c2c65e21Sny155746 int 1679c2c65e21Sny155746 is_in_list(char *str) 16807c478bd9Sstevel@tonic-gate { 16817c478bd9Sstevel@tonic-gate int i; 16827c478bd9Sstevel@tonic-gate 16837c478bd9Sstevel@tonic-gate /* 16847c478bd9Sstevel@tonic-gate * Only need to compare the strlen(str_list[i]) bytes. 16857c478bd9Sstevel@tonic-gate * That way .stab will match on .stab* sections, and 16867c478bd9Sstevel@tonic-gate * .debug will match on .debug* sections. 16877c478bd9Sstevel@tonic-gate */ 1688c2c65e21Sny155746 for (i = 0; debug_sections[i] != NULL; i++) { 1689c2c65e21Sny155746 if (strncmp(debug_sections[i], str, 1690c2c65e21Sny155746 strlen(debug_sections[i])) == 0) { 16917c478bd9Sstevel@tonic-gate return (1); 16927c478bd9Sstevel@tonic-gate } 16937c478bd9Sstevel@tonic-gate } 16947c478bd9Sstevel@tonic-gate return (0); 16957c478bd9Sstevel@tonic-gate } 16967c478bd9Sstevel@tonic-gate 16977c478bd9Sstevel@tonic-gate /* 16987c478bd9Sstevel@tonic-gate * default_magic - 16997c478bd9Sstevel@tonic-gate * allocate space for and create the default magic file 17007c478bd9Sstevel@tonic-gate * name string. 17017c478bd9Sstevel@tonic-gate */ 17027c478bd9Sstevel@tonic-gate 17037c478bd9Sstevel@tonic-gate static void 17047c478bd9Sstevel@tonic-gate default_magic(void) 17057c478bd9Sstevel@tonic-gate { 17067c478bd9Sstevel@tonic-gate const char *msg_locale = setlocale(LC_MESSAGES, NULL); 17077c478bd9Sstevel@tonic-gate struct stat statbuf; 17087c478bd9Sstevel@tonic-gate 17099a411307Srie if ((dfile = malloc(strlen(msg_locale) + 35)) == NULL) { 17109a411307Srie int err = errno; 17119a411307Srie (void) fprintf(stderr, gettext("%s: malloc failed: %s\n"), 17129a411307Srie File, strerror(err)); 17137c478bd9Sstevel@tonic-gate exit(2); 17147c478bd9Sstevel@tonic-gate } 17157c478bd9Sstevel@tonic-gate (void) snprintf(dfile, strlen(msg_locale) + 35, 17167c478bd9Sstevel@tonic-gate "/usr/lib/locale/%s/LC_MESSAGES/magic", msg_locale); 17177c478bd9Sstevel@tonic-gate if (stat(dfile, &statbuf) != 0) { 17187c478bd9Sstevel@tonic-gate (void) strcpy(dfile, "/etc/magic"); 17197c478bd9Sstevel@tonic-gate } 17207c478bd9Sstevel@tonic-gate } 17217c478bd9Sstevel@tonic-gate 17227c478bd9Sstevel@tonic-gate /* 17237c478bd9Sstevel@tonic-gate * add_to_mlist - 17247c478bd9Sstevel@tonic-gate * Add the given magic_file filename string to the list of magic 17257c478bd9Sstevel@tonic-gate * files (mlist). This list of files will later be examined, and 17267c478bd9Sstevel@tonic-gate * each magic file's entries will be added in order to 17277c478bd9Sstevel@tonic-gate * the mtab table. 17287c478bd9Sstevel@tonic-gate * 17297c478bd9Sstevel@tonic-gate * The first flag is set to 1 to add to the first list, mlist1. 17307c478bd9Sstevel@tonic-gate * The first flag is set to 0 to add to the second list, mlist2. 17317c478bd9Sstevel@tonic-gate */ 17327c478bd9Sstevel@tonic-gate 17337c478bd9Sstevel@tonic-gate static void 17347c478bd9Sstevel@tonic-gate add_to_mlist(char *magic_file, int first) 17357c478bd9Sstevel@tonic-gate { 17367c478bd9Sstevel@tonic-gate char **mlist; /* ordered list of magic files */ 17377c478bd9Sstevel@tonic-gate size_t mlist_sz; /* number of pointers allocated for mlist */ 17387c478bd9Sstevel@tonic-gate char **mlistp; /* next entry in mlist */ 17397c478bd9Sstevel@tonic-gate size_t mlistp_off; 17407c478bd9Sstevel@tonic-gate 17417c478bd9Sstevel@tonic-gate if (first) { 17427c478bd9Sstevel@tonic-gate mlist = mlist1; 17437c478bd9Sstevel@tonic-gate mlist_sz = mlist1_sz; 17447c478bd9Sstevel@tonic-gate mlistp = mlist1p; 17457c478bd9Sstevel@tonic-gate } else { 17467c478bd9Sstevel@tonic-gate mlist = mlist2; 17477c478bd9Sstevel@tonic-gate mlist_sz = mlist2_sz; 17487c478bd9Sstevel@tonic-gate mlistp = mlist2p; 17497c478bd9Sstevel@tonic-gate } 17507c478bd9Sstevel@tonic-gate 17517c478bd9Sstevel@tonic-gate if (mlist == NULL) { /* initial mlist allocation */ 17529a411307Srie if ((mlist = calloc(MLIST_SZ, sizeof (char *))) == NULL) { 17539a411307Srie int err = errno; 17549a411307Srie (void) fprintf(stderr, gettext("%s: malloc " 17559a411307Srie "failed: %s\n"), File, strerror(err)); 17567c478bd9Sstevel@tonic-gate exit(2); 17577c478bd9Sstevel@tonic-gate } 17587c478bd9Sstevel@tonic-gate mlist_sz = MLIST_SZ; 17597c478bd9Sstevel@tonic-gate mlistp = mlist; 17607c478bd9Sstevel@tonic-gate } 17617c478bd9Sstevel@tonic-gate if ((mlistp - mlist) >= mlist_sz) { 17627c478bd9Sstevel@tonic-gate mlistp_off = mlistp - mlist; 17637c478bd9Sstevel@tonic-gate mlist_sz *= 2; 17649a411307Srie if ((mlist = realloc(mlist, 17657c478bd9Sstevel@tonic-gate mlist_sz * sizeof (char *))) == NULL) { 17669a411307Srie int err = errno; 17679a411307Srie (void) fprintf(stderr, gettext("%s: malloc " 17689a411307Srie "failed: %s\n"), File, strerror(err)); 17697c478bd9Sstevel@tonic-gate exit(2); 17707c478bd9Sstevel@tonic-gate } 17717c478bd9Sstevel@tonic-gate mlistp = mlist + mlistp_off; 17727c478bd9Sstevel@tonic-gate } 17737c478bd9Sstevel@tonic-gate /* 17747c478bd9Sstevel@tonic-gate * now allocate memory for and copy the 17757c478bd9Sstevel@tonic-gate * magic file name string 17767c478bd9Sstevel@tonic-gate */ 17777c478bd9Sstevel@tonic-gate if ((*mlistp = malloc(strlen(magic_file) + 1)) == NULL) { 17789a411307Srie int err = errno; 17799a411307Srie (void) fprintf(stderr, gettext("%s: malloc failed: %s\n"), 17809a411307Srie File, strerror(err)); 17817c478bd9Sstevel@tonic-gate exit(2); 17827c478bd9Sstevel@tonic-gate } 17837c478bd9Sstevel@tonic-gate (void) strlcpy(*mlistp, magic_file, strlen(magic_file) + 1); 17847c478bd9Sstevel@tonic-gate mlistp++; 17857c478bd9Sstevel@tonic-gate 17867c478bd9Sstevel@tonic-gate if (first) { 17877c478bd9Sstevel@tonic-gate mlist1 = mlist; 17887c478bd9Sstevel@tonic-gate mlist1_sz = mlist_sz; 17897c478bd9Sstevel@tonic-gate mlist1p = mlistp; 17907c478bd9Sstevel@tonic-gate } else { 17917c478bd9Sstevel@tonic-gate mlist2 = mlist; 17927c478bd9Sstevel@tonic-gate mlist2_sz = mlist_sz; 17937c478bd9Sstevel@tonic-gate mlist2p = mlistp; 17947c478bd9Sstevel@tonic-gate } 17957c478bd9Sstevel@tonic-gate } 17967c478bd9Sstevel@tonic-gate 17977c478bd9Sstevel@tonic-gate static void 17987c478bd9Sstevel@tonic-gate fd_cleanup(void) 17997c478bd9Sstevel@tonic-gate { 18007c478bd9Sstevel@tonic-gate if (ifd != -1) { 18017c478bd9Sstevel@tonic-gate (void) close(ifd); 18027c478bd9Sstevel@tonic-gate ifd = -1; 18037c478bd9Sstevel@tonic-gate } 18047c478bd9Sstevel@tonic-gate if (elffd != -1) { 18057c478bd9Sstevel@tonic-gate (void) close(elffd); 18067c478bd9Sstevel@tonic-gate elffd = -1; 18077c478bd9Sstevel@tonic-gate } 18087c478bd9Sstevel@tonic-gate } 1809