BSD 3 development
[unix-history] / .ref-BSD-2 / upgrade / src / strings.c
CommitLineData
69739ccc
BJ
1#include <stdio.h>
2struct exec {
3 int a_magic;
4 unsigned a_text, a_data, a_bss, a_syms, a_entry, a_trsize, a_drsize;
5};
6#define A_MAGIC1 0407
7#define A_MAGIC2 0410
8#define A_MAGIC3 0411
9#define A_MAGIC4 0405
10#include <ctype.h>
11
12/*
13long ftell();
14*/
15
16/*
17 * Strings - extract strings from an object file for whatever
18 *
19 * Bill Joy UCB
20 * April 22, 1978
21 *
22 * The algorithm is to look for sequences of "non-junk" characters
23 * The variable "minlen" is the minimum length string printed.
24 * This helps get rid of garbage.
25 * Default minimum string length is 4 characters.
26 */
27
28struct exec header;
29
30char *infile = "Standard input";
31int oflg;
32int asdata;
33long offset;
34int minlength = 4;
35
36main(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 [ - ] [ -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 }
78 fseek(stdin, (long) 0, 0);
79 if (asdata ||
80 fread((char *)&header, sizeof header, 1, stdin) != 1 ||
81 !ismagic(header.a_magic)) {
82 fseek(stdin, (long) 0, 0);
83 find((long) 100000000L);
84 continue;
85 }
86 fseek(stdin, (long) header.a_text, 1);
87 find((long) header.a_data);
88 } while (argc > 0);
89}
90
91find(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/*
107 if (oflg)
108 printf("%7D ", ftell(stdin) - cc - 1);
109*/
110 printf("%s\n", buf);
111 }
112 cp = buf, cc = 0;
113 } else {
114 if (cp < &buf[sizeof buf - 2])
115 *cp++ = c;
116 cc++;
117 }
118 if (ferror(stdin) || feof(stdin))
119 break;
120 }
121}
122
123dirt(c)
124 int c;
125{
126
127 switch (c) {
128
129 case '\n':
130 case '\f':
131 return (0);
132
133 case 0177:
134 return (1);
135
136 default:
137 return (c > 0200 || c < ' ');
138 }
139}
140
141ismagic(a)
142 int a;
143{
144
145 switch (a) {
146
147 case A_MAGIC1:
148 case A_MAGIC2:
149 case A_MAGIC3:
150 case A_MAGIC4:
151 return (1);
152 }
153 return (0);
154}