/*
 * strings - extract strings of ascii text from a binary file
 *
 * Copyright 1989 Edwin Hoogerbeets
 *
 * This code is freely redistributable as long as no charge other than
 * reasonable copying fees is levied for it.
 *
 *
 * Usage: strings [file ...]
 *
 */
#include <stdio.h>
#define MINLENGTH 4

#define isprint(a) ((a) > 31 && (a) < 128)

extern char *malloc();

main(argc,argv)
int argc;
char **argv;
{
  register FILE *in;
  register int i;

  if ( argc < 2 ) {
    strings(stdin);
  } else {
    for ( i = 1; i < argc; i++) {

      if ( (in = fopen(argv[i],"r")) != NULL ) {
        if ( argc > 2 )
          printf("\nFile %s contains:\n",argv[i]);
        strings(in);
        fclose(in);
      }
    }
  }
}

strings(in)
FILE *in;
{
  register int n, index, temp;
  register char *buf = malloc(BUFSIZ+1);

  if ( buf ) {
    while ( n = fread(&buf[0],1,BUFSIZ,in) ) {

      index = 0;
      while ( index < n ) {

        /* pass over unprintable characters. */
        while ( index < n && !isprint(buf[index]) )
          ++index;

        if ( index < n ) {

          /* remember the start of the printable string */
          temp = index;

          /* search through the printable characters */
          while ( index < n && isprint(buf[index]) )
            ++index;

          /*
           * only print something out if the length of the printable
           * string is at least MINLENGTH
           */
          if ( index - temp >= MINLENGTH ) {
            /*
             * zap the first unprintable character to form a printable string
             * starting at temp.
             */
            buf[index] = '\0';

            printf("%s\n",&buf[temp]);
          }
        }
      }
    }

    free(buf);
  } else {
    printf("Strings error! Not enough memory.\n");
  }
}

_wb_parse() {}



