Annotation of src/usr.bin/strings/strings.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: strings.c,v 1.7 1995/02/15 15:49:19 jtc Exp $ */
! 2:
! 3: /*
! 4: * Copyright (c) 1980, 1987, 1993
! 5: * The Regents of the University of California. All rights reserved.
! 6: *
! 7: * Redistribution and use in source and binary forms, with or without
! 8: * modification, are permitted provided that the following conditions
! 9: * are met:
! 10: * 1. Redistributions of source code must retain the above copyright
! 11: * notice, this list of conditions and the following disclaimer.
! 12: * 2. Redistributions in binary form must reproduce the above copyright
! 13: * notice, this list of conditions and the following disclaimer in the
! 14: * documentation and/or other materials provided with the distribution.
! 15: * 3. All advertising materials mentioning features or use of this software
! 16: * must display the following acknowledgement:
! 17: * This product includes software developed by the University of
! 18: * California, Berkeley and its contributors.
! 19: * 4. Neither the name of the University nor the names of its contributors
! 20: * may be used to endorse or promote products derived from this software
! 21: * without specific prior written permission.
! 22: *
! 23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 33: * SUCH DAMAGE.
! 34: */
! 35:
! 36: #ifndef lint
! 37: static char copyright[] =
! 38: "@(#) Copyright (c) 1980, 1987, 1993\n\
! 39: The Regents of the University of California. All rights reserved.\n";
! 40: #endif /* not lint */
! 41:
! 42: #ifndef lint
! 43: #if 0
! 44: static char sccsid[] = "@(#)strings.c 8.2 (Berkeley) 1/28/94";
! 45: #endif
! 46: static char rcsid[] = "$NetBSD: strings.c,v 1.7 1995/02/15 15:49:19 jtc Exp $";
! 47: #endif /* not lint */
! 48:
! 49: #include <sys/types.h>
! 50:
! 51: #include <a.out.h>
! 52: #include <ctype.h>
! 53: #include <errno.h>
! 54: #include <fcntl.h>
! 55: #include <stdio.h>
! 56: #include <stdlib.h>
! 57: #include <string.h>
! 58: #include <locale.h>
! 59: #include <unistd.h>
! 60:
! 61: #define FORMAT_DEC "%07ld "
! 62: #define FORMAT_OCT "%07lo "
! 63: #define FORMAT_HEX "%07lx "
! 64:
! 65: #define DEF_LEN 4 /* default minimum string length */
! 66: #define ISSTR(ch) (isascii(ch) && (isprint(ch) || ch == '\t'))
! 67:
! 68: typedef struct exec EXEC; /* struct exec cast */
! 69:
! 70: static long foff; /* offset in the file */
! 71: static int hcnt, /* head count */
! 72: head_len, /* length of header */
! 73: read_len; /* length to read */
! 74: static u_char hbfr[sizeof(EXEC)]; /* buffer for struct exec */
! 75:
! 76: static void usage();
! 77:
! 78: main(argc, argv)
! 79: int argc;
! 80: char **argv;
! 81: {
! 82: extern char *optarg;
! 83: extern int optind;
! 84: register int ch, cnt;
! 85: register u_char *C;
! 86: EXEC *head;
! 87: int exitcode, minlen;
! 88: short asdata, fflg;
! 89: u_char *bfr;
! 90: char *file, *p;
! 91: char *offset_format;
! 92:
! 93: setlocale(LC_ALL, "");
! 94:
! 95: /*
! 96: * for backward compatibility, allow '-' to specify 'a' flag; no
! 97: * longer documented in the man page or usage string.
! 98: */
! 99: asdata = exitcode = fflg = 0;
! 100: offset_format = NULL;
! 101: minlen = -1;
! 102: while ((ch = getopt(argc, argv, "-0123456789an:oft:")) != -1)
! 103: switch((char)ch) {
! 104: case '0': case '1': case '2': case '3': case '4':
! 105: case '5': case '6': case '7': case '8': case '9':
! 106: /*
! 107: * kludge: strings was originally designed to take
! 108: * a number after a dash.
! 109: */
! 110: if (minlen == -1) {
! 111: p = argv[optind - 1];
! 112: if (p[0] == '-' && p[1] == ch && !p[2])
! 113: minlen = atoi(++p);
! 114: else
! 115: minlen = atoi(argv[optind] + 1);
! 116: }
! 117: break;
! 118: case '-':
! 119: case 'a':
! 120: asdata = 1;
! 121: break;
! 122: case 'f':
! 123: fflg = 1;
! 124: break;
! 125: case 'n':
! 126: minlen = atoi(optarg);
! 127: break;
! 128: case 'o':
! 129: offset_format = FORMAT_OCT;
! 130: break;
! 131: case 't':
! 132: switch (*optarg) {
! 133: case 'o':
! 134: offset_format = FORMAT_OCT;
! 135: break;
! 136: case 'd':
! 137: offset_format = FORMAT_DEC;
! 138: break;
! 139: case 'x':
! 140: offset_format = FORMAT_HEX;
! 141: break;
! 142: default:
! 143: usage();
! 144: /* NOTREACHED */
! 145: }
! 146: break;
! 147: case '?':
! 148: default:
! 149: usage();
! 150: }
! 151: argc -= optind;
! 152: argv += optind;
! 153:
! 154: if (minlen == -1)
! 155: minlen = DEF_LEN;
! 156: else if (minlen < 1) {
! 157: (void)fprintf(stderr, "strings: length less than 1\n");
! 158: exit (1);
! 159: }
! 160:
! 161: if (!(bfr = malloc(minlen))) {
! 162: (void)fprintf(stderr, "strings: %s\n", strerror(errno));
! 163: exit(1);
! 164: }
! 165: bfr[minlen] = '\0';
! 166: file = "stdin";
! 167: do {
! 168: if (*argv) {
! 169: file = *argv++;
! 170: if (!freopen(file, "r", stdin)) {
! 171: (void)fprintf(stderr,
! 172: "strings: %s: %s\n", file, strerror(errno));
! 173: exitcode = 1;
! 174: goto nextfile;
! 175: }
! 176: }
! 177: foff = 0;
! 178: #define DO_EVERYTHING() {read_len = -1; head_len = 0; goto start;}
! 179: read_len = -1;
! 180: if (asdata)
! 181: DO_EVERYTHING()
! 182: else {
! 183: head = (EXEC *)hbfr;
! 184: if ((head_len =
! 185: read(fileno(stdin), head, sizeof(EXEC))) == -1)
! 186: DO_EVERYTHING()
! 187: if (head_len == sizeof(EXEC) && !N_BADMAG(*head)) {
! 188: foff = N_TXTOFF(*head);
! 189: if (fseek(stdin, foff, SEEK_SET) == -1)
! 190: DO_EVERYTHING()
! 191: read_len = head->a_text + head->a_data;
! 192: head_len = 0;
! 193: }
! 194: else
! 195: hcnt = 0;
! 196: }
! 197: start:
! 198: for (cnt = 0; (ch = getch()) != EOF;) {
! 199: if (ISSTR(ch)) {
! 200: if (!cnt)
! 201: C = bfr;
! 202: *C++ = ch;
! 203: if (++cnt < minlen)
! 204: continue;
! 205:
! 206: if (fflg)
! 207: printf("%s:", file);
! 208:
! 209: if (offset_format)
! 210: printf(offset_format, foff - minlen);
! 211:
! 212: printf("%s", bfr);
! 213:
! 214: while ((ch = getch()) != EOF && ISSTR(ch))
! 215: putchar((char)ch);
! 216: putchar('\n');
! 217: }
! 218: cnt = 0;
! 219: }
! 220: nextfile: ;
! 221: } while (*argv);
! 222: exit(exitcode);
! 223: }
! 224:
! 225: /*
! 226: * getch --
! 227: * get next character from wherever
! 228: */
! 229: getch()
! 230: {
! 231: ++foff;
! 232: if (head_len) {
! 233: if (hcnt < head_len)
! 234: return((int)hbfr[hcnt++]);
! 235: head_len = 0;
! 236: }
! 237: if (read_len == -1 || read_len-- > 0)
! 238: return(getchar());
! 239: return(EOF);
! 240: }
! 241:
! 242: static void
! 243: usage()
! 244: {
! 245: (void)fprintf(stderr,
! 246: "usage: strings [-afo] [-n length] [-t {o,d,x}] [file ... ]\n");
! 247: exit(1);
! 248: }