home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / tools / exe2bin / exe2com / strings.c < prev   
Encoding:
C/C++ Source or Header  |  1987-12-30  |  3.3 KB  |  119 lines

  1. /*
  2.  *      S T R I N G S . C
  3.  *
  4.  *      This program is used to display strings in binary files
  5.  *
  6.  *    Steve Sampson, Public Domain (p) November 1987
  7.  *
  8.  *     Updated 12/29/87 - P. Lyall (76703,4230)
  9.  *
  10.  *    - bug fix: in_string was previously never reset to FALSE,
  11.  *      resulting in erroneous marking of string start.
  12.  *
  13.  *    - hex offset of each string start is now provided to ease
  14.  *      hacking/patching of files.
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <ctype.h>
  19.  
  20. #ifdef MSDOS
  21. #include <fcntl.h>
  22. #define    READ    O_RDONLY
  23. #define    pflinit()
  24. #else
  25. #include <lowio.h>
  26. #endif
  27.  
  28. #define FALSE   0
  29. #define TRUE    ~FALSE
  30. #define DEFCHARS 3
  31. #define MAXLINE 256
  32.  
  33.  
  34. main(argc, argv)
  35. int     argc;
  36. char    *argv[];
  37. {
  38.      int     i, ercode, lptr, nchars = DEFCHARS, fd, in_string;
  39.      char    *fname, inbuff[MAXLINE], outbuff[MAXLINE];
  40.      long    sofar = 0L, sstart;
  41.  
  42.      pflinit();
  43.  
  44.      if (argc < 2 || argc > 3)  
  45.      {
  46.           fprintf(stderr, "Usage: strings [n] <filename>\n");
  47.           fprintf(stderr, "Where n is the minimum width (default is 3)\n");
  48.           exit(0);
  49.      }
  50.  
  51.      if (argc == 3)  
  52.      {
  53.           nchars = abs(atoi(argv[1]));
  54.           if (nchars < 1 || nchars > MAXLINE - 1)  
  55.           {
  56.                fprintf(stderr, "strings: Maximum width is %d\n", MAXLINE - 1);
  57.                exit(1);
  58.           }
  59.  
  60.           fname  = argv[2];
  61.      }
  62.      else
  63.           fname  = argv[1];
  64.  
  65.  
  66.      if ((fd = open(fname, READ)) == -1)  
  67.      {
  68.           fprintf(stderr, "strings: File `%s' Not Found\n", fname);
  69.           exit(1);
  70.      }
  71.  
  72.      lptr = 0;                               /* Index into outbuff        */
  73.      in_string = FALSE;                      /* Not in a string yet       */
  74.  
  75.      while ((ercode = read(fd, inbuff, MAXLINE)) > 0)  
  76.      {
  77.           for (i = 0; i < ercode; i++)  
  78.           {
  79.  
  80.                /* Search entire buffer */
  81.  
  82.                if (in_string && isprint(inbuff[i]))
  83.  
  84.                     /* We are in a string and are continuing */
  85.  
  86.                     outbuff[lptr++] = inbuff[i];
  87.  
  88.                else 
  89.                     if (isprint(inbuff[i]))   /* would this char start one ? */
  90.                     {
  91.                          outbuff[lptr++] = inbuff[i]; /* start new one */
  92.                          in_string = TRUE;      /* We're in a string now */
  93.                          sstart = i+sofar;      /* mark string start */
  94.                     }
  95.                     else 
  96.                          if (in_string) /* if in one, would this end it? */
  97.                          {
  98.                               /* End of string */
  99.                               if (lptr >= nchars)  /* enough to tell about? */
  100.                               {
  101.                                    /* Found a long-enough string */
  102.                                    outbuff[lptr] = '\0';
  103.  
  104.                                    /* Terminate the string and print */
  105.  
  106.                                    printf("%6lx: %s\n", sstart, outbuff);
  107.                               }
  108.  
  109.                               in_string = FALSE; /* PWL */
  110.                               lptr = 0;       /* Start at beginning again */
  111.                          }
  112.  
  113.           }
  114.  
  115.           sofar += (long)ercode;   /* update how far we've read into buffers */
  116.      }
  117.  
  118.      close(fd);
  119. }
  120.