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
5*1ba99545Sceastha * Common Development and Distribution License (the "License").
6*1ba99545Sceastha * 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 /*
22*1ba99545Sceastha * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
287c478bd9Sstevel@tonic-gate * All Rights Reserved
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate * Copyright (c) 1987, 1988 Microsoft Corporation
337c478bd9Sstevel@tonic-gate * All Rights Reserved
347c478bd9Sstevel@tonic-gate */
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate * Copyright (c) 1979 Regents of the University of California
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include "a.out.h"
447c478bd9Sstevel@tonic-gate #include <ctype.h>
457c478bd9Sstevel@tonic-gate #include <wchar.h>
467c478bd9Sstevel@tonic-gate #include <wctype.h>
477c478bd9Sstevel@tonic-gate #include <libelf.h>
487c478bd9Sstevel@tonic-gate #include <sys/elf.h>
497c478bd9Sstevel@tonic-gate #include <locale.h>
507c478bd9Sstevel@tonic-gate #include <string.h>
517c478bd9Sstevel@tonic-gate #include <stdlib.h>
527c478bd9Sstevel@tonic-gate #include <sys/types.h>
537c478bd9Sstevel@tonic-gate #include <unistd.h>
547c478bd9Sstevel@tonic-gate #include <limits.h>
557c478bd9Sstevel@tonic-gate #include <widec.h>
567c478bd9Sstevel@tonic-gate #include <gelf.h>
577c478bd9Sstevel@tonic-gate #include <errno.h>
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate #define NOTOUT 0
617c478bd9Sstevel@tonic-gate #define AOUT 1
627c478bd9Sstevel@tonic-gate #define ELF 4
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate struct aexec ahdr;
657c478bd9Sstevel@tonic-gate
66*1ba99545Sceastha /* used to maintain a list of program sections to look in */
67*1ba99545Sceastha typedef struct sec_name {
68*1ba99545Sceastha char *name;
69*1ba99545Sceastha struct sec_name *next;
70*1ba99545Sceastha } sec_name_t;
71*1ba99545Sceastha
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate * function prototypes
747c478bd9Sstevel@tonic-gate */
757c478bd9Sstevel@tonic-gate static void Usage();
767c478bd9Sstevel@tonic-gate static void find(long);
777c478bd9Sstevel@tonic-gate static int ismagic(int, struct aexec *, FILE *);
787c478bd9Sstevel@tonic-gate static int tryelf(FILE *);
797c478bd9Sstevel@tonic-gate static int dirt(int, int);
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate * Strings - extract strings from an object file for whatever
847c478bd9Sstevel@tonic-gate *
857c478bd9Sstevel@tonic-gate * The algorithm is to look for sequences of "non-junk" characters
867c478bd9Sstevel@tonic-gate * The variable "minlen" is the minimum length string printed.
877c478bd9Sstevel@tonic-gate * This helps get rid of garbage.
887c478bd9Sstevel@tonic-gate * Default minimum string length is 4 characters.
897c478bd9Sstevel@tonic-gate *
907c478bd9Sstevel@tonic-gate */
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate #define DEF_MIN_STRING 4
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate static int tflg;
957c478bd9Sstevel@tonic-gate static char t_format;
967c478bd9Sstevel@tonic-gate static int aflg;
977c478bd9Sstevel@tonic-gate static int minlength = 0;
987c478bd9Sstevel@tonic-gate static int isClocale = 0;
997c478bd9Sstevel@tonic-gate static char *buf = NULL;
1007c478bd9Sstevel@tonic-gate static char *tbuf = NULL;
1017c478bd9Sstevel@tonic-gate static size_t buf_size = 0;
1027c478bd9Sstevel@tonic-gate static int rc = 0; /* exit code */
1037c478bd9Sstevel@tonic-gate
104*1ba99545Sceastha /*
105*1ba99545Sceastha * Returns 0 when sections have been successfully looked through,
106*1ba99545Sceastha * otherwise returns 1.
107*1ba99545Sceastha */
108*1ba99545Sceastha static int
look_in_sections(char * file,sec_name_t * seclistptr)109*1ba99545Sceastha look_in_sections(char *file, sec_name_t *seclistptr)
110*1ba99545Sceastha {
111*1ba99545Sceastha int fd = fileno(stdin);
112*1ba99545Sceastha int found_sec;
113*1ba99545Sceastha int rc = 0;
114*1ba99545Sceastha Elf *elf;
115*1ba99545Sceastha GElf_Ehdr ehdr;
116*1ba99545Sceastha Elf_Scn *scn;
117*1ba99545Sceastha GElf_Shdr shdr;
118*1ba99545Sceastha
119*1ba99545Sceastha (void) lseek(fd, 0L, 0);
120*1ba99545Sceastha elf = elf_begin(fd, ELF_C_READ, NULL);
121*1ba99545Sceastha if (gelf_getehdr(elf, &ehdr) == (GElf_Ehdr *)NULL) {
122*1ba99545Sceastha (void) fprintf(stderr, "%s: %s\n", file, elf_errmsg(-1));
123*1ba99545Sceastha (void) elf_end(elf);
124*1ba99545Sceastha return (1);
125*1ba99545Sceastha }
126*1ba99545Sceastha scn = 0;
127*1ba99545Sceastha while ((scn = elf_nextscn(elf, scn)) != 0) {
128*1ba99545Sceastha found_sec = 0;
129*1ba99545Sceastha if (gelf_getshdr(scn, &shdr) == (GElf_Shdr *)0) {
130*1ba99545Sceastha (void) fprintf(stderr, "%s: %s\n", file,
131*1ba99545Sceastha elf_errmsg(-1));
132*1ba99545Sceastha rc = 1;
133*1ba99545Sceastha continue;
134*1ba99545Sceastha }
135*1ba99545Sceastha
136*1ba99545Sceastha if (seclistptr != NULL) {
137*1ba99545Sceastha char *scn_name;
138*1ba99545Sceastha
139*1ba99545Sceastha /* Only look in the specified section(s). */
140*1ba99545Sceastha if ((scn_name = elf_strptr(elf, ehdr.e_shstrndx,
141*1ba99545Sceastha (size_t)shdr.sh_name)) == (char *)NULL) {
142*1ba99545Sceastha (void) fprintf(stderr, "%s: %s\n", file,
143*1ba99545Sceastha elf_errmsg(-1));
144*1ba99545Sceastha rc = 1;
145*1ba99545Sceastha continue;
146*1ba99545Sceastha } else {
147*1ba99545Sceastha sec_name_t *sptr;
148*1ba99545Sceastha
149*1ba99545Sceastha for (sptr = seclistptr; sptr != NULL;
150*1ba99545Sceastha sptr = sptr->next) {
151*1ba99545Sceastha if (strcmp(scn_name, sptr->name) == 0) {
152*1ba99545Sceastha found_sec = 1;
153*1ba99545Sceastha break;
154*1ba99545Sceastha }
155*1ba99545Sceastha }
156*1ba99545Sceastha }
157*1ba99545Sceastha } else {
158*1ba99545Sceastha /*
159*1ba99545Sceastha * Look through program sections that are
160*1ba99545Sceastha * loaded in memory.
161*1ba99545Sceastha */
162*1ba99545Sceastha if ((shdr.sh_flags & SHF_ALLOC) &&
163*1ba99545Sceastha (shdr.sh_type == SHT_PROGBITS)) {
164*1ba99545Sceastha found_sec = 1;
165*1ba99545Sceastha }
166*1ba99545Sceastha }
167*1ba99545Sceastha if (found_sec == 1) {
168*1ba99545Sceastha (void) fseek(stdin, (long)shdr.sh_offset, 0);
169*1ba99545Sceastha find((long)shdr.sh_size);
170*1ba99545Sceastha }
171*1ba99545Sceastha }
172*1ba99545Sceastha return (rc);
173*1ba99545Sceastha }
174*1ba99545Sceastha
1754fce32e1Smuffin int
main(argc,argv)1767c478bd9Sstevel@tonic-gate main(argc, argv)
1777c478bd9Sstevel@tonic-gate int argc;
1787c478bd9Sstevel@tonic-gate char *argv[];
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate int hsize;
1817c478bd9Sstevel@tonic-gate int htype;
1827c478bd9Sstevel@tonic-gate char *locale;
1837c478bd9Sstevel@tonic-gate int opt;
1847c478bd9Sstevel@tonic-gate int i;
185*1ba99545Sceastha sec_name_t *seclistptr = NULL;
186*1ba99545Sceastha sec_name_t *seclistendptr;
187*1ba99545Sceastha sec_name_t *sptr;
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1927c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
1937c478bd9Sstevel@tonic-gate #endif
1947c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate locale = setlocale(LC_CTYPE, NULL);
1977c478bd9Sstevel@tonic-gate if ((strcmp(locale, "C") == 0) ||
1987c478bd9Sstevel@tonic-gate (strcmp(locale, "POSIX") == 0)) {
1997c478bd9Sstevel@tonic-gate isClocale = 1;
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate /* check for non-standard "-" option */
2037c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) {
2047c478bd9Sstevel@tonic-gate if (strcmp(argv[i], "-") == 0) {
2057c478bd9Sstevel@tonic-gate aflg++;
2067c478bd9Sstevel@tonic-gate while (i < argc) {
2077c478bd9Sstevel@tonic-gate argv[i] = argv[i+1];
2087c478bd9Sstevel@tonic-gate i++;
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate argc--;
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate /* get options */
215*1ba99545Sceastha while ((opt = getopt(argc, argv, "1234567890an:N:ot:")) != -1) {
2167c478bd9Sstevel@tonic-gate switch (opt) {
2177c478bd9Sstevel@tonic-gate case 'a':
2187c478bd9Sstevel@tonic-gate aflg++;
2197c478bd9Sstevel@tonic-gate break;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate case 'n':
2227c478bd9Sstevel@tonic-gate minlength = (int)strtol(optarg, (char **)NULL,
2237c478bd9Sstevel@tonic-gate 10);
2247c478bd9Sstevel@tonic-gate break;
2257c478bd9Sstevel@tonic-gate
226*1ba99545Sceastha case 'N':
227*1ba99545Sceastha if (((sptr = malloc(sizeof (sec_name_t)))
228*1ba99545Sceastha == NULL) || ((sptr->name = strdup(optarg))
229*1ba99545Sceastha == NULL)) {
230*1ba99545Sceastha (void) fprintf(stderr, gettext(
231*1ba99545Sceastha "Cannot allocate memory: "
232*1ba99545Sceastha "%s\n"), strerror(errno));
233*1ba99545Sceastha exit(1);
234*1ba99545Sceastha }
235*1ba99545Sceastha if (seclistptr == NULL) {
236*1ba99545Sceastha seclistptr = sptr;
237*1ba99545Sceastha seclistptr->next = NULL;
238*1ba99545Sceastha seclistendptr = sptr;
239*1ba99545Sceastha } else {
240*1ba99545Sceastha seclistendptr->next = sptr;
241*1ba99545Sceastha seclistendptr = sptr;
242*1ba99545Sceastha }
243*1ba99545Sceastha break;
244*1ba99545Sceastha
2457c478bd9Sstevel@tonic-gate case 'o':
2467c478bd9Sstevel@tonic-gate tflg++;
2477c478bd9Sstevel@tonic-gate t_format = 'd';
2487c478bd9Sstevel@tonic-gate break;
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate case 't':
2517c478bd9Sstevel@tonic-gate tflg++;
2527c478bd9Sstevel@tonic-gate t_format = *optarg;
2537c478bd9Sstevel@tonic-gate if (t_format != 'd' && t_format != 'o' &&
2547c478bd9Sstevel@tonic-gate t_format != 'x')
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2577c478bd9Sstevel@tonic-gate gettext("Invalid format\n"));
2587c478bd9Sstevel@tonic-gate Usage();
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate break;
2617c478bd9Sstevel@tonic-gate case '0':
2627c478bd9Sstevel@tonic-gate case '1':
2637c478bd9Sstevel@tonic-gate case '2':
2647c478bd9Sstevel@tonic-gate case '3':
2657c478bd9Sstevel@tonic-gate case '4':
2667c478bd9Sstevel@tonic-gate case '5':
2677c478bd9Sstevel@tonic-gate case '6':
2687c478bd9Sstevel@tonic-gate case '7':
2697c478bd9Sstevel@tonic-gate case '8':
2707c478bd9Sstevel@tonic-gate case '9':
2717c478bd9Sstevel@tonic-gate minlength *= 10;
2727c478bd9Sstevel@tonic-gate minlength += opt - '0';
2737c478bd9Sstevel@tonic-gate break;
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate default:
2767c478bd9Sstevel@tonic-gate Usage();
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate /* if min string not specified, use default */
2817c478bd9Sstevel@tonic-gate if (!minlength)
2827c478bd9Sstevel@tonic-gate minlength = DEF_MIN_STRING;
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate /* dynamic allocation of char buffer array */
2867c478bd9Sstevel@tonic-gate buf = (char *)malloc(BUFSIZ);
2877c478bd9Sstevel@tonic-gate if (buf == NULL) {
2887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Cannot allocate memory: %s\n"),
2897c478bd9Sstevel@tonic-gate strerror(errno));
2907c478bd9Sstevel@tonic-gate exit(1);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate buf_size = BUFSIZ;
2937c478bd9Sstevel@tonic-gate tbuf = buf;
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate /* for each file operand */
2977c478bd9Sstevel@tonic-gate do {
2987c478bd9Sstevel@tonic-gate if (argv[optind] != NULL) {
2997c478bd9Sstevel@tonic-gate if (freopen(argv[optind], "r", stdin) == NULL) {
3007c478bd9Sstevel@tonic-gate perror(argv[optind]);
3017c478bd9Sstevel@tonic-gate rc = 1;
3027c478bd9Sstevel@tonic-gate optind++;
3037c478bd9Sstevel@tonic-gate continue;
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate optind++;
3067c478bd9Sstevel@tonic-gate } else
3077c478bd9Sstevel@tonic-gate aflg++;
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate if (aflg)
3107c478bd9Sstevel@tonic-gate htype = NOTOUT;
3117c478bd9Sstevel@tonic-gate else {
3127c478bd9Sstevel@tonic-gate hsize = fread((char *)&ahdr, sizeof (char),
3137c478bd9Sstevel@tonic-gate sizeof (ahdr), stdin);
3147c478bd9Sstevel@tonic-gate htype = ismagic(hsize, &ahdr, stdin);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate switch (htype) {
3177c478bd9Sstevel@tonic-gate case AOUT:
3187c478bd9Sstevel@tonic-gate (void) fseek(stdin, (long)ADATAPOS(&ahdr), 0);
3197c478bd9Sstevel@tonic-gate find((long)ahdr.xa_data);
3207c478bd9Sstevel@tonic-gate continue;
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate case ELF:
3237c478bd9Sstevel@tonic-gate /*
3247c478bd9Sstevel@tonic-gate * Will take care of COFF M32 and i386 also
325*1ba99545Sceastha * As well as ELF M32, i386 and Sparc (32-
326*1ba99545Sceastha * and 64-bit)
3277c478bd9Sstevel@tonic-gate */
328*1ba99545Sceastha rc = look_in_sections(argv[optind - 1],
329*1ba99545Sceastha seclistptr);
3307c478bd9Sstevel@tonic-gate continue;
3317c478bd9Sstevel@tonic-gate
3327c478bd9Sstevel@tonic-gate case NOTOUT:
3337c478bd9Sstevel@tonic-gate default:
3347c478bd9Sstevel@tonic-gate if (!aflg)
3357c478bd9Sstevel@tonic-gate (void) fseek(stdin, (long)0, 0);
3367c478bd9Sstevel@tonic-gate find(LONG_MAX);
3377c478bd9Sstevel@tonic-gate continue;
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate } while (argv[optind] != NULL);
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate return (rc);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate static void
find(cnt)3457c478bd9Sstevel@tonic-gate find(cnt)
3467c478bd9Sstevel@tonic-gate long cnt;
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate int c;
3497c478bd9Sstevel@tonic-gate int cc;
3507c478bd9Sstevel@tonic-gate int cr;
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate cc = 0;
3537c478bd9Sstevel@tonic-gate for (c = ~EOF; (cnt > 0) && (c != EOF); cnt--) {
3547c478bd9Sstevel@tonic-gate c = getc(stdin);
3557c478bd9Sstevel@tonic-gate if (!(cr = dirt(c, cc))) {
3567c478bd9Sstevel@tonic-gate if (cc >= minlength) {
3577c478bd9Sstevel@tonic-gate if (tflg) {
3587c478bd9Sstevel@tonic-gate switch (t_format) {
3597c478bd9Sstevel@tonic-gate case 'd':
3607c478bd9Sstevel@tonic-gate (void) printf("%7ld ",
3617c478bd9Sstevel@tonic-gate ftell(stdin) - cc - 1);
3627c478bd9Sstevel@tonic-gate break;
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate case 'o':
3657c478bd9Sstevel@tonic-gate (void) printf("%7lo ",
3667c478bd9Sstevel@tonic-gate ftell(stdin) - cc - 1);
3677c478bd9Sstevel@tonic-gate break;
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate case 'x':
3707c478bd9Sstevel@tonic-gate (void) printf("%7lx ",
3717c478bd9Sstevel@tonic-gate ftell(stdin) - cc - 1);
3727c478bd9Sstevel@tonic-gate break;
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate if (cc >= buf_size)
3777c478bd9Sstevel@tonic-gate buf[buf_size-1] = '\0';
3787c478bd9Sstevel@tonic-gate else
3797c478bd9Sstevel@tonic-gate buf[cc] = '\0';
3807c478bd9Sstevel@tonic-gate (void) puts(buf);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate cc = 0;
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate cc += cr;
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate static int
dirt(c,cc)3897c478bd9Sstevel@tonic-gate dirt(c, cc)
3907c478bd9Sstevel@tonic-gate int c;
3917c478bd9Sstevel@tonic-gate int cc;
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate char mbuf[MB_LEN_MAX + 1];
3947c478bd9Sstevel@tonic-gate int len, len1, i;
3957c478bd9Sstevel@tonic-gate wchar_t wc;
3967c478bd9Sstevel@tonic-gate int r_val;
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate if (isascii(c)) {
3997c478bd9Sstevel@tonic-gate if (isprint(c)) {
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate * If character count is greater than dynamic
4027c478bd9Sstevel@tonic-gate * char buffer size, then increase char buffer size.
4037c478bd9Sstevel@tonic-gate */
4047c478bd9Sstevel@tonic-gate if (cc >= (buf_size-2)) {
4057c478bd9Sstevel@tonic-gate if (tbuf != NULL) {
4067c478bd9Sstevel@tonic-gate buf_size += BUFSIZ;
4077c478bd9Sstevel@tonic-gate tbuf = (char *)realloc(buf, buf_size);
4087c478bd9Sstevel@tonic-gate if (tbuf == NULL) {
4097c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4107c478bd9Sstevel@tonic-gate gettext("Cannot allocate memory: %s\n"),
4117c478bd9Sstevel@tonic-gate strerror(errno));
4127c478bd9Sstevel@tonic-gate buf_size -= BUFSIZ;
4137c478bd9Sstevel@tonic-gate rc = 1;
4147c478bd9Sstevel@tonic-gate return (0);
4157c478bd9Sstevel@tonic-gate } else {
4167c478bd9Sstevel@tonic-gate buf = tbuf;
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate } else {
4197c478bd9Sstevel@tonic-gate return (0);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate buf[cc] = c;
4237c478bd9Sstevel@tonic-gate return (1);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate return (0);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate if (isClocale)
4297c478bd9Sstevel@tonic-gate return (0);
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate r_val = 0;
4327c478bd9Sstevel@tonic-gate mbuf[0] = c;
4337c478bd9Sstevel@tonic-gate for (len = 1; len < (unsigned int)MB_CUR_MAX; len++) {
4347c478bd9Sstevel@tonic-gate if ((signed char)
4357c478bd9Sstevel@tonic-gate (mbuf[len] = getc(stdin)) == -1)
4367c478bd9Sstevel@tonic-gate break;
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate mbuf[len] = 0;
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate if ((len1 = mbtowc(&wc, mbuf, len)) <= 0) {
4417c478bd9Sstevel@tonic-gate len1 = 1;
4427c478bd9Sstevel@tonic-gate goto _unget;
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate if (iswprint(wc)) {
4467c478bd9Sstevel@tonic-gate if ((cc + len1) >= (buf_size-2)) {
4477c478bd9Sstevel@tonic-gate if (tbuf != NULL) {
4487c478bd9Sstevel@tonic-gate buf_size += BUFSIZ;
4497c478bd9Sstevel@tonic-gate tbuf = (char *)realloc(buf, buf_size);
4507c478bd9Sstevel@tonic-gate if (tbuf == NULL) {
4517c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4527c478bd9Sstevel@tonic-gate gettext("Cannot allocate memory: %s\n"),
4537c478bd9Sstevel@tonic-gate strerror(errno));
4547c478bd9Sstevel@tonic-gate buf_size -= BUFSIZ;
4557c478bd9Sstevel@tonic-gate rc = 1;
4567c478bd9Sstevel@tonic-gate return (0);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate buf = tbuf;
4597c478bd9Sstevel@tonic-gate } else {
4607c478bd9Sstevel@tonic-gate return (0);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate for (i = 0; i < len1; i++, cc++)
4647c478bd9Sstevel@tonic-gate buf[cc] = mbuf[i];
4657c478bd9Sstevel@tonic-gate r_val = len1;
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate
4687c478bd9Sstevel@tonic-gate _unget:
4697c478bd9Sstevel@tonic-gate for (len--; len >= len1; len--)
4707c478bd9Sstevel@tonic-gate (void) ungetc(mbuf[len], stdin);
4717c478bd9Sstevel@tonic-gate return (r_val);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate static int
ismagic(hsize,hdr,fp)4767c478bd9Sstevel@tonic-gate ismagic(hsize, hdr, fp)
4777c478bd9Sstevel@tonic-gate int hsize;
4787c478bd9Sstevel@tonic-gate struct aexec *hdr;
4797c478bd9Sstevel@tonic-gate FILE *fp;
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate switch (hdr->xa_magic) {
4827c478bd9Sstevel@tonic-gate case A_MAGIC1:
4837c478bd9Sstevel@tonic-gate case A_MAGIC2:
4847c478bd9Sstevel@tonic-gate case A_MAGIC3:
4857c478bd9Sstevel@tonic-gate case A_MAGIC4:
4867c478bd9Sstevel@tonic-gate if (hsize < sizeof (struct aexec))
4877c478bd9Sstevel@tonic-gate return (NOTOUT);
4887c478bd9Sstevel@tonic-gate else
4897c478bd9Sstevel@tonic-gate return (AOUT);
4907c478bd9Sstevel@tonic-gate default:
4917c478bd9Sstevel@tonic-gate break;
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate return (tryelf(fp));
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate
4967c478bd9Sstevel@tonic-gate
4977c478bd9Sstevel@tonic-gate static int
tryelf(fp)4987c478bd9Sstevel@tonic-gate tryelf(fp)
4997c478bd9Sstevel@tonic-gate FILE *fp;
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate int fd;
5027c478bd9Sstevel@tonic-gate Elf *elf;
5037c478bd9Sstevel@tonic-gate GElf_Ehdr ehdr;
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate fd = fileno(fp);
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate if ((elf_version(EV_CURRENT)) == EV_NONE) {
5087c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", elf_errmsg(-1));
5097c478bd9Sstevel@tonic-gate return (NOTOUT);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate (void) lseek(fd, 0L, 0);
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
5157c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", elf_errmsg(-1));
5167c478bd9Sstevel@tonic-gate return (NOTOUT);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate
5197c478bd9Sstevel@tonic-gate switch (elf_kind(elf)) {
5207c478bd9Sstevel@tonic-gate case ELF_K_AR:
5217c478bd9Sstevel@tonic-gate /*
5227c478bd9Sstevel@tonic-gate * This should try to run strings on each element
5237c478bd9Sstevel@tonic-gate * of the archive. For now, just search entire
5247c478bd9Sstevel@tonic-gate * file (-a), as strings has always done
5257c478bd9Sstevel@tonic-gate * for archives.
5267c478bd9Sstevel@tonic-gate */
5277c478bd9Sstevel@tonic-gate case ELF_K_NONE:
5287c478bd9Sstevel@tonic-gate (void) elf_end(elf);
5297c478bd9Sstevel@tonic-gate return (NOTOUT);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == (GElf_Ehdr *)NULL) {
5337c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", elf_errmsg(-1));
5347c478bd9Sstevel@tonic-gate (void) elf_end(elf);
5357c478bd9Sstevel@tonic-gate return (NOTOUT);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate
5387c478bd9Sstevel@tonic-gate if ((ehdr.e_type == ET_CORE) || (ehdr.e_type == ET_NONE)) {
5397c478bd9Sstevel@tonic-gate (void) elf_end(elf);
5407c478bd9Sstevel@tonic-gate return (NOTOUT);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate
5437c478bd9Sstevel@tonic-gate (void) elf_end(elf);
5447c478bd9Sstevel@tonic-gate
5457c478bd9Sstevel@tonic-gate return (ELF);
5467c478bd9Sstevel@tonic-gate
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate static void
Usage()5517c478bd9Sstevel@tonic-gate Usage()
5527c478bd9Sstevel@tonic-gate {
5537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
554*1ba99545Sceastha "Usage: strings [-a | -] [-t format | -o] [-n number | -number]"
555*1ba99545Sceastha "\n\t[-N name] [file]...\n"));
5567c478bd9Sstevel@tonic-gate exit(1);
5577c478bd9Sstevel@tonic-gate }
558