[comp.os.minix] strings

tholm@uvicctr.UUCP (Terrence W. Holm) (05/05/88)

EFTH Minix report #1  - May 1988 -  strings(1)


This is an implementation of strings(1) that we
wrote for Minix. Please consider this a public
domain program. If you can't think of a use for
this program now, we believe you will - some day :-)

A "man" page is included.

[Note: we are using "gres" in our shar files.]


echo x - strings.1
gres '^X' '' > strings.1 << '/'
XNAME
X    strings(1)          - find printable strings in a file
X
XSYNOPSIS
X    strings  [ -o ]  [ -number ]  [ file ]
X
XDESCRIPTION
X    The "file", or standard input if "file" is not specified,
X    is scanned for possible character strings. A "string" is
X    a sequence of 4 or more printable ASCII characters followed
X    by a '\0', '\n' or '\r'.
X
X    This information might be useful for identifying an object 
X    file.
X
XOPTIONS
X    -o		Also display the character offset of the start
X		of the string from the beginning of the file.
X		Thus, the first character of the file is at
X		offset zero (0). The offset is printed in decimal.
X
X    -number	Minimum sequence length which is considered a
X		string (the default is 4). Note that the program
X		truncates all strings over 78 characters, to
X		aid readability of the output.
X
XSEE ALSO
X    od(1)
/
echo x - strings.c
gres '^X' '' > strings.c << '/'
X/****************************************************************/
X/*								*/
X/*	strings.c						*/
X/*								*/
X/*		Find printable strings in a file.		*/
X/*		This is the code for strings(1).		*/
X/*								*/
X/****************************************************************/
X/*   origination        1988-Apr-14               T. Holm	*/
X/****************************************************************/
X
X
X
X
X/****************************************************************/
X/*								*/
X/*								*/
X/*  Usage:    strings  [ -o ]  [ -number ]  [ file ]		*/
X/*								*/
X/*								*/
X/*  The "file", or standard input if "file" is not specified,	*/
X/*  is scanned for possible character strings. A "string" is	*/
X/*  a sequence of 4 or more printable ASCII characters followed */
X/*  by a '\0', '\n' or '\r'.					*/
X/*								*/
X/*								*/
X/*    -o       Also display the character offset of the start	*/
X/*	       of the string from the beginning of the file,	*/
X/*	       in decimal.					*/
X/*								*/
X/*    -number  Minimum sequence length which is considered a	*/
X/*	       string (the default is 4).			*/
X/*								*/
X/*								*/
X/****************************************************************/
X
X
X
X
X#include <stdio.h>
X
X
X#define   FALSE			0
X#define   TRUE 			1
X#define   MAX_STRING_LENGTH	78
X
X
X
X
X
Xmain( argc, argv )
X  int   argc;
X  char *argv[];
X
X  {
X  char  *my_name       = argv[0];   /*  The name of this program      */
X
X  char   echo_position = FALSE;	    /*  "-o" means echo where found   */
X  long   position      = 0;	    /*  Where string was found	      */
X
X  int    string_size   = 4;	    /*  "-#" sets minimum string size */
X  int    count         = 0;         /*  Size of string so far         */
X  
X  char   string[MAX_STRING_LENGTH]; /*  A possible printable string   */
X  int    c;
X
X
X
X  /********  Parse the arguments  ********/
X
X  --argc;
X  ++argv;
X
X
X  if ( argc > 0  &&  strcmp( argv[0], "-o" ) == 0 )
X    {
X    echo_position = TRUE;
X
X    --argc;
X    ++argv;
X    }
X
X
X  if ( argc > 0  &&  *argv[0] == '-' )
X    {
X    string_size = atoi( argv[0]+1 );
X
X    if ( string_size <= 0 )
X      string_size = 1;
X
X    --argc;
X    ++argv;
X    }
X
X
X  if ( argc > 0 )
X    {
X    if( (freopen( argv[0], "r", stdin )) == NULL )
X      {
X      fprintf( stderr, "%s:  Can not open file \"%s\"\n", my_name, argv[0] );
X      exit( 1 );
X      }
X
X    --argc;
X    ++argv;
X    }
X
X
X  if ( argc > 0 )
X    {
X    fprintf( stderr, "Usage:  %s  [ -o ]  [ -number ]  [ file ]\n", my_name );
X    exit( 1 );
X    }
X
X
X
X
X  /********  The main loop  ********/
X
X
X  while( (c=getchar()) != EOF )
X    {
X    ++position;
X
X
X    if (  c == '\0'  ||  c == '\n'  ||  c == '\r'  )
X      {
X      /*  Found the end of a printable string  */
X
X      if ( count >= string_size )
X	Print( string, count, echo_position, position - 1 - count );
X
X      count = 0;
X      }
X
X
X    else if (  c >= ' '  &&  c < '\177'  ||  c == '\t'  )
X      {
X      /*  Append another printable character to the string  */
X
X      string[count] = c;
X      ++count;
X
X      if ( count == MAX_STRING_LENGTH )
X	{
X	Print( string, count, echo_position, position - count );
X	count = 0;
X	}
X      }
X
X
X    else  /*  Not a printable character  */
X      count = 0;
X
X    }  /*  end while(getchar())  */
X
X  exit( 0 );
X  }
X
X
X
X
X
X
X
X
X/****************************************************************/
X/*								*/
X/*	Print( string, count, echo_position, position )		*/
X/*								*/
X/*		If "echo_position" is TRUE then output the	*/
X/*		"position" in the file. "count" printable	*/
X/*		characters in "string" are output.		*/
X/*								*/
X/****************************************************************/
X
X
XPrint( string, count, echo_position, position )
X  char   string[];
X  int    count;
X  char   echo_position;
X  long   position;
X
X  {
X  if ( echo_position )
X    printf( "%7D ", position );
X
X  fwrite( string, 1, count, stdout );
X
X  putchar( '\n' );
X  }
/