xref: /csrg-svn/usr.bin/strings/strings.c (revision 17267)
1*17267Ssam #ifndef lint
2*17267Ssam static char *sccsid = "@(#)strings.c	4.2 (Berkeley) 10/18/84";
3*17267Ssam #endif
4*17267Ssam 
51105Sbill #include <stdio.h>
61105Sbill #include <a.out.h>
71105Sbill #include <ctype.h>
8*17267Ssam #include <sys/file.h>
91105Sbill 
101105Sbill long	ftell();
111105Sbill 
121105Sbill /*
131105Sbill  * strings
141105Sbill  */
151105Sbill 
161105Sbill struct	exec header;
171105Sbill 
181105Sbill char	*infile = "Standard input";
191105Sbill int	oflg;
201105Sbill int	asdata;
211105Sbill long	offset;
221105Sbill int	minlength = 4;
231105Sbill 
241105Sbill main(argc, argv)
251105Sbill 	int argc;
261105Sbill 	char *argv[];
271105Sbill {
281105Sbill 
291105Sbill 	argc--, argv++;
301105Sbill 	while (argc > 0 && argv[0][0] == '-') {
311105Sbill 		register int i;
321105Sbill 		if (argv[0][1] == 0)
331105Sbill 			asdata++;
341105Sbill 		else for (i = 1; argv[0][i] != 0; i++) switch (argv[0][i]) {
351105Sbill 
361105Sbill 		case 'o':
371105Sbill 			oflg++;
381105Sbill 			break;
391105Sbill 
401105Sbill 		case 'a':
411105Sbill 			asdata++;
421105Sbill 			break;
431105Sbill 
441105Sbill 		default:
451105Sbill 			if (!isdigit(argv[0][i])) {
461105Sbill 				fprintf(stderr, "Usage: strings [ -a ] [ -o ] [ -# ] [ file ... ]\n");
471105Sbill 				exit(1);
481105Sbill 			}
491105Sbill 			minlength = argv[0][i] - '0';
501105Sbill 			for (i++; isdigit(argv[0][i]); i++)
511105Sbill 				minlength = minlength * 10 + argv[0][i] - '0';
521105Sbill 			i--;
531105Sbill 			break;
541105Sbill 		}
551105Sbill 		argc--, argv++;
561105Sbill 	}
571105Sbill 	do {
581105Sbill 		if (argc > 0) {
591105Sbill 			if (freopen(argv[0], "r", stdin) == NULL) {
601105Sbill 				perror(argv[0]);
611105Sbill 				exit(1);
621105Sbill 			}
631105Sbill 			infile = argv[0];
641105Sbill 			argc--, argv++;
651105Sbill 		}
66*17267Ssam 		fseek(stdin, (long) 0, L_SET);
671105Sbill 		if (asdata ||
681105Sbill 		    fread((char *)&header, sizeof header, 1, stdin) != 1 ||
691105Sbill 		    N_BADMAG(header)) {
70*17267Ssam 			fseek(stdin, (long) 0, L_SET);
711105Sbill 			find((long) 100000000L);
721105Sbill 			continue;
731105Sbill 		}
74*17267Ssam 		fseek(stdin, (long) N_TXTOFF(header)+header.a_text, L_SET);
751105Sbill 		find((long) header.a_data);
761105Sbill 	} while (argc > 0);
771105Sbill }
781105Sbill 
791105Sbill find(cnt)
801105Sbill 	long cnt;
811105Sbill {
821105Sbill 	static char buf[BUFSIZ];
831105Sbill 	register char *cp;
841105Sbill 	register int c, cc;
851105Sbill 
861105Sbill 	cp = buf, cc = 0;
871105Sbill 	for (; cnt != 0; cnt--) {
881105Sbill 		c = getc(stdin);
891105Sbill 		if (c == '\n' || dirt(c) || cnt == 0) {
901105Sbill 			if (cp > buf && cp[-1] == '\n')
911105Sbill 				--cp;
921105Sbill 			*cp++ = 0;
931105Sbill 			if (cp > &buf[minlength]) {
941105Sbill 				if (oflg)
951105Sbill 					printf("%7D ", ftell(stdin) - cc - 1);
961105Sbill 				printf("%s\n", buf);
971105Sbill 			}
981105Sbill 			cp = buf, cc = 0;
991105Sbill 		} else {
1001105Sbill 			if (cp < &buf[sizeof buf - 2])
1011105Sbill 				*cp++ = c;
1021105Sbill 			cc++;
1031105Sbill 		}
1041105Sbill 		if (ferror(stdin) || feof(stdin))
1051105Sbill 			break;
1061105Sbill 	}
1071105Sbill }
1081105Sbill 
1091105Sbill dirt(c)
1101105Sbill 	int c;
1111105Sbill {
1121105Sbill 
1131105Sbill 	switch (c) {
1141105Sbill 
1151105Sbill 	case '\n':
1161105Sbill 	case '\f':
1171105Sbill 		return (0);
1181105Sbill 
1191105Sbill 	case 0177:
1201105Sbill 		return (1);
1211105Sbill 
1221105Sbill 	default:
1231105Sbill 		return (c > 0200 || c < ' ');
1241105Sbill 	}
1251105Sbill }
126