1 /* 2 * Copyright (c) 1980, 1987 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1980, 1987 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)strings.c 5.6 (Berkeley) 06/01/90"; 16 #endif /* not lint */ 17 18 #include <sys/types.h> 19 #include <sys/file.h> 20 #include <a.out.h> 21 #include <stdio.h> 22 #include <ctype.h> 23 24 #define DEF_LEN 4 /* default minimum string length */ 25 #define ISSTR(ch) (isascii(ch) && (isprint(ch) || ch == '\t')) 26 27 typedef struct exec EXEC; /* struct exec cast */ 28 29 static long foff; /* offset in the file */ 30 static int hcnt, /* head count */ 31 head_len, /* length of header */ 32 read_len; /* length to read */ 33 static u_char hbfr[sizeof(EXEC)]; /* buffer for struct exec */ 34 35 main(argc, argv) 36 int argc; 37 char **argv; 38 { 39 extern char *optarg; 40 extern int optind; 41 register int ch, cnt; 42 register u_char *C; 43 EXEC *head; 44 int minlen; 45 short asdata, oflg; 46 u_char *bfr; 47 char *file, *p, *malloc(); 48 49 /* 50 * for backward compatibility, allow '-' to specify 'a' flag; no 51 * longer documented in the man page or usage string. 52 */ 53 asdata = oflg = 0; 54 minlen = -1; 55 while ((ch = getopt(argc, argv, "-0123456789ao")) != EOF) 56 switch((char)ch) { 57 case '0': case '1': case '2': case '3': case '4': 58 case '5': case '6': case '7': case '8': case '9': 59 /* 60 * kludge: strings was originally designed to take 61 * a number after a dash. 62 */ 63 if (minlen == -1) { 64 p = argv[optind - 1]; 65 if (p[0] == '-' && p[1] == ch && !p[2]) 66 minlen = atoi(++p); 67 else 68 minlen = atoi(argv[optind] + 1); 69 } 70 break; 71 case '-': 72 case 'a': 73 asdata = 1; 74 break; 75 case 'o': 76 oflg = 1; 77 break; 78 case '?': 79 default: 80 fprintf(stderr, 81 "usage: strings [-ao] [-#] [file ... ]\n"); 82 exit(1); 83 } 84 argc -= optind; 85 argv += optind; 86 87 if (minlen == -1) 88 minlen = DEF_LEN; 89 90 if (!(bfr = (u_char *)malloc((u_int)minlen))) { 91 fputs("strings: no space.\n", stderr); 92 exit(1); 93 } 94 bfr[minlen] = '\0'; 95 file = "stdin"; 96 do { 97 if (*argv) { 98 if (!freopen(*argv, "r", stdin)) { 99 perror(*argv); 100 exit(1); 101 } 102 file = *argv++; 103 } 104 foff = 0; 105 read_len = -1; 106 if (asdata) 107 head_len = 0; 108 else { 109 head = (EXEC *)hbfr; 110 if ((head_len = read(fileno(stdin), (char *)head, sizeof(EXEC))) == -1) { 111 perror(file); 112 exit(1); 113 } 114 if (head_len == sizeof(EXEC) && !N_BADMAG(*head)) { 115 foff = N_TXTOFF(*head) + head->a_text; 116 if (fseek(stdin, foff, L_SET) == -1) { 117 perror(file); 118 exit(1); 119 } 120 read_len = head->a_data; 121 head_len = 0; 122 } 123 else 124 hcnt = 0; 125 } 126 for (cnt = 0; (ch = getch()) != EOF;) { 127 if (ISSTR(ch)) { 128 if (!cnt) 129 C = bfr; 130 *C++ = ch; 131 if (++cnt < minlen) 132 continue; 133 if (oflg) 134 printf("%07ld %s", foff - minlen, 135 (char *)bfr); 136 else 137 fputs((char *)bfr, stdout); 138 while ((ch = getch()) != EOF && ISSTR(ch)) 139 putchar((char)ch); 140 putchar('\n'); 141 } 142 cnt = 0; 143 } 144 } while (*argv); 145 exit(0); 146 } 147 148 /* 149 * getch -- 150 * get next character from wherever 151 */ 152 getch() 153 { 154 ++foff; 155 if (head_len) { 156 if (hcnt < head_len) 157 return((int)hbfr[hcnt++]); 158 head_len = 0; 159 } 160 if (read_len == -1 || read_len-- > 0) 161 return(getchar()); 162 return(EOF); 163 } 164