[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.15

1.15    ! deraadt     1: /*     $OpenBSD: strings.c,v 1.14 2008/06/01 21:42:30 sobrado 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.
1.10      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #include <sys/types.h>
                     34:
                     35: #include <a.out.h>
                     36: #include <ctype.h>
                     37: #include <errno.h>
                     38: #include <fcntl.h>
                     39: #include <stdio.h>
                     40: #include <stdlib.h>
                     41: #include <string.h>
                     42: #include <locale.h>
                     43: #include <unistd.h>
1.12      mickey     44: #include <err.h>
1.1       deraadt    45:
                     46: #define FORMAT_DEC "%07ld "
                     47: #define FORMAT_OCT "%07lo "
                     48: #define FORMAT_HEX "%07lx "
                     49:
                     50: #define DEF_LEN                4               /* default minimum string length */
                     51: #define ISSTR(ch)      (isascii(ch) && (isprint(ch) || ch == '\t'))
                     52:
                     53: typedef struct exec    EXEC;           /* struct exec cast */
                     54:
                     55: static long    foff;                   /* offset in the file */
                     56: static int     hcnt,                   /* head count */
                     57:                head_len,               /* length of header */
                     58:                read_len;               /* length to read */
                     59: static u_char  hbfr[sizeof(EXEC)];     /* buffer for struct exec */
                     60:
1.7       millert    61: static void usage(void);
                     62: int getch(void);
1.1       deraadt    63:
1.4       deraadt    64: int
1.11      deraadt    65: main(int argc, char *argv[])
1.1       deraadt    66: {
                     67:        extern char *optarg;
                     68:        extern int optind;
1.6       mpech      69:        int ch, cnt;
                     70:        u_char *C;
1.1       deraadt    71:        EXEC *head;
1.5       provos     72:        int exitcode, minlen, maxlen, bfrlen;
1.1       deraadt    73:        short asdata, fflg;
                     74:        u_char *bfr;
                     75:        char *file, *p;
                     76:        char *offset_format;
                     77:
                     78:        setlocale(LC_ALL, "");
                     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 = 0;
                     85:        offset_format = NULL;
                     86:        minlen = -1;
1.5       provos     87:        maxlen = -1;
1.9       millert    88:        while ((ch = getopt(argc, argv, "0123456789an:m:oft:-")) != -1)
1.1       deraadt    89:                switch((char)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;
1.5       provos    114:                case 'm':
                    115:                        maxlen = atoi(optarg);
                    116:                        break;
1.1       deraadt   117:                case 'o':
                    118:                        offset_format = FORMAT_OCT;
                    119:                        break;
                    120:                case 't':
                    121:                        switch (*optarg) {
                    122:                        case 'o':
                    123:                                offset_format = FORMAT_OCT;
                    124:                                break;
                    125:                        case 'd':
                    126:                                offset_format = FORMAT_DEC;
                    127:                                break;
                    128:                        case 'x':
                    129:                                offset_format = FORMAT_HEX;
                    130:                                break;
                    131:                        default:
                    132:                                usage();
                    133:                                /* NOTREACHED */
                    134:                        }
                    135:                        break;
                    136:                case '?':
                    137:                default:
                    138:                        usage();
                    139:                }
                    140:        argc -= optind;
                    141:        argv += optind;
                    142:
                    143:        if (minlen == -1)
                    144:                minlen = DEF_LEN;
1.12      mickey    145:        else if (minlen < 1)
                    146:                errx(1, "length less than 1");
                    147:        if (maxlen != -1 && maxlen < minlen)
                    148:                errx(1, "max length less than min");
1.5       provos    149:        bfrlen = maxlen == -1 ? minlen : maxlen;
                    150:        bfr = malloc(bfrlen + 1);
1.12      mickey    151:        if (!bfr)
                    152:                err(1, "malloc");
1.5       provos    153:        bfr[bfrlen] = '\0';
1.1       deraadt   154:        file = "stdin";
                    155:        do {
                    156:                if (*argv) {
                    157:                        file = *argv++;
                    158:                        if (!freopen(file, "r", stdin)) {
1.12      mickey    159:                                warn("%s", file);
1.1       deraadt   160:                                exitcode = 1;
                    161:                                goto nextfile;
                    162:                        }
                    163:                }
                    164:                foff = 0;
                    165: #define DO_EVERYTHING()                {read_len = -1; head_len = 0; goto start;}
                    166:                read_len = -1;
                    167:                if (asdata)
                    168:                        DO_EVERYTHING()
                    169:                else {
                    170:                        head = (EXEC *)hbfr;
                    171:                        if ((head_len =
                    172:                            read(fileno(stdin), head, sizeof(EXEC))) == -1)
                    173:                                DO_EVERYTHING()
                    174:                        if (head_len == sizeof(EXEC) && !N_BADMAG(*head)) {
                    175:                                foff = N_TXTOFF(*head);
                    176:                                if (fseek(stdin, foff, SEEK_SET) == -1)
                    177:                                        DO_EVERYTHING()
                    178:                                read_len = head->a_text + head->a_data;
                    179:                                head_len = 0;
                    180:                        }
                    181:                        else
                    182:                                hcnt = 0;
                    183:                }
                    184: start:
1.13      ray       185:                for (cnt = 0, C = bfr; (ch = getch()) != EOF;) {
1.1       deraadt   186:                        if (ISSTR(ch)) {
                    187:                                *C++ = ch;
                    188:                                if (++cnt < minlen)
                    189:                                        continue;
1.5       provos    190:                                if (maxlen != -1) {
                    191:                                        while ((ch = getch()) != EOF &&
                    192:                                               ISSTR(ch) && cnt++ < maxlen)
                    193:                                                *C++ = ch;
                    194:                                        if (ch == EOF ||
                    195:                                            (ch != 0 && ch != '\n')) {
                    196:                                                /* get all of too big string */
                    197:                                                while ((ch = getch()) != EOF &&
                    198:                                                       ISSTR(ch))
                    199:                                                        ;
                    200:                                                ungetc(ch, stdin);
                    201:                                                goto out;
                    202:                                        }
                    203:                                        *C = 0;
                    204:                                }
1.1       deraadt   205:
                    206:                                if (fflg)
                    207:                                        printf("%s:", file);
                    208:
                    209:                                if (offset_format)
                    210:                                        printf(offset_format, foff - minlen);
                    211:
                    212:                                printf("%s", bfr);
1.5       provos    213:
                    214:                                if (maxlen == -1)
                    215:                                        while ((ch = getch()) != EOF &&
                    216:                                               ISSTR(ch))
                    217:                                                putchar((char)ch);
1.1       deraadt   218:                                putchar('\n');
1.5       provos    219:                        out:
1.8       millert   220:                                ;
1.1       deraadt   221:                        }
                    222:                        cnt = 0;
1.13      ray       223:                        C = bfr;
1.1       deraadt   224:                }
                    225: nextfile: ;
                    226:        } while (*argv);
                    227:        exit(exitcode);
                    228: }
                    229:
                    230: /*
                    231:  * getch --
                    232:  *     get next character from wherever
                    233:  */
1.4       deraadt   234: int
1.11      deraadt   235: getch(void)
1.1       deraadt   236: {
                    237:        ++foff;
                    238:        if (head_len) {
                    239:                if (hcnt < head_len)
                    240:                        return((int)hbfr[hcnt++]);
                    241:                head_len = 0;
                    242:        }
                    243:        if (read_len == -1 || read_len-- > 0)
                    244:                return(getchar());
                    245:        return(EOF);
                    246: }
                    247:
                    248: static void
1.11      deraadt   249: usage(void)
1.1       deraadt   250: {
                    251:        (void)fprintf(stderr,
1.14      sobrado   252:            "usage: strings [-afo] [-m number] [-n number] [-t radix] [file ...]\n");
1.1       deraadt   253:        exit(1);
                    254: }