home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / BDSC / BDSC-3 / FS.C < prev    next >
Text File  |  2000-06-30  |  2KB  |  106 lines

  1. /*--------------------------------------
  2.  *
  3.  *    fs  - find string
  4.  *
  5.  *    Usage:    fs str files ....
  6.  *
  7.  *    Description:
  8.  *        Finds the string "str" in the 
  9.  *    named files.  Ignores cases.
  10.  *    Gives line numbers "str" is found on.  
  11.  *    Uses directed io, so one can use:
  12.  *    "fs str1 file |fs str2".
  13.  *
  14.  *    Author:    Dan Sunday
  15.  *    Date:    11-1-81
  16.  *--------------------------------------*/
  17.  
  18. #include "a:bdscio.h"
  19. #include "b:dio.h"
  20.  
  21. FILE    *infile;
  22. char    inbuf [BUFSIZ],
  23.     line [128];
  24.  
  25.  
  26. main (argc,argv)    /* fs string file1 file2 ... */
  27. char **argv;
  28. begin
  29.     int i,k,n,f;
  30.     char *ap,**p,*pat;
  31.  
  32.     dioinit(&argc,argv);
  33.     infile = inbuf;
  34.  
  35.     if (argc < 2)
  36.     begin
  37.         fprintf (4,"Usage: 'fs string file1 [file2 ...]'");
  38.         dioflush();
  39.         exit (ERROR);
  40.     end
  41.  
  42.     p = argv + 1;
  43.     pat = *p++;
  44.     f = 1;
  45.  
  46.     if (argc==2)
  47.     begin
  48.         infile = 0;
  49.         ap = "\0";
  50.         getchar(); ungetch();
  51.         goto StdIn;
  52.     end
  53.  
  54.     while (++f < argc)
  55.     begin
  56.         ap = *p++;
  57.         if (fopen (ap,infile) == ERROR)
  58.         begin
  59.             printf ("can't open %s\n",ap);
  60.             dioflush();
  61.             exit (ERROR);
  62.         end
  63. StdIn:
  64.         for (i=1;;++i)
  65.         begin
  66.             if (fgets (line,infile) == NULL)
  67.                 break;
  68.             if (substr (pat,line))
  69.             begin
  70.                 putchar('!');
  71.                 printf ("%s :%d: %s",
  72.                     ap, i, line);
  73.             end
  74.         end
  75.     end
  76.  
  77.     dioflush();
  78.     exit (OK);
  79. end
  80.  
  81.  
  82. substr (pat,str)
  83. char *pat,*str;
  84. begin
  85.     int i,j,k,l;
  86.     char *p,*q;
  87.     i = 0;
  88.     k = strlen (pat);
  89.     l = strlen (str);
  90.  
  91.     while (i+k < l)
  92.     begin
  93.         p = pat + k;
  94.         q = str + i + k;
  95.         j = k;
  96.  
  97.         while (j--)
  98.             if (*--p != toupper(*--q))
  99.                 break;
  100.             else if (j==0) return TRUE;
  101.         ++i;
  102.     end
  103.  
  104.     return FALSE;
  105. end
  106.