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 /*
29ca3e8d88SDave 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
main(int argc,char ** argv)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;
208*6e987ca0SDavid Höppner int bflg = 0;
2097c478bd9Sstevel@tonic-gate int cflg = 0;
2107c478bd9Sstevel@tonic-gate int eflg = 0;
2117c478bd9Sstevel@tonic-gate int fflg = 0;
2127c478bd9Sstevel@tonic-gate char *ap = NULL;
2137c478bd9Sstevel@tonic-gate int pathlen;
2147c478bd9Sstevel@tonic-gate char **filep;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
2177c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
2187c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
2197c478bd9Sstevel@tonic-gate #endif
2207c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
2217c478bd9Sstevel@tonic-gate
222*6e987ca0SDavid Höppner while ((ch = getopt(argc, argv, "M:bcdf:him:")) != EOF) {
2237c478bd9Sstevel@tonic-gate switch (ch) {
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate case 'M':
2267c478bd9Sstevel@tonic-gate add_to_mlist(optarg, !dflg);
2277c478bd9Sstevel@tonic-gate M_flg++;
2287c478bd9Sstevel@tonic-gate break;
2297c478bd9Sstevel@tonic-gate
230*6e987ca0SDavid Höppner case 'b':
231*6e987ca0SDavid Höppner bflg++;
232*6e987ca0SDavid Höppner break;
233*6e987ca0SDavid Höppner
2347c478bd9Sstevel@tonic-gate case 'c':
2357c478bd9Sstevel@tonic-gate cflg++;
2367c478bd9Sstevel@tonic-gate break;
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate case 'd':
2397c478bd9Sstevel@tonic-gate if (!dflg) {
2407c478bd9Sstevel@tonic-gate default_magic();
2417c478bd9Sstevel@tonic-gate add_to_mlist(dfile, 0);
2427c478bd9Sstevel@tonic-gate dflg++;
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate break;
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate case 'f':
2477c478bd9Sstevel@tonic-gate fflg++;
2489a411307Srie errno = 0;
2497c478bd9Sstevel@tonic-gate if ((fl = fopen(optarg, "r")) == NULL) {
2509a411307Srie int err = errno;
2519a411307Srie (void) fprintf(stderr, gettext("%s: cannot "
2529a411307Srie "open file %s: %s\n"), File, optarg,
2539a411307Srie err ? strerror(err) : "");
2547c478bd9Sstevel@tonic-gate usage();
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate pathlen = pathconf("/", _PC_PATH_MAX);
2577c478bd9Sstevel@tonic-gate if (pathlen == -1) {
2589a411307Srie int err = errno;
2599a411307Srie (void) fprintf(stderr, gettext("%s: cannot "
2609a411307Srie "determine maximum path length: %s\n"),
2619a411307Srie File, strerror(err));
2627c478bd9Sstevel@tonic-gate exit(1);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate pathlen += 2; /* for null and newline in fgets */
2659a411307Srie if ((ap = malloc(pathlen * sizeof (char))) == NULL) {
2669a411307Srie int err = errno;
2679a411307Srie (void) fprintf(stderr, gettext("%s: malloc "
2689a411307Srie "failed: %s\n"), File, strerror(err));
2699a411307Srie exit(2);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate break;
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate case 'h':
2747c478bd9Sstevel@tonic-gate hflg++;
2757c478bd9Sstevel@tonic-gate break;
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate case 'i':
2787c478bd9Sstevel@tonic-gate iflg++;
2797c478bd9Sstevel@tonic-gate break;
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate case 'm':
2827c478bd9Sstevel@tonic-gate add_to_mlist(optarg, !dflg);
2837c478bd9Sstevel@tonic-gate mflg++;
2847c478bd9Sstevel@tonic-gate break;
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate case '?':
2877c478bd9Sstevel@tonic-gate eflg++;
2887c478bd9Sstevel@tonic-gate break;
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate if (!cflg && !fflg && (eflg || optind == argc))
2927c478bd9Sstevel@tonic-gate usage();
2937c478bd9Sstevel@tonic-gate if (iflg && (dflg || mflg || M_flg)) {
2947c478bd9Sstevel@tonic-gate usage();
2957c478bd9Sstevel@tonic-gate }
296*6e987ca0SDavid Höppner if ((iflg && cflg) || (cflg && bflg)) {
2977c478bd9Sstevel@tonic-gate usage();
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate if (!dflg && !mflg && !M_flg && !iflg) {
3017c478bd9Sstevel@tonic-gate /* no -d, -m, nor -M option; also -i option doesn't need magic */
3027c478bd9Sstevel@tonic-gate default_magic();
3037c478bd9Sstevel@tonic-gate if (f_mkmtab(dfile, cflg, 0) == -1) {
3047c478bd9Sstevel@tonic-gate exit(2);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate else if (mflg && !M_flg && !dflg) {
3097c478bd9Sstevel@tonic-gate /* -m specified without -d nor -M */
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate #ifdef XPG4 /* For SUSv3 only */
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate * The default position-dependent magic file tests
3157c478bd9Sstevel@tonic-gate * in /etc/magic will follow all the -m magic tests.
3167c478bd9Sstevel@tonic-gate */
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate for (filep = mlist1; filep < mlist1p; filep++) {
3197c478bd9Sstevel@tonic-gate if (f_mkmtab(*filep, cflg, 1) == -1) {
3207c478bd9Sstevel@tonic-gate exit(2);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate default_magic();
3247c478bd9Sstevel@tonic-gate if (f_mkmtab(dfile, cflg, 0) == -1) {
3257c478bd9Sstevel@tonic-gate exit(2);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate #else /* !XPG4 */
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate * Retain Solaris file behavior for -m before SUSv3,
3307c478bd9Sstevel@tonic-gate * when the new -d and -M options are not specified.
3317c478bd9Sstevel@tonic-gate * Use the -m file specified in place of the default
3327c478bd9Sstevel@tonic-gate * /etc/magic file. Solaris file will
3337c478bd9Sstevel@tonic-gate * now allow more than one magic file to be specified
3347c478bd9Sstevel@tonic-gate * with multiple -m options, for consistency with
3357c478bd9Sstevel@tonic-gate * other behavior.
3367c478bd9Sstevel@tonic-gate *
3377c478bd9Sstevel@tonic-gate * Put the magic table(s) specified by -m into
3387c478bd9Sstevel@tonic-gate * the second magic table instead of the first
3397c478bd9Sstevel@tonic-gate * (as indicated by the last argument to f_mkmtab()),
3407c478bd9Sstevel@tonic-gate * since they replace the /etc/magic tests and
3417c478bd9Sstevel@tonic-gate * must be executed alongside the default
3427c478bd9Sstevel@tonic-gate * position-sensitive tests.
3437c478bd9Sstevel@tonic-gate */
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate for (filep = mlist1; filep < mlist1p; filep++) {
3467c478bd9Sstevel@tonic-gate if (f_mkmtab(*filep, cflg, 0) == -1) {
3477c478bd9Sstevel@tonic-gate exit(2);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate #endif /* XPG4 */
3517c478bd9Sstevel@tonic-gate } else {
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate * For any other combination of -d, -m, and -M,
3547c478bd9Sstevel@tonic-gate * use the magic files in command-line order.
3557c478bd9Sstevel@tonic-gate * Store the entries from the two separate lists of magic
3567c478bd9Sstevel@tonic-gate * files, if any, into two separate magic file tables.
3577c478bd9Sstevel@tonic-gate * mlist1: magic tests executed before default magic tests
3587c478bd9Sstevel@tonic-gate * mlist2: default magic tests and after
3597c478bd9Sstevel@tonic-gate */
3607c478bd9Sstevel@tonic-gate for (filep = mlist1; filep && (filep < mlist1p); filep++) {
3617c478bd9Sstevel@tonic-gate if (f_mkmtab(*filep, cflg, 1) == -1) {
3627c478bd9Sstevel@tonic-gate exit(2);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate for (filep = mlist2; filep && (filep < mlist2p); filep++) {
3667c478bd9Sstevel@tonic-gate if (f_mkmtab(*filep, cflg, 0) == -1) {
3677c478bd9Sstevel@tonic-gate exit(2);
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate /* Initialize the magic file variables; check both magic tables */
3737c478bd9Sstevel@tonic-gate tmpmax = f_getmaxoffset(1);
3747c478bd9Sstevel@tonic-gate maxmagicoffset = f_getmaxoffset(0);
3757c478bd9Sstevel@tonic-gate if (maxmagicoffset < tmpmax) {
3767c478bd9Sstevel@tonic-gate maxmagicoffset = tmpmax;
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate if (maxmagicoffset < (intmax_t)FBSZ)
3797c478bd9Sstevel@tonic-gate maxmagicoffset = (intmax_t)FBSZ;
3809a411307Srie if ((magicbuf = malloc(maxmagicoffset)) == NULL) {
3819a411307Srie int err = errno;
3829a411307Srie (void) fprintf(stderr, gettext("%s: malloc failed: %s\n"),
3839a411307Srie File, strerror(err));
3847c478bd9Sstevel@tonic-gate exit(2);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate if (cflg) {
3887c478bd9Sstevel@tonic-gate f_prtmtab();
3897c478bd9Sstevel@tonic-gate if (ferror(stdout) != 0) {
3909a411307Srie (void) fprintf(stderr, gettext("%s: error writing to "
3919a411307Srie "stdout\n"), File);
3927c478bd9Sstevel@tonic-gate exit(1);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate if (fclose(stdout) != 0) {
3959a411307Srie int err = errno;
3969a411307Srie (void) fprintf(stderr, gettext("%s: fclose "
3979a411307Srie "failed: %s\n"), File, strerror(err));
3987c478bd9Sstevel@tonic-gate exit(1);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate exit(0);
4017c478bd9Sstevel@tonic-gate }
4029a411307Srie
4037c478bd9Sstevel@tonic-gate for (; fflg || optind < argc; optind += !fflg) {
4047c478bd9Sstevel@tonic-gate register int l;
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate if (fflg) {
4077c478bd9Sstevel@tonic-gate if ((p = fgets(ap, pathlen, fl)) == NULL) {
4087c478bd9Sstevel@tonic-gate fflg = 0;
4097c478bd9Sstevel@tonic-gate optind--;
4107c478bd9Sstevel@tonic-gate continue;
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate l = strlen(p);
4137c478bd9Sstevel@tonic-gate if (l > 0)
4147c478bd9Sstevel@tonic-gate p[l - 1] = '\0';
4157c478bd9Sstevel@tonic-gate } else
4167c478bd9Sstevel@tonic-gate p = argv[optind];
417*6e987ca0SDavid Höppner
418*6e987ca0SDavid Höppner if (!bflg)
4197c478bd9Sstevel@tonic-gate prf(p); /* print "file_name:<tab>" */
4207c478bd9Sstevel@tonic-gate
4217c478bd9Sstevel@tonic-gate if (type(p))
4227c478bd9Sstevel@tonic-gate tret = 1;
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate if (ap != NULL)
4257c478bd9Sstevel@tonic-gate free(ap);
4269a411307Srie if (tret != 0)
4277c478bd9Sstevel@tonic-gate exit(tret);
4289a411307Srie
4297c478bd9Sstevel@tonic-gate if (ferror(stdout) != 0) {
4309a411307Srie (void) fprintf(stderr, gettext("%s: error writing to "
4319a411307Srie "stdout\n"), File);
4327c478bd9Sstevel@tonic-gate exit(1);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate if (fclose(stdout) != 0) {
4359a411307Srie int err = errno;
4369a411307Srie (void) fprintf(stderr, gettext("%s: fclose failed: %s\n"),
4379a411307Srie File, strerror(err));
4387c478bd9Sstevel@tonic-gate exit(1);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate return (0);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate
4437c478bd9Sstevel@tonic-gate static int
type(char * file)4447c478bd9Sstevel@tonic-gate type(char *file)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate int cc;
4477c478bd9Sstevel@tonic-gate char buf[BUFSIZ];
4487c478bd9Sstevel@tonic-gate int (*statf)() = hflg ? lstat64 : stat64;
4497c478bd9Sstevel@tonic-gate
4507c478bd9Sstevel@tonic-gate i = 0; /* reset index to beginning of file */
4517c478bd9Sstevel@tonic-gate ifd = -1;
4527c478bd9Sstevel@tonic-gate if ((*statf)(file, &mbuf) < 0) {
4537c478bd9Sstevel@tonic-gate if (statf == lstat64 || lstat64(file, &mbuf) < 0) {
4549a411307Srie int err = errno;
4557c478bd9Sstevel@tonic-gate (void) printf(gettext("cannot open: %s\n"),
4569a411307Srie strerror(err));
4577c478bd9Sstevel@tonic-gate return (0); /* POSIX.2 */
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate switch (mbuf.st_mode & S_IFMT) {
4617c478bd9Sstevel@tonic-gate case S_IFREG:
4627c478bd9Sstevel@tonic-gate if (iflg) {
4637c478bd9Sstevel@tonic-gate (void) printf(gettext("regular file\n"));
4647c478bd9Sstevel@tonic-gate return (0);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate break;
4677c478bd9Sstevel@tonic-gate case S_IFCHR:
4687c478bd9Sstevel@tonic-gate (void) printf(gettext("character"));
4697c478bd9Sstevel@tonic-gate goto spcl;
4707c478bd9Sstevel@tonic-gate
4717c478bd9Sstevel@tonic-gate case S_IFDIR:
4727c478bd9Sstevel@tonic-gate (void) printf(gettext("directory\n"));
4737c478bd9Sstevel@tonic-gate return (0);
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate case S_IFIFO:
4767c478bd9Sstevel@tonic-gate (void) printf(gettext("fifo\n"));
4777c478bd9Sstevel@tonic-gate return (0);
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate case S_IFLNK:
4807c478bd9Sstevel@tonic-gate if ((cc = readlink(file, buf, BUFSIZ)) < 0) {
4819a411307Srie int err = errno;
4827c478bd9Sstevel@tonic-gate (void) printf(gettext("readlink error: %s\n"),
4839a411307Srie strerror(err));
4847c478bd9Sstevel@tonic-gate return (1);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate buf[cc] = '\0';
4877c478bd9Sstevel@tonic-gate (void) printf(gettext("symbolic link to %s\n"), buf);
4887c478bd9Sstevel@tonic-gate return (0);
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate case S_IFBLK:
4917c478bd9Sstevel@tonic-gate (void) printf(gettext("block"));
4927c478bd9Sstevel@tonic-gate /* major and minor, see sys/mkdev.h */
4937c478bd9Sstevel@tonic-gate spcl:
4947c478bd9Sstevel@tonic-gate (void) printf(gettext(" special (%d/%d)\n"),
4957c478bd9Sstevel@tonic-gate major(mbuf.st_rdev), minor(mbuf.st_rdev));
4967c478bd9Sstevel@tonic-gate return (0);
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate case S_IFSOCK:
4997c478bd9Sstevel@tonic-gate (void) printf("socket\n");
5007c478bd9Sstevel@tonic-gate /* FIXME, should open and try to getsockname. */
5017c478bd9Sstevel@tonic-gate return (0);
5027c478bd9Sstevel@tonic-gate
5037c478bd9Sstevel@tonic-gate case S_IFDOOR:
5047c478bd9Sstevel@tonic-gate if (get_door_target(file, buf, sizeof (buf)) == 0)
5057c478bd9Sstevel@tonic-gate (void) printf(gettext("door to %s\n"), buf);
5067c478bd9Sstevel@tonic-gate else
5077c478bd9Sstevel@tonic-gate (void) printf(gettext("door\n"));
5087c478bd9Sstevel@tonic-gate return (0);
5097c478bd9Sstevel@tonic-gate
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) {
5137c478bd9Sstevel@tonic-gate (void) printf(gettext("libelf is out of date\n"));
5147c478bd9Sstevel@tonic-gate return (1);
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate ifd = open64(file, O_RDONLY);
5187c478bd9Sstevel@tonic-gate if (ifd < 0) {
5199a411307Srie int err = errno;
5209a411307Srie (void) printf(gettext("cannot open: %s\n"), strerror(err));
5217c478bd9Sstevel@tonic-gate return (0); /* POSIX.2 */
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate /* need another fd for elf, since we might want to read the file too */
5257c478bd9Sstevel@tonic-gate elffd = open64(file, O_RDONLY);
5267c478bd9Sstevel@tonic-gate if (elffd < 0) {
5279a411307Srie int err = errno;
5289a411307Srie (void) printf(gettext("cannot open: %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 = read(ifd, fbuf, FBSZ)) == -1) {
5349a411307Srie int err = errno;
5359a411307Srie (void) printf(gettext("cannot read: %s\n"), strerror(err));
5367c478bd9Sstevel@tonic-gate (void) close(ifd);
5377c478bd9Sstevel@tonic-gate ifd = -1;
5387c478bd9Sstevel@tonic-gate return (0); /* POSIX.2 */
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate if (fbsz == 0) {
5417c478bd9Sstevel@tonic-gate (void) printf(gettext("empty file\n"));
5427c478bd9Sstevel@tonic-gate fd_cleanup();
5437c478bd9Sstevel@tonic-gate return (0);
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate * First try user-specified position-dependent magic tests, if any,
5487c478bd9Sstevel@tonic-gate * which need to execute before the default tests.
5497c478bd9Sstevel@tonic-gate */
5507c478bd9Sstevel@tonic-gate if ((mread = pread(ifd, (void*)magicbuf, (size_t)maxmagicoffset,
5517c478bd9Sstevel@tonic-gate (off_t)0)) == -1) {
5529a411307Srie int err = errno;
5539a411307Srie (void) printf(gettext("cannot read: %s\n"), strerror(err));
5547c478bd9Sstevel@tonic-gate fd_cleanup();
5557c478bd9Sstevel@tonic-gate return (0);
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate
5587c478bd9Sstevel@tonic-gate /*
5597c478bd9Sstevel@tonic-gate * ChecK against Magic Table entries.
5607c478bd9Sstevel@tonic-gate * Check first magic table for magic tests to be applied
5617c478bd9Sstevel@tonic-gate * before default tests.
5627c478bd9Sstevel@tonic-gate * If no default tests are to be applied, all magic tests
5637c478bd9Sstevel@tonic-gate * should occur in this magic table.
5647c478bd9Sstevel@tonic-gate */
5657c478bd9Sstevel@tonic-gate switch (f_ckmtab(magicbuf, mread, 1)) {
5667c478bd9Sstevel@tonic-gate case -1: /* Error */
5677c478bd9Sstevel@tonic-gate exit(2);
5687c478bd9Sstevel@tonic-gate break;
5697c478bd9Sstevel@tonic-gate case 0: /* Not magic */
5707c478bd9Sstevel@tonic-gate break;
5717c478bd9Sstevel@tonic-gate default: /* Switch is magic index */
5727c478bd9Sstevel@tonic-gate (void) putchar('\n');
5737c478bd9Sstevel@tonic-gate fd_cleanup();
5747c478bd9Sstevel@tonic-gate return (0);
5757c478bd9Sstevel@tonic-gate /* NOTREACHED */
5767c478bd9Sstevel@tonic-gate break;
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate
5797c478bd9Sstevel@tonic-gate if (dflg || !M_flg) {
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate * default position-dependent tests,
5827c478bd9Sstevel@tonic-gate * plus non-default magic tests, if any
5837c478bd9Sstevel@tonic-gate */
5849a411307Srie switch (def_position_tests(file)) {
5857c478bd9Sstevel@tonic-gate case -1: /* error */
5867c478bd9Sstevel@tonic-gate fd_cleanup();
5877c478bd9Sstevel@tonic-gate return (1);
5887c478bd9Sstevel@tonic-gate case 1: /* matching type found */
5897c478bd9Sstevel@tonic-gate fd_cleanup();
5907c478bd9Sstevel@tonic-gate return (0);
5917c478bd9Sstevel@tonic-gate /* NOTREACHED */
5927c478bd9Sstevel@tonic-gate break;
5937c478bd9Sstevel@tonic-gate case 0: /* no matching type found */
5947c478bd9Sstevel@tonic-gate break;
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate /* default context-sensitive tests */
5977c478bd9Sstevel@tonic-gate def_context_tests();
5987c478bd9Sstevel@tonic-gate } else {
5997c478bd9Sstevel@tonic-gate /* no more tests to apply; no match was found */
6007c478bd9Sstevel@tonic-gate (void) printf(gettext("data\n"));
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate fd_cleanup();
6037c478bd9Sstevel@tonic-gate return (0);
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate
6067c478bd9Sstevel@tonic-gate /*
6077c478bd9Sstevel@tonic-gate * def_position_tests() - applies default position-sensitive tests,
6087c478bd9Sstevel@tonic-gate * looking for values in specific positions in the file.
6097c478bd9Sstevel@tonic-gate * These are followed by default (followed by possibly some
6107c478bd9Sstevel@tonic-gate * non-default) magic file tests.
6117c478bd9Sstevel@tonic-gate *
6127c478bd9Sstevel@tonic-gate * All position-sensitive tests, default or otherwise, must
6137c478bd9Sstevel@tonic-gate * be applied before context-sensitive tests, to avoid
6147c478bd9Sstevel@tonic-gate * false context-sensitive matches.
6157c478bd9Sstevel@tonic-gate *
6167c478bd9Sstevel@tonic-gate * Returns -1 on error which should result in error (non-zero)
6177c478bd9Sstevel@tonic-gate * exit status for the file utility.
6187c478bd9Sstevel@tonic-gate * Returns 0 if no matching file type found.
6197c478bd9Sstevel@tonic-gate * Returns 1 if matching file type found.
6207c478bd9Sstevel@tonic-gate */
6217c478bd9Sstevel@tonic-gate
6227c478bd9Sstevel@tonic-gate static int
def_position_tests(char * file)6239a411307Srie def_position_tests(char *file)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate if (sccs()) { /* look for "1hddddd" where d is a digit */
6267c478bd9Sstevel@tonic-gate (void) printf("sccs \n");
6277c478bd9Sstevel@tonic-gate return (1);
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate if (fbuf[0] == '#' && fbuf[1] == '!' && shellscript(fbuf+2, &mbuf))
6307c478bd9Sstevel@tonic-gate return (1);
631c2c65e21Sny155746
632c2c65e21Sny155746 if (elf_check(file) == 0) {
6337c478bd9Sstevel@tonic-gate (void) putchar('\n');
6347c478bd9Sstevel@tonic-gate return (1);
6357c478bd9Sstevel@tonic-gate /* LINTED: pointer cast may result in improper alignment */
6367c478bd9Sstevel@tonic-gate } else if (*(int *)fbuf == CORE_MAGIC) {
6377c478bd9Sstevel@tonic-gate /* LINTED: pointer cast may result in improper alignment */
6387c478bd9Sstevel@tonic-gate struct core *corep = (struct core *)fbuf;
6397c478bd9Sstevel@tonic-gate
6407c478bd9Sstevel@tonic-gate (void) printf("a.out core file");
6417c478bd9Sstevel@tonic-gate
6427c478bd9Sstevel@tonic-gate if (*(corep->c_cmdname) != '\0')
6437c478bd9Sstevel@tonic-gate (void) printf(" from '%s'", corep->c_cmdname);
6447c478bd9Sstevel@tonic-gate (void) putchar('\n');
6457c478bd9Sstevel@tonic-gate return (1);
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate
6487c478bd9Sstevel@tonic-gate /*
649c13de8f6Sab196087 * Runtime linker (ld.so.1) configuration file.
650c13de8f6Sab196087 */
651c13de8f6Sab196087 if (is_rtld_config())
652c13de8f6Sab196087 return (1);
653c13de8f6Sab196087
654c13de8f6Sab196087 /*
6557c478bd9Sstevel@tonic-gate * ZIP files, JAR files, and Java executables
6567c478bd9Sstevel@tonic-gate */
6577c478bd9Sstevel@tonic-gate if (zipfile(fbuf, ifd))
6587c478bd9Sstevel@tonic-gate return (1);
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate if (is_crash_dump(fbuf, ifd))
6617c478bd9Sstevel@tonic-gate return (1);
6627c478bd9Sstevel@tonic-gate
6637c478bd9Sstevel@tonic-gate /*
6647c478bd9Sstevel@tonic-gate * ChecK against Magic Table entries.
6657c478bd9Sstevel@tonic-gate * The magic entries checked here always start with default
6667c478bd9Sstevel@tonic-gate * magic tests and may be followed by other, non-default magic
6677c478bd9Sstevel@tonic-gate * tests. If no default tests are to be executed, all the
6687c478bd9Sstevel@tonic-gate * magic tests should have been in the first magic table.
6697c478bd9Sstevel@tonic-gate */
6707c478bd9Sstevel@tonic-gate switch (f_ckmtab(magicbuf, mread, 0)) {
6717c478bd9Sstevel@tonic-gate case -1: /* Error */
6727c478bd9Sstevel@tonic-gate exit(2);
6737c478bd9Sstevel@tonic-gate break;
6747c478bd9Sstevel@tonic-gate case 0: /* Not magic */
6757c478bd9Sstevel@tonic-gate return (0);
6767c478bd9Sstevel@tonic-gate /* NOTREACHED */
6777c478bd9Sstevel@tonic-gate break;
6787c478bd9Sstevel@tonic-gate default: /* Switch is magic index */
6797c478bd9Sstevel@tonic-gate
6807c478bd9Sstevel@tonic-gate /*
6817c478bd9Sstevel@tonic-gate * f_ckmtab recognizes file type,
6827c478bd9Sstevel@tonic-gate * check if it is PostScript.
6837c478bd9Sstevel@tonic-gate * if not, check if elf or a.out
6847c478bd9Sstevel@tonic-gate */
6857c478bd9Sstevel@tonic-gate if (magicbuf[0] == '%' && magicbuf[1] == '!') {
6867c478bd9Sstevel@tonic-gate (void) putchar('\n');
6877c478bd9Sstevel@tonic-gate } else {
6887c478bd9Sstevel@tonic-gate
6897c478bd9Sstevel@tonic-gate /*
6907c478bd9Sstevel@tonic-gate * Check that the file is executable (dynamic
6917c478bd9Sstevel@tonic-gate * objects must be executable to be exec'ed,
6927c478bd9Sstevel@tonic-gate * shared objects need not be, but by convention
6937c478bd9Sstevel@tonic-gate * should be executable).
6947c478bd9Sstevel@tonic-gate *
6957c478bd9Sstevel@tonic-gate * Note that we should already have processed
6967c478bd9Sstevel@tonic-gate * the file if it was an ELF file.
6977c478bd9Sstevel@tonic-gate */
6987c478bd9Sstevel@tonic-gate ar_coff_or_aout(elffd);
6997c478bd9Sstevel@tonic-gate (void) putchar('\n');
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate return (1);
7027c478bd9Sstevel@tonic-gate /* NOTREACHED */
7037c478bd9Sstevel@tonic-gate break;
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate
7067c478bd9Sstevel@tonic-gate return (0); /* file was not identified */
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate
7097c478bd9Sstevel@tonic-gate /*
7107c478bd9Sstevel@tonic-gate * def_context_tests() - default context-sensitive tests.
7117c478bd9Sstevel@tonic-gate * These are the last tests to be applied.
7127c478bd9Sstevel@tonic-gate * If no match is found, prints out "data".
7137c478bd9Sstevel@tonic-gate */
7147c478bd9Sstevel@tonic-gate
7157c478bd9Sstevel@tonic-gate static void
def_context_tests(void)7167c478bd9Sstevel@tonic-gate def_context_tests(void)
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate int j;
7197c478bd9Sstevel@tonic-gate int nl;
7207c478bd9Sstevel@tonic-gate char ch;
7217c478bd9Sstevel@tonic-gate int len;
7227c478bd9Sstevel@tonic-gate
7237c478bd9Sstevel@tonic-gate if (ccom() == 0)
7247c478bd9Sstevel@tonic-gate goto notc;
7257c478bd9Sstevel@tonic-gate while (fbuf[i] == '#') {
7267c478bd9Sstevel@tonic-gate j = i;
7277c478bd9Sstevel@tonic-gate while (fbuf[i++] != '\n') {
7287c478bd9Sstevel@tonic-gate if (i - j > 255) {
7297c478bd9Sstevel@tonic-gate (void) printf(gettext("data\n"));
7307c478bd9Sstevel@tonic-gate return;
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate if (i >= fbsz)
7337c478bd9Sstevel@tonic-gate goto notc;
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate if (ccom() == 0)
7367c478bd9Sstevel@tonic-gate goto notc;
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate check:
7397c478bd9Sstevel@tonic-gate if (lookup(c) == 1) {
7407c478bd9Sstevel@tonic-gate while ((ch = fbuf[i]) != ';' && ch != '{') {
7417c478bd9Sstevel@tonic-gate if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
7427c478bd9Sstevel@tonic-gate len = 1;
7437c478bd9Sstevel@tonic-gate i += len;
7447c478bd9Sstevel@tonic-gate if (i >= fbsz)
7457c478bd9Sstevel@tonic-gate goto notc;
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate (void) printf(gettext("c program text"));
7487c478bd9Sstevel@tonic-gate goto outa;
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate nl = 0;
7517c478bd9Sstevel@tonic-gate while (fbuf[i] != '(') {
7527c478bd9Sstevel@tonic-gate if (fbuf[i] <= 0)
7537c478bd9Sstevel@tonic-gate goto notas;
7547c478bd9Sstevel@tonic-gate if (fbuf[i] == ';') {
7557c478bd9Sstevel@tonic-gate i++;
7567c478bd9Sstevel@tonic-gate goto check;
7577c478bd9Sstevel@tonic-gate }
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 (fbuf[i++] == '\n')
7667c478bd9Sstevel@tonic-gate if (nl++ > 6)
7677c478bd9Sstevel@tonic-gate goto notc;
7687c478bd9Sstevel@tonic-gate if (i >= fbsz)
7697c478bd9Sstevel@tonic-gate goto notc;
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate while (fbuf[i] != '{') {
7727c478bd9Sstevel@tonic-gate if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
7737c478bd9Sstevel@tonic-gate len = 1;
7747c478bd9Sstevel@tonic-gate if (fbuf[i] == '\n')
7757c478bd9Sstevel@tonic-gate if (nl++ > 6)
7767c478bd9Sstevel@tonic-gate goto notc;
7777c478bd9Sstevel@tonic-gate i += len;
7787c478bd9Sstevel@tonic-gate if (i >= fbsz)
7797c478bd9Sstevel@tonic-gate goto notc;
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate (void) printf(gettext("c program text"));
7827c478bd9Sstevel@tonic-gate goto outa;
7837c478bd9Sstevel@tonic-gate notc:
7847c478bd9Sstevel@tonic-gate i = 0; /* reset to begining of file again */
7857c478bd9Sstevel@tonic-gate while (fbuf[i] == 'c' || fbuf[i] == 'C'|| fbuf[i] == '!' ||
7867c478bd9Sstevel@tonic-gate fbuf[i] == '*' || fbuf[i] == '\n') {
7877c478bd9Sstevel@tonic-gate while (fbuf[i++] != '\n')
7887c478bd9Sstevel@tonic-gate if (i >= fbsz)
7897c478bd9Sstevel@tonic-gate goto notfort;
7907c478bd9Sstevel@tonic-gate }
7917c478bd9Sstevel@tonic-gate if (lookup(fort) == 1) {
7927c478bd9Sstevel@tonic-gate (void) printf(gettext("fortran program text"));
7937c478bd9Sstevel@tonic-gate goto outa;
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate notfort: /* looking for assembler program */
7967c478bd9Sstevel@tonic-gate i = 0; /* reset to beginning of file again */
7977c478bd9Sstevel@tonic-gate if (ccom() == 0) /* assembler programs may contain */
7987c478bd9Sstevel@tonic-gate /* c-style comments */
7997c478bd9Sstevel@tonic-gate goto notas;
8007c478bd9Sstevel@tonic-gate if (ascom() == 0)
8017c478bd9Sstevel@tonic-gate goto notas;
8027c478bd9Sstevel@tonic-gate j = i - 1;
8037c478bd9Sstevel@tonic-gate if (fbuf[i] == '.') {
8047c478bd9Sstevel@tonic-gate i++;
8057c478bd9Sstevel@tonic-gate if (lookup(as) == 1) {
8067c478bd9Sstevel@tonic-gate (void) printf(gettext("assembler program text"));
8077c478bd9Sstevel@tonic-gate goto outa;
8087c478bd9Sstevel@tonic-gate } else if (j != -1 && fbuf[j] == '\n' && isalpha(fbuf[j + 2])) {
8097c478bd9Sstevel@tonic-gate (void) printf(
8107c478bd9Sstevel@tonic-gate gettext("[nt]roff, tbl, or eqn input text"));
8117c478bd9Sstevel@tonic-gate goto outa;
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate while (lookup(asc) == 0) {
8157c478bd9Sstevel@tonic-gate if (ccom() == 0)
8167c478bd9Sstevel@tonic-gate goto notas;
8177c478bd9Sstevel@tonic-gate if (ascom() == 0)
8187c478bd9Sstevel@tonic-gate goto notas;
8197c478bd9Sstevel@tonic-gate while (fbuf[i] != '\n' && fbuf[i++] != ':') {
8207c478bd9Sstevel@tonic-gate if (i >= fbsz)
8217c478bd9Sstevel@tonic-gate goto notas;
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate while (fbuf[i] == '\n' || fbuf[i] == ' ' || fbuf[i] == '\t')
8247c478bd9Sstevel@tonic-gate if (i++ >= fbsz)
8257c478bd9Sstevel@tonic-gate goto notas;
8267c478bd9Sstevel@tonic-gate j = i - 1;
8277c478bd9Sstevel@tonic-gate if (fbuf[i] == '.') {
8287c478bd9Sstevel@tonic-gate i++;
8297c478bd9Sstevel@tonic-gate if (lookup(as) == 1) {
8307c478bd9Sstevel@tonic-gate (void) printf(
8317c478bd9Sstevel@tonic-gate gettext("assembler program text"));
8327c478bd9Sstevel@tonic-gate goto outa;
8337c478bd9Sstevel@tonic-gate } else if (fbuf[j] == '\n' && isalpha(fbuf[j+2])) {
8347c478bd9Sstevel@tonic-gate (void) printf(
8357c478bd9Sstevel@tonic-gate gettext("[nt]roff, tbl, or eqn input "
8367c478bd9Sstevel@tonic-gate "text"));
8377c478bd9Sstevel@tonic-gate goto outa;
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate (void) printf(gettext("assembler program text"));
8427c478bd9Sstevel@tonic-gate goto outa;
8437c478bd9Sstevel@tonic-gate notas:
8447c478bd9Sstevel@tonic-gate /* start modification for multibyte env */
8457c478bd9Sstevel@tonic-gate IS_ascii = 1;
8467c478bd9Sstevel@tonic-gate if (fbsz < FBSZ)
8477c478bd9Sstevel@tonic-gate Max = fbsz;
8487c478bd9Sstevel@tonic-gate else
8497c478bd9Sstevel@tonic-gate Max = FBSZ - MB_LEN_MAX; /* prevent cut of wchar read */
8507c478bd9Sstevel@tonic-gate /* end modification for multibyte env */
8517c478bd9Sstevel@tonic-gate
8527c478bd9Sstevel@tonic-gate for (i = 0; i < Max; /* null */)
8537c478bd9Sstevel@tonic-gate if (fbuf[i] & 0200) {
8547c478bd9Sstevel@tonic-gate IS_ascii = 0;
8557c478bd9Sstevel@tonic-gate if (fbuf[0] == '\100' && fbuf[1] == '\357') {
8567c478bd9Sstevel@tonic-gate (void) printf(gettext("troff output\n"));
8577c478bd9Sstevel@tonic-gate return;
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate /* start modification for multibyte env */
8607c478bd9Sstevel@tonic-gate if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX))
8617c478bd9Sstevel@tonic-gate <= 0 || !iswprint(wchar)) {
8627c478bd9Sstevel@tonic-gate (void) printf(gettext("data\n"));
8637c478bd9Sstevel@tonic-gate return;
8647c478bd9Sstevel@tonic-gate }
8657c478bd9Sstevel@tonic-gate i += length;
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate else
8687c478bd9Sstevel@tonic-gate i++;
8697c478bd9Sstevel@tonic-gate i = fbsz;
8707c478bd9Sstevel@tonic-gate /* end modification for multibyte env */
8717c478bd9Sstevel@tonic-gate if (mbuf.st_mode&(S_IXUSR|S_IXGRP|S_IXOTH))
8727c478bd9Sstevel@tonic-gate (void) printf(gettext("commands text"));
8737c478bd9Sstevel@tonic-gate else if (troffint(fbuf, fbsz))
8747c478bd9Sstevel@tonic-gate (void) printf(gettext("troff intermediate output text"));
8757c478bd9Sstevel@tonic-gate else if (english(fbuf, fbsz))
8767c478bd9Sstevel@tonic-gate (void) printf(gettext("English text"));
8777c478bd9Sstevel@tonic-gate else if (IS_ascii)
8787c478bd9Sstevel@tonic-gate (void) printf(gettext("ascii text"));
8797c478bd9Sstevel@tonic-gate else
8807c478bd9Sstevel@tonic-gate (void) printf(gettext("text")); /* for multibyte env */
8817c478bd9Sstevel@tonic-gate outa:
8827c478bd9Sstevel@tonic-gate /*
8837c478bd9Sstevel@tonic-gate * This code is to make sure that no MB char is cut in half
8847c478bd9Sstevel@tonic-gate * while still being used.
8857c478bd9Sstevel@tonic-gate */
8867c478bd9Sstevel@tonic-gate fbsz = (fbsz < FBSZ ? fbsz : fbsz - MB_CUR_MAX + 1);
8877c478bd9Sstevel@tonic-gate while (i < fbsz) {
8887c478bd9Sstevel@tonic-gate if (isascii(fbuf[i])) {
8897c478bd9Sstevel@tonic-gate i++;
8907c478bd9Sstevel@tonic-gate continue;
8917c478bd9Sstevel@tonic-gate } else {
8927c478bd9Sstevel@tonic-gate if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX))
8937c478bd9Sstevel@tonic-gate <= 0 || !iswprint(wchar)) {
8947c478bd9Sstevel@tonic-gate (void) printf(gettext(" with garbage\n"));
8957c478bd9Sstevel@tonic-gate return;
8967c478bd9Sstevel@tonic-gate }
8977c478bd9Sstevel@tonic-gate i = i + length;
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate (void) printf("\n");
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate
9037c478bd9Sstevel@tonic-gate static int
troffint(char * bp,int n)9047c478bd9Sstevel@tonic-gate troffint(char *bp, int n)
9057c478bd9Sstevel@tonic-gate {
9067c478bd9Sstevel@tonic-gate int k;
9077c478bd9Sstevel@tonic-gate
9087c478bd9Sstevel@tonic-gate i = 0;
9097c478bd9Sstevel@tonic-gate for (k = 0; k < 6; k++) {
9107c478bd9Sstevel@tonic-gate if (lookup(troff) == 0)
9117c478bd9Sstevel@tonic-gate return (0);
9127c478bd9Sstevel@tonic-gate if (lookup(troff) == 0)
9137c478bd9Sstevel@tonic-gate return (0);
9147c478bd9Sstevel@tonic-gate while (i < n && bp[i] != '\n')
9157c478bd9Sstevel@tonic-gate i++;
9167c478bd9Sstevel@tonic-gate if (i++ >= n)
9177c478bd9Sstevel@tonic-gate return (0);
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate return (1);
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate
9227c478bd9Sstevel@tonic-gate static void
ar_coff_or_aout(int elffd)9237c478bd9Sstevel@tonic-gate ar_coff_or_aout(int elffd)
9247c478bd9Sstevel@tonic-gate {
9257c478bd9Sstevel@tonic-gate Elf *elf;
9267c478bd9Sstevel@tonic-gate
9277c478bd9Sstevel@tonic-gate /*
9287c478bd9Sstevel@tonic-gate * Get the files elf descriptor and process it as an elf or
9297c478bd9Sstevel@tonic-gate * a.out (4.x) file.
9307c478bd9Sstevel@tonic-gate */
9317c478bd9Sstevel@tonic-gate
9327c478bd9Sstevel@tonic-gate elf = elf_begin(elffd, ELF_C_READ, (Elf *)0);
9337c478bd9Sstevel@tonic-gate switch (elf_kind(elf)) {
9347c478bd9Sstevel@tonic-gate case ELF_K_AR :
9357c478bd9Sstevel@tonic-gate (void) printf(gettext(", not a dynamic executable "
9367c478bd9Sstevel@tonic-gate "or shared object"));
9377c478bd9Sstevel@tonic-gate break;
9387c478bd9Sstevel@tonic-gate case ELF_K_COFF:
9397c478bd9Sstevel@tonic-gate (void) printf(gettext(", unsupported or unknown "
9407c478bd9Sstevel@tonic-gate "file type"));
9417c478bd9Sstevel@tonic-gate break;
9427c478bd9Sstevel@tonic-gate default:
9437c478bd9Sstevel@tonic-gate /*
9447c478bd9Sstevel@tonic-gate * This is either an unknown file or an aout format
9457c478bd9Sstevel@tonic-gate * At this time, we don't print dynamic/stripped
9467c478bd9Sstevel@tonic-gate * info. on a.out or non-Elf binaries.
9477c478bd9Sstevel@tonic-gate */
9487c478bd9Sstevel@tonic-gate break;
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate (void) elf_end(elf);
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate
9537c478bd9Sstevel@tonic-gate
9547c478bd9Sstevel@tonic-gate static void
print_elf_type(Elf_Info EI)955c2c65e21Sny155746 print_elf_type(Elf_Info EI)
9567c478bd9Sstevel@tonic-gate {
957c2c65e21Sny155746 switch (EI.type) {
9587c478bd9Sstevel@tonic-gate case ET_NONE:
9597c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("unknown type"));
9607c478bd9Sstevel@tonic-gate break;
9617c478bd9Sstevel@tonic-gate case ET_REL:
9627c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("relocatable"));
9637c478bd9Sstevel@tonic-gate break;
9647c478bd9Sstevel@tonic-gate case ET_EXEC:
9657c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("executable"));
9667c478bd9Sstevel@tonic-gate break;
9677c478bd9Sstevel@tonic-gate case ET_DYN:
9687c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("dynamic lib"));
9697c478bd9Sstevel@tonic-gate break;
9707c478bd9Sstevel@tonic-gate default:
9717c478bd9Sstevel@tonic-gate break;
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate
9757c478bd9Sstevel@tonic-gate static void
print_elf_machine(int machine)9767c478bd9Sstevel@tonic-gate print_elf_machine(int machine)
9777c478bd9Sstevel@tonic-gate {
978ca1e0a81Sab196087 /*
979ca1e0a81Sab196087 * This table must be kept in sync with the EM_ constants
980ca1e0a81Sab196087 * in /usr/include/sys/elf.h.
981ca1e0a81Sab196087 */
982ca1e0a81Sab196087 static const char *mach_str[EM_NUM] = {
983ca1e0a81Sab196087 "unknown machine", /* 0 - EM_NONE */
984ca1e0a81Sab196087 "WE32100", /* 1 - EM_M32 */
985ca1e0a81Sab196087 "SPARC", /* 2 - EM_SPARC */
986ca1e0a81Sab196087 "80386", /* 3 - EM_386 */
987ca1e0a81Sab196087 "M68000", /* 4 - EM_68K */
988ca1e0a81Sab196087 "M88000", /* 5 - EM_88K */
989ca1e0a81Sab196087 "80486", /* 6 - EM_486 */
990ca1e0a81Sab196087 "i860", /* 7 - EM_860 */
991ca1e0a81Sab196087 "MIPS RS3000 Big-Endian", /* 8 - EM_MIPS */
992ca1e0a81Sab196087 "S/370", /* 9 - EM_S370 */
993ca1e0a81Sab196087 "MIPS RS3000 Little-Endian", /* 10 - EM_MIPS_RS3_LE */
994ca1e0a81Sab196087 "MIPS RS6000", /* 11 - EM_RS6000 */
995ca1e0a81Sab196087 NULL, /* 12 - EM_UNKNOWN12 */
996ca1e0a81Sab196087 NULL, /* 13 - EM_UNKNOWN13 */
997ca1e0a81Sab196087 NULL, /* 14 - EM_UNKNOWN14 */
998ca1e0a81Sab196087 "PA-RISC", /* 15 - EM_PA_RISC */
999ca1e0a81Sab196087 "nCUBE", /* 16 - EM_nCUBE */
1000ca1e0a81Sab196087 "VPP500", /* 17 - EM_VPP500 */
1001ca1e0a81Sab196087 "SPARC32PLUS", /* 18 - EM_SPARC32PLUS */
1002ca1e0a81Sab196087 "i960", /* 19 - EM_960 */
1003ca1e0a81Sab196087 "PowerPC", /* 20 - EM_PPC */
1004ca1e0a81Sab196087 "PowerPC64", /* 21 - EM_PPC64 */
10051638af81Sab196087 "S/390", /* 22 - EM_S390 */
1006ca1e0a81Sab196087 NULL, /* 23 - EM_UNKNOWN23 */
1007ca1e0a81Sab196087 NULL, /* 24 - EM_UNKNOWN24 */
1008ca1e0a81Sab196087 NULL, /* 25 - EM_UNKNOWN25 */
1009ca1e0a81Sab196087 NULL, /* 26 - EM_UNKNOWN26 */
1010ca1e0a81Sab196087 NULL, /* 27 - EM_UNKNOWN27 */
1011ca1e0a81Sab196087 NULL, /* 28 - EM_UNKNOWN28 */
1012ca1e0a81Sab196087 NULL, /* 29 - EM_UNKNOWN29 */
1013ca1e0a81Sab196087 NULL, /* 30 - EM_UNKNOWN30 */
1014ca1e0a81Sab196087 NULL, /* 31 - EM_UNKNOWN31 */
1015ca1e0a81Sab196087 NULL, /* 32 - EM_UNKNOWN32 */
1016ca1e0a81Sab196087 NULL, /* 33 - EM_UNKNOWN33 */
1017ca1e0a81Sab196087 NULL, /* 34 - EM_UNKNOWN34 */
1018ca1e0a81Sab196087 NULL, /* 35 - EM_UNKNOWN35 */
1019ca1e0a81Sab196087 "V800", /* 36 - EM_V800 */
1020ca1e0a81Sab196087 "FR20", /* 37 - EM_FR20 */
1021ca1e0a81Sab196087 "RH32", /* 38 - EM_RH32 */
1022ca1e0a81Sab196087 "RCE", /* 39 - EM_RCE */
1023ca1e0a81Sab196087 "ARM", /* 40 - EM_ARM */
1024ca1e0a81Sab196087 "Alpha", /* 41 - EM_ALPHA */
1025ca1e0a81Sab196087 "S/390", /* 42 - EM_SH */
1026ca1e0a81Sab196087 "SPARCV9", /* 43 - EM_SPARCV9 */
1027ca1e0a81Sab196087 "Tricore", /* 44 - EM_TRICORE */
1028ca1e0a81Sab196087 "ARC", /* 45 - EM_ARC */
1029ca1e0a81Sab196087 "H8/300", /* 46 - EM_H8_300 */
1030ca1e0a81Sab196087 "H8/300H", /* 47 - EM_H8_300H */
1031ca1e0a81Sab196087 "H8S", /* 48 - EM_H8S */
1032ca1e0a81Sab196087 "H8/500", /* 49 - EM_H8_500 */
1033ca1e0a81Sab196087 "IA64", /* 50 - EM_IA_64 */
1034ca1e0a81Sab196087 "MIPS-X", /* 51 - EM_MIPS_X */
1035ca1e0a81Sab196087 "Coldfire", /* 52 - EM_COLDFIRE */
1036ca1e0a81Sab196087 "M68HC12", /* 53 - EM_68HC12 */
1037ca1e0a81Sab196087 "MMA", /* 54 - EM_MMA */
1038ca1e0a81Sab196087 "PCP", /* 55 - EM_PCP */
1039ca1e0a81Sab196087 "nCPU", /* 56 - EM_NCPU */
1040ca1e0a81Sab196087 "NDR1", /* 57 - EM_NDR1 */
1041ca1e0a81Sab196087 "Starcore", /* 58 - EM_STARCORE */
1042ca1e0a81Sab196087 "ME16", /* 59 - EM_ME16 */
1043ca1e0a81Sab196087 "ST100", /* 60 - EM_ST100 */
1044ca1e0a81Sab196087 "TINYJ", /* 61 - EM_TINYJ */
1045ca1e0a81Sab196087 "AMD64", /* 62 - EM_AMD64 */
1046ca1e0a81Sab196087 "PDSP", /* 63 - EM_PDSP */
1047ca1e0a81Sab196087 NULL, /* 64 - EM_UNKNOWN64 */
1048ca1e0a81Sab196087 NULL, /* 65 - EM_UNKNOWN65 */
1049ca1e0a81Sab196087 "FX66", /* 66 - EM_FX66 */
1050ca1e0a81Sab196087 "ST9 PLUS", /* 67 - EM_ST9PLUS */
1051ca1e0a81Sab196087 "ST7", /* 68 - EM_ST7 */
1052ca1e0a81Sab196087 "68HC16", /* 69 - EM_68HC16 */
1053ca1e0a81Sab196087 "68HC11", /* 70 - EM_68HC11 */
1054ca1e0a81Sab196087 "68H08", /* 71 - EM_68HC08 */
1055ca1e0a81Sab196087 "68HC05", /* 72 - EM_68HC05 */
1056ca1e0a81Sab196087 "SVX", /* 73 - EM_SVX */
1057ca1e0a81Sab196087 "ST19", /* 74 - EM_ST19 */
1058ca1e0a81Sab196087 "VAX", /* 75 - EM_VAX */
1059ca1e0a81Sab196087 "CRIS", /* 76 - EM_CRIS */
1060ca1e0a81Sab196087 "Javelin", /* 77 - EM_JAVELIN */
1061ca1e0a81Sab196087 "Firepath", /* 78 - EM_FIREPATH */
1062ca1e0a81Sab196087 "ZSP", /* 79 - EM_ZSP */
1063ca1e0a81Sab196087 "MMIX", /* 80 - EM_MMIX */
1064ca1e0a81Sab196087 "HUANY", /* 81 - EM_HUANY */
1065ca1e0a81Sab196087 "Prism", /* 82 - EM_PRISM */
1066ca1e0a81Sab196087 "AVR", /* 83 - EM_AVR */
1067ca1e0a81Sab196087 "FR30", /* 84 - EM_FR30 */
1068ca1e0a81Sab196087 "D10V", /* 85 - EM_D10V */
1069ca1e0a81Sab196087 "D30V", /* 86 - EM_D30V */
1070ca1e0a81Sab196087 "V850", /* 87 - EM_V850 */
1071ca1e0a81Sab196087 "M32R", /* 88 - EM_M32R */
1072ca1e0a81Sab196087 "MN10300", /* 89 - EM_MN10300 */
1073ca1e0a81Sab196087 "MN10200", /* 90 - EM_MN10200 */
1074ca1e0a81Sab196087 "picoJava", /* 91 - EM_PJ */
1075ca1e0a81Sab196087 "OpenRISC", /* 92 - EM_OPENRISC */
1076ca1e0a81Sab196087 "Tangent-A5", /* 93 - EM_ARC_A5 */
1077ca1e0a81Sab196087 "Xtensa" /* 94 - EM_XTENSA */
1078ca1e0a81Sab196087 };
1079ca1e0a81Sab196087 /* If new machine is added, refuse to compile until we're updated */
1080ca1e0a81Sab196087 #if EM_NUM != 95
1081ca1e0a81Sab196087 #error "Number of known ELF machine constants has changed"
1082ca1e0a81Sab196087 #endif
1083ca1e0a81Sab196087
1084ca1e0a81Sab196087 const char *str;
1085ca1e0a81Sab196087
1086ca1e0a81Sab196087 if ((machine < EM_NONE) || (machine >= EM_NUM))
1087ca1e0a81Sab196087 machine = EM_NONE;
1088ca1e0a81Sab196087
1089ca1e0a81Sab196087 str = mach_str[machine];
1090ca1e0a81Sab196087 if (str)
1091ca1e0a81Sab196087 (void) printf(" %s", str);
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate
10947c478bd9Sstevel@tonic-gate static void
print_elf_datatype(int datatype)10957c478bd9Sstevel@tonic-gate print_elf_datatype(int datatype)
10967c478bd9Sstevel@tonic-gate {
10977c478bd9Sstevel@tonic-gate switch (datatype) {
10987c478bd9Sstevel@tonic-gate case ELFDATA2LSB:
1099ca1e0a81Sab196087 (void) printf(" LSB");
11007c478bd9Sstevel@tonic-gate break;
11017c478bd9Sstevel@tonic-gate case ELFDATA2MSB:
1102ca1e0a81Sab196087 (void) printf(" MSB");
11037c478bd9Sstevel@tonic-gate break;
11047c478bd9Sstevel@tonic-gate default:
11057c478bd9Sstevel@tonic-gate break;
11067c478bd9Sstevel@tonic-gate }
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate
11097c478bd9Sstevel@tonic-gate static void
print_elf_class(int class)11107c478bd9Sstevel@tonic-gate print_elf_class(int class)
11117c478bd9Sstevel@tonic-gate {
11127c478bd9Sstevel@tonic-gate switch (class) {
11137c478bd9Sstevel@tonic-gate case ELFCLASS32:
11147c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("32-bit"));
11157c478bd9Sstevel@tonic-gate break;
11167c478bd9Sstevel@tonic-gate case ELFCLASS64:
11177c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("64-bit"));
11187c478bd9Sstevel@tonic-gate break;
11197c478bd9Sstevel@tonic-gate default:
11207c478bd9Sstevel@tonic-gate break;
11217c478bd9Sstevel@tonic-gate }
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate
11247c478bd9Sstevel@tonic-gate static void
print_elf_flags(Elf_Info EI)1125c2c65e21Sny155746 print_elf_flags(Elf_Info EI)
11267c478bd9Sstevel@tonic-gate {
1127c2c65e21Sny155746 unsigned int flags;
1128c2c65e21Sny155746
1129c2c65e21Sny155746 flags = EI.flags;
1130c2c65e21Sny155746 switch (EI.machine) {
11317c478bd9Sstevel@tonic-gate case EM_SPARCV9:
11327c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_EXT_MASK) {
11337c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_SUN_US3) {
11347c478bd9Sstevel@tonic-gate (void) printf("%s", gettext(
11357c478bd9Sstevel@tonic-gate ", UltraSPARC3 Extensions Required"));
11367c478bd9Sstevel@tonic-gate } else if (flags & EF_SPARC_SUN_US1) {
11377c478bd9Sstevel@tonic-gate (void) printf("%s", gettext(
11387c478bd9Sstevel@tonic-gate ", UltraSPARC1 Extensions Required"));
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_HAL_R1)
11417c478bd9Sstevel@tonic-gate (void) printf("%s", gettext(
11427c478bd9Sstevel@tonic-gate ", HaL R1 Extensions Required"));
11437c478bd9Sstevel@tonic-gate }
11447c478bd9Sstevel@tonic-gate break;
11457c478bd9Sstevel@tonic-gate case EM_SPARC32PLUS:
11467c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_32PLUS)
11477c478bd9Sstevel@tonic-gate (void) printf("%s", gettext(", V8+ Required"));
11487c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_SUN_US3) {
11497c478bd9Sstevel@tonic-gate (void) printf("%s",
11507c478bd9Sstevel@tonic-gate gettext(", UltraSPARC3 Extensions Required"));
11517c478bd9Sstevel@tonic-gate } else if (flags & EF_SPARC_SUN_US1) {
11527c478bd9Sstevel@tonic-gate (void) printf("%s",
11537c478bd9Sstevel@tonic-gate gettext(", UltraSPARC1 Extensions Required"));
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_HAL_R1)
11567c478bd9Sstevel@tonic-gate (void) printf("%s",
11577c478bd9Sstevel@tonic-gate gettext(", HaL R1 Extensions Required"));
11587c478bd9Sstevel@tonic-gate break;
11597c478bd9Sstevel@tonic-gate default:
11607c478bd9Sstevel@tonic-gate break;
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate }
11637c478bd9Sstevel@tonic-gate
1164c2c65e21Sny155746 /*
1165c2c65e21Sny155746 * check_ident: checks the ident field of the presumeably
1166c2c65e21Sny155746 * elf file. If check fails, this is not an
1167c2c65e21Sny155746 * elf file.
1168c2c65e21Sny155746 */
11697c478bd9Sstevel@tonic-gate static int
check_ident(unsigned char * ident,int fd)1170c2c65e21Sny155746 check_ident(unsigned char *ident, int fd)
11717c478bd9Sstevel@tonic-gate {
1172c2c65e21Sny155746 int class;
1173c2c65e21Sny155746 if (pread64(fd, ident, EI_NIDENT, 0) != EI_NIDENT)
1174c2c65e21Sny155746 return (ELF_READ_FAIL);
1175c2c65e21Sny155746 class = ident[EI_CLASS];
1176c2c65e21Sny155746 if (class != ELFCLASS32 && class != ELFCLASS64)
1177c2c65e21Sny155746 return (ELF_READ_FAIL);
1178c2c65e21Sny155746 if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
1179c2c65e21Sny155746 ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3)
1180c2c65e21Sny155746 return (ELF_READ_FAIL);
1181c2c65e21Sny155746
1182c2c65e21Sny155746 return (ELF_READ_OKAY);
1183c2c65e21Sny155746 }
1184c2c65e21Sny155746
1185c2c65e21Sny155746 static int
elf_check(char * file)1186c2c65e21Sny155746 elf_check(char *file)
1187c2c65e21Sny155746 {
1188c2c65e21Sny155746 Elf_Info EInfo;
1189c2c65e21Sny155746 int class, version, format;
1190c2c65e21Sny155746 unsigned char ident[EI_NIDENT];
1191c2c65e21Sny155746
1192c2c65e21Sny155746 (void) memset(&EInfo, 0, sizeof (Elf_Info));
1193c2c65e21Sny155746 EInfo.file = file;
11947c478bd9Sstevel@tonic-gate
11957c478bd9Sstevel@tonic-gate /*
1196c2c65e21Sny155746 * Verify information in file indentifier.
1197c2c65e21Sny155746 * Return quietly if not elf; Different type of file.
11987c478bd9Sstevel@tonic-gate */
1199c2c65e21Sny155746 if (check_ident(ident, elffd) == ELF_READ_FAIL)
12007c478bd9Sstevel@tonic-gate return (1);
12017c478bd9Sstevel@tonic-gate
12027c478bd9Sstevel@tonic-gate /*
1203c2c65e21Sny155746 * Read the elf headers for processing and get the
1204c2c65e21Sny155746 * get the needed information in Elf_Info struct.
12057c478bd9Sstevel@tonic-gate */
1206c2c65e21Sny155746 class = ident[EI_CLASS];
1207c2c65e21Sny155746 if (class == ELFCLASS32) {
1208c2c65e21Sny155746 if (elf_read32(elffd, &EInfo) == ELF_READ_FAIL) {
12099a411307Srie (void) fprintf(stderr, gettext("%s: %s: can't "
1210c2c65e21Sny155746 "read ELF header\n"), File, file);
12117c478bd9Sstevel@tonic-gate return (1);
12127c478bd9Sstevel@tonic-gate }
1213c2c65e21Sny155746 } else if (class == ELFCLASS64) {
1214c2c65e21Sny155746 if (elf_read64(elffd, &EInfo) == ELF_READ_FAIL) {
1215c2c65e21Sny155746 (void) fprintf(stderr, gettext("%s: %s: can't "
1216c2c65e21Sny155746 "read ELF header\n"), File, file);
12177c478bd9Sstevel@tonic-gate return (1);
12187c478bd9Sstevel@tonic-gate }
1219c2c65e21Sny155746 } else {
1220c2c65e21Sny155746 /* something wrong */
1221c2c65e21Sny155746 return (1);
1222c2c65e21Sny155746 }
1223c2c65e21Sny155746
1224c2c65e21Sny155746 /* version not in ident then 1 */
1225c2c65e21Sny155746 version = ident[EI_VERSION] ? ident[EI_VERSION] : 1;
1226c2c65e21Sny155746
1227c2c65e21Sny155746 format = ident[EI_DATA];
12287c478bd9Sstevel@tonic-gate (void) printf("%s", gettext("ELF"));
1229c2c65e21Sny155746 print_elf_class(class);
1230c2c65e21Sny155746 print_elf_datatype(format);
1231c2c65e21Sny155746 print_elf_type(EInfo);
12327c478bd9Sstevel@tonic-gate
1233c2c65e21Sny155746 if (EInfo.core_type != EC_NOTCORE) {
1234c2c65e21Sny155746 /* Print what kind of core is this */
1235c2c65e21Sny155746 if (EInfo.core_type == EC_OLDCORE)
1236c2c65e21Sny155746 (void) printf(" %s", gettext("pre-2.6 core file"));
1237c2c65e21Sny155746 else
1238c2c65e21Sny155746 (void) printf(" %s", gettext("core file"));
1239c2c65e21Sny155746 }
1240c2c65e21Sny155746
1241c2c65e21Sny155746 /* Print machine info */
1242c2c65e21Sny155746 print_elf_machine(EInfo.machine);
1243c2c65e21Sny155746
1244c2c65e21Sny155746 /* Print Version */
1245c2c65e21Sny155746 if (version == 1)
1246c2c65e21Sny155746 (void) printf(" %s %d", gettext("Version"), version);
1247c2c65e21Sny155746
1248c2c65e21Sny155746 /* Print Flags */
1249c2c65e21Sny155746 print_elf_flags(EInfo);
1250c2c65e21Sny155746
1251c2c65e21Sny155746 /* Last bit, if it is a core */
1252c2c65e21Sny155746 if (EInfo.core_type != EC_NOTCORE) {
1253c2c65e21Sny155746 /* Print the program name that dumped this core */
1254c2c65e21Sny155746 (void) printf(gettext(", from '%s'"), EInfo.fname);
1255c2c65e21Sny155746 return (0);
1256c2c65e21Sny155746 }
1257c2c65e21Sny155746
1258c2c65e21Sny155746 /* Print Capabilities */
1259c2c65e21Sny155746 if (EInfo.cap_str[0] != '\0')
1260c2c65e21Sny155746 (void) printf(" [%s]", EInfo.cap_str);
1261c2c65e21Sny155746
1262c2c65e21Sny155746 if ((EInfo.type != ET_EXEC) && (EInfo.type != ET_DYN))
12637c478bd9Sstevel@tonic-gate return (0);
12647c478bd9Sstevel@tonic-gate
1265c2c65e21Sny155746 /* Print if it is dynamically linked */
1266c2c65e21Sny155746 if (EInfo.dynamic)
12677c478bd9Sstevel@tonic-gate (void) printf(gettext(", dynamically linked"));
12687c478bd9Sstevel@tonic-gate else
12697c478bd9Sstevel@tonic-gate (void) printf(gettext(", statically linked"));
12707c478bd9Sstevel@tonic-gate
1271c2c65e21Sny155746 /* Printf it it is stripped */
1272c2c65e21Sny155746 if (EInfo.stripped & E_SYMTAB) {
12737c478bd9Sstevel@tonic-gate (void) printf(gettext(", not stripped"));
1274c2c65e21Sny155746 if (!(EInfo.stripped & E_DBGINF)) {
12757c478bd9Sstevel@tonic-gate (void) printf(gettext(
12767c478bd9Sstevel@tonic-gate ", no debugging information available"));
12777c478bd9Sstevel@tonic-gate }
12787c478bd9Sstevel@tonic-gate } else {
12797c478bd9Sstevel@tonic-gate (void) printf(gettext(", stripped"));
12807c478bd9Sstevel@tonic-gate }
1281c2c65e21Sny155746
1282c2c65e21Sny155746 return (0);
12837c478bd9Sstevel@tonic-gate }
12847c478bd9Sstevel@tonic-gate
12857c478bd9Sstevel@tonic-gate /*
1286c13de8f6Sab196087 * is_rtld_config - If file is a runtime linker config file, prints
1287c13de8f6Sab196087 * the description and returns True (1). Otherwise, silently returns
1288c13de8f6Sab196087 * False (0).
1289c13de8f6Sab196087 */
1290c13de8f6Sab196087 int
is_rtld_config(void)1291c13de8f6Sab196087 is_rtld_config(void)
1292c13de8f6Sab196087 {
1293c13de8f6Sab196087 Rtc_id *id;
1294c13de8f6Sab196087
1295c13de8f6Sab196087 if ((fbsz >= sizeof (*id)) && RTC_ID_TEST(fbuf)) {
1296c13de8f6Sab196087 (void) printf(gettext("Runtime Linking Configuration"));
1297c13de8f6Sab196087 id = (Rtc_id *) fbuf;
1298c13de8f6Sab196087 print_elf_class(id->id_class);
1299c13de8f6Sab196087 print_elf_datatype(id->id_data);
1300c13de8f6Sab196087 print_elf_machine(id->id_machine);
1301c13de8f6Sab196087 (void) printf("\n");
1302c13de8f6Sab196087 return (1);
1303c13de8f6Sab196087 }
1304c13de8f6Sab196087
1305c13de8f6Sab196087 return (0);
1306c13de8f6Sab196087 }
1307c13de8f6Sab196087
1308c13de8f6Sab196087 /*
13097c478bd9Sstevel@tonic-gate * lookup -
13107c478bd9Sstevel@tonic-gate * Attempts to match one of the strings from a list, 'tab',
13117c478bd9Sstevel@tonic-gate * with what is in the file, starting at the current index position 'i'.
13127c478bd9Sstevel@tonic-gate * Looks past any initial whitespace and expects whitespace or other
13137c478bd9Sstevel@tonic-gate * delimiting characters to follow the matched string.
13147c478bd9Sstevel@tonic-gate * A match identifies the file as being 'assembler', 'fortran', 'c', etc.
13157c478bd9Sstevel@tonic-gate * Returns 1 for a successful match, 0 otherwise.
13167c478bd9Sstevel@tonic-gate */
13177c478bd9Sstevel@tonic-gate static int
lookup(char ** tab)13187c478bd9Sstevel@tonic-gate lookup(char **tab)
13197c478bd9Sstevel@tonic-gate {
13207c478bd9Sstevel@tonic-gate register char r;
13217c478bd9Sstevel@tonic-gate register int k, j, l;
13227c478bd9Sstevel@tonic-gate
13237c478bd9Sstevel@tonic-gate while (fbuf[i] == ' ' || fbuf[i] == '\t' || fbuf[i] == '\n')
13247c478bd9Sstevel@tonic-gate i++;
13257c478bd9Sstevel@tonic-gate for (j = 0; tab[j] != 0; j++) {
13267c478bd9Sstevel@tonic-gate l = 0;
1327ca3e8d88SDave Plauger for (k = i; ((r = tab[j][l++]) == fbuf[k] && r != '\0'); k++)
1328ca3e8d88SDave Plauger ;
13297c478bd9Sstevel@tonic-gate if (r == '\0')
13307c478bd9Sstevel@tonic-gate if (fbuf[k] == ' ' || fbuf[k] == '\n' ||
13317c478bd9Sstevel@tonic-gate fbuf[k] == '\t' || fbuf[k] == '{' ||
13327c478bd9Sstevel@tonic-gate fbuf[k] == '/') {
13337c478bd9Sstevel@tonic-gate i = k;
13347c478bd9Sstevel@tonic-gate return (1);
13357c478bd9Sstevel@tonic-gate }
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate return (0);
13387c478bd9Sstevel@tonic-gate }
13397c478bd9Sstevel@tonic-gate
13407c478bd9Sstevel@tonic-gate /*
13417c478bd9Sstevel@tonic-gate * ccom -
13427c478bd9Sstevel@tonic-gate * Increments the current index 'i' into the file buffer 'fbuf' past any
13437c478bd9Sstevel@tonic-gate * whitespace lines and C-style comments found, starting at the current
13447c478bd9Sstevel@tonic-gate * position of 'i'. Returns 1 as long as we don't increment i past the
13457c478bd9Sstevel@tonic-gate * size of fbuf (fbsz). Otherwise, returns 0.
13467c478bd9Sstevel@tonic-gate */
13477c478bd9Sstevel@tonic-gate
13487c478bd9Sstevel@tonic-gate static int
ccom(void)13497c478bd9Sstevel@tonic-gate ccom(void)
13507c478bd9Sstevel@tonic-gate {
13517c478bd9Sstevel@tonic-gate register char cc;
13527c478bd9Sstevel@tonic-gate int len;
13537c478bd9Sstevel@tonic-gate
13547c478bd9Sstevel@tonic-gate while ((cc = fbuf[i]) == ' ' || cc == '\t' || cc == '\n')
13557c478bd9Sstevel@tonic-gate if (i++ >= fbsz)
13567c478bd9Sstevel@tonic-gate return (0);
13577c478bd9Sstevel@tonic-gate if (fbuf[i] == '/' && fbuf[i+1] == '*') {
13587c478bd9Sstevel@tonic-gate i += 2;
13597c478bd9Sstevel@tonic-gate while (fbuf[i] != '*' || fbuf[i+1] != '/') {
13607c478bd9Sstevel@tonic-gate if (fbuf[i] == '\\')
13617c478bd9Sstevel@tonic-gate i++;
13627c478bd9Sstevel@tonic-gate if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
13637c478bd9Sstevel@tonic-gate len = 1;
13647c478bd9Sstevel@tonic-gate i += len;
13657c478bd9Sstevel@tonic-gate if (i >= fbsz)
13667c478bd9Sstevel@tonic-gate return (0);
13677c478bd9Sstevel@tonic-gate }
13687c478bd9Sstevel@tonic-gate if ((i += 2) >= fbsz)
13697c478bd9Sstevel@tonic-gate return (0);
13707c478bd9Sstevel@tonic-gate }
13717c478bd9Sstevel@tonic-gate if (fbuf[i] == '\n')
13727c478bd9Sstevel@tonic-gate if (ccom() == 0)
13737c478bd9Sstevel@tonic-gate return (0);
13747c478bd9Sstevel@tonic-gate return (1);
13757c478bd9Sstevel@tonic-gate }
13767c478bd9Sstevel@tonic-gate
13777c478bd9Sstevel@tonic-gate /*
13787c478bd9Sstevel@tonic-gate * ascom -
13797c478bd9Sstevel@tonic-gate * Increments the current index 'i' into the file buffer 'fbuf' past
13807c478bd9Sstevel@tonic-gate * consecutive assembler program comment lines starting with ASCOMCHAR,
13817c478bd9Sstevel@tonic-gate * starting at the current position of 'i'.
13827c478bd9Sstevel@tonic-gate * Returns 1 as long as we don't increment i past the
13837c478bd9Sstevel@tonic-gate * size of fbuf (fbsz). Otherwise returns 0.
13847c478bd9Sstevel@tonic-gate */
13857c478bd9Sstevel@tonic-gate
13867c478bd9Sstevel@tonic-gate static int
ascom(void)13877c478bd9Sstevel@tonic-gate ascom(void)
13887c478bd9Sstevel@tonic-gate {
13897c478bd9Sstevel@tonic-gate while (fbuf[i] == ASCOMCHAR) {
13907c478bd9Sstevel@tonic-gate i++;
13917c478bd9Sstevel@tonic-gate while (fbuf[i++] != '\n')
13927c478bd9Sstevel@tonic-gate if (i >= fbsz)
13937c478bd9Sstevel@tonic-gate return (0);
13947c478bd9Sstevel@tonic-gate while (fbuf[i] == '\n')
13957c478bd9Sstevel@tonic-gate if (i++ >= fbsz)
13967c478bd9Sstevel@tonic-gate return (0);
13977c478bd9Sstevel@tonic-gate }
13987c478bd9Sstevel@tonic-gate return (1);
13997c478bd9Sstevel@tonic-gate }
14007c478bd9Sstevel@tonic-gate
14017c478bd9Sstevel@tonic-gate static int
sccs(void)14027c478bd9Sstevel@tonic-gate sccs(void)
14037c478bd9Sstevel@tonic-gate { /* look for "1hddddd" where d is a digit */
14047c478bd9Sstevel@tonic-gate register int j;
14057c478bd9Sstevel@tonic-gate
14067c478bd9Sstevel@tonic-gate if (fbuf[0] == 1 && fbuf[1] == 'h') {
14077c478bd9Sstevel@tonic-gate for (j = 2; j <= 6; j++) {
14087c478bd9Sstevel@tonic-gate if (isdigit(fbuf[j]))
14097c478bd9Sstevel@tonic-gate continue;
14107c478bd9Sstevel@tonic-gate else
14117c478bd9Sstevel@tonic-gate return (0);
14127c478bd9Sstevel@tonic-gate }
14137c478bd9Sstevel@tonic-gate } else {
14147c478bd9Sstevel@tonic-gate return (0);
14157c478bd9Sstevel@tonic-gate }
14167c478bd9Sstevel@tonic-gate return (1);
14177c478bd9Sstevel@tonic-gate }
14187c478bd9Sstevel@tonic-gate
14197c478bd9Sstevel@tonic-gate static int
english(char * bp,int n)14207c478bd9Sstevel@tonic-gate english(char *bp, int n)
14217c478bd9Sstevel@tonic-gate {
14227c478bd9Sstevel@tonic-gate #define NASC 128 /* number of ascii char ?? */
14237c478bd9Sstevel@tonic-gate register int j, vow, freq, rare, len;
14247c478bd9Sstevel@tonic-gate register int badpun = 0, punct = 0;
14257c478bd9Sstevel@tonic-gate int ct[NASC];
14267c478bd9Sstevel@tonic-gate
14277c478bd9Sstevel@tonic-gate if (n < 50)
14287c478bd9Sstevel@tonic-gate return (0); /* no point in statistics on squibs */
14297c478bd9Sstevel@tonic-gate for (j = 0; j < NASC; j++)
14307c478bd9Sstevel@tonic-gate ct[j] = 0;
14317c478bd9Sstevel@tonic-gate for (j = 0; j < n; j += len) {
14327c478bd9Sstevel@tonic-gate if ((unsigned char)bp[j] < NASC)
14337c478bd9Sstevel@tonic-gate ct[bp[j]|040]++;
14347c478bd9Sstevel@tonic-gate switch (bp[j]) {
14357c478bd9Sstevel@tonic-gate case '.':
14367c478bd9Sstevel@tonic-gate case ',':
14377c478bd9Sstevel@tonic-gate case ')':
14387c478bd9Sstevel@tonic-gate case '%':
14397c478bd9Sstevel@tonic-gate case ';':
14407c478bd9Sstevel@tonic-gate case ':':
14417c478bd9Sstevel@tonic-gate case '?':
14427c478bd9Sstevel@tonic-gate punct++;
14437c478bd9Sstevel@tonic-gate if (j < n-1 && bp[j+1] != ' ' && bp[j+1] != '\n')
14447c478bd9Sstevel@tonic-gate badpun++;
14457c478bd9Sstevel@tonic-gate }
14467c478bd9Sstevel@tonic-gate if ((len = mblen(&bp[j], MB_CUR_MAX)) <= 0)
14477c478bd9Sstevel@tonic-gate len = 1;
14487c478bd9Sstevel@tonic-gate }
14497c478bd9Sstevel@tonic-gate if (badpun*5 > punct)
14507c478bd9Sstevel@tonic-gate return (0);
14517c478bd9Sstevel@tonic-gate vow = ct['a'] + ct['e'] + ct['i'] + ct['o'] + ct['u'];
14527c478bd9Sstevel@tonic-gate freq = ct['e'] + ct['t'] + ct['a'] + ct['i'] + ct['o'] + ct['n'];
14537c478bd9Sstevel@tonic-gate rare = ct['v'] + ct['j'] + ct['k'] + ct['q'] + ct['x'] + ct['z'];
14547c478bd9Sstevel@tonic-gate if (2*ct[';'] > ct['e'])
14557c478bd9Sstevel@tonic-gate return (0);
14567c478bd9Sstevel@tonic-gate if ((ct['>'] + ct['<'] + ct['/']) > ct['e'])
14577c478bd9Sstevel@tonic-gate return (0); /* shell file test */
14587c478bd9Sstevel@tonic-gate return (vow * 5 >= n - ct[' '] && freq >= 10 * rare);
14597c478bd9Sstevel@tonic-gate }
14607c478bd9Sstevel@tonic-gate
14617c478bd9Sstevel@tonic-gate
14627c478bd9Sstevel@tonic-gate static int
shellscript(char buf[],struct stat64 * sb)14637c478bd9Sstevel@tonic-gate shellscript(char buf[], struct stat64 *sb)
14647c478bd9Sstevel@tonic-gate {
14657c478bd9Sstevel@tonic-gate char *tp, *cp, *xp, *up, *gp;
14667c478bd9Sstevel@tonic-gate
14677c478bd9Sstevel@tonic-gate cp = strchr(buf, '\n');
14687c478bd9Sstevel@tonic-gate if (cp == NULL || cp - fbuf > fbsz)
14697c478bd9Sstevel@tonic-gate return (0);
14707c478bd9Sstevel@tonic-gate for (tp = buf; tp != cp && isspace((unsigned char)*tp); tp++)
14717c478bd9Sstevel@tonic-gate if (!isascii(*tp))
14727c478bd9Sstevel@tonic-gate return (0);
14737c478bd9Sstevel@tonic-gate for (xp = tp; tp != cp && !isspace((unsigned char)*tp); tp++)
14747c478bd9Sstevel@tonic-gate if (!isascii(*tp))
14757c478bd9Sstevel@tonic-gate return (0);
14767c478bd9Sstevel@tonic-gate if (tp == xp)
14777c478bd9Sstevel@tonic-gate return (0);
14787c478bd9Sstevel@tonic-gate if (sb->st_mode & S_ISUID)
14797c478bd9Sstevel@tonic-gate up = gettext("set-uid ");
14807c478bd9Sstevel@tonic-gate else
14817c478bd9Sstevel@tonic-gate up = "";
14827c478bd9Sstevel@tonic-gate
14837c478bd9Sstevel@tonic-gate if (sb->st_mode & S_ISGID)
14847c478bd9Sstevel@tonic-gate gp = gettext("set-gid ");
14857c478bd9Sstevel@tonic-gate else
14867c478bd9Sstevel@tonic-gate gp = "";
14877c478bd9Sstevel@tonic-gate
14887c478bd9Sstevel@tonic-gate if (strncmp(xp, "/bin/sh", tp - xp) == 0)
14897c478bd9Sstevel@tonic-gate xp = gettext("shell");
14907c478bd9Sstevel@tonic-gate else if (strncmp(xp, "/bin/csh", tp - xp) == 0)
14917c478bd9Sstevel@tonic-gate xp = gettext("c-shell");
14927c478bd9Sstevel@tonic-gate else if (strncmp(xp, "/usr/sbin/dtrace", tp - xp) == 0)
14937c478bd9Sstevel@tonic-gate xp = gettext("DTrace");
14947c478bd9Sstevel@tonic-gate else
14957c478bd9Sstevel@tonic-gate *tp = '\0';
14967c478bd9Sstevel@tonic-gate /*
14977c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE
14987c478bd9Sstevel@tonic-gate * This message is printed by file command for shell scripts.
14997c478bd9Sstevel@tonic-gate * The first %s is for the translation for "set-uid " (if the script
15007c478bd9Sstevel@tonic-gate * has the set-uid bit set), or is for an empty string (if the
15017c478bd9Sstevel@tonic-gate * script does not have the set-uid bit set).
15027c478bd9Sstevel@tonic-gate * Similarly, the second %s is for the translation for "set-gid ",
15037c478bd9Sstevel@tonic-gate * or is for an empty string.
15047c478bd9Sstevel@tonic-gate * The third %s is for the translation for either: "shell", "c-shell",
15057c478bd9Sstevel@tonic-gate * or "DTrace", or is for the pathname of the program the script
15067c478bd9Sstevel@tonic-gate * executes.
15077c478bd9Sstevel@tonic-gate */
15087c478bd9Sstevel@tonic-gate (void) printf(gettext("%s%sexecutable %s script\n"), up, gp, xp);
15097c478bd9Sstevel@tonic-gate return (1);
15107c478bd9Sstevel@tonic-gate }
15117c478bd9Sstevel@tonic-gate
15127c478bd9Sstevel@tonic-gate static int
get_door_target(char * file,char * buf,size_t bufsize)15137c478bd9Sstevel@tonic-gate get_door_target(char *file, char *buf, size_t bufsize)
15147c478bd9Sstevel@tonic-gate {
15157c478bd9Sstevel@tonic-gate int fd;
15167c478bd9Sstevel@tonic-gate door_info_t di;
15177c478bd9Sstevel@tonic-gate psinfo_t psinfo;
15187c478bd9Sstevel@tonic-gate
15197c478bd9Sstevel@tonic-gate if ((fd = open64(file, O_RDONLY)) < 0 ||
15207c478bd9Sstevel@tonic-gate door_info(fd, &di) != 0) {
15217c478bd9Sstevel@tonic-gate if (fd >= 0)
15227c478bd9Sstevel@tonic-gate (void) close(fd);
15237c478bd9Sstevel@tonic-gate return (-1);
15247c478bd9Sstevel@tonic-gate }
15257c478bd9Sstevel@tonic-gate (void) close(fd);
15267c478bd9Sstevel@tonic-gate
15277c478bd9Sstevel@tonic-gate (void) sprintf(buf, "/proc/%ld/psinfo", di.di_target);
15287c478bd9Sstevel@tonic-gate if ((fd = open64(buf, O_RDONLY)) < 0 ||
15297c478bd9Sstevel@tonic-gate read(fd, &psinfo, sizeof (psinfo)) != sizeof (psinfo)) {
15307c478bd9Sstevel@tonic-gate if (fd >= 0)
15317c478bd9Sstevel@tonic-gate (void) close(fd);
15327c478bd9Sstevel@tonic-gate return (-1);
15337c478bd9Sstevel@tonic-gate }
15347c478bd9Sstevel@tonic-gate (void) close(fd);
15357c478bd9Sstevel@tonic-gate
15367c478bd9Sstevel@tonic-gate (void) snprintf(buf, bufsize, "%s[%ld]", psinfo.pr_fname, di.di_target);
15377c478bd9Sstevel@tonic-gate return (0);
15387c478bd9Sstevel@tonic-gate }
15397c478bd9Sstevel@tonic-gate
15407c478bd9Sstevel@tonic-gate /*
15417c478bd9Sstevel@tonic-gate * ZIP file header information
15427c478bd9Sstevel@tonic-gate */
15437c478bd9Sstevel@tonic-gate #define SIGSIZ 4
15447c478bd9Sstevel@tonic-gate #define LOCSIG "PK\003\004"
15457c478bd9Sstevel@tonic-gate #define LOCHDRSIZ 30
15467c478bd9Sstevel@tonic-gate
15477c478bd9Sstevel@tonic-gate #define CH(b, n) (((unsigned char *)(b))[n])
15487c478bd9Sstevel@tonic-gate #define SH(b, n) (CH(b, n) | (CH(b, n+1) << 8))
15497c478bd9Sstevel@tonic-gate #define LG(b, n) (SH(b, n) | (SH(b, n+2) << 16))
15507c478bd9Sstevel@tonic-gate
15517c478bd9Sstevel@tonic-gate #define LOCNAM(b) (SH(b, 26)) /* filename size */
15527c478bd9Sstevel@tonic-gate #define LOCEXT(b) (SH(b, 28)) /* extra field size */
15537c478bd9Sstevel@tonic-gate
15547c478bd9Sstevel@tonic-gate #define XFHSIZ 4 /* header id, data size */
15557c478bd9Sstevel@tonic-gate #define XFHID(b) (SH(b, 0)) /* extract field header id */
15567c478bd9Sstevel@tonic-gate #define XFDATASIZ(b) (SH(b, 2)) /* extract field data size */
15577c478bd9Sstevel@tonic-gate #define XFJAVASIG 0xcafe /* java executables */
15587c478bd9Sstevel@tonic-gate
15597c478bd9Sstevel@tonic-gate static int
zipfile(char * fbuf,int fd)15607c478bd9Sstevel@tonic-gate zipfile(char *fbuf, int fd)
15617c478bd9Sstevel@tonic-gate {
15627c478bd9Sstevel@tonic-gate off_t xoff, xoff_end;
15637c478bd9Sstevel@tonic-gate
15647c478bd9Sstevel@tonic-gate if (strncmp(fbuf, LOCSIG, SIGSIZ) != 0)
15657c478bd9Sstevel@tonic-gate return (0);
15667c478bd9Sstevel@tonic-gate
15677c478bd9Sstevel@tonic-gate xoff = LOCHDRSIZ + LOCNAM(fbuf);
15687c478bd9Sstevel@tonic-gate xoff_end = xoff + LOCEXT(fbuf);
15697c478bd9Sstevel@tonic-gate
15707c478bd9Sstevel@tonic-gate while (xoff < xoff_end) {
15717c478bd9Sstevel@tonic-gate char xfhdr[XFHSIZ];
15727c478bd9Sstevel@tonic-gate
15737c478bd9Sstevel@tonic-gate if (pread(fd, xfhdr, XFHSIZ, xoff) != XFHSIZ)
15747c478bd9Sstevel@tonic-gate break;
15757c478bd9Sstevel@tonic-gate
15767c478bd9Sstevel@tonic-gate if (XFHID(xfhdr) == XFJAVASIG) {
1577ea51a530Sny155746 (void) printf("%s\n", gettext("java archive file"));
15787c478bd9Sstevel@tonic-gate return (1);
15797c478bd9Sstevel@tonic-gate }
15807c478bd9Sstevel@tonic-gate xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr);
15817c478bd9Sstevel@tonic-gate }
15827c478bd9Sstevel@tonic-gate
15837c478bd9Sstevel@tonic-gate /*
15847c478bd9Sstevel@tonic-gate * We could just print "ZIP archive" here.
15857c478bd9Sstevel@tonic-gate *
15867c478bd9Sstevel@tonic-gate * However, customers may be using their own entries in
15877c478bd9Sstevel@tonic-gate * /etc/magic to distinguish one kind of ZIP file from another, so
15887c478bd9Sstevel@tonic-gate * let's defer the printing of "ZIP archive" to there.
15897c478bd9Sstevel@tonic-gate */
15907c478bd9Sstevel@tonic-gate return (0);
15917c478bd9Sstevel@tonic-gate }
15927c478bd9Sstevel@tonic-gate
15937c478bd9Sstevel@tonic-gate static int
is_crash_dump(const char * buf,int fd)15947c478bd9Sstevel@tonic-gate is_crash_dump(const char *buf, int fd)
15957c478bd9Sstevel@tonic-gate {
15967c478bd9Sstevel@tonic-gate /* LINTED: pointer cast may result in improper alignment */
15977c478bd9Sstevel@tonic-gate const dumphdr_t *dhp = (const dumphdr_t *)buf;
15987c478bd9Sstevel@tonic-gate
15997c478bd9Sstevel@tonic-gate /*
16007c478bd9Sstevel@tonic-gate * The current DUMP_MAGIC string covers Solaris 7 and later releases.
16017c478bd9Sstevel@tonic-gate * The utsname struct is only present in dumphdr_t's with dump_version
16027c478bd9Sstevel@tonic-gate * greater than or equal to 9.
16037c478bd9Sstevel@tonic-gate */
16047c478bd9Sstevel@tonic-gate if (dhp->dump_magic == DUMP_MAGIC) {
16057c478bd9Sstevel@tonic-gate print_dumphdr(fd, dhp, return_uint32, NATIVE_ISA);
16067c478bd9Sstevel@tonic-gate
16077c478bd9Sstevel@tonic-gate } else if (dhp->dump_magic == swap_uint32(DUMP_MAGIC)) {
16087c478bd9Sstevel@tonic-gate print_dumphdr(fd, dhp, swap_uint32, OTHER_ISA);
16097c478bd9Sstevel@tonic-gate
16107c478bd9Sstevel@tonic-gate } else if (dhp->dump_magic == OLD_DUMP_MAGIC ||
16117c478bd9Sstevel@tonic-gate dhp->dump_magic == swap_uint32(OLD_DUMP_MAGIC)) {
16127c478bd9Sstevel@tonic-gate char *isa = (dhp->dump_magic == OLD_DUMP_MAGIC ?
16137c478bd9Sstevel@tonic-gate NATIVE_ISA : OTHER_ISA);
16147c478bd9Sstevel@tonic-gate (void) printf(gettext("SunOS 32-bit %s crash dump\n"), isa);
16157c478bd9Sstevel@tonic-gate
16167c478bd9Sstevel@tonic-gate } else {
16177c478bd9Sstevel@tonic-gate return (0);
16187c478bd9Sstevel@tonic-gate }
16197c478bd9Sstevel@tonic-gate
16207c478bd9Sstevel@tonic-gate return (1);
16217c478bd9Sstevel@tonic-gate }
16227c478bd9Sstevel@tonic-gate
16237c478bd9Sstevel@tonic-gate static void
print_dumphdr(const int fd,const dumphdr_t * dhp,uint32_t (* swap)(uint32_t),const char * isa)16247c478bd9Sstevel@tonic-gate print_dumphdr(const int fd, const dumphdr_t *dhp, uint32_t (*swap)(uint32_t),
16257c478bd9Sstevel@tonic-gate const char *isa)
16267c478bd9Sstevel@tonic-gate {
16277c478bd9Sstevel@tonic-gate dumphdr_t dh;
16287c478bd9Sstevel@tonic-gate
16297c478bd9Sstevel@tonic-gate /*
16307c478bd9Sstevel@tonic-gate * A dumphdr_t is bigger than FBSZ, so we have to manually read the
16317c478bd9Sstevel@tonic-gate * rest of it.
16327c478bd9Sstevel@tonic-gate */
16337c478bd9Sstevel@tonic-gate if (swap(dhp->dump_version) > 8 && pread(fd, &dh, sizeof (dumphdr_t),
16347c478bd9Sstevel@tonic-gate (off_t)0) == sizeof (dumphdr_t)) {
1635ca3e8d88SDave Plauger const char *c = swap(dh.dump_flags) & DF_COMPRESSED ?
1636ca3e8d88SDave Plauger "compressed " : "";
1637ca3e8d88SDave Plauger const char *l = swap(dh.dump_flags) & DF_LIVE ?
1638ca3e8d88SDave Plauger "live" : "crash";
1639ca3e8d88SDave Plauger
16407c478bd9Sstevel@tonic-gate (void) printf(gettext(
1641ca3e8d88SDave Plauger "%s %s %s %u-bit %s %s%s dump from '%s'\n"),
16427c478bd9Sstevel@tonic-gate dh.dump_utsname.sysname, dh.dump_utsname.release,
16437c478bd9Sstevel@tonic-gate dh.dump_utsname.version, swap(dh.dump_wordsize), isa,
1644ca3e8d88SDave Plauger c, l, dh.dump_utsname.nodename);
16457c478bd9Sstevel@tonic-gate } else {
16467c478bd9Sstevel@tonic-gate (void) printf(gettext("SunOS %u-bit %s crash dump\n"),
16477c478bd9Sstevel@tonic-gate swap(dhp->dump_wordsize), isa);
16487c478bd9Sstevel@tonic-gate }
16497c478bd9Sstevel@tonic-gate }
16507c478bd9Sstevel@tonic-gate
16517c478bd9Sstevel@tonic-gate static void
usage(void)16527c478bd9Sstevel@tonic-gate usage(void)
16537c478bd9Sstevel@tonic-gate {
16547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1655*6e987ca0SDavid Höppner "usage: file [-bdh] [-M mfile] [-m mfile] [-f ffile] file ...\n"
1656*6e987ca0SDavid Höppner " file [-bdh] [-M mfile] [-m mfile] -f ffile\n"
1657*6e987ca0SDavid Höppner " file -i [-bh] [-f ffile] file ...\n"
1658*6e987ca0SDavid Höppner " file -i [-bh] -f ffile\n"
16597c478bd9Sstevel@tonic-gate " file -c [-d] [-M mfile] [-m mfile]\n"));
16607c478bd9Sstevel@tonic-gate exit(2);
16617c478bd9Sstevel@tonic-gate }
16627c478bd9Sstevel@tonic-gate
16637c478bd9Sstevel@tonic-gate static uint32_t
swap_uint32(uint32_t in)16647c478bd9Sstevel@tonic-gate swap_uint32(uint32_t in)
16657c478bd9Sstevel@tonic-gate {
16667c478bd9Sstevel@tonic-gate uint32_t out;
16677c478bd9Sstevel@tonic-gate
16687c478bd9Sstevel@tonic-gate out = (in & 0x000000ff) << 24;
16697c478bd9Sstevel@tonic-gate out |= (in & 0x0000ff00) << 8; /* >> 8 << 16 */
16707c478bd9Sstevel@tonic-gate out |= (in & 0x00ff0000) >> 8; /* >> 16 << 8 */
16717c478bd9Sstevel@tonic-gate out |= (in & 0xff000000) >> 24;
16727c478bd9Sstevel@tonic-gate
16737c478bd9Sstevel@tonic-gate return (out);
16747c478bd9Sstevel@tonic-gate }
16757c478bd9Sstevel@tonic-gate
16767c478bd9Sstevel@tonic-gate static uint32_t
return_uint32(uint32_t in)16777c478bd9Sstevel@tonic-gate return_uint32(uint32_t in)
16787c478bd9Sstevel@tonic-gate {
16797c478bd9Sstevel@tonic-gate return (in);
16807c478bd9Sstevel@tonic-gate }
16817c478bd9Sstevel@tonic-gate
16827c478bd9Sstevel@tonic-gate /*
16837c478bd9Sstevel@tonic-gate * Check if str is in the string list str_list.
16847c478bd9Sstevel@tonic-gate */
1685c2c65e21Sny155746 int
is_in_list(char * str)1686c2c65e21Sny155746 is_in_list(char *str)
16877c478bd9Sstevel@tonic-gate {
16887c478bd9Sstevel@tonic-gate int i;
16897c478bd9Sstevel@tonic-gate
16907c478bd9Sstevel@tonic-gate /*
16917c478bd9Sstevel@tonic-gate * Only need to compare the strlen(str_list[i]) bytes.
16927c478bd9Sstevel@tonic-gate * That way .stab will match on .stab* sections, and
16937c478bd9Sstevel@tonic-gate * .debug will match on .debug* sections.
16947c478bd9Sstevel@tonic-gate */
1695c2c65e21Sny155746 for (i = 0; debug_sections[i] != NULL; i++) {
1696c2c65e21Sny155746 if (strncmp(debug_sections[i], str,
1697c2c65e21Sny155746 strlen(debug_sections[i])) == 0) {
16987c478bd9Sstevel@tonic-gate return (1);
16997c478bd9Sstevel@tonic-gate }
17007c478bd9Sstevel@tonic-gate }
17017c478bd9Sstevel@tonic-gate return (0);
17027c478bd9Sstevel@tonic-gate }
17037c478bd9Sstevel@tonic-gate
17047c478bd9Sstevel@tonic-gate /*
17057c478bd9Sstevel@tonic-gate * default_magic -
17067c478bd9Sstevel@tonic-gate * allocate space for and create the default magic file
17077c478bd9Sstevel@tonic-gate * name string.
17087c478bd9Sstevel@tonic-gate */
17097c478bd9Sstevel@tonic-gate
17107c478bd9Sstevel@tonic-gate static void
default_magic(void)17117c478bd9Sstevel@tonic-gate default_magic(void)
17127c478bd9Sstevel@tonic-gate {
17137c478bd9Sstevel@tonic-gate const char *msg_locale = setlocale(LC_MESSAGES, NULL);
17147c478bd9Sstevel@tonic-gate struct stat statbuf;
17157c478bd9Sstevel@tonic-gate
17169a411307Srie if ((dfile = malloc(strlen(msg_locale) + 35)) == NULL) {
17179a411307Srie int err = errno;
17189a411307Srie (void) fprintf(stderr, gettext("%s: malloc failed: %s\n"),
17199a411307Srie File, strerror(err));
17207c478bd9Sstevel@tonic-gate exit(2);
17217c478bd9Sstevel@tonic-gate }
17227c478bd9Sstevel@tonic-gate (void) snprintf(dfile, strlen(msg_locale) + 35,
17237c478bd9Sstevel@tonic-gate "/usr/lib/locale/%s/LC_MESSAGES/magic", msg_locale);
17247c478bd9Sstevel@tonic-gate if (stat(dfile, &statbuf) != 0) {
17257c478bd9Sstevel@tonic-gate (void) strcpy(dfile, "/etc/magic");
17267c478bd9Sstevel@tonic-gate }
17277c478bd9Sstevel@tonic-gate }
17287c478bd9Sstevel@tonic-gate
17297c478bd9Sstevel@tonic-gate /*
17307c478bd9Sstevel@tonic-gate * add_to_mlist -
17317c478bd9Sstevel@tonic-gate * Add the given magic_file filename string to the list of magic
17327c478bd9Sstevel@tonic-gate * files (mlist). This list of files will later be examined, and
17337c478bd9Sstevel@tonic-gate * each magic file's entries will be added in order to
17347c478bd9Sstevel@tonic-gate * the mtab table.
17357c478bd9Sstevel@tonic-gate *
17367c478bd9Sstevel@tonic-gate * The first flag is set to 1 to add to the first list, mlist1.
17377c478bd9Sstevel@tonic-gate * The first flag is set to 0 to add to the second list, mlist2.
17387c478bd9Sstevel@tonic-gate */
17397c478bd9Sstevel@tonic-gate
17407c478bd9Sstevel@tonic-gate static void
add_to_mlist(char * magic_file,int first)17417c478bd9Sstevel@tonic-gate add_to_mlist(char *magic_file, int first)
17427c478bd9Sstevel@tonic-gate {
17437c478bd9Sstevel@tonic-gate char **mlist; /* ordered list of magic files */
17447c478bd9Sstevel@tonic-gate size_t mlist_sz; /* number of pointers allocated for mlist */
17457c478bd9Sstevel@tonic-gate char **mlistp; /* next entry in mlist */
17467c478bd9Sstevel@tonic-gate size_t mlistp_off;
17477c478bd9Sstevel@tonic-gate
17487c478bd9Sstevel@tonic-gate if (first) {
17497c478bd9Sstevel@tonic-gate mlist = mlist1;
17507c478bd9Sstevel@tonic-gate mlist_sz = mlist1_sz;
17517c478bd9Sstevel@tonic-gate mlistp = mlist1p;
17527c478bd9Sstevel@tonic-gate } else {
17537c478bd9Sstevel@tonic-gate mlist = mlist2;
17547c478bd9Sstevel@tonic-gate mlist_sz = mlist2_sz;
17557c478bd9Sstevel@tonic-gate mlistp = mlist2p;
17567c478bd9Sstevel@tonic-gate }
17577c478bd9Sstevel@tonic-gate
17587c478bd9Sstevel@tonic-gate if (mlist == NULL) { /* initial mlist allocation */
17599a411307Srie if ((mlist = calloc(MLIST_SZ, sizeof (char *))) == NULL) {
17609a411307Srie int err = errno;
17619a411307Srie (void) fprintf(stderr, gettext("%s: malloc "
17629a411307Srie "failed: %s\n"), File, strerror(err));
17637c478bd9Sstevel@tonic-gate exit(2);
17647c478bd9Sstevel@tonic-gate }
17657c478bd9Sstevel@tonic-gate mlist_sz = MLIST_SZ;
17667c478bd9Sstevel@tonic-gate mlistp = mlist;
17677c478bd9Sstevel@tonic-gate }
17687c478bd9Sstevel@tonic-gate if ((mlistp - mlist) >= mlist_sz) {
17697c478bd9Sstevel@tonic-gate mlistp_off = mlistp - mlist;
17707c478bd9Sstevel@tonic-gate mlist_sz *= 2;
17719a411307Srie if ((mlist = realloc(mlist,
17727c478bd9Sstevel@tonic-gate mlist_sz * sizeof (char *))) == NULL) {
17739a411307Srie int err = errno;
17749a411307Srie (void) fprintf(stderr, gettext("%s: malloc "
17759a411307Srie "failed: %s\n"), File, strerror(err));
17767c478bd9Sstevel@tonic-gate exit(2);
17777c478bd9Sstevel@tonic-gate }
17787c478bd9Sstevel@tonic-gate mlistp = mlist + mlistp_off;
17797c478bd9Sstevel@tonic-gate }
17807c478bd9Sstevel@tonic-gate /*
17817c478bd9Sstevel@tonic-gate * now allocate memory for and copy the
17827c478bd9Sstevel@tonic-gate * magic file name string
17837c478bd9Sstevel@tonic-gate */
17847c478bd9Sstevel@tonic-gate if ((*mlistp = malloc(strlen(magic_file) + 1)) == NULL) {
17859a411307Srie int err = errno;
17869a411307Srie (void) fprintf(stderr, gettext("%s: malloc failed: %s\n"),
17879a411307Srie File, strerror(err));
17887c478bd9Sstevel@tonic-gate exit(2);
17897c478bd9Sstevel@tonic-gate }
17907c478bd9Sstevel@tonic-gate (void) strlcpy(*mlistp, magic_file, strlen(magic_file) + 1);
17917c478bd9Sstevel@tonic-gate mlistp++;
17927c478bd9Sstevel@tonic-gate
17937c478bd9Sstevel@tonic-gate if (first) {
17947c478bd9Sstevel@tonic-gate mlist1 = mlist;
17957c478bd9Sstevel@tonic-gate mlist1_sz = mlist_sz;
17967c478bd9Sstevel@tonic-gate mlist1p = mlistp;
17977c478bd9Sstevel@tonic-gate } else {
17987c478bd9Sstevel@tonic-gate mlist2 = mlist;
17997c478bd9Sstevel@tonic-gate mlist2_sz = mlist_sz;
18007c478bd9Sstevel@tonic-gate mlist2p = mlistp;
18017c478bd9Sstevel@tonic-gate }
18027c478bd9Sstevel@tonic-gate }
18037c478bd9Sstevel@tonic-gate
18047c478bd9Sstevel@tonic-gate static void
fd_cleanup(void)18057c478bd9Sstevel@tonic-gate fd_cleanup(void)
18067c478bd9Sstevel@tonic-gate {
18077c478bd9Sstevel@tonic-gate if (ifd != -1) {
18087c478bd9Sstevel@tonic-gate (void) close(ifd);
18097c478bd9Sstevel@tonic-gate ifd = -1;
18107c478bd9Sstevel@tonic-gate }
18117c478bd9Sstevel@tonic-gate if (elffd != -1) {
18127c478bd9Sstevel@tonic-gate (void) close(elffd);
18137c478bd9Sstevel@tonic-gate elffd = -1;
18147c478bd9Sstevel@tonic-gate }
18157c478bd9Sstevel@tonic-gate }
1816