home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c222 / 1.ddi / UTILS / GFIND.C < prev   
Encoding:
C/C++ Source or Header  |  1990-05-24  |  4.4 KB  |  178 lines

  1. /*********************
  2.  *
  3.  *  gfind.c - Find strings in filenames which match wildcard.
  4.  *
  5.  *  Purpose: This program expands the wildcard input to all files and
  6.  *           outputs them to the standard output.
  7.  *
  8.  *       gfind  [string]  [file]  <options>      to find string in file
  9.  *               where string can include ? wildcard character
  10.  *                     file can include ? or * wildcard characters
  11.  *
  12.  *       options:        -n      output line number found
  13.  *                       -c      search case sensitive
  14.  *                       -f      output file name second (for sort)
  15.  *                       -F      output file name first (before string)
  16.  *                       -s      output string name
  17.  *                       -x      suppress output of text line found in
  18.  *
  19.  *       default is:     -n
  20.  *
  21.  *  Blackstar C Function Library
  22.  *  (c) Copyright 1985 Sterling Castle Software
  23.  *
  24.  *******/
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <process.h>
  29. #include "blackstr.h"
  30.  
  31. char *fptr[200], fbuff[8192], temp[MAXLINE];
  32. char *(*search)();    /* pointer to match function */
  33.  
  34. /********
  35.  *
  36.  *   fstrfile(fnam,strptr,opt) - find strings in file
  37.  *
  38.  **/
  39.  
  40. void fstrfile(char *fnam, char *strptr, int opt){
  41.     char buff[139];
  42.     char *bptr;
  43.     int ln,i,lf;
  44.     FILE *fp;
  45.     extern char *(*search)();
  46.  
  47.     lf = i = 0;
  48.     ln = 1;
  49.     strupr(fnam);
  50.     printf("\n ---- %s \n",fnam);
  51.     if ((fp=fopen(fnam,"r")) != 0)
  52.     while (fgets(buff,138,fp)) {
  53.         if ((bptr = (*search)(buff, strptr)) != 0) {
  54.         lf = FALSE;
  55.         if (opt&0x80)
  56.             printf("%s/ ",fnam);
  57.         if (opt&0x02)
  58.             printf("%s/ ",strncpy(temp,bptr,strlen(strptr)));
  59.         if (opt&0x01) 
  60.             printf("%s/ ",fnam);        /* for str first */
  61.         if (opt&0x04)
  62.             printf("%4d/ ",ln);
  63.         if (opt&0x08) {
  64.             printf("%s",buff);
  65.             lf = TRUE;
  66.             }
  67.         if (!lf)
  68.             printf("\n");
  69.             }
  70.         ++ln;
  71.         i=0;
  72.         }
  73.     else
  74.     printf("\n   ERROR  file not found -- %s",fnam);
  75.     fclose(fp);
  76.     }
  77.  
  78.  
  79. /********
  80.  *
  81.  *   get_opts(nopt,optr)
  82.  *
  83.  **/
  84.  
  85. int get_opts(int nopt, char *optr[]){
  86.     int i,opt;
  87.     char *ptr;
  88.     extern char *(*search)();
  89.     extern char *st_instr(),*st_instri();
  90.  
  91.     opt = 0x0c;            /* default to print line number and text line */
  92.     search = st_instri;        /* ignore case is default */
  93.     if(nopt<=3)
  94.     return(12);        /* text and line number */
  95.     for(i=3; i<nopt; ++i) {
  96.     ptr = optr[i];
  97.     switch (ptr[1]) {
  98.       case 'c':
  99.           search = st_instr;
  100.           break;
  101.       case 'l':
  102.       case 'n':
  103.           opt |= 4;       /* line number */
  104.           break;
  105.       case 'f':
  106.           opt |= 1;       /* file name */
  107.           break;
  108.       case 's':
  109.           opt |= 2;       /* string name */
  110.           break;
  111.       case 'x':
  112.           opt &= 0xf7;    /* no text line */
  113.           break;
  114.       case 'F':
  115.           opt |= 0x80;    /* file name first */
  116.           break;
  117.        }
  118.         }
  119.     return(opt);
  120.     }
  121.  
  122.  
  123. /********
  124.  *
  125.  *   main() - This program expands the wildcard input to all files and
  126.  *            outputs them to the standard output.
  127.  **/
  128.  
  129. void main(int argc, char *argv[]){
  130.     int i, fcnt, opt;
  131.     char str[80], *ptr;
  132.  
  133.     if (argc<3) {
  134.     printf("Usage:  gfind <string> <file> [options]\n\n");
  135.     printf("\t<string> may include ? wildcard.\n");
  136.     printf("\t <file>  may include ? or * wildcards.\n\n");
  137.     printf("\tOptions:\n");
  138.     printf("\t-n      output line number found\n");
  139.     printf("\t-c      search case sensitive\n");
  140.     printf("\t-f      output file name second (for sort)\n");
  141.     printf("\t-F      output file name first\n");
  142.     printf("\t-s      output string name\n");
  143.     printf("\t-x      suppress output of text line found in\n\n");
  144.     printf("\tdefault:\n");
  145.     printf("\t-n\n");
  146.     exit(-1);
  147.         }
  148.  
  149.     strcpy(str, argv[1]);
  150.     ptr = argv[2];          /* point to file name */
  151.     if (st_wild(ptr)){
  152.     fcnt = fl_files(fbuff, ptr, TRUE);  /* get wildcard names */
  153.     st_ptrs(fbuff, fptr, fcnt);         /* pointers to fptr */
  154.         }
  155.  
  156.     else {
  157.     fptr[0] = fbuff;             /* put file name in fbuff */
  158.     strcpy (fptr[0], argv[2]);
  159.     fptr[1] = 0;
  160.     fcnt = 1;
  161.         }
  162.  
  163.     /* Get options */
  164.     i = 0;
  165.     opt = get_opts(argc, argv);
  166.  
  167.     /* Now search files */
  168.     while(fptr[i]) {
  169.     fstrfile(fptr[i], str, opt); /* all parms, string first */
  170.     ++i;
  171.         }
  172.  
  173.     if(!fcnt) {
  174.     printf("No files found. ");
  175.     exit(-1);
  176.         }
  177.     }
  178.