xref: /csrg-svn/usr.bin/strings/strings.c (revision 42691)
121577Sdist /*
235980Sbostic  * Copyright (c) 1980, 1987 The Regents of the University of California.
335980Sbostic  * All rights reserved.
435980Sbostic  *
5*42691Sbostic  * %sccs.include.redist.c%
621577Sdist  */
721577Sdist 
817267Ssam #ifndef lint
921577Sdist char copyright[] =
1035980Sbostic "@(#) Copyright (c) 1980, 1987 The Regents of the University of California.\n\
1121577Sdist  All rights reserved.\n";
1235980Sbostic #endif /* not lint */
1317267Ssam 
1421577Sdist #ifndef lint
15*42691Sbostic static char sccsid[] = "@(#)strings.c	5.6 (Berkeley) 06/01/90";
1635980Sbostic #endif /* not lint */
1721577Sdist 
1830142Sbostic #include <sys/types.h>
1930142Sbostic #include <sys/file.h>
2030142Sbostic #include <a.out.h>
211105Sbill #include <stdio.h>
221105Sbill #include <ctype.h>
231105Sbill 
2430142Sbostic #define DEF_LEN		4		/* default minimum string length */
2530142Sbostic #define ISSTR(ch)	(isascii(ch) && (isprint(ch) || ch == '\t'))
261105Sbill 
2730142Sbostic typedef struct exec	EXEC;		/* struct exec cast */
281105Sbill 
2930142Sbostic static long	foff;			/* offset in the file */
3030142Sbostic static int	hcnt,			/* head count */
3130142Sbostic 		head_len,		/* length of header */
3230142Sbostic 		read_len;		/* length to read */
3330142Sbostic static u_char	hbfr[sizeof(EXEC)];	/* buffer for struct exec */
341105Sbill 
3535980Sbostic main(argc, argv)
3635980Sbostic 	int argc;
3735980Sbostic 	char **argv;
381105Sbill {
3935980Sbostic 	extern char *optarg;
4035980Sbostic 	extern int optind;
4135980Sbostic 	register int ch, cnt;
4235980Sbostic 	register u_char *C;
4335980Sbostic 	EXEC *head;
4435980Sbostic 	int minlen;
4535980Sbostic 	short asdata, oflg;
4635980Sbostic 	u_char *bfr;
4735980Sbostic 	char *file, *p, *malloc();
481105Sbill 
4930142Sbostic 	/*
5030142Sbostic 	 * for backward compatibility, allow '-' to specify 'a' flag; no
5130142Sbostic 	 * longer documented in the man page or usage string.
5230142Sbostic 	 */
5339523Sbostic 	asdata = oflg = 0;
5435980Sbostic 	minlen = -1;
5535980Sbostic 	while ((ch = getopt(argc, argv, "-0123456789ao")) != EOF)
5635980Sbostic 		switch((char)ch) {
5735980Sbostic 		case '0': case '1': case '2': case '3': case '4':
5835980Sbostic 		case '5': case '6': case '7': case '8': case '9':
5935980Sbostic 			/*
6035980Sbostic 			 * kludge: strings was originally designed to take
6135980Sbostic 			 * a number after a dash.
6235980Sbostic 			 */
6335980Sbostic 			if (minlen == -1) {
6435980Sbostic 				p = argv[optind - 1];
6535980Sbostic 				if (p[0] == '-' && p[1] == ch && !p[2])
6635980Sbostic 					minlen = atoi(++p);
6735980Sbostic 				else
6835980Sbostic 					minlen = atoi(argv[optind] + 1);
691105Sbill 			}
7035980Sbostic 			break;
7135980Sbostic 		case '-':
7235980Sbostic 		case 'a':
7335980Sbostic 			asdata = 1;
7435980Sbostic 			break;
7535980Sbostic 		case 'o':
7635980Sbostic 			oflg = 1;
7735980Sbostic 			break;
7835980Sbostic 		case '?':
7935980Sbostic 		default:
8035980Sbostic 			fprintf(stderr,
8135980Sbostic 			    "usage: strings [-ao] [-#] [file ... ]\n");
8235980Sbostic 			exit(1);
8335980Sbostic 		}
8435980Sbostic 	argc -= optind;
8535980Sbostic 	argv += optind;
8630142Sbostic 
8735980Sbostic 	if (minlen == -1)
8835980Sbostic 		minlen = DEF_LEN;
8935980Sbostic 
9030142Sbostic 	if (!(bfr = (u_char *)malloc((u_int)minlen))) {
9135980Sbostic 		fputs("strings: no space.\n", stderr);
9235980Sbostic 		exit(1);
9330142Sbostic 	}
9435980Sbostic 	bfr[minlen] = '\0';
9530142Sbostic 	file = "stdin";
961105Sbill 	do {
9730142Sbostic 		if (*argv) {
9835980Sbostic 			if (!freopen(*argv, "r", stdin)) {
9930142Sbostic 				perror(*argv);
10035980Sbostic 				exit(1);
1011105Sbill 			}
10230142Sbostic 			file = *argv++;
1031105Sbill 		}
10430142Sbostic 		foff = 0;
10535980Sbostic 		read_len = -1;
10630142Sbostic 		if (asdata)
10730142Sbostic 			head_len = 0;
10830142Sbostic 		else {
10930142Sbostic 			head = (EXEC *)hbfr;
11035980Sbostic 			if ((head_len = read(fileno(stdin), (char *)head, sizeof(EXEC))) == -1) {
11130142Sbostic 				perror(file);
11235980Sbostic 				exit(1);
11330142Sbostic 			}
11430142Sbostic 			if (head_len == sizeof(EXEC) && !N_BADMAG(*head)) {
11530142Sbostic 				foff = N_TXTOFF(*head) + head->a_text;
11635980Sbostic 				if (fseek(stdin, foff, L_SET) == -1) {
11730142Sbostic 					perror(file);
11835980Sbostic 					exit(1);
11930142Sbostic 				}
12030142Sbostic 				read_len = head->a_data;
12130142Sbostic 				head_len = 0;
12230142Sbostic 			}
12330142Sbostic 			else
12430142Sbostic 				hcnt = 0;
1251105Sbill 		}
12635980Sbostic 		for (cnt = 0; (ch = getch()) != EOF;) {
12730142Sbostic 			if (ISSTR(ch)) {
12830142Sbostic 				if (!cnt)
12930142Sbostic 					C = bfr;
13030142Sbostic 				*C++ = ch;
13130142Sbostic 				if (++cnt < minlen)
13230142Sbostic 					continue;
1331105Sbill 				if (oflg)
13435980Sbostic 					printf("%07ld %s", foff - minlen,
13535980Sbostic 					    (char *)bfr);
13630142Sbostic 				else
13735980Sbostic 					fputs((char *)bfr, stdout);
13830142Sbostic 				while ((ch = getch()) != EOF && ISSTR(ch))
13930142Sbostic 					putchar((char)ch);
14030142Sbostic 				putchar('\n');
1411105Sbill 			}
14230142Sbostic 			cnt = 0;
1431105Sbill 		}
14430142Sbostic 	} while (*argv);
14535980Sbostic 	exit(0);
1461105Sbill }
1471105Sbill 
14830142Sbostic /*
14930142Sbostic  * getch --
15030142Sbostic  *	get next character from wherever
15130142Sbostic  */
15230142Sbostic getch()
1531105Sbill {
15430142Sbostic 	++foff;
15530142Sbostic 	if (head_len) {
15630142Sbostic 		if (hcnt < head_len)
15730142Sbostic 			return((int)hbfr[hcnt++]);
15830142Sbostic 		head_len = 0;
1591105Sbill 	}
16035980Sbostic 	if (read_len == -1 || read_len-- > 0)
16130142Sbostic 		return(getchar());
16230142Sbostic 	return(EOF);
1631105Sbill }
164