Annotation of src/usr.bin/strings/strings.c, Revision 1.8
1.8 ! millert 1: /* $OpenBSD: strings.c,v 1.7 2002/02/16 21:27:53 millert Exp $ */
1.1 deraadt 2: /* $NetBSD: strings.c,v 1.7 1995/02/15 15:49:19 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1980, 1987, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) Copyright (c) 1980, 1987, 1993\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)strings.c 8.2 (Berkeley) 1/28/94";
46: #endif
1.8 ! millert 47: static char rcsid[] = "$OpenBSD: strings.c,v 1.7 2002/02/16 21:27:53 millert Exp $";
1.1 deraadt 48: #endif /* not lint */
49:
50: #include <sys/types.h>
51:
52: #include <a.out.h>
53: #include <ctype.h>
54: #include <errno.h>
55: #include <fcntl.h>
56: #include <stdio.h>
57: #include <stdlib.h>
58: #include <string.h>
59: #include <locale.h>
60: #include <unistd.h>
61:
62: #define FORMAT_DEC "%07ld "
63: #define FORMAT_OCT "%07lo "
64: #define FORMAT_HEX "%07lx "
65:
66: #define DEF_LEN 4 /* default minimum string length */
67: #define ISSTR(ch) (isascii(ch) && (isprint(ch) || ch == '\t'))
68:
69: typedef struct exec EXEC; /* struct exec cast */
70:
71: static long foff; /* offset in the file */
72: static int hcnt, /* head count */
73: head_len, /* length of header */
74: read_len; /* length to read */
75: static u_char hbfr[sizeof(EXEC)]; /* buffer for struct exec */
76:
1.7 millert 77: static void usage(void);
78: int getch(void);
1.1 deraadt 79:
1.4 deraadt 80: int
1.1 deraadt 81: main(argc, argv)
82: int argc;
83: char **argv;
84: {
85: extern char *optarg;
86: extern int optind;
1.6 mpech 87: int ch, cnt;
88: u_char *C;
1.1 deraadt 89: EXEC *head;
1.5 provos 90: int exitcode, minlen, maxlen, bfrlen;
1.1 deraadt 91: short asdata, fflg;
92: u_char *bfr;
93: char *file, *p;
94: char *offset_format;
95:
96: setlocale(LC_ALL, "");
97:
98: /*
99: * for backward compatibility, allow '-' to specify 'a' flag; no
100: * longer documented in the man page or usage string.
101: */
102: asdata = exitcode = fflg = 0;
103: offset_format = NULL;
104: minlen = -1;
1.5 provos 105: maxlen = -1;
106: while ((ch = getopt(argc, argv, "-0123456789an:m:oft:")) != -1)
1.1 deraadt 107: switch((char)ch) {
108: case '0': case '1': case '2': case '3': case '4':
109: case '5': case '6': case '7': case '8': case '9':
110: /*
111: * kludge: strings was originally designed to take
112: * a number after a dash.
113: */
114: if (minlen == -1) {
115: p = argv[optind - 1];
116: if (p[0] == '-' && p[1] == ch && !p[2])
117: minlen = atoi(++p);
118: else
119: minlen = atoi(argv[optind] + 1);
120: }
121: break;
122: case '-':
123: case 'a':
124: asdata = 1;
125: break;
126: case 'f':
127: fflg = 1;
128: break;
129: case 'n':
130: minlen = atoi(optarg);
131: break;
1.5 provos 132: case 'm':
133: maxlen = atoi(optarg);
134: break;
1.1 deraadt 135: case 'o':
136: offset_format = FORMAT_OCT;
137: break;
138: case 't':
139: switch (*optarg) {
140: case 'o':
141: offset_format = FORMAT_OCT;
142: break;
143: case 'd':
144: offset_format = FORMAT_DEC;
145: break;
146: case 'x':
147: offset_format = FORMAT_HEX;
148: break;
149: default:
150: usage();
151: /* NOTREACHED */
152: }
153: break;
154: case '?':
155: default:
156: usage();
157: }
158: argc -= optind;
159: argv += optind;
160:
161: if (minlen == -1)
162: minlen = DEF_LEN;
163: else if (minlen < 1) {
164: (void)fprintf(stderr, "strings: length less than 1\n");
165: exit (1);
166: }
1.5 provos 167: if (maxlen != -1 && maxlen < minlen) {
168: (void)fprintf(stderr, "strings: max length less than min\n");
169: exit (1);
170: }
171: bfrlen = maxlen == -1 ? minlen : maxlen;
172: bfr = malloc(bfrlen + 1);
173: if (!bfr) {
1.1 deraadt 174: (void)fprintf(stderr, "strings: %s\n", strerror(errno));
175: exit(1);
176: }
1.5 provos 177: bfr[bfrlen] = '\0';
1.1 deraadt 178: file = "stdin";
179: do {
180: if (*argv) {
181: file = *argv++;
182: if (!freopen(file, "r", stdin)) {
183: (void)fprintf(stderr,
184: "strings: %s: %s\n", file, strerror(errno));
185: exitcode = 1;
186: goto nextfile;
187: }
188: }
189: foff = 0;
190: #define DO_EVERYTHING() {read_len = -1; head_len = 0; goto start;}
191: read_len = -1;
192: if (asdata)
193: DO_EVERYTHING()
194: else {
195: head = (EXEC *)hbfr;
196: if ((head_len =
197: read(fileno(stdin), head, sizeof(EXEC))) == -1)
198: DO_EVERYTHING()
199: if (head_len == sizeof(EXEC) && !N_BADMAG(*head)) {
200: foff = N_TXTOFF(*head);
201: if (fseek(stdin, foff, SEEK_SET) == -1)
202: DO_EVERYTHING()
203: read_len = head->a_text + head->a_data;
204: head_len = 0;
205: }
206: else
207: hcnt = 0;
208: }
209: start:
210: for (cnt = 0; (ch = getch()) != EOF;) {
211: if (ISSTR(ch)) {
212: if (!cnt)
213: C = bfr;
214: *C++ = ch;
215: if (++cnt < minlen)
216: continue;
1.5 provos 217: if (maxlen != -1) {
218: while ((ch = getch()) != EOF &&
219: ISSTR(ch) && cnt++ < maxlen)
220: *C++ = ch;
221: if (ch == EOF ||
222: (ch != 0 && ch != '\n')) {
223: /* get all of too big string */
224: while ((ch = getch()) != EOF &&
225: ISSTR(ch))
226: ;
227: ungetc(ch, stdin);
228: goto out;
229: }
230: *C = 0;
231: }
1.1 deraadt 232:
233: if (fflg)
234: printf("%s:", file);
235:
236: if (offset_format)
237: printf(offset_format, foff - minlen);
238:
239: printf("%s", bfr);
1.5 provos 240:
241: if (maxlen == -1)
242: while ((ch = getch()) != EOF &&
243: ISSTR(ch))
244: putchar((char)ch);
1.1 deraadt 245: putchar('\n');
1.5 provos 246: out:
1.8 ! millert 247: ;
1.1 deraadt 248: }
249: cnt = 0;
250: }
251: nextfile: ;
252: } while (*argv);
253: exit(exitcode);
254: }
255:
256: /*
257: * getch --
258: * get next character from wherever
259: */
1.4 deraadt 260: int
1.1 deraadt 261: getch()
262: {
263: ++foff;
264: if (head_len) {
265: if (hcnt < head_len)
266: return((int)hbfr[hcnt++]);
267: head_len = 0;
268: }
269: if (read_len == -1 || read_len-- > 0)
270: return(getchar());
271: return(EOF);
272: }
273:
274: static void
275: usage()
276: {
277: (void)fprintf(stderr,
278: "usage: strings [-afo] [-n length] [-t {o,d,x}] [file ... ]\n");
279: exit(1);
280: }