Annotation of src/usr.bin/strings/strings.c, Revision 1.4
1.4 ! deraadt 1: /* $OpenBSD: strings.c,v 1.3 1997/06/17 23:53:16 deraadt 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.4 ! deraadt 47: static char rcsid[] = "$OpenBSD: strings.c,v 1.3 1997/06/17 23:53:16 deraadt 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.4 ! deraadt 77: static void usage __P((void));
! 78: int getch __P((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;
87: register int ch, cnt;
88: register u_char *C;
89: EXEC *head;
90: int exitcode, minlen;
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;
105: while ((ch = getopt(argc, argv, "-0123456789an:oft:")) != -1)
106: switch((char)ch) {
107: case '0': case '1': case '2': case '3': case '4':
108: case '5': case '6': case '7': case '8': case '9':
109: /*
110: * kludge: strings was originally designed to take
111: * a number after a dash.
112: */
113: if (minlen == -1) {
114: p = argv[optind - 1];
115: if (p[0] == '-' && p[1] == ch && !p[2])
116: minlen = atoi(++p);
117: else
118: minlen = atoi(argv[optind] + 1);
119: }
120: break;
121: case '-':
122: case 'a':
123: asdata = 1;
124: break;
125: case 'f':
126: fflg = 1;
127: break;
128: case 'n':
129: minlen = atoi(optarg);
130: break;
131: case 'o':
132: offset_format = FORMAT_OCT;
133: break;
134: case 't':
135: switch (*optarg) {
136: case 'o':
137: offset_format = FORMAT_OCT;
138: break;
139: case 'd':
140: offset_format = FORMAT_DEC;
141: break;
142: case 'x':
143: offset_format = FORMAT_HEX;
144: break;
145: default:
146: usage();
147: /* NOTREACHED */
148: }
149: break;
150: case '?':
151: default:
152: usage();
153: }
154: argc -= optind;
155: argv += optind;
156:
157: if (minlen == -1)
158: minlen = DEF_LEN;
159: else if (minlen < 1) {
160: (void)fprintf(stderr, "strings: length less than 1\n");
161: exit (1);
162: }
163:
1.3 deraadt 164: if (!(bfr = malloc(minlen + 1))) {
1.1 deraadt 165: (void)fprintf(stderr, "strings: %s\n", strerror(errno));
166: exit(1);
167: }
168: bfr[minlen] = '\0';
169: file = "stdin";
170: do {
171: if (*argv) {
172: file = *argv++;
173: if (!freopen(file, "r", stdin)) {
174: (void)fprintf(stderr,
175: "strings: %s: %s\n", file, strerror(errno));
176: exitcode = 1;
177: goto nextfile;
178: }
179: }
180: foff = 0;
181: #define DO_EVERYTHING() {read_len = -1; head_len = 0; goto start;}
182: read_len = -1;
183: if (asdata)
184: DO_EVERYTHING()
185: else {
186: head = (EXEC *)hbfr;
187: if ((head_len =
188: read(fileno(stdin), head, sizeof(EXEC))) == -1)
189: DO_EVERYTHING()
190: if (head_len == sizeof(EXEC) && !N_BADMAG(*head)) {
191: foff = N_TXTOFF(*head);
192: if (fseek(stdin, foff, SEEK_SET) == -1)
193: DO_EVERYTHING()
194: read_len = head->a_text + head->a_data;
195: head_len = 0;
196: }
197: else
198: hcnt = 0;
199: }
200: start:
201: for (cnt = 0; (ch = getch()) != EOF;) {
202: if (ISSTR(ch)) {
203: if (!cnt)
204: C = bfr;
205: *C++ = ch;
206: if (++cnt < minlen)
207: continue;
208:
209: if (fflg)
210: printf("%s:", file);
211:
212: if (offset_format)
213: printf(offset_format, foff - minlen);
214:
215: printf("%s", bfr);
216:
217: while ((ch = getch()) != EOF && ISSTR(ch))
218: putchar((char)ch);
219: putchar('\n');
220: }
221: cnt = 0;
222: }
223: nextfile: ;
224: } while (*argv);
225: exit(exitcode);
226: }
227:
228: /*
229: * getch --
230: * get next character from wherever
231: */
1.4 ! deraadt 232: int
1.1 deraadt 233: getch()
234: {
235: ++foff;
236: if (head_len) {
237: if (hcnt < head_len)
238: return((int)hbfr[hcnt++]);
239: head_len = 0;
240: }
241: if (read_len == -1 || read_len-- > 0)
242: return(getchar());
243: return(EOF);
244: }
245:
246: static void
247: usage()
248: {
249: (void)fprintf(stderr,
250: "usage: strings [-afo] [-n length] [-t {o,d,x}] [file ... ]\n");
251: exit(1);
252: }