1*21577Sdist /* 2*21577Sdist * Copyright (c) 1980 Regents of the University of California. 3*21577Sdist * All rights reserved. The Berkeley software License Agreement 4*21577Sdist * specifies the terms and conditions for redistribution. 5*21577Sdist */ 6*21577Sdist 717267Ssam #ifndef lint 8*21577Sdist char copyright[] = 9*21577Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 10*21577Sdist All rights reserved.\n"; 11*21577Sdist #endif not lint 1217267Ssam 13*21577Sdist #ifndef lint 14*21577Sdist static char sccsid[] = "@(#)strings.c 5.1 (Berkeley) 05/31/85"; 15*21577Sdist #endif not lint 16*21577Sdist 171105Sbill #include <stdio.h> 181105Sbill #include <a.out.h> 191105Sbill #include <ctype.h> 2017267Ssam #include <sys/file.h> 211105Sbill 221105Sbill long ftell(); 231105Sbill 241105Sbill /* 251105Sbill * strings 261105Sbill */ 271105Sbill 281105Sbill struct exec header; 291105Sbill 301105Sbill char *infile = "Standard input"; 311105Sbill int oflg; 321105Sbill int asdata; 331105Sbill long offset; 341105Sbill int minlength = 4; 351105Sbill 361105Sbill main(argc, argv) 371105Sbill int argc; 381105Sbill char *argv[]; 391105Sbill { 401105Sbill 411105Sbill argc--, argv++; 421105Sbill while (argc > 0 && argv[0][0] == '-') { 431105Sbill register int i; 441105Sbill if (argv[0][1] == 0) 451105Sbill asdata++; 461105Sbill else for (i = 1; argv[0][i] != 0; i++) switch (argv[0][i]) { 471105Sbill 481105Sbill case 'o': 491105Sbill oflg++; 501105Sbill break; 511105Sbill 521105Sbill case 'a': 531105Sbill asdata++; 541105Sbill break; 551105Sbill 561105Sbill default: 571105Sbill if (!isdigit(argv[0][i])) { 581105Sbill fprintf(stderr, "Usage: strings [ -a ] [ -o ] [ -# ] [ file ... ]\n"); 591105Sbill exit(1); 601105Sbill } 611105Sbill minlength = argv[0][i] - '0'; 621105Sbill for (i++; isdigit(argv[0][i]); i++) 631105Sbill minlength = minlength * 10 + argv[0][i] - '0'; 641105Sbill i--; 651105Sbill break; 661105Sbill } 671105Sbill argc--, argv++; 681105Sbill } 691105Sbill do { 701105Sbill if (argc > 0) { 711105Sbill if (freopen(argv[0], "r", stdin) == NULL) { 721105Sbill perror(argv[0]); 731105Sbill exit(1); 741105Sbill } 751105Sbill infile = argv[0]; 761105Sbill argc--, argv++; 771105Sbill } 7817267Ssam fseek(stdin, (long) 0, L_SET); 791105Sbill if (asdata || 801105Sbill fread((char *)&header, sizeof header, 1, stdin) != 1 || 811105Sbill N_BADMAG(header)) { 8217267Ssam fseek(stdin, (long) 0, L_SET); 831105Sbill find((long) 100000000L); 841105Sbill continue; 851105Sbill } 8617267Ssam fseek(stdin, (long) N_TXTOFF(header)+header.a_text, L_SET); 871105Sbill find((long) header.a_data); 881105Sbill } while (argc > 0); 891105Sbill } 901105Sbill 911105Sbill find(cnt) 921105Sbill long cnt; 931105Sbill { 941105Sbill static char buf[BUFSIZ]; 951105Sbill register char *cp; 961105Sbill register int c, cc; 971105Sbill 981105Sbill cp = buf, cc = 0; 991105Sbill for (; cnt != 0; cnt--) { 1001105Sbill c = getc(stdin); 1011105Sbill if (c == '\n' || dirt(c) || cnt == 0) { 1021105Sbill if (cp > buf && cp[-1] == '\n') 1031105Sbill --cp; 1041105Sbill *cp++ = 0; 1051105Sbill if (cp > &buf[minlength]) { 1061105Sbill if (oflg) 1071105Sbill printf("%7D ", ftell(stdin) - cc - 1); 1081105Sbill printf("%s\n", buf); 1091105Sbill } 1101105Sbill cp = buf, cc = 0; 1111105Sbill } else { 1121105Sbill if (cp < &buf[sizeof buf - 2]) 1131105Sbill *cp++ = c; 1141105Sbill cc++; 1151105Sbill } 1161105Sbill if (ferror(stdin) || feof(stdin)) 1171105Sbill break; 1181105Sbill } 1191105Sbill } 1201105Sbill 1211105Sbill dirt(c) 1221105Sbill int c; 1231105Sbill { 1241105Sbill 1251105Sbill switch (c) { 1261105Sbill 1271105Sbill case '\n': 1281105Sbill case '\f': 1291105Sbill return (0); 1301105Sbill 1311105Sbill case 0177: 1321105Sbill return (1); 1331105Sbill 1341105Sbill default: 1351105Sbill return (c > 0200 || c < ' '); 1361105Sbill } 1371105Sbill } 138