1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* 28*0Sstevel@tonic-gate * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T 29*0Sstevel@tonic-gate * All Rights Reserved 30*0Sstevel@tonic-gate */ 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate /* 33*0Sstevel@tonic-gate * Copyright (c) 1987, 1988 Microsoft Corporation 34*0Sstevel@tonic-gate * All Rights Reserved 35*0Sstevel@tonic-gate */ 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate /* 38*0Sstevel@tonic-gate * Copyright (c) 1979 Regents of the University of California 39*0Sstevel@tonic-gate */ 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate #include <stdio.h> 44*0Sstevel@tonic-gate #include "a.out.h" 45*0Sstevel@tonic-gate #include <ctype.h> 46*0Sstevel@tonic-gate #include <wchar.h> 47*0Sstevel@tonic-gate #include <wctype.h> 48*0Sstevel@tonic-gate #include <libelf.h> 49*0Sstevel@tonic-gate #include <sys/elf.h> 50*0Sstevel@tonic-gate #include <locale.h> 51*0Sstevel@tonic-gate #include <string.h> 52*0Sstevel@tonic-gate #include <stdlib.h> 53*0Sstevel@tonic-gate #include <sys/types.h> 54*0Sstevel@tonic-gate #include <unistd.h> 55*0Sstevel@tonic-gate #include <limits.h> 56*0Sstevel@tonic-gate #include <widec.h> 57*0Sstevel@tonic-gate #include <gelf.h> 58*0Sstevel@tonic-gate #include <errno.h> 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate #define NOTOUT 0 62*0Sstevel@tonic-gate #define AOUT 1 63*0Sstevel@tonic-gate #define ELF 4 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate struct aexec ahdr; 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate /* 68*0Sstevel@tonic-gate * function prototypes 69*0Sstevel@tonic-gate */ 70*0Sstevel@tonic-gate static void Usage(); 71*0Sstevel@tonic-gate static void find(long); 72*0Sstevel@tonic-gate static int ismagic(int, struct aexec *, FILE *); 73*0Sstevel@tonic-gate static int tryelf(FILE *); 74*0Sstevel@tonic-gate static int dirt(int, int); 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate /* 78*0Sstevel@tonic-gate * Strings - extract strings from an object file for whatever 79*0Sstevel@tonic-gate * 80*0Sstevel@tonic-gate * The algorithm is to look for sequences of "non-junk" characters 81*0Sstevel@tonic-gate * The variable "minlen" is the minimum length string printed. 82*0Sstevel@tonic-gate * This helps get rid of garbage. 83*0Sstevel@tonic-gate * Default minimum string length is 4 characters. 84*0Sstevel@tonic-gate * 85*0Sstevel@tonic-gate */ 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate #define DEF_MIN_STRING 4 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate static int tflg; 90*0Sstevel@tonic-gate static char t_format; 91*0Sstevel@tonic-gate static int aflg; 92*0Sstevel@tonic-gate static int minlength = 0; 93*0Sstevel@tonic-gate static int isClocale = 0; 94*0Sstevel@tonic-gate static char *buf = NULL; 95*0Sstevel@tonic-gate static char *tbuf = NULL; 96*0Sstevel@tonic-gate static size_t buf_size = 0; 97*0Sstevel@tonic-gate static int rc = 0; /* exit code */ 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate main(argc, argv) 101*0Sstevel@tonic-gate int argc; 102*0Sstevel@tonic-gate char *argv[]; 103*0Sstevel@tonic-gate { 104*0Sstevel@tonic-gate int hsize; 105*0Sstevel@tonic-gate int htype; 106*0Sstevel@tonic-gate int fd; 107*0Sstevel@tonic-gate Elf *elf; 108*0Sstevel@tonic-gate GElf_Ehdr ehdr; 109*0Sstevel@tonic-gate Elf_Scn *scn; 110*0Sstevel@tonic-gate GElf_Shdr shdr; 111*0Sstevel@tonic-gate char *scn_name; 112*0Sstevel@tonic-gate char *locale; 113*0Sstevel@tonic-gate int opt; 114*0Sstevel@tonic-gate int i; 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 119*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 120*0Sstevel@tonic-gate #endif 121*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate locale = setlocale(LC_CTYPE, NULL); 124*0Sstevel@tonic-gate if ((strcmp(locale, "C") == 0) || 125*0Sstevel@tonic-gate (strcmp(locale, "POSIX") == 0)) { 126*0Sstevel@tonic-gate isClocale = 1; 127*0Sstevel@tonic-gate } 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate /* check for non-standard "-" option */ 130*0Sstevel@tonic-gate for (i = 1; i < argc; i++) { 131*0Sstevel@tonic-gate if (strcmp(argv[i], "-") == 0) { 132*0Sstevel@tonic-gate aflg++; 133*0Sstevel@tonic-gate while (i < argc) { 134*0Sstevel@tonic-gate argv[i] = argv[i+1]; 135*0Sstevel@tonic-gate i++; 136*0Sstevel@tonic-gate } 137*0Sstevel@tonic-gate argc--; 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate /* get options */ 142*0Sstevel@tonic-gate while ((opt = getopt(argc, argv, "1234567890an:ot:")) != -1) { 143*0Sstevel@tonic-gate switch (opt) { 144*0Sstevel@tonic-gate case 'a': 145*0Sstevel@tonic-gate aflg++; 146*0Sstevel@tonic-gate break; 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate case 'n': 149*0Sstevel@tonic-gate minlength = (int)strtol(optarg, (char **)NULL, 150*0Sstevel@tonic-gate 10); 151*0Sstevel@tonic-gate break; 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate case 'o': 154*0Sstevel@tonic-gate tflg++; 155*0Sstevel@tonic-gate t_format = 'd'; 156*0Sstevel@tonic-gate break; 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate case 't': 159*0Sstevel@tonic-gate tflg++; 160*0Sstevel@tonic-gate t_format = *optarg; 161*0Sstevel@tonic-gate if (t_format != 'd' && t_format != 'o' && 162*0Sstevel@tonic-gate t_format != 'x') 163*0Sstevel@tonic-gate { 164*0Sstevel@tonic-gate (void) fprintf(stderr, 165*0Sstevel@tonic-gate gettext("Invalid format\n")); 166*0Sstevel@tonic-gate Usage(); 167*0Sstevel@tonic-gate } 168*0Sstevel@tonic-gate break; 169*0Sstevel@tonic-gate case '0': 170*0Sstevel@tonic-gate case '1': 171*0Sstevel@tonic-gate case '2': 172*0Sstevel@tonic-gate case '3': 173*0Sstevel@tonic-gate case '4': 174*0Sstevel@tonic-gate case '5': 175*0Sstevel@tonic-gate case '6': 176*0Sstevel@tonic-gate case '7': 177*0Sstevel@tonic-gate case '8': 178*0Sstevel@tonic-gate case '9': 179*0Sstevel@tonic-gate minlength *= 10; 180*0Sstevel@tonic-gate minlength += opt - '0'; 181*0Sstevel@tonic-gate break; 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate default: 184*0Sstevel@tonic-gate Usage(); 185*0Sstevel@tonic-gate } 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate /* if min string not specified, use default */ 189*0Sstevel@tonic-gate if (!minlength) 190*0Sstevel@tonic-gate minlength = DEF_MIN_STRING; 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate /* dynamic allocation of char buffer array */ 194*0Sstevel@tonic-gate buf = (char *)malloc(BUFSIZ); 195*0Sstevel@tonic-gate if (buf == NULL) { 196*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("Cannot allocate memory: %s\n"), 197*0Sstevel@tonic-gate strerror(errno)); 198*0Sstevel@tonic-gate exit(1); 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate buf_size = BUFSIZ; 201*0Sstevel@tonic-gate tbuf = buf; 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate /* for each file operand */ 205*0Sstevel@tonic-gate do { 206*0Sstevel@tonic-gate if (argv[optind] != NULL) { 207*0Sstevel@tonic-gate if (freopen(argv[optind], "r", stdin) == NULL) { 208*0Sstevel@tonic-gate perror(argv[optind]); 209*0Sstevel@tonic-gate rc = 1; 210*0Sstevel@tonic-gate optind++; 211*0Sstevel@tonic-gate continue; 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate optind++; 214*0Sstevel@tonic-gate } else 215*0Sstevel@tonic-gate aflg++; 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate if (aflg) 218*0Sstevel@tonic-gate htype = NOTOUT; 219*0Sstevel@tonic-gate else { 220*0Sstevel@tonic-gate hsize = fread((char *)&ahdr, sizeof (char), 221*0Sstevel@tonic-gate sizeof (ahdr), stdin); 222*0Sstevel@tonic-gate htype = ismagic(hsize, &ahdr, stdin); 223*0Sstevel@tonic-gate } 224*0Sstevel@tonic-gate switch (htype) { 225*0Sstevel@tonic-gate case AOUT: 226*0Sstevel@tonic-gate (void) fseek(stdin, (long)ADATAPOS(&ahdr), 0); 227*0Sstevel@tonic-gate find((long)ahdr.xa_data); 228*0Sstevel@tonic-gate continue; 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate case ELF: 231*0Sstevel@tonic-gate /* 232*0Sstevel@tonic-gate * Will take care of COFF M32 and i386 also 233*0Sstevel@tonic-gate * As well as ELF M32, i386 and Sparc (32- and 64-bit) 234*0Sstevel@tonic-gate */ 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate fd = fileno(stdin); 237*0Sstevel@tonic-gate (void) lseek(fd, 0L, 0); 238*0Sstevel@tonic-gate elf = elf_begin(fd, ELF_C_READ, NULL); 239*0Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == 240*0Sstevel@tonic-gate (GElf_Ehdr *)NULL) { 241*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", 242*0Sstevel@tonic-gate argv[optind-1], elf_errmsg(-1)); 243*0Sstevel@tonic-gate (void) elf_end(elf); 244*0Sstevel@tonic-gate rc = 1; 245*0Sstevel@tonic-gate continue; 246*0Sstevel@tonic-gate } 247*0Sstevel@tonic-gate scn = 0; 248*0Sstevel@tonic-gate while ((scn = elf_nextscn(elf, scn)) != 0) 249*0Sstevel@tonic-gate { 250*0Sstevel@tonic-gate if (gelf_getshdr(scn, &shdr) == 251*0Sstevel@tonic-gate (GElf_Shdr *)0) { 252*0Sstevel@tonic-gate (void) fprintf(stderr, 253*0Sstevel@tonic-gate "%s: %s\n", argv[optind-1], 254*0Sstevel@tonic-gate elf_errmsg(-1)); 255*0Sstevel@tonic-gate rc = 1; 256*0Sstevel@tonic-gate continue; 257*0Sstevel@tonic-gate } 258*0Sstevel@tonic-gate if ((scn_name = elf_strptr(elf, 259*0Sstevel@tonic-gate ehdr.e_shstrndx, 260*0Sstevel@tonic-gate (size_t)shdr.sh_name)) 261*0Sstevel@tonic-gate == (char *)NULL) { 262*0Sstevel@tonic-gate (void) fprintf(stderr, 263*0Sstevel@tonic-gate "%s: %s\n", argv[optind-1], 264*0Sstevel@tonic-gate elf_errmsg(-1)); 265*0Sstevel@tonic-gate rc = 1; 266*0Sstevel@tonic-gate continue; 267*0Sstevel@tonic-gate } 268*0Sstevel@tonic-gate /* 269*0Sstevel@tonic-gate * There is more than one .data section 270*0Sstevel@tonic-gate */ 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate if ((strcmp(scn_name, ".rodata") 273*0Sstevel@tonic-gate == 0) || 274*0Sstevel@tonic-gate (strcmp(scn_name, ".rodata1") 275*0Sstevel@tonic-gate == 0) || 276*0Sstevel@tonic-gate (strcmp(scn_name, ".data") 277*0Sstevel@tonic-gate == 0) || 278*0Sstevel@tonic-gate (strcmp(scn_name, ".data1") 279*0Sstevel@tonic-gate == 0)) 280*0Sstevel@tonic-gate { 281*0Sstevel@tonic-gate (void) fseek(stdin, 282*0Sstevel@tonic-gate (long)shdr.sh_offset, 0); 283*0Sstevel@tonic-gate find((long)shdr.sh_size); 284*0Sstevel@tonic-gate } 285*0Sstevel@tonic-gate } 286*0Sstevel@tonic-gate continue; 287*0Sstevel@tonic-gate 288*0Sstevel@tonic-gate case NOTOUT: 289*0Sstevel@tonic-gate default: 290*0Sstevel@tonic-gate if (!aflg) 291*0Sstevel@tonic-gate (void) fseek(stdin, (long)0, 0); 292*0Sstevel@tonic-gate find(LONG_MAX); 293*0Sstevel@tonic-gate continue; 294*0Sstevel@tonic-gate } 295*0Sstevel@tonic-gate } while (argv[optind] != NULL); 296*0Sstevel@tonic-gate 297*0Sstevel@tonic-gate return (rc); 298*0Sstevel@tonic-gate } 299*0Sstevel@tonic-gate 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate static void 302*0Sstevel@tonic-gate find(cnt) 303*0Sstevel@tonic-gate long cnt; 304*0Sstevel@tonic-gate { 305*0Sstevel@tonic-gate int c; 306*0Sstevel@tonic-gate int cc; 307*0Sstevel@tonic-gate int cr; 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate cc = 0; 310*0Sstevel@tonic-gate for (c = ~EOF; (cnt > 0) && (c != EOF); cnt--) { 311*0Sstevel@tonic-gate c = getc(stdin); 312*0Sstevel@tonic-gate if (!(cr = dirt(c, cc))) { 313*0Sstevel@tonic-gate if (cc >= minlength) { 314*0Sstevel@tonic-gate if (tflg) { 315*0Sstevel@tonic-gate switch (t_format) { 316*0Sstevel@tonic-gate case 'd': 317*0Sstevel@tonic-gate (void) printf("%7ld ", 318*0Sstevel@tonic-gate ftell(stdin) - cc - 1); 319*0Sstevel@tonic-gate break; 320*0Sstevel@tonic-gate 321*0Sstevel@tonic-gate case 'o': 322*0Sstevel@tonic-gate (void) printf("%7lo ", 323*0Sstevel@tonic-gate ftell(stdin) - cc - 1); 324*0Sstevel@tonic-gate break; 325*0Sstevel@tonic-gate 326*0Sstevel@tonic-gate case 'x': 327*0Sstevel@tonic-gate (void) printf("%7lx ", 328*0Sstevel@tonic-gate ftell(stdin) - cc - 1); 329*0Sstevel@tonic-gate break; 330*0Sstevel@tonic-gate } 331*0Sstevel@tonic-gate } 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gate if (cc >= buf_size) 334*0Sstevel@tonic-gate buf[buf_size-1] = '\0'; 335*0Sstevel@tonic-gate else 336*0Sstevel@tonic-gate buf[cc] = '\0'; 337*0Sstevel@tonic-gate (void) puts(buf); 338*0Sstevel@tonic-gate } 339*0Sstevel@tonic-gate cc = 0; 340*0Sstevel@tonic-gate } 341*0Sstevel@tonic-gate cc += cr; 342*0Sstevel@tonic-gate } 343*0Sstevel@tonic-gate } 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate static int 346*0Sstevel@tonic-gate dirt(c, cc) 347*0Sstevel@tonic-gate int c; 348*0Sstevel@tonic-gate int cc; 349*0Sstevel@tonic-gate { 350*0Sstevel@tonic-gate char mbuf[MB_LEN_MAX + 1]; 351*0Sstevel@tonic-gate int len, len1, i; 352*0Sstevel@tonic-gate wchar_t wc; 353*0Sstevel@tonic-gate int r_val; 354*0Sstevel@tonic-gate 355*0Sstevel@tonic-gate if (isascii(c)) { 356*0Sstevel@tonic-gate if (isprint(c)) { 357*0Sstevel@tonic-gate /* 358*0Sstevel@tonic-gate * If character count is greater than dynamic 359*0Sstevel@tonic-gate * char buffer size, then increase char buffer size. 360*0Sstevel@tonic-gate */ 361*0Sstevel@tonic-gate if (cc >= (buf_size-2)) { 362*0Sstevel@tonic-gate if (tbuf != NULL) { 363*0Sstevel@tonic-gate buf_size += BUFSIZ; 364*0Sstevel@tonic-gate tbuf = (char *)realloc(buf, buf_size); 365*0Sstevel@tonic-gate if (tbuf == NULL) { 366*0Sstevel@tonic-gate (void) fprintf(stderr, 367*0Sstevel@tonic-gate gettext("Cannot allocate memory: %s\n"), 368*0Sstevel@tonic-gate strerror(errno)); 369*0Sstevel@tonic-gate buf_size -= BUFSIZ; 370*0Sstevel@tonic-gate rc = 1; 371*0Sstevel@tonic-gate return (0); 372*0Sstevel@tonic-gate } else { 373*0Sstevel@tonic-gate buf = tbuf; 374*0Sstevel@tonic-gate } 375*0Sstevel@tonic-gate } else { 376*0Sstevel@tonic-gate return (0); 377*0Sstevel@tonic-gate } 378*0Sstevel@tonic-gate } 379*0Sstevel@tonic-gate buf[cc] = c; 380*0Sstevel@tonic-gate return (1); 381*0Sstevel@tonic-gate } 382*0Sstevel@tonic-gate return (0); 383*0Sstevel@tonic-gate } 384*0Sstevel@tonic-gate 385*0Sstevel@tonic-gate if (isClocale) 386*0Sstevel@tonic-gate return (0); 387*0Sstevel@tonic-gate 388*0Sstevel@tonic-gate r_val = 0; 389*0Sstevel@tonic-gate mbuf[0] = c; 390*0Sstevel@tonic-gate for (len = 1; len < (unsigned int)MB_CUR_MAX; len++) { 391*0Sstevel@tonic-gate if ((signed char) 392*0Sstevel@tonic-gate (mbuf[len] = getc(stdin)) == -1) 393*0Sstevel@tonic-gate break; 394*0Sstevel@tonic-gate } 395*0Sstevel@tonic-gate mbuf[len] = 0; 396*0Sstevel@tonic-gate 397*0Sstevel@tonic-gate if ((len1 = mbtowc(&wc, mbuf, len)) <= 0) { 398*0Sstevel@tonic-gate len1 = 1; 399*0Sstevel@tonic-gate goto _unget; 400*0Sstevel@tonic-gate } 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate if (iswprint(wc)) { 403*0Sstevel@tonic-gate if ((cc + len1) >= (buf_size-2)) { 404*0Sstevel@tonic-gate if (tbuf != NULL) { 405*0Sstevel@tonic-gate buf_size += BUFSIZ; 406*0Sstevel@tonic-gate tbuf = (char *)realloc(buf, buf_size); 407*0Sstevel@tonic-gate if (tbuf == NULL) { 408*0Sstevel@tonic-gate (void) fprintf(stderr, 409*0Sstevel@tonic-gate gettext("Cannot allocate memory: %s\n"), 410*0Sstevel@tonic-gate strerror(errno)); 411*0Sstevel@tonic-gate buf_size -= BUFSIZ; 412*0Sstevel@tonic-gate rc = 1; 413*0Sstevel@tonic-gate return (0); 414*0Sstevel@tonic-gate } 415*0Sstevel@tonic-gate buf = tbuf; 416*0Sstevel@tonic-gate } else { 417*0Sstevel@tonic-gate return (0); 418*0Sstevel@tonic-gate } 419*0Sstevel@tonic-gate } 420*0Sstevel@tonic-gate for (i = 0; i < len1; i++, cc++) 421*0Sstevel@tonic-gate buf[cc] = mbuf[i]; 422*0Sstevel@tonic-gate r_val = len1; 423*0Sstevel@tonic-gate } 424*0Sstevel@tonic-gate 425*0Sstevel@tonic-gate _unget: 426*0Sstevel@tonic-gate for (len--; len >= len1; len--) 427*0Sstevel@tonic-gate (void) ungetc(mbuf[len], stdin); 428*0Sstevel@tonic-gate return (r_val); 429*0Sstevel@tonic-gate } 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate 432*0Sstevel@tonic-gate static int 433*0Sstevel@tonic-gate ismagic(hsize, hdr, fp) 434*0Sstevel@tonic-gate int hsize; 435*0Sstevel@tonic-gate struct aexec *hdr; 436*0Sstevel@tonic-gate FILE *fp; 437*0Sstevel@tonic-gate { 438*0Sstevel@tonic-gate switch (hdr->xa_magic) { 439*0Sstevel@tonic-gate case A_MAGIC1: 440*0Sstevel@tonic-gate case A_MAGIC2: 441*0Sstevel@tonic-gate case A_MAGIC3: 442*0Sstevel@tonic-gate case A_MAGIC4: 443*0Sstevel@tonic-gate if (hsize < sizeof (struct aexec)) 444*0Sstevel@tonic-gate return (NOTOUT); 445*0Sstevel@tonic-gate else 446*0Sstevel@tonic-gate return (AOUT); 447*0Sstevel@tonic-gate default: 448*0Sstevel@tonic-gate break; 449*0Sstevel@tonic-gate } 450*0Sstevel@tonic-gate return (tryelf(fp)); 451*0Sstevel@tonic-gate } 452*0Sstevel@tonic-gate 453*0Sstevel@tonic-gate 454*0Sstevel@tonic-gate static int 455*0Sstevel@tonic-gate tryelf(fp) 456*0Sstevel@tonic-gate FILE *fp; 457*0Sstevel@tonic-gate { 458*0Sstevel@tonic-gate int fd; 459*0Sstevel@tonic-gate Elf *elf; 460*0Sstevel@tonic-gate GElf_Ehdr ehdr; 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gate fd = fileno(fp); 463*0Sstevel@tonic-gate 464*0Sstevel@tonic-gate if ((elf_version(EV_CURRENT)) == EV_NONE) { 465*0Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", elf_errmsg(-1)); 466*0Sstevel@tonic-gate return (NOTOUT); 467*0Sstevel@tonic-gate } 468*0Sstevel@tonic-gate 469*0Sstevel@tonic-gate (void) lseek(fd, 0L, 0); 470*0Sstevel@tonic-gate 471*0Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 472*0Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", elf_errmsg(-1)); 473*0Sstevel@tonic-gate return (NOTOUT); 474*0Sstevel@tonic-gate } 475*0Sstevel@tonic-gate 476*0Sstevel@tonic-gate switch (elf_kind(elf)) { 477*0Sstevel@tonic-gate case ELF_K_AR: 478*0Sstevel@tonic-gate /* 479*0Sstevel@tonic-gate * This should try to run strings on each element 480*0Sstevel@tonic-gate * of the archive. For now, just search entire 481*0Sstevel@tonic-gate * file (-a), as strings has always done 482*0Sstevel@tonic-gate * for archives. 483*0Sstevel@tonic-gate */ 484*0Sstevel@tonic-gate case ELF_K_NONE: 485*0Sstevel@tonic-gate (void) elf_end(elf); 486*0Sstevel@tonic-gate return (NOTOUT); 487*0Sstevel@tonic-gate } 488*0Sstevel@tonic-gate 489*0Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == (GElf_Ehdr *)NULL) { 490*0Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", elf_errmsg(-1)); 491*0Sstevel@tonic-gate (void) elf_end(elf); 492*0Sstevel@tonic-gate return (NOTOUT); 493*0Sstevel@tonic-gate } 494*0Sstevel@tonic-gate 495*0Sstevel@tonic-gate if ((ehdr.e_type == ET_CORE) || (ehdr.e_type == ET_NONE)) { 496*0Sstevel@tonic-gate (void) elf_end(elf); 497*0Sstevel@tonic-gate return (NOTOUT); 498*0Sstevel@tonic-gate } 499*0Sstevel@tonic-gate 500*0Sstevel@tonic-gate (void) elf_end(elf); 501*0Sstevel@tonic-gate 502*0Sstevel@tonic-gate return (ELF); 503*0Sstevel@tonic-gate 504*0Sstevel@tonic-gate } 505*0Sstevel@tonic-gate 506*0Sstevel@tonic-gate 507*0Sstevel@tonic-gate static void 508*0Sstevel@tonic-gate Usage() 509*0Sstevel@tonic-gate { 510*0Sstevel@tonic-gate (void) fprintf(stderr, gettext( 511*0Sstevel@tonic-gate "Usage: strings [-|-a] [-t format] [-n #] [file ...]\n" 512*0Sstevel@tonic-gate " strings [-|-a] [-o] [-#] [file ...]\n")); 513*0Sstevel@tonic-gate exit(1); 514*0Sstevel@tonic-gate } 515