/* Program: SEARCH Author : Kim Moser Date : 4/12/89 System : IBM PC / Borland Turbo-C 2.0 Descrip: Searches files whose names are specified at stdin for any occurence of bytes in file named argv[1]. Prints names of matching files to stdout, and offset where text was found to stderr. Usage : SEARCH @ | [-] [[-] []] Where is file containing bytes to be searched for, or indicates literal text to be searched for. Note: If first character of text is '@', pass text as "@text", SWITCH MEANING c case-insensitive search O suppress displaying of offsets M suppress displaying of messages W suppress displaying of warnings */ #include #include #include #include #include #include int MSG=1, WARN=1, OFFSET=1; long int LEN; /* Length of text to be searched for */ char *TEXT=NULL; /* Text to search for (will be allocated later) */ char *FBUF=NULL; /* Buffer for files to be searched (will be allocated later) */ int ignorecase=0; long int OFS=0, LENGTH=-1; /* Offset and # bytes (from offset) to search for TEXT */ /* (-1 indicates not specified, so default to EOF */ char FROMEND=0; /* Whether OFS is from EOF (0=BOF, 1=EOF) */ void credits(void) { static int yet=0; if (!yet++ && MSG) fprintf( stderr, "SEARCH v1.0 (4/12/89) Copyright (c) 1989 Kim Moser All Rights Reserved\n" ); } void usage(void) { credits(); if (!MSG) return; fprintf( stderr, " Searches files whose names are specified at stdin for any occurence\n" ); fprintf( stderr, " occurence of bytes in file named argv[1]. Prints names of matching files\n" ); fprintf( stderr, " to stdout, and offset where text was found to stderr.\n\n" ); fprintf( stderr, " Usage: SEARCH @ | [[-]] [[-] []]\n" ); fprintf( stderr, " where is file containing bytes for which to search, or\n" ); fprintf( stderr, " indicates literal text for which to be searched.\n\n" ); fprintf( stderr, " SWITCH MEANING\n" ); fprintf( stderr, " c case-insensitive search\n" ); fprintf( stderr, " O suppress offsets\n" ); fprintf( stderr, " M suppress messages\n" ); fprintf( stderr, " W suppress warnings\n" ); exit(1); } void dofile(f) char *f; /* Searches file 'f' for TEXT of length LEN */ { FILE *fp; long int flen; long int sofar=0; int result; int nameyet=0; /* Whether filename was printed yet */ long int i; char *spot; unsigned long int shift; /* # bytes to shift (0=none) */ long int adj; /* Adjusted (from BOF) OFS (note: will be == OFS if !FROMEND */ long int run; /* Run length adjusted for OFS, FROMEND, and flen */ /* fprintf( stderr, "Searching for: '" ); for (i=0; i=flen)||*/ (runflen))|| FROMEND&&( (OFS>=flen)||(runflen)) ) { if (MSG) fprintf( stderr, " (too short to search)\n" ); fclose(fp); return; /* There might be more files to process */ } if (MSG) { fprintf( stderr, " (ofs=%s%ld", (FROMEND?"-":""), OFS ); if (FROMEND) fprintf( stderr, " (%ld from beginning)", adj ); fprintf( stderr, ", run=%ld)\n", run ); } if ( fseek(fp,adj,SEEK_SET) ) { if (WARN) fprintf( stderr, "Unable to set file pointer for file '%s' to %ld.\n", f, adj ); fclose(fp); return; /* There might be more files to process */ } /* Fill 'FBUF' with first 'LEN' bytes from 'fp': */ for (i=0; i=run) { /* We're done before we have to read any more bytes */ break; } */ for (i=LEN-shift; i | [-] [[-] []] 0 1 2 2/3 3/4 */ if (argc<2) usage(); for (i=2; i