home *** CD-ROM | disk | FTP | other *** search
- /*
- *
- * strings - list strings of printable characters in a binary
- * file.
- *
- * SYNOPSYS:
- * strings [-nnn] [-o] [filespec]
- * - where filespec is any valid DOS filename
- * with or without a preceeding path and
- * drive specifier.
- *
- * Written by Paul Frattaroli
- * copyright(c) 1990 by Paul Frattaroli
- *
- * Adapted from the UNIX version.
- *
- * I have written this code completely on my own, and have taken
- * nothing from other sources. While I connot say that the idea
- * for this program is mine, the implementation is.
- *
- * Note: UNIX is a trademark of AT&T.
- *
- * This program is written in Borland C++ version 2.0
- *
- * $Log: strings.c $
- * Revision 1.2 91/07/30 23:27:26 pfratar
- * Put copyright and RCSid defs in wrong place...what a dunce.
- *
- * Revision 1.1 91/07/30 23:22:25 pfratar
- * Initial revision
- *
- *
- */
-
- /* includes */
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
-
- /* some definitions */
- #define TRUE 1
- #define FALSE 0
- #define MAXLEN 2048 /* a 2K buffer should be enough */
- #define VERSION "1.0"
-
- /* function prototypes */
- void print_usage( void );
-
- /* global variables */
- char Buffer[MAXLEN]; /* buffer to hold strings as they are read */
-
- static char RCSid[]="$Header: C:\BC\SRC\RCS\strings.c 1.2 91/07/30 23:27:26 pfratar Exp Locker: pfratar $";
- static char copyright[]="copyright (c) 1991 by Paul Frattaroli Version:";
-
- /* start of code */
- main( int argc, char *argv[] ) {
-
- char inchar; /* the character that was just read */
- int counter; /* counts how many chars in buffer */
- unsigned long int charc; /* character count */
- int i, j; /* iteration variables */
- int MinStrLen; /* set to minimum length to be a string */
- int OFFSET; /* boolean - show offset in file or not */
- int NOFILES; /* boolean - any files on command-line? */
- FILE *infile; /* file pointer */
- char *ptr, *bptr; /* general use char string pointers */
-
- /**** Initialization ****/
- counter = 0;
- MinStrLen = 4;
- OFFSET = FALSE;
- NOFILES = TRUE;
- charc = 0;
-
- if( argc == 1 ) {
- fprintf( stderr, "%s: Too few arguments!\n", argv[0] );
- print_usage();
- exit(0);
- }
- /**** Parse command line arguments ****/
- for( i = 1; i < argc; i++ ) {
-
- strcpy( bptr, argv[i] ); /* make a backup copy of argv[i] */
- if( argv[i][0] == '-' ) {
- for( j = 1; j < strlen( argv[i] ); j++ ) {
- /* attempt to extract a "number" argument */
- if( isdigit( argv[i][j] ) ) {
- /* extract a number, if user gives an invalid argument ?? */
- /* Oh well, can't do anything about that */
- ptr = strtok( bptr, "-ohOH" );
- /* alter counter j to skip over rest of number */
- j += strlen( ptr ) - 1;
- /* assign minimum string length */
- MinStrLen = atoi( ptr );
- /* check bounds */
- if( MinStrLen < 1 || MinStrLen >= MAXLEN ) {
- fprintf( stderr, "\n%s: \"number\" argument out of range.\n",
- argv[0] );
- print_usage();
- exit(0);
- }
- }
- else {
- switch( argv[i][j] ) {
- case 'o': OFFSET = TRUE; break;
- case 'O': OFFSET = TRUE; break;
- case 'h': print_usage(); exit(0); break;
- case 'H': print_usage(); exit(0); break;
- default :
- fprintf( stderr, "\n%s: Invalid Option -%c\n",
- argv[0], argv[i][j] );
- print_usage();
- exit(0);
- } /* switch */
- } /* else */
- } /* for j */
- } /* if */
- } /* for i */
-
-
- /**** Process each file on the command line ****/
- for( i = 1; i < argc; i++ ) {
-
- /* skip flag arguments */
- if( argv[i][0] == '-' ) continue;
-
- /* open file */
- if( ( infile = fopen( argv[i], "rb" ) ) == NULL ) {
- fprintf( stderr, "\n%s: Cannot open file %s\n\n", argv[0], argv[i] );
- continue;
- }
-
- /* print banner */
- printf( "\n************\nFile: %s\n************\n\n", argv[i] );
- /* a file was specified on the command-line */
- NOFILES = FALSE;
-
- /**** loop until end of file ****/
- while( ! feof( infile ) ) {
- /* get char from "infile" */
- inchar = fgetc( infile );
- /* increase char count */
- ++charc;
- /* find printable chars */
- if( isprint( inchar ) ) {
- /* if printable, add to buffer and increase counter */
- Buffer[counter] = inchar;
- /* check bounds */
- if( ++counter == MAXLEN )
- fprintf( stderr, "\n%s: Buffer Overflow\n", argv[0] );
- }
- else {
- /* if not printable */
- if( counter >= MinStrLen ) {
- /* if length >= the minimum string length then print string */
- Buffer[counter] = '\0';
- /* if user wants offset give it to them */
- if( OFFSET ) printf( "%x:\t", ( charc - counter ) );
- /* print buffer */
- printf( "%s\n", Buffer );
- }
- /* reset the counter */
- counter = 0;
- } /* else */
- /* go get next char */
- } /* while */
- fclose( infile );
- charc = 0;
- } /* for */
- /* go do next file */
- if( NOFILES ) {
- fprintf( stderr, "%s: No file(s) specified.\n", argv[0] );
- print_usage();
- }
- } /* main */
- /* finished program */
-
-
- /**** print usage message to assist mis-guided user ****/
- void print_usage( void ) {
- fprintf( stderr, "\nstrings - find printable strings in a binary file\n" );
- fprintf( stderr, "%s %s\n", copyright, VERSION );
- fprintf( stderr, "Usage:\tstrings [ -ho ] [ -num ] filename ...\n" );
- fprintf( stderr, "\t-h: Show this message.\n" );
- fprintf( stderr, "\t-o: Show offset of string(s) in file (in hex).\n" );
- fprintf( stderr, "\t-num: Use num as the minimum string length. If\n" );
- fprintf( stderr, "\tnot specified, the default is 4. " );
- fprintf( stderr, "The range for\n\t\"num\" is 1 to %i.\n\n", MAXLEN - 1 );
- }
-
-