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