Commit | Line | Data |
---|---|---|
22e155fc DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
8 | char copyright[] = | |
9 | "@(#) Copyright (c) 1980 Regents of the University of California.\n\ | |
10 | All rights reserved.\n"; | |
11 | #endif not lint | |
12 | ||
052c3e48 | 13 | #ifndef lint |
22e155fc DF |
14 | static char sccsid[] = "@(#)strings.c 5.1 (Berkeley) %G%"; |
15 | #endif not lint | |
052c3e48 | 16 | |
ca5a877f BJ |
17 | #include <stdio.h> |
18 | #include <a.out.h> | |
19 | #include <ctype.h> | |
052c3e48 | 20 | #include <sys/file.h> |
ca5a877f BJ |
21 | |
22 | long ftell(); | |
23 | ||
24 | /* | |
25 | * strings | |
26 | */ | |
27 | ||
28 | struct exec header; | |
29 | ||
30 | char *infile = "Standard input"; | |
31 | int oflg; | |
32 | int asdata; | |
33 | long offset; | |
34 | int minlength = 4; | |
35 | ||
36 | main(argc, argv) | |
37 | int argc; | |
38 | char *argv[]; | |
39 | { | |
40 | ||
41 | argc--, argv++; | |
42 | while (argc > 0 && argv[0][0] == '-') { | |
43 | register int i; | |
44 | if (argv[0][1] == 0) | |
45 | asdata++; | |
46 | else for (i = 1; argv[0][i] != 0; i++) switch (argv[0][i]) { | |
47 | ||
48 | case 'o': | |
49 | oflg++; | |
50 | break; | |
51 | ||
52 | case 'a': | |
53 | asdata++; | |
54 | break; | |
55 | ||
56 | default: | |
57 | if (!isdigit(argv[0][i])) { | |
58 | fprintf(stderr, "Usage: strings [ -a ] [ -o ] [ -# ] [ file ... ]\n"); | |
59 | exit(1); | |
60 | } | |
61 | minlength = argv[0][i] - '0'; | |
62 | for (i++; isdigit(argv[0][i]); i++) | |
63 | minlength = minlength * 10 + argv[0][i] - '0'; | |
64 | i--; | |
65 | break; | |
66 | } | |
67 | argc--, argv++; | |
68 | } | |
69 | do { | |
70 | if (argc > 0) { | |
71 | if (freopen(argv[0], "r", stdin) == NULL) { | |
72 | perror(argv[0]); | |
73 | exit(1); | |
74 | } | |
75 | infile = argv[0]; | |
76 | argc--, argv++; | |
77 | } | |
052c3e48 | 78 | fseek(stdin, (long) 0, L_SET); |
ca5a877f BJ |
79 | if (asdata || |
80 | fread((char *)&header, sizeof header, 1, stdin) != 1 || | |
81 | N_BADMAG(header)) { | |
052c3e48 | 82 | fseek(stdin, (long) 0, L_SET); |
ca5a877f BJ |
83 | find((long) 100000000L); |
84 | continue; | |
85 | } | |
052c3e48 | 86 | fseek(stdin, (long) N_TXTOFF(header)+header.a_text, L_SET); |
ca5a877f BJ |
87 | find((long) header.a_data); |
88 | } while (argc > 0); | |
89 | } | |
90 | ||
91 | find(cnt) | |
92 | long cnt; | |
93 | { | |
94 | static char buf[BUFSIZ]; | |
95 | register char *cp; | |
96 | register int c, cc; | |
97 | ||
98 | cp = buf, cc = 0; | |
99 | for (; cnt != 0; cnt--) { | |
100 | c = getc(stdin); | |
101 | if (c == '\n' || dirt(c) || cnt == 0) { | |
102 | if (cp > buf && cp[-1] == '\n') | |
103 | --cp; | |
104 | *cp++ = 0; | |
105 | if (cp > &buf[minlength]) { | |
106 | if (oflg) | |
107 | printf("%7D ", ftell(stdin) - cc - 1); | |
108 | printf("%s\n", buf); | |
109 | } | |
110 | cp = buf, cc = 0; | |
111 | } else { | |
112 | if (cp < &buf[sizeof buf - 2]) | |
113 | *cp++ = c; | |
114 | cc++; | |
115 | } | |
116 | if (ferror(stdin) || feof(stdin)) | |
117 | break; | |
118 | } | |
119 | } | |
120 | ||
121 | dirt(c) | |
122 | int c; | |
123 | { | |
124 | ||
125 | switch (c) { | |
126 | ||
127 | case '\n': | |
128 | case '\f': | |
129 | return (0); | |
130 | ||
131 | case 0177: | |
132 | return (1); | |
133 | ||
134 | default: | |
135 | return (c > 0200 || c < ' '); | |
136 | } | |
137 | } |