10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*4026Sceastha * Common Development and Distribution License (the "License").
6*4026Sceastha * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*4026Sceastha * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
280Sstevel@tonic-gate * All Rights Reserved
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate * Copyright (c) 1987, 1988 Microsoft Corporation
330Sstevel@tonic-gate * All Rights Reserved
340Sstevel@tonic-gate */
350Sstevel@tonic-gate
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate * Copyright (c) 1979 Regents of the University of California
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include "a.out.h"
440Sstevel@tonic-gate #include <ctype.h>
450Sstevel@tonic-gate #include <wchar.h>
460Sstevel@tonic-gate #include <wctype.h>
470Sstevel@tonic-gate #include <libelf.h>
480Sstevel@tonic-gate #include <sys/elf.h>
490Sstevel@tonic-gate #include <locale.h>
500Sstevel@tonic-gate #include <string.h>
510Sstevel@tonic-gate #include <stdlib.h>
520Sstevel@tonic-gate #include <sys/types.h>
530Sstevel@tonic-gate #include <unistd.h>
540Sstevel@tonic-gate #include <limits.h>
550Sstevel@tonic-gate #include <widec.h>
560Sstevel@tonic-gate #include <gelf.h>
570Sstevel@tonic-gate #include <errno.h>
580Sstevel@tonic-gate
590Sstevel@tonic-gate
600Sstevel@tonic-gate #define NOTOUT 0
610Sstevel@tonic-gate #define AOUT 1
620Sstevel@tonic-gate #define ELF 4
630Sstevel@tonic-gate
640Sstevel@tonic-gate struct aexec ahdr;
650Sstevel@tonic-gate
66*4026Sceastha /* used to maintain a list of program sections to look in */
67*4026Sceastha typedef struct sec_name {
68*4026Sceastha char *name;
69*4026Sceastha struct sec_name *next;
70*4026Sceastha } sec_name_t;
71*4026Sceastha
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate * function prototypes
740Sstevel@tonic-gate */
750Sstevel@tonic-gate static void Usage();
760Sstevel@tonic-gate static void find(long);
770Sstevel@tonic-gate static int ismagic(int, struct aexec *, FILE *);
780Sstevel@tonic-gate static int tryelf(FILE *);
790Sstevel@tonic-gate static int dirt(int, int);
800Sstevel@tonic-gate
810Sstevel@tonic-gate
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate * Strings - extract strings from an object file for whatever
840Sstevel@tonic-gate *
850Sstevel@tonic-gate * The algorithm is to look for sequences of "non-junk" characters
860Sstevel@tonic-gate * The variable "minlen" is the minimum length string printed.
870Sstevel@tonic-gate * This helps get rid of garbage.
880Sstevel@tonic-gate * Default minimum string length is 4 characters.
890Sstevel@tonic-gate *
900Sstevel@tonic-gate */
910Sstevel@tonic-gate
920Sstevel@tonic-gate #define DEF_MIN_STRING 4
930Sstevel@tonic-gate
940Sstevel@tonic-gate static int tflg;
950Sstevel@tonic-gate static char t_format;
960Sstevel@tonic-gate static int aflg;
970Sstevel@tonic-gate static int minlength = 0;
980Sstevel@tonic-gate static int isClocale = 0;
990Sstevel@tonic-gate static char *buf = NULL;
1000Sstevel@tonic-gate static char *tbuf = NULL;
1010Sstevel@tonic-gate static size_t buf_size = 0;
1020Sstevel@tonic-gate static int rc = 0; /* exit code */
1030Sstevel@tonic-gate
104*4026Sceastha /*
105*4026Sceastha * Returns 0 when sections have been successfully looked through,
106*4026Sceastha * otherwise returns 1.
107*4026Sceastha */
108*4026Sceastha static int
look_in_sections(char * file,sec_name_t * seclistptr)109*4026Sceastha look_in_sections(char *file, sec_name_t *seclistptr)
110*4026Sceastha {
111*4026Sceastha int fd = fileno(stdin);
112*4026Sceastha int found_sec;
113*4026Sceastha int rc = 0;
114*4026Sceastha Elf *elf;
115*4026Sceastha GElf_Ehdr ehdr;
116*4026Sceastha Elf_Scn *scn;
117*4026Sceastha GElf_Shdr shdr;
118*4026Sceastha
119*4026Sceastha (void) lseek(fd, 0L, 0);
120*4026Sceastha elf = elf_begin(fd, ELF_C_READ, NULL);
121*4026Sceastha if (gelf_getehdr(elf, &ehdr) == (GElf_Ehdr *)NULL) {
122*4026Sceastha (void) fprintf(stderr, "%s: %s\n", file, elf_errmsg(-1));
123*4026Sceastha (void) elf_end(elf);
124*4026Sceastha return (1);
125*4026Sceastha }
126*4026Sceastha scn = 0;
127*4026Sceastha while ((scn = elf_nextscn(elf, scn)) != 0) {
128*4026Sceastha found_sec = 0;
129*4026Sceastha if (gelf_getshdr(scn, &shdr) == (GElf_Shdr *)0) {
130*4026Sceastha (void) fprintf(stderr, "%s: %s\n", file,
131*4026Sceastha elf_errmsg(-1));
132*4026Sceastha rc = 1;
133*4026Sceastha continue;
134*4026Sceastha }
135*4026Sceastha
136*4026Sceastha if (seclistptr != NULL) {
137*4026Sceastha char *scn_name;
138*4026Sceastha
139*4026Sceastha /* Only look in the specified section(s). */
140*4026Sceastha if ((scn_name = elf_strptr(elf, ehdr.e_shstrndx,
141*4026Sceastha (size_t)shdr.sh_name)) == (char *)NULL) {
142*4026Sceastha (void) fprintf(stderr, "%s: %s\n", file,
143*4026Sceastha elf_errmsg(-1));
144*4026Sceastha rc = 1;
145*4026Sceastha continue;
146*4026Sceastha } else {
147*4026Sceastha sec_name_t *sptr;
148*4026Sceastha
149*4026Sceastha for (sptr = seclistptr; sptr != NULL;
150*4026Sceastha sptr = sptr->next) {
151*4026Sceastha if (strcmp(scn_name, sptr->name) == 0) {
152*4026Sceastha found_sec = 1;
153*4026Sceastha break;
154*4026Sceastha }
155*4026Sceastha }
156*4026Sceastha }
157*4026Sceastha } else {
158*4026Sceastha /*
159*4026Sceastha * Look through program sections that are
160*4026Sceastha * loaded in memory.
161*4026Sceastha */
162*4026Sceastha if ((shdr.sh_flags & SHF_ALLOC) &&
163*4026Sceastha (shdr.sh_type == SHT_PROGBITS)) {
164*4026Sceastha found_sec = 1;
165*4026Sceastha }
166*4026Sceastha }
167*4026Sceastha if (found_sec == 1) {
168*4026Sceastha (void) fseek(stdin, (long)shdr.sh_offset, 0);
169*4026Sceastha find((long)shdr.sh_size);
170*4026Sceastha }
171*4026Sceastha }
172*4026Sceastha return (rc);
173*4026Sceastha }
174*4026Sceastha
175213Smuffin int
main(argc,argv)1760Sstevel@tonic-gate main(argc, argv)
1770Sstevel@tonic-gate int argc;
1780Sstevel@tonic-gate char *argv[];
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate int hsize;
1810Sstevel@tonic-gate int htype;
182*4026Sceastha char *locale;
1830Sstevel@tonic-gate int opt;
1840Sstevel@tonic-gate int i;
185*4026Sceastha sec_name_t *seclistptr = NULL;
186*4026Sceastha sec_name_t *seclistendptr;
187*4026Sceastha sec_name_t *sptr;
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1920Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
1930Sstevel@tonic-gate #endif
1940Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate locale = setlocale(LC_CTYPE, NULL);
1970Sstevel@tonic-gate if ((strcmp(locale, "C") == 0) ||
1980Sstevel@tonic-gate (strcmp(locale, "POSIX") == 0)) {
1990Sstevel@tonic-gate isClocale = 1;
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate /* check for non-standard "-" option */
2030Sstevel@tonic-gate for (i = 1; i < argc; i++) {
2040Sstevel@tonic-gate if (strcmp(argv[i], "-") == 0) {
2050Sstevel@tonic-gate aflg++;
2060Sstevel@tonic-gate while (i < argc) {
2070Sstevel@tonic-gate argv[i] = argv[i+1];
2080Sstevel@tonic-gate i++;
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate argc--;
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate /* get options */
215*4026Sceastha while ((opt = getopt(argc, argv, "1234567890an:N:ot:")) != -1) {
2160Sstevel@tonic-gate switch (opt) {
2170Sstevel@tonic-gate case 'a':
2180Sstevel@tonic-gate aflg++;
2190Sstevel@tonic-gate break;
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate case 'n':
2220Sstevel@tonic-gate minlength = (int)strtol(optarg, (char **)NULL,
2230Sstevel@tonic-gate 10);
2240Sstevel@tonic-gate break;
2250Sstevel@tonic-gate
226*4026Sceastha case 'N':
227*4026Sceastha if (((sptr = malloc(sizeof (sec_name_t)))
228*4026Sceastha == NULL) || ((sptr->name = strdup(optarg))
229*4026Sceastha == NULL)) {
230*4026Sceastha (void) fprintf(stderr, gettext(
231*4026Sceastha "Cannot allocate memory: "
232*4026Sceastha "%s\n"), strerror(errno));
233*4026Sceastha exit(1);
234*4026Sceastha }
235*4026Sceastha if (seclistptr == NULL) {
236*4026Sceastha seclistptr = sptr;
237*4026Sceastha seclistptr->next = NULL;
238*4026Sceastha seclistendptr = sptr;
239*4026Sceastha } else {
240*4026Sceastha seclistendptr->next = sptr;
241*4026Sceastha seclistendptr = sptr;
242*4026Sceastha }
243*4026Sceastha break;
244*4026Sceastha
2450Sstevel@tonic-gate case 'o':
2460Sstevel@tonic-gate tflg++;
2470Sstevel@tonic-gate t_format = 'd';
2480Sstevel@tonic-gate break;
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate case 't':
2510Sstevel@tonic-gate tflg++;
2520Sstevel@tonic-gate t_format = *optarg;
2530Sstevel@tonic-gate if (t_format != 'd' && t_format != 'o' &&
2540Sstevel@tonic-gate t_format != 'x')
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate (void) fprintf(stderr,
2570Sstevel@tonic-gate gettext("Invalid format\n"));
2580Sstevel@tonic-gate Usage();
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate break;
2610Sstevel@tonic-gate case '0':
2620Sstevel@tonic-gate case '1':
2630Sstevel@tonic-gate case '2':
2640Sstevel@tonic-gate case '3':
2650Sstevel@tonic-gate case '4':
2660Sstevel@tonic-gate case '5':
2670Sstevel@tonic-gate case '6':
2680Sstevel@tonic-gate case '7':
2690Sstevel@tonic-gate case '8':
2700Sstevel@tonic-gate case '9':
2710Sstevel@tonic-gate minlength *= 10;
2720Sstevel@tonic-gate minlength += opt - '0';
2730Sstevel@tonic-gate break;
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate default:
2760Sstevel@tonic-gate Usage();
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate /* if min string not specified, use default */
2810Sstevel@tonic-gate if (!minlength)
2820Sstevel@tonic-gate minlength = DEF_MIN_STRING;
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate /* dynamic allocation of char buffer array */
2860Sstevel@tonic-gate buf = (char *)malloc(BUFSIZ);
2870Sstevel@tonic-gate if (buf == NULL) {
2880Sstevel@tonic-gate (void) fprintf(stderr, gettext("Cannot allocate memory: %s\n"),
2890Sstevel@tonic-gate strerror(errno));
2900Sstevel@tonic-gate exit(1);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate buf_size = BUFSIZ;
2930Sstevel@tonic-gate tbuf = buf;
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate /* for each file operand */
2970Sstevel@tonic-gate do {
2980Sstevel@tonic-gate if (argv[optind] != NULL) {
2990Sstevel@tonic-gate if (freopen(argv[optind], "r", stdin) == NULL) {
3000Sstevel@tonic-gate perror(argv[optind]);
3010Sstevel@tonic-gate rc = 1;
3020Sstevel@tonic-gate optind++;
3030Sstevel@tonic-gate continue;
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate optind++;
3060Sstevel@tonic-gate } else
3070Sstevel@tonic-gate aflg++;
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate if (aflg)
3100Sstevel@tonic-gate htype = NOTOUT;
3110Sstevel@tonic-gate else {
3120Sstevel@tonic-gate hsize = fread((char *)&ahdr, sizeof (char),
3130Sstevel@tonic-gate sizeof (ahdr), stdin);
3140Sstevel@tonic-gate htype = ismagic(hsize, &ahdr, stdin);
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate switch (htype) {
3170Sstevel@tonic-gate case AOUT:
3180Sstevel@tonic-gate (void) fseek(stdin, (long)ADATAPOS(&ahdr), 0);
3190Sstevel@tonic-gate find((long)ahdr.xa_data);
3200Sstevel@tonic-gate continue;
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate case ELF:
323*4026Sceastha /*
324*4026Sceastha * Will take care of COFF M32 and i386 also
325*4026Sceastha * As well as ELF M32, i386 and Sparc (32-
326*4026Sceastha * and 64-bit)
327*4026Sceastha */
328*4026Sceastha rc = look_in_sections(argv[optind - 1],
329*4026Sceastha seclistptr);
3300Sstevel@tonic-gate continue;
3310Sstevel@tonic-gate
3320Sstevel@tonic-gate case NOTOUT:
3330Sstevel@tonic-gate default:
3340Sstevel@tonic-gate if (!aflg)
3350Sstevel@tonic-gate (void) fseek(stdin, (long)0, 0);
3360Sstevel@tonic-gate find(LONG_MAX);
3370Sstevel@tonic-gate continue;
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate } while (argv[optind] != NULL);
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate return (rc);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate static void
find(cnt)3450Sstevel@tonic-gate find(cnt)
3460Sstevel@tonic-gate long cnt;
3470Sstevel@tonic-gate {
3480Sstevel@tonic-gate int c;
3490Sstevel@tonic-gate int cc;
3500Sstevel@tonic-gate int cr;
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate cc = 0;
3530Sstevel@tonic-gate for (c = ~EOF; (cnt > 0) && (c != EOF); cnt--) {
3540Sstevel@tonic-gate c = getc(stdin);
3550Sstevel@tonic-gate if (!(cr = dirt(c, cc))) {
3560Sstevel@tonic-gate if (cc >= minlength) {
3570Sstevel@tonic-gate if (tflg) {
3580Sstevel@tonic-gate switch (t_format) {
3590Sstevel@tonic-gate case 'd':
3600Sstevel@tonic-gate (void) printf("%7ld ",
3610Sstevel@tonic-gate ftell(stdin) - cc - 1);
3620Sstevel@tonic-gate break;
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate case 'o':
3650Sstevel@tonic-gate (void) printf("%7lo ",
3660Sstevel@tonic-gate ftell(stdin) - cc - 1);
3670Sstevel@tonic-gate break;
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate case 'x':
3700Sstevel@tonic-gate (void) printf("%7lx ",
3710Sstevel@tonic-gate ftell(stdin) - cc - 1);
3720Sstevel@tonic-gate break;
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate if (cc >= buf_size)
3770Sstevel@tonic-gate buf[buf_size-1] = '\0';
3780Sstevel@tonic-gate else
3790Sstevel@tonic-gate buf[cc] = '\0';
3800Sstevel@tonic-gate (void) puts(buf);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate cc = 0;
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate cc += cr;
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate static int
dirt(c,cc)3890Sstevel@tonic-gate dirt(c, cc)
3900Sstevel@tonic-gate int c;
3910Sstevel@tonic-gate int cc;
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate char mbuf[MB_LEN_MAX + 1];
3940Sstevel@tonic-gate int len, len1, i;
3950Sstevel@tonic-gate wchar_t wc;
3960Sstevel@tonic-gate int r_val;
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate if (isascii(c)) {
3990Sstevel@tonic-gate if (isprint(c)) {
4000Sstevel@tonic-gate /*
4010Sstevel@tonic-gate * If character count is greater than dynamic
4020Sstevel@tonic-gate * char buffer size, then increase char buffer size.
4030Sstevel@tonic-gate */
4040Sstevel@tonic-gate if (cc >= (buf_size-2)) {
4050Sstevel@tonic-gate if (tbuf != NULL) {
4060Sstevel@tonic-gate buf_size += BUFSIZ;
4070Sstevel@tonic-gate tbuf = (char *)realloc(buf, buf_size);
4080Sstevel@tonic-gate if (tbuf == NULL) {
4090Sstevel@tonic-gate (void) fprintf(stderr,
4100Sstevel@tonic-gate gettext("Cannot allocate memory: %s\n"),
4110Sstevel@tonic-gate strerror(errno));
4120Sstevel@tonic-gate buf_size -= BUFSIZ;
4130Sstevel@tonic-gate rc = 1;
4140Sstevel@tonic-gate return (0);
4150Sstevel@tonic-gate } else {
4160Sstevel@tonic-gate buf = tbuf;
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate } else {
4190Sstevel@tonic-gate return (0);
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate buf[cc] = c;
4230Sstevel@tonic-gate return (1);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate return (0);
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate if (isClocale)
4290Sstevel@tonic-gate return (0);
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate r_val = 0;
4320Sstevel@tonic-gate mbuf[0] = c;
4330Sstevel@tonic-gate for (len = 1; len < (unsigned int)MB_CUR_MAX; len++) {
4340Sstevel@tonic-gate if ((signed char)
4350Sstevel@tonic-gate (mbuf[len] = getc(stdin)) == -1)
4360Sstevel@tonic-gate break;
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate mbuf[len] = 0;
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate if ((len1 = mbtowc(&wc, mbuf, len)) <= 0) {
4410Sstevel@tonic-gate len1 = 1;
4420Sstevel@tonic-gate goto _unget;
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate
4450Sstevel@tonic-gate if (iswprint(wc)) {
4460Sstevel@tonic-gate if ((cc + len1) >= (buf_size-2)) {
4470Sstevel@tonic-gate if (tbuf != NULL) {
4480Sstevel@tonic-gate buf_size += BUFSIZ;
4490Sstevel@tonic-gate tbuf = (char *)realloc(buf, buf_size);
4500Sstevel@tonic-gate if (tbuf == NULL) {
4510Sstevel@tonic-gate (void) fprintf(stderr,
4520Sstevel@tonic-gate gettext("Cannot allocate memory: %s\n"),
4530Sstevel@tonic-gate strerror(errno));
4540Sstevel@tonic-gate buf_size -= BUFSIZ;
4550Sstevel@tonic-gate rc = 1;
4560Sstevel@tonic-gate return (0);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate buf = tbuf;
4590Sstevel@tonic-gate } else {
4600Sstevel@tonic-gate return (0);
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate for (i = 0; i < len1; i++, cc++)
4640Sstevel@tonic-gate buf[cc] = mbuf[i];
4650Sstevel@tonic-gate r_val = len1;
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate _unget:
4690Sstevel@tonic-gate for (len--; len >= len1; len--)
4700Sstevel@tonic-gate (void) ungetc(mbuf[len], stdin);
4710Sstevel@tonic-gate return (r_val);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate static int
ismagic(hsize,hdr,fp)4760Sstevel@tonic-gate ismagic(hsize, hdr, fp)
4770Sstevel@tonic-gate int hsize;
4780Sstevel@tonic-gate struct aexec *hdr;
4790Sstevel@tonic-gate FILE *fp;
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate switch (hdr->xa_magic) {
4820Sstevel@tonic-gate case A_MAGIC1:
4830Sstevel@tonic-gate case A_MAGIC2:
4840Sstevel@tonic-gate case A_MAGIC3:
4850Sstevel@tonic-gate case A_MAGIC4:
4860Sstevel@tonic-gate if (hsize < sizeof (struct aexec))
4870Sstevel@tonic-gate return (NOTOUT);
4880Sstevel@tonic-gate else
4890Sstevel@tonic-gate return (AOUT);
4900Sstevel@tonic-gate default:
4910Sstevel@tonic-gate break;
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate return (tryelf(fp));
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate static int
tryelf(fp)4980Sstevel@tonic-gate tryelf(fp)
4990Sstevel@tonic-gate FILE *fp;
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate int fd;
5020Sstevel@tonic-gate Elf *elf;
5030Sstevel@tonic-gate GElf_Ehdr ehdr;
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate fd = fileno(fp);
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate if ((elf_version(EV_CURRENT)) == EV_NONE) {
5080Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", elf_errmsg(-1));
5090Sstevel@tonic-gate return (NOTOUT);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate (void) lseek(fd, 0L, 0);
5130Sstevel@tonic-gate
5140Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
5150Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", elf_errmsg(-1));
5160Sstevel@tonic-gate return (NOTOUT);
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate
5190Sstevel@tonic-gate switch (elf_kind(elf)) {
5200Sstevel@tonic-gate case ELF_K_AR:
5210Sstevel@tonic-gate /*
5220Sstevel@tonic-gate * This should try to run strings on each element
5230Sstevel@tonic-gate * of the archive. For now, just search entire
5240Sstevel@tonic-gate * file (-a), as strings has always done
5250Sstevel@tonic-gate * for archives.
5260Sstevel@tonic-gate */
5270Sstevel@tonic-gate case ELF_K_NONE:
5280Sstevel@tonic-gate (void) elf_end(elf);
5290Sstevel@tonic-gate return (NOTOUT);
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate
5320Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == (GElf_Ehdr *)NULL) {
5330Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", elf_errmsg(-1));
5340Sstevel@tonic-gate (void) elf_end(elf);
5350Sstevel@tonic-gate return (NOTOUT);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate if ((ehdr.e_type == ET_CORE) || (ehdr.e_type == ET_NONE)) {
5390Sstevel@tonic-gate (void) elf_end(elf);
5400Sstevel@tonic-gate return (NOTOUT);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate
5430Sstevel@tonic-gate (void) elf_end(elf);
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate return (ELF);
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate
5500Sstevel@tonic-gate static void
Usage()5510Sstevel@tonic-gate Usage()
5520Sstevel@tonic-gate {
5530Sstevel@tonic-gate (void) fprintf(stderr, gettext(
554*4026Sceastha "Usage: strings [-a | -] [-t format | -o] [-n number | -number]"
555*4026Sceastha "\n\t[-N name] [file]...\n"));
5560Sstevel@tonic-gate exit(1);
5570Sstevel@tonic-gate }
558