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