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