]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/strings/strings.c
This commit was generated by cvs2svn to compensate for changes in r68700,
[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 const 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 #if 0
42 static char sccsid[] = "@(#)strings.c   8.2 (Berkeley) 1/28/94";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47
48 #include <sys/types.h>
49
50 #include <a.out.h>
51 #include <ctype.h>
52 #include <err.h>
53 #include <fcntl.h>
54 #include <locale.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 #define DEF_LEN         4               /* default minimum string length */
61 #define ISSTR(ch)       (isalnum(ch) || ispunct(ch) || \
62                          (isspace(ch) && (!iscntrl(ch) || ch == '\t')) || \
63                          (isascii(ch) && isprint(ch)))
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
73 int getch __P((void));
74 static void usage __P((void));
75
76 int
77 main(argc, argv)
78         int argc;
79         char **argv;
80 {
81         register int ch, cnt;
82         register u_char *C;
83         EXEC *head;
84         int exitcode, minlen;
85         short asdata, oflg, fflg;
86         u_char *bfr;
87         char *file, *p;
88
89         (void) setlocale(LC_CTYPE, "");
90
91         /*
92          * for backward compatibility, allow '-' to specify 'a' flag; no
93          * longer documented in the man page or usage string.
94          */
95         asdata = exitcode = fflg = oflg = 0;
96         minlen = -1;
97         while ((ch = getopt(argc, argv, "-0123456789an:of")) != -1)
98                 switch (ch) {
99                 case '0': case '1': case '2': case '3': case '4':
100                 case '5': case '6': case '7': case '8': case '9':
101                         /*
102                          * kludge: strings was originally designed to take
103                          * a number after a dash.
104                          */
105                         if (minlen == -1) {
106                                 p = argv[optind - 1];
107                                 if (p[0] == '-' && p[1] == ch && !p[2])
108                                         minlen = atoi(++p);
109                                 else
110                                         minlen = atoi(argv[optind] + 1);
111                         }
112                         break;
113                 case '-':
114                 case 'a':
115                         asdata = 1;
116                         break;
117                 case 'f':
118                         fflg = 1;
119                         break;
120                 case 'n':
121                         minlen = atoi(optarg);
122                         break;
123                 case 'o':
124                         oflg = 1;
125                         break;
126                 case '?':
127                 default:
128                         usage();
129                 }
130         argc -= optind;
131         argv += optind;
132
133         if (minlen == -1)
134                 minlen = DEF_LEN;
135         else if (minlen < 1)
136                 errx(1, "length less than 1");
137
138         if (!(bfr = malloc((u_int)minlen + 1)))
139                 errx(1, "malloc");
140         bfr[minlen] = '\0';
141         file = "stdin";
142         do {
143                 if (*argv) {
144                         file = *argv++;
145                         if (!freopen(file, "r", stdin)) {
146                                 warn("%s", file);
147                                 exitcode = 1;
148                                 goto nextfile;
149                         }
150                 }
151                 foff = 0;
152 #define DO_EVERYTHING()         {read_len = -1; head_len = 0; goto start;}
153                 read_len = -1;
154                 if (asdata)
155                         DO_EVERYTHING()
156                 else {
157                         head = (EXEC *)hbfr;
158                         if ((head_len =
159                             read(fileno(stdin), head, sizeof(EXEC))) == -1)
160                                 DO_EVERYTHING()
161                         if (head_len == sizeof(EXEC) && !N_BADMAG(*head)) {
162                                 foff = N_TXTOFF(*head);
163                                 if (fseek(stdin, foff, SEEK_SET) == -1)
164                                         DO_EVERYTHING()
165                                 read_len = head->a_text + head->a_data;
166                                 head_len = 0;
167                         }
168                         else
169                                 hcnt = 0;
170                 }
171 start:
172                 for (cnt = 0; (ch = getch()) != EOF;) {
173                         if (ISSTR(ch)) {
174                                 if (!cnt)
175                                         C = bfr;
176                                 *C++ = ch;
177                                 if (++cnt < minlen)
178                                         continue;
179                                 if (fflg)
180                                         printf("%s:", file);
181                                 if (oflg)
182                                         printf("%07ld %s",
183                                             foff - minlen, (char *)bfr);
184                                 else
185                                         printf("%s", bfr);
186                                 while ((ch = getch()) != EOF && ISSTR(ch))
187                                         putchar((char)ch);
188                                 putchar('\n');
189                         }
190                         cnt = 0;
191                 }
192 nextfile: ;
193         } while (*argv);
194         exit(exitcode);
195 }
196
197 /*
198  * getch --
199  *      get next character from wherever
200  */
201 int
202 getch()
203 {
204         ++foff;
205         if (head_len) {
206                 if (hcnt < head_len)
207                         return((int)hbfr[hcnt++]);
208                 head_len = 0;
209         }
210         if (read_len == -1 || read_len-- > 0)
211                 return(getchar());
212         return(EOF);
213 }
214
215 static void
216 usage()
217 {
218         (void)fprintf(stderr,
219             "usage: strings [-afo] [-n length] [file ... ]\n");
220         exit(1);
221 }