[BACK]Return to strings.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / strings

Annotation of src/usr.bin/strings/strings.c, Revision 1.2

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