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