home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / spencer_2bsd.tar.gz / 2bsd.tar / src / strings.c < prev    next >
C/C++ Source or Header  |  1980-02-17  |  2KB  |  145 lines

  1. /* Copyright (c) 1979 Regents of the University of California */
  2. #include <stdio.h>
  3. #include <a.out.h>
  4. #include <ctype.h>
  5.  
  6. long    ftell();
  7.  
  8. /*
  9.  * Strings - extract strings from an object file for whatever
  10.  *
  11.  * Bill Joy UCB 
  12.  * April 22, 1978
  13.  *
  14.  * The algorithm is to look for sequences of "non-junk" characters
  15.  * The variable "minlen" is the minimum length string printed.
  16.  * This helps get rid of garbage.
  17.  * Default minimum string length is 4 characters.
  18.  */
  19.  
  20. struct    exec header;
  21.  
  22. char    *infile = "Standard input";
  23. int    oflg;
  24. int    asdata;
  25. long    offset;
  26. int    minlength = 4;
  27.  
  28. main(argc, argv)
  29.     int argc;
  30.     char *argv[];
  31. {
  32.  
  33.     argc--, argv++;
  34.     while (argc > 0 && argv[0][0] == '-') {
  35.         register int i;
  36.         if (argv[0][1] == 0)
  37.             asdata++;
  38.         else for (i = 1; argv[0][i] != 0; i++) switch (argv[0][i]) {
  39.  
  40.         case 'o':
  41.             oflg++;
  42.             break;
  43.  
  44.         case 'a':
  45.             asdata++;
  46.             break;
  47.  
  48.         default:
  49.             if (!isdigit(argv[0][i])) {
  50.                 fprintf(stderr, "Usage: strings [ - ] [ -o ] [ -# ] [ file ... ]\n");
  51.                 exit(1);
  52.             }
  53.             minlength = argv[0][i] - '0';
  54.             for (i++; isdigit(argv[0][i]); i++)
  55.                 minlength = minlength * 10 + argv[0][i] - '0';
  56.             i--;
  57.             break;
  58.         }
  59.         argc--, argv++;
  60.     }
  61.     do {
  62.         if (argc > 0) {
  63.             if (freopen(argv[0], "r", stdin) == NULL) {
  64.                 perror(argv[0]);
  65.                 exit(1);
  66.             }
  67.             infile = argv[0];
  68.             argc--, argv++;
  69.         }
  70.         fseek(stdin, (long) 0, 0);
  71.         if (asdata ||
  72.             fread((char *)&header, sizeof header, 1, stdin) != 1 || 
  73.             !ismagic(header.a_magic)) {
  74.             fseek(stdin, (long) 0, 0);
  75.             find((long) 100000000L);
  76.             continue;
  77.         }
  78.         fseek(stdin, (long) header.a_text, 1);
  79.         find((long) header.a_data);
  80.     } while (argc > 0);
  81. }
  82.  
  83. find(cnt)
  84.     long cnt;
  85. {
  86.     static char buf[BUFSIZ];
  87.     register char *cp;
  88.     register int c, cc;
  89.  
  90.     cp = buf, cc = 0;
  91.     for (; cnt != 0; cnt--) {
  92.         c = getc(stdin);
  93.         if (c == '\n' || dirt(c) || cnt == 0) {
  94.             if (cp > buf && cp[-1] == '\n')
  95.                 --cp;
  96.             *cp++ = 0;
  97.             if (cp > &buf[minlength]) {
  98.                 if (oflg)
  99.                     printf("%7D ", ftell(stdin) - cc - 1);
  100.                 printf("%s\n", buf);
  101.             }
  102.             cp = buf, cc = 0;
  103.         } else {
  104.             if (cp < &buf[sizeof buf - 2])
  105.                 *cp++ = c;
  106.             cc++;
  107.         }
  108.         if (ferror(stdin) || feof(stdin))
  109.             break;
  110.     }
  111. }
  112.  
  113. dirt(c)
  114.     int c;
  115. {
  116.  
  117.     switch (c) {
  118.  
  119.     case '\n':
  120.     case '\f':
  121.         return (0);
  122.  
  123.     case 0177:
  124.         return (1);
  125.  
  126.     default:
  127.         return (c > 0200 || c < ' ');
  128.     }
  129. }
  130.  
  131. ismagic(a)
  132.     int a;
  133. {
  134.  
  135.     switch (a) {
  136.  
  137.     case A_MAGIC1:
  138.     case A_MAGIC2:
  139.     case A_MAGIC3:
  140.     case A_MAGIC4:
  141.         return (1);
  142.     }
  143.     return (0);
  144. }
  145.