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