home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / strings / strings1 / strings.c next >
Encoding:
C/C++ Source or Header  |  1991-07-31  |  6.0 KB  |  192 lines

  1. /*
  2.  *
  3.  *   strings - list strings of printable characters in a binary
  4.  *             file.
  5.  *
  6.  *   SYNOPSYS:
  7.  *    strings [-nnn] [-o] [filespec]
  8.  *         - where filespec is any valid DOS filename
  9.  *           with or without a preceeding path and
  10.  *           drive specifier.
  11.  *
  12.  *   Written by Paul Frattaroli
  13.  *   copyright(c) 1990 by Paul Frattaroli
  14.  *
  15.  *   Adapted from the UNIX version.
  16.  *
  17.  *   I have written this code completely on my own, and have taken
  18.  *   nothing from other sources.  While I connot say that the idea
  19.  *   for this program is mine, the implementation is.
  20.  *
  21.  *   Note: UNIX is a trademark of AT&T.
  22.  *
  23.  *   This program is written in Borland C++ version 2.0
  24.  *
  25.  * $Log:    strings.c $
  26.  * Revision 1.2  91/07/30  23:27:26  pfratar
  27.  * Put copyright and RCSid defs in wrong place...what a dunce.
  28.  * 
  29.  * Revision 1.1  91/07/30  23:22:25  pfratar
  30.  * Initial revision
  31.  * 
  32.  *
  33.  */
  34.  
  35. /* includes */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <ctype.h>
  39. #include <string.h>
  40.  
  41. /* some definitions */
  42. #define TRUE 1
  43. #define FALSE 0
  44. #define MAXLEN 2048     /* a 2K buffer should be enough */
  45. #define VERSION "1.0"
  46.  
  47. /* function prototypes */
  48. void print_usage( void );
  49.  
  50. /* global variables */
  51. char Buffer[MAXLEN];    /* buffer to hold strings as they are read */
  52.  
  53. static char RCSid[]="$Header: C:\BC\SRC\RCS\strings.c 1.2 91/07/30 23:27:26 pfratar Exp Locker: pfratar $";
  54. static char copyright[]="copyright (c) 1991 by Paul Frattaroli Version:";
  55.  
  56. /* start of code */
  57. main( int argc, char *argv[] ) {
  58.  
  59. char inchar;                /* the character that was just read */
  60. int counter;                /* counts how many chars in buffer */
  61. unsigned long int charc;    /* character count */
  62. int i, j;                   /* iteration variables */
  63. int MinStrLen;              /* set to minimum length to be a string */
  64. int OFFSET;                 /* boolean - show offset in file or not */
  65. int NOFILES;                /* boolean - any files on command-line? */
  66. FILE *infile;               /* file pointer */
  67. char *ptr, *bptr;           /* general use char string pointers */
  68.  
  69.     /**** Initialization ****/
  70. counter = 0;
  71. MinStrLen = 4;
  72. OFFSET = FALSE;
  73. NOFILES = TRUE;
  74. charc = 0;
  75.  
  76.   if( argc == 1 ) {
  77.     fprintf( stderr, "%s: Too few arguments!\n", argv[0] );
  78.     print_usage();
  79.     exit(0);
  80.   }
  81.     /**** Parse command line arguments ****/
  82.   for( i = 1; i < argc; i++ ) {
  83.   
  84.     strcpy( bptr, argv[i] );    /* make a backup copy of argv[i] */
  85.     if( argv[i][0] == '-' ) {
  86.       for( j = 1; j < strlen( argv[i] ); j++ ) {
  87.         /* attempt to extract a "number" argument */
  88.         if( isdigit( argv[i][j] ) ) {
  89.         /* extract a number, if user gives an invalid argument ?? */
  90.         /* Oh well, can't do anything about that */
  91.           ptr = strtok( bptr, "-ohOH" );
  92.         /* alter counter j to skip over rest of number */
  93.           j += strlen( ptr ) - 1;
  94.         /* assign minimum string length */
  95.           MinStrLen = atoi( ptr );
  96.         /* check bounds */
  97.           if( MinStrLen < 1 || MinStrLen >= MAXLEN ) {
  98.             fprintf( stderr, "\n%s: \"number\" argument out of range.\n",
  99.                      argv[0] );
  100.             print_usage();
  101.             exit(0);
  102.           }
  103.         }
  104.         else {
  105.           switch( argv[i][j] ) {
  106.             case 'o': OFFSET = TRUE; break;
  107.             case 'O': OFFSET = TRUE; break;
  108.             case 'h': print_usage(); exit(0); break;
  109.             case 'H': print_usage(); exit(0); break;
  110.             default : 
  111.                 fprintf( stderr, "\n%s: Invalid Option -%c\n",
  112.                   argv[0], argv[i][j] );
  113.                 print_usage();
  114.                 exit(0);
  115.           }        /* switch */
  116.         }        /* else */
  117.       }        /* for j */
  118.     }        /* if */
  119.   }        /* for i */
  120.   
  121.  
  122.     /**** Process each file on the command line ****/
  123.   for( i = 1; i < argc; i++ ) {
  124.   
  125.         /* skip flag arguments */
  126.     if( argv[i][0] == '-' ) continue;
  127.  
  128.         /* open file */
  129.     if( ( infile = fopen( argv[i], "rb" ) ) == NULL ) {
  130.         fprintf( stderr, "\n%s: Cannot open file %s\n\n", argv[0], argv[i] );
  131.         continue;
  132.     }
  133.   
  134.         /* print banner */
  135.     printf( "\n************\nFile: %s\n************\n\n", argv[i] );
  136.         /* a file was specified on the command-line */
  137.     NOFILES = FALSE;
  138.  
  139.     /**** loop until end of file ****/
  140.     while( ! feof( infile ) ) {
  141.         /* get char from "infile" */
  142.       inchar = fgetc( infile );
  143.         /* increase char count */
  144.       ++charc;
  145.         /* find printable chars */
  146.       if( isprint( inchar ) ) {
  147.         /* if printable, add to buffer and increase counter */
  148.         Buffer[counter] = inchar;
  149.         /* check bounds */
  150.         if( ++counter == MAXLEN )
  151.           fprintf( stderr, "\n%s: Buffer Overflow\n", argv[0] );
  152.       }
  153.       else {
  154.         /* if not printable */
  155.         if( counter >= MinStrLen ) {
  156.         /* if length >= the minimum string length then print string */
  157.           Buffer[counter] = '\0';
  158.         /* if user wants offset give it to them */
  159.           if( OFFSET ) printf( "%x:\t", ( charc - counter ) );
  160.         /* print buffer */
  161.           printf( "%s\n", Buffer );
  162.         }
  163.         /* reset the counter */
  164.         counter = 0;
  165.       }       /* else */
  166.         /* go get next char */
  167.     }       /* while */
  168.     fclose( infile );
  169.     charc = 0;
  170.   }       /* for */
  171.         /* go do next file */
  172.   if( NOFILES ) {
  173.     fprintf( stderr, "%s: No file(s) specified.\n", argv[0] );
  174.     print_usage();
  175.   }
  176. }       /* main */
  177.     /* finished program */
  178.  
  179.  
  180.     /**** print usage message to assist mis-guided user ****/
  181. void print_usage( void ) {
  182.   fprintf( stderr, "\nstrings - find printable strings in a binary file\n" );
  183.   fprintf( stderr, "%s %s\n", copyright, VERSION );
  184.   fprintf( stderr, "Usage:\tstrings [ -ho ] [ -num ] filename ...\n" );
  185.   fprintf( stderr, "\t-h:   Show this message.\n" );
  186.   fprintf( stderr, "\t-o:   Show offset of string(s) in file (in hex).\n" );
  187.   fprintf( stderr, "\t-num: Use num as the minimum string length.  If\n" );
  188.   fprintf( stderr, "\tnot specified, the default is 4.  " );
  189.   fprintf( stderr, "The range for\n\t\"num\" is 1 to %i.\n\n", MAXLEN - 1 );
  190. }
  191.  
  192.