home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / dos_ency / 14 / find.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  2.6 KB  |  67 lines

  1. /*
  2.         FIND.C          Searches text stream for a string.
  3.  
  4.         Usage:          FIND "pattern" [< source] [> destination]
  5.  
  6.         by Ray Duncan, June 1987
  7.  
  8. */
  9.  
  10. #include <stdio.h>
  11.  
  12. #define TAB     '\x09'                  /* ASCII tab character (^I) */
  13. #define BLANK   '\x20'                  /* ASCII space character */
  14.  
  15. #define TAB_WIDTH 8                     /* columns per tab stop */
  16.  
  17. static char input[256];                 /* buffer for line from input */
  18. static char output[256];                /* buffer for line to output */
  19. static char pattern[256];               /* buffer for search pattern */
  20.  
  21. main(argc,argv)
  22. int argc;
  23. char *argv[];
  24. {       int line=0;                     /* initialize line variable */
  25.  
  26.         if ( argc < 2 )                 /* was search pattern supplied? */
  27.         {       puts("find: missing pattern.");
  28.                 exit(1);                /* abort if not */
  29.         }
  30.         strcpy(pattern,argv[1]);        /* save copy of string to find */
  31.         strupr(pattern);                /* fold it to uppercase */      
  32.         while( gets(input) != NULL )    /* read a line from input */
  33.         {       line++;                 /* count lines */
  34.                 strcpy(output,input);   /* save copy of input string */
  35.                 strupr(input);          /* fold input to uppercase */
  36.                                         /* if line contains pattern */
  37.                 if( strstr(input,pattern) )
  38.                                         /* write it to standard output */
  39.                         writeline(line,output);
  40.         }
  41.         exit(0);                        /* terminate at end of file */
  42. }
  43.  
  44. /*
  45.         WRITELINE: Write line number and text to standard output,
  46.         expanding any tab characters to stops defined by TAB_WIDTH.
  47. */
  48.  
  49. writeline(line,p)
  50. int line;
  51. char *p;
  52. {       int i=0;                        /* index to original line text */
  53.         int col=0;                      /* actual output column counter */
  54.         printf("\n%4d: ",line);         /* write line number */
  55.         while( p[i]!=NULL )             /* while end of line not reached */
  56.         {       if(p[i]==TAB)           /* if current char = tab, expand it */
  57.                 {       do putchar(BLANK);
  58.                         while((++col % TAB_WIDTH) != 0);
  59.                 }
  60.                 else                    /* otherwise just send character */
  61.                 {       putchar(p[i]);
  62.                         col++;          /* count columns */
  63.                 }
  64.                 i++;                    /* advance through output line */
  65.         }
  66. }
  67.