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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T 297c478bd9Sstevel@tonic-gate * All Rights Reserved 307c478bd9Sstevel@tonic-gate */ 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate /* 337c478bd9Sstevel@tonic-gate * Copyright (c) 1987, 1988 Microsoft Corporation 347c478bd9Sstevel@tonic-gate * All Rights Reserved 357c478bd9Sstevel@tonic-gate */ 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /* 387c478bd9Sstevel@tonic-gate * Copyright (c) 1979 Regents of the University of California 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include <stdio.h> 447c478bd9Sstevel@tonic-gate #include "a.out.h" 457c478bd9Sstevel@tonic-gate #include <ctype.h> 467c478bd9Sstevel@tonic-gate #include <wchar.h> 477c478bd9Sstevel@tonic-gate #include <wctype.h> 487c478bd9Sstevel@tonic-gate #include <libelf.h> 497c478bd9Sstevel@tonic-gate #include <sys/elf.h> 507c478bd9Sstevel@tonic-gate #include <locale.h> 517c478bd9Sstevel@tonic-gate #include <string.h> 527c478bd9Sstevel@tonic-gate #include <stdlib.h> 537c478bd9Sstevel@tonic-gate #include <sys/types.h> 547c478bd9Sstevel@tonic-gate #include <unistd.h> 557c478bd9Sstevel@tonic-gate #include <limits.h> 567c478bd9Sstevel@tonic-gate #include <widec.h> 577c478bd9Sstevel@tonic-gate #include <gelf.h> 587c478bd9Sstevel@tonic-gate #include <errno.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #define NOTOUT 0 627c478bd9Sstevel@tonic-gate #define AOUT 1 637c478bd9Sstevel@tonic-gate #define ELF 4 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate struct aexec ahdr; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate /* 687c478bd9Sstevel@tonic-gate * function prototypes 697c478bd9Sstevel@tonic-gate */ 707c478bd9Sstevel@tonic-gate static void Usage(); 717c478bd9Sstevel@tonic-gate static void find(long); 727c478bd9Sstevel@tonic-gate static int ismagic(int, struct aexec *, FILE *); 737c478bd9Sstevel@tonic-gate static int tryelf(FILE *); 747c478bd9Sstevel@tonic-gate static int dirt(int, int); 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate /* 787c478bd9Sstevel@tonic-gate * Strings - extract strings from an object file for whatever 797c478bd9Sstevel@tonic-gate * 807c478bd9Sstevel@tonic-gate * The algorithm is to look for sequences of "non-junk" characters 817c478bd9Sstevel@tonic-gate * The variable "minlen" is the minimum length string printed. 827c478bd9Sstevel@tonic-gate * This helps get rid of garbage. 837c478bd9Sstevel@tonic-gate * Default minimum string length is 4 characters. 847c478bd9Sstevel@tonic-gate * 857c478bd9Sstevel@tonic-gate */ 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate #define DEF_MIN_STRING 4 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate static int tflg; 907c478bd9Sstevel@tonic-gate static char t_format; 917c478bd9Sstevel@tonic-gate static int aflg; 927c478bd9Sstevel@tonic-gate static int minlength = 0; 937c478bd9Sstevel@tonic-gate static int isClocale = 0; 947c478bd9Sstevel@tonic-gate static char *buf = NULL; 957c478bd9Sstevel@tonic-gate static char *tbuf = NULL; 967c478bd9Sstevel@tonic-gate static size_t buf_size = 0; 977c478bd9Sstevel@tonic-gate static int rc = 0; /* exit code */ 987c478bd9Sstevel@tonic-gate 99*4fce32e1Smuffin int 1007c478bd9Sstevel@tonic-gate main(argc, argv) 1017c478bd9Sstevel@tonic-gate int argc; 1027c478bd9Sstevel@tonic-gate char *argv[]; 1037c478bd9Sstevel@tonic-gate { 1047c478bd9Sstevel@tonic-gate int hsize; 1057c478bd9Sstevel@tonic-gate int htype; 1067c478bd9Sstevel@tonic-gate int fd; 1077c478bd9Sstevel@tonic-gate Elf *elf; 1087c478bd9Sstevel@tonic-gate GElf_Ehdr ehdr; 1097c478bd9Sstevel@tonic-gate Elf_Scn *scn; 1107c478bd9Sstevel@tonic-gate GElf_Shdr shdr; 1117c478bd9Sstevel@tonic-gate char *scn_name; 1127c478bd9Sstevel@tonic-gate char *locale; 1137c478bd9Sstevel@tonic-gate int opt; 1147c478bd9Sstevel@tonic-gate int i; 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 1197c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 1207c478bd9Sstevel@tonic-gate #endif 1217c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate locale = setlocale(LC_CTYPE, NULL); 1247c478bd9Sstevel@tonic-gate if ((strcmp(locale, "C") == 0) || 1257c478bd9Sstevel@tonic-gate (strcmp(locale, "POSIX") == 0)) { 1267c478bd9Sstevel@tonic-gate isClocale = 1; 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate /* check for non-standard "-" option */ 1307c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) { 1317c478bd9Sstevel@tonic-gate if (strcmp(argv[i], "-") == 0) { 1327c478bd9Sstevel@tonic-gate aflg++; 1337c478bd9Sstevel@tonic-gate while (i < argc) { 1347c478bd9Sstevel@tonic-gate argv[i] = argv[i+1]; 1357c478bd9Sstevel@tonic-gate i++; 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate argc--; 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /* get options */ 1427c478bd9Sstevel@tonic-gate while ((opt = getopt(argc, argv, "1234567890an:ot:")) != -1) { 1437c478bd9Sstevel@tonic-gate switch (opt) { 1447c478bd9Sstevel@tonic-gate case 'a': 1457c478bd9Sstevel@tonic-gate aflg++; 1467c478bd9Sstevel@tonic-gate break; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate case 'n': 1497c478bd9Sstevel@tonic-gate minlength = (int)strtol(optarg, (char **)NULL, 1507c478bd9Sstevel@tonic-gate 10); 1517c478bd9Sstevel@tonic-gate break; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate case 'o': 1547c478bd9Sstevel@tonic-gate tflg++; 1557c478bd9Sstevel@tonic-gate t_format = 'd'; 1567c478bd9Sstevel@tonic-gate break; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate case 't': 1597c478bd9Sstevel@tonic-gate tflg++; 1607c478bd9Sstevel@tonic-gate t_format = *optarg; 1617c478bd9Sstevel@tonic-gate if (t_format != 'd' && t_format != 'o' && 1627c478bd9Sstevel@tonic-gate t_format != 'x') 1637c478bd9Sstevel@tonic-gate { 1647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1657c478bd9Sstevel@tonic-gate gettext("Invalid format\n")); 1667c478bd9Sstevel@tonic-gate Usage(); 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate break; 1697c478bd9Sstevel@tonic-gate case '0': 1707c478bd9Sstevel@tonic-gate case '1': 1717c478bd9Sstevel@tonic-gate case '2': 1727c478bd9Sstevel@tonic-gate case '3': 1737c478bd9Sstevel@tonic-gate case '4': 1747c478bd9Sstevel@tonic-gate case '5': 1757c478bd9Sstevel@tonic-gate case '6': 1767c478bd9Sstevel@tonic-gate case '7': 1777c478bd9Sstevel@tonic-gate case '8': 1787c478bd9Sstevel@tonic-gate case '9': 1797c478bd9Sstevel@tonic-gate minlength *= 10; 1807c478bd9Sstevel@tonic-gate minlength += opt - '0'; 1817c478bd9Sstevel@tonic-gate break; 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate default: 1847c478bd9Sstevel@tonic-gate Usage(); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate /* if min string not specified, use default */ 1897c478bd9Sstevel@tonic-gate if (!minlength) 1907c478bd9Sstevel@tonic-gate minlength = DEF_MIN_STRING; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate /* dynamic allocation of char buffer array */ 1947c478bd9Sstevel@tonic-gate buf = (char *)malloc(BUFSIZ); 1957c478bd9Sstevel@tonic-gate if (buf == NULL) { 1967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Cannot allocate memory: %s\n"), 1977c478bd9Sstevel@tonic-gate strerror(errno)); 1987c478bd9Sstevel@tonic-gate exit(1); 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate buf_size = BUFSIZ; 2017c478bd9Sstevel@tonic-gate tbuf = buf; 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate /* for each file operand */ 2057c478bd9Sstevel@tonic-gate do { 2067c478bd9Sstevel@tonic-gate if (argv[optind] != NULL) { 2077c478bd9Sstevel@tonic-gate if (freopen(argv[optind], "r", stdin) == NULL) { 2087c478bd9Sstevel@tonic-gate perror(argv[optind]); 2097c478bd9Sstevel@tonic-gate rc = 1; 2107c478bd9Sstevel@tonic-gate optind++; 2117c478bd9Sstevel@tonic-gate continue; 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate optind++; 2147c478bd9Sstevel@tonic-gate } else 2157c478bd9Sstevel@tonic-gate aflg++; 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate if (aflg) 2187c478bd9Sstevel@tonic-gate htype = NOTOUT; 2197c478bd9Sstevel@tonic-gate else { 2207c478bd9Sstevel@tonic-gate hsize = fread((char *)&ahdr, sizeof (char), 2217c478bd9Sstevel@tonic-gate sizeof (ahdr), stdin); 2227c478bd9Sstevel@tonic-gate htype = ismagic(hsize, &ahdr, stdin); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate switch (htype) { 2257c478bd9Sstevel@tonic-gate case AOUT: 2267c478bd9Sstevel@tonic-gate (void) fseek(stdin, (long)ADATAPOS(&ahdr), 0); 2277c478bd9Sstevel@tonic-gate find((long)ahdr.xa_data); 2287c478bd9Sstevel@tonic-gate continue; 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate case ELF: 2317c478bd9Sstevel@tonic-gate /* 2327c478bd9Sstevel@tonic-gate * Will take care of COFF M32 and i386 also 2337c478bd9Sstevel@tonic-gate * As well as ELF M32, i386 and Sparc (32- and 64-bit) 2347c478bd9Sstevel@tonic-gate */ 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate fd = fileno(stdin); 2377c478bd9Sstevel@tonic-gate (void) lseek(fd, 0L, 0); 2387c478bd9Sstevel@tonic-gate elf = elf_begin(fd, ELF_C_READ, NULL); 2397c478bd9Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == 2407c478bd9Sstevel@tonic-gate (GElf_Ehdr *)NULL) { 2417c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", 2427c478bd9Sstevel@tonic-gate argv[optind-1], elf_errmsg(-1)); 2437c478bd9Sstevel@tonic-gate (void) elf_end(elf); 2447c478bd9Sstevel@tonic-gate rc = 1; 2457c478bd9Sstevel@tonic-gate continue; 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate scn = 0; 2487c478bd9Sstevel@tonic-gate while ((scn = elf_nextscn(elf, scn)) != 0) 2497c478bd9Sstevel@tonic-gate { 2507c478bd9Sstevel@tonic-gate if (gelf_getshdr(scn, &shdr) == 2517c478bd9Sstevel@tonic-gate (GElf_Shdr *)0) { 2527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2537c478bd9Sstevel@tonic-gate "%s: %s\n", argv[optind-1], 2547c478bd9Sstevel@tonic-gate elf_errmsg(-1)); 2557c478bd9Sstevel@tonic-gate rc = 1; 2567c478bd9Sstevel@tonic-gate continue; 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate if ((scn_name = elf_strptr(elf, 2597c478bd9Sstevel@tonic-gate ehdr.e_shstrndx, 2607c478bd9Sstevel@tonic-gate (size_t)shdr.sh_name)) 2617c478bd9Sstevel@tonic-gate == (char *)NULL) { 2627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2637c478bd9Sstevel@tonic-gate "%s: %s\n", argv[optind-1], 2647c478bd9Sstevel@tonic-gate elf_errmsg(-1)); 2657c478bd9Sstevel@tonic-gate rc = 1; 2667c478bd9Sstevel@tonic-gate continue; 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate /* 2697c478bd9Sstevel@tonic-gate * There is more than one .data section 2707c478bd9Sstevel@tonic-gate */ 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate if ((strcmp(scn_name, ".rodata") 2737c478bd9Sstevel@tonic-gate == 0) || 2747c478bd9Sstevel@tonic-gate (strcmp(scn_name, ".rodata1") 2757c478bd9Sstevel@tonic-gate == 0) || 2767c478bd9Sstevel@tonic-gate (strcmp(scn_name, ".data") 2777c478bd9Sstevel@tonic-gate == 0) || 2787c478bd9Sstevel@tonic-gate (strcmp(scn_name, ".data1") 2797c478bd9Sstevel@tonic-gate == 0)) 2807c478bd9Sstevel@tonic-gate { 2817c478bd9Sstevel@tonic-gate (void) fseek(stdin, 2827c478bd9Sstevel@tonic-gate (long)shdr.sh_offset, 0); 2837c478bd9Sstevel@tonic-gate find((long)shdr.sh_size); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate continue; 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate case NOTOUT: 2897c478bd9Sstevel@tonic-gate default: 2907c478bd9Sstevel@tonic-gate if (!aflg) 2917c478bd9Sstevel@tonic-gate (void) fseek(stdin, (long)0, 0); 2927c478bd9Sstevel@tonic-gate find(LONG_MAX); 2937c478bd9Sstevel@tonic-gate continue; 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate } while (argv[optind] != NULL); 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate return (rc); 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate static void 3027c478bd9Sstevel@tonic-gate find(cnt) 3037c478bd9Sstevel@tonic-gate long cnt; 3047c478bd9Sstevel@tonic-gate { 3057c478bd9Sstevel@tonic-gate int c; 3067c478bd9Sstevel@tonic-gate int cc; 3077c478bd9Sstevel@tonic-gate int cr; 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate cc = 0; 3107c478bd9Sstevel@tonic-gate for (c = ~EOF; (cnt > 0) && (c != EOF); cnt--) { 3117c478bd9Sstevel@tonic-gate c = getc(stdin); 3127c478bd9Sstevel@tonic-gate if (!(cr = dirt(c, cc))) { 3137c478bd9Sstevel@tonic-gate if (cc >= minlength) { 3147c478bd9Sstevel@tonic-gate if (tflg) { 3157c478bd9Sstevel@tonic-gate switch (t_format) { 3167c478bd9Sstevel@tonic-gate case 'd': 3177c478bd9Sstevel@tonic-gate (void) printf("%7ld ", 3187c478bd9Sstevel@tonic-gate ftell(stdin) - cc - 1); 3197c478bd9Sstevel@tonic-gate break; 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate case 'o': 3227c478bd9Sstevel@tonic-gate (void) printf("%7lo ", 3237c478bd9Sstevel@tonic-gate ftell(stdin) - cc - 1); 3247c478bd9Sstevel@tonic-gate break; 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate case 'x': 3277c478bd9Sstevel@tonic-gate (void) printf("%7lx ", 3287c478bd9Sstevel@tonic-gate ftell(stdin) - cc - 1); 3297c478bd9Sstevel@tonic-gate break; 3307c478bd9Sstevel@tonic-gate } 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate if (cc >= buf_size) 3347c478bd9Sstevel@tonic-gate buf[buf_size-1] = '\0'; 3357c478bd9Sstevel@tonic-gate else 3367c478bd9Sstevel@tonic-gate buf[cc] = '\0'; 3377c478bd9Sstevel@tonic-gate (void) puts(buf); 3387c478bd9Sstevel@tonic-gate } 3397c478bd9Sstevel@tonic-gate cc = 0; 3407c478bd9Sstevel@tonic-gate } 3417c478bd9Sstevel@tonic-gate cc += cr; 3427c478bd9Sstevel@tonic-gate } 3437c478bd9Sstevel@tonic-gate } 3447c478bd9Sstevel@tonic-gate 3457c478bd9Sstevel@tonic-gate static int 3467c478bd9Sstevel@tonic-gate dirt(c, cc) 3477c478bd9Sstevel@tonic-gate int c; 3487c478bd9Sstevel@tonic-gate int cc; 3497c478bd9Sstevel@tonic-gate { 3507c478bd9Sstevel@tonic-gate char mbuf[MB_LEN_MAX + 1]; 3517c478bd9Sstevel@tonic-gate int len, len1, i; 3527c478bd9Sstevel@tonic-gate wchar_t wc; 3537c478bd9Sstevel@tonic-gate int r_val; 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate if (isascii(c)) { 3567c478bd9Sstevel@tonic-gate if (isprint(c)) { 3577c478bd9Sstevel@tonic-gate /* 3587c478bd9Sstevel@tonic-gate * If character count is greater than dynamic 3597c478bd9Sstevel@tonic-gate * char buffer size, then increase char buffer size. 3607c478bd9Sstevel@tonic-gate */ 3617c478bd9Sstevel@tonic-gate if (cc >= (buf_size-2)) { 3627c478bd9Sstevel@tonic-gate if (tbuf != NULL) { 3637c478bd9Sstevel@tonic-gate buf_size += BUFSIZ; 3647c478bd9Sstevel@tonic-gate tbuf = (char *)realloc(buf, buf_size); 3657c478bd9Sstevel@tonic-gate if (tbuf == NULL) { 3667c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3677c478bd9Sstevel@tonic-gate gettext("Cannot allocate memory: %s\n"), 3687c478bd9Sstevel@tonic-gate strerror(errno)); 3697c478bd9Sstevel@tonic-gate buf_size -= BUFSIZ; 3707c478bd9Sstevel@tonic-gate rc = 1; 3717c478bd9Sstevel@tonic-gate return (0); 3727c478bd9Sstevel@tonic-gate } else { 3737c478bd9Sstevel@tonic-gate buf = tbuf; 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate } else { 3767c478bd9Sstevel@tonic-gate return (0); 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate buf[cc] = c; 3807c478bd9Sstevel@tonic-gate return (1); 3817c478bd9Sstevel@tonic-gate } 3827c478bd9Sstevel@tonic-gate return (0); 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate if (isClocale) 3867c478bd9Sstevel@tonic-gate return (0); 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate r_val = 0; 3897c478bd9Sstevel@tonic-gate mbuf[0] = c; 3907c478bd9Sstevel@tonic-gate for (len = 1; len < (unsigned int)MB_CUR_MAX; len++) { 3917c478bd9Sstevel@tonic-gate if ((signed char) 3927c478bd9Sstevel@tonic-gate (mbuf[len] = getc(stdin)) == -1) 3937c478bd9Sstevel@tonic-gate break; 3947c478bd9Sstevel@tonic-gate } 3957c478bd9Sstevel@tonic-gate mbuf[len] = 0; 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate if ((len1 = mbtowc(&wc, mbuf, len)) <= 0) { 3987c478bd9Sstevel@tonic-gate len1 = 1; 3997c478bd9Sstevel@tonic-gate goto _unget; 4007c478bd9Sstevel@tonic-gate } 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate if (iswprint(wc)) { 4037c478bd9Sstevel@tonic-gate if ((cc + len1) >= (buf_size-2)) { 4047c478bd9Sstevel@tonic-gate if (tbuf != NULL) { 4057c478bd9Sstevel@tonic-gate buf_size += BUFSIZ; 4067c478bd9Sstevel@tonic-gate tbuf = (char *)realloc(buf, buf_size); 4077c478bd9Sstevel@tonic-gate if (tbuf == NULL) { 4087c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4097c478bd9Sstevel@tonic-gate gettext("Cannot allocate memory: %s\n"), 4107c478bd9Sstevel@tonic-gate strerror(errno)); 4117c478bd9Sstevel@tonic-gate buf_size -= BUFSIZ; 4127c478bd9Sstevel@tonic-gate rc = 1; 4137c478bd9Sstevel@tonic-gate return (0); 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate buf = tbuf; 4167c478bd9Sstevel@tonic-gate } else { 4177c478bd9Sstevel@tonic-gate return (0); 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate } 4207c478bd9Sstevel@tonic-gate for (i = 0; i < len1; i++, cc++) 4217c478bd9Sstevel@tonic-gate buf[cc] = mbuf[i]; 4227c478bd9Sstevel@tonic-gate r_val = len1; 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate _unget: 4267c478bd9Sstevel@tonic-gate for (len--; len >= len1; len--) 4277c478bd9Sstevel@tonic-gate (void) ungetc(mbuf[len], stdin); 4287c478bd9Sstevel@tonic-gate return (r_val); 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate static int 4337c478bd9Sstevel@tonic-gate ismagic(hsize, hdr, fp) 4347c478bd9Sstevel@tonic-gate int hsize; 4357c478bd9Sstevel@tonic-gate struct aexec *hdr; 4367c478bd9Sstevel@tonic-gate FILE *fp; 4377c478bd9Sstevel@tonic-gate { 4387c478bd9Sstevel@tonic-gate switch (hdr->xa_magic) { 4397c478bd9Sstevel@tonic-gate case A_MAGIC1: 4407c478bd9Sstevel@tonic-gate case A_MAGIC2: 4417c478bd9Sstevel@tonic-gate case A_MAGIC3: 4427c478bd9Sstevel@tonic-gate case A_MAGIC4: 4437c478bd9Sstevel@tonic-gate if (hsize < sizeof (struct aexec)) 4447c478bd9Sstevel@tonic-gate return (NOTOUT); 4457c478bd9Sstevel@tonic-gate else 4467c478bd9Sstevel@tonic-gate return (AOUT); 4477c478bd9Sstevel@tonic-gate default: 4487c478bd9Sstevel@tonic-gate break; 4497c478bd9Sstevel@tonic-gate } 4507c478bd9Sstevel@tonic-gate return (tryelf(fp)); 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate static int 4557c478bd9Sstevel@tonic-gate tryelf(fp) 4567c478bd9Sstevel@tonic-gate FILE *fp; 4577c478bd9Sstevel@tonic-gate { 4587c478bd9Sstevel@tonic-gate int fd; 4597c478bd9Sstevel@tonic-gate Elf *elf; 4607c478bd9Sstevel@tonic-gate GElf_Ehdr ehdr; 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate fd = fileno(fp); 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate if ((elf_version(EV_CURRENT)) == EV_NONE) { 4657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", elf_errmsg(-1)); 4667c478bd9Sstevel@tonic-gate return (NOTOUT); 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate (void) lseek(fd, 0L, 0); 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 4727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", elf_errmsg(-1)); 4737c478bd9Sstevel@tonic-gate return (NOTOUT); 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate switch (elf_kind(elf)) { 4777c478bd9Sstevel@tonic-gate case ELF_K_AR: 4787c478bd9Sstevel@tonic-gate /* 4797c478bd9Sstevel@tonic-gate * This should try to run strings on each element 4807c478bd9Sstevel@tonic-gate * of the archive. For now, just search entire 4817c478bd9Sstevel@tonic-gate * file (-a), as strings has always done 4827c478bd9Sstevel@tonic-gate * for archives. 4837c478bd9Sstevel@tonic-gate */ 4847c478bd9Sstevel@tonic-gate case ELF_K_NONE: 4857c478bd9Sstevel@tonic-gate (void) elf_end(elf); 4867c478bd9Sstevel@tonic-gate return (NOTOUT); 4877c478bd9Sstevel@tonic-gate } 4887c478bd9Sstevel@tonic-gate 4897c478bd9Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == (GElf_Ehdr *)NULL) { 4907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", elf_errmsg(-1)); 4917c478bd9Sstevel@tonic-gate (void) elf_end(elf); 4927c478bd9Sstevel@tonic-gate return (NOTOUT); 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate if ((ehdr.e_type == ET_CORE) || (ehdr.e_type == ET_NONE)) { 4967c478bd9Sstevel@tonic-gate (void) elf_end(elf); 4977c478bd9Sstevel@tonic-gate return (NOTOUT); 4987c478bd9Sstevel@tonic-gate } 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate (void) elf_end(elf); 5017c478bd9Sstevel@tonic-gate 5027c478bd9Sstevel@tonic-gate return (ELF); 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate static void 5087c478bd9Sstevel@tonic-gate Usage() 5097c478bd9Sstevel@tonic-gate { 5107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 5117c478bd9Sstevel@tonic-gate "Usage: strings [-|-a] [-t format] [-n #] [file ...]\n" 5127c478bd9Sstevel@tonic-gate " strings [-|-a] [-o] [-#] [file ...]\n")); 5137c478bd9Sstevel@tonic-gate exit(1); 5147c478bd9Sstevel@tonic-gate } 515