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