home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 21 / CD_ASCQ_21_040595.iso / dos / prg / c / freedos3 / source / jh_utils / ffind.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-07  |  2.2 KB  |  95 lines

  1. /*
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.  
  7.    This program is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.    GNU General Public License for more details.
  11.  
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program; if not, write to the Free Software
  14.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.    */
  16.  
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <string.h>
  20. #include "freedos.h"
  21.  
  22. /*****************************************************************************
  23.  * This function prints out all lines containing a substring.  There are some
  24.  * conditions that may be passed to the function.
  25.  *
  26.  * Author: James Hall
  27.  */
  28.  
  29. void 
  30. ffind (char sz[], FILE * p, int iNot, int iCount, int iNum, int iIgnore)
  31. {
  32.   int i, iLen;
  33.   long lLine = 0, lTotal = 0;
  34.   char *c, szTemp[MAX_STR], szString[MAX_STR];
  35.  
  36.   /* Convert to upper if needed */
  37.  
  38.   if (iIgnore)
  39.     {
  40.       iLen = strlen (sz);
  41.       for (i = 0; i < iLen; i++)
  42.     sz[i] = toupper (sz[i]);
  43.     }
  44.  
  45.   /* Scan the file until EOF */
  46.  
  47.   while (fgets (szTemp, MAX_STR, p) != NULL)
  48.     {
  49.  
  50.       /* Remove the trailing newline */
  51.  
  52.       iLen = strlen (szTemp);
  53.       if (szTemp[iLen - 1] == '\n')
  54.     szTemp[iLen - 1] = '\0';
  55.  
  56.       /* Increment number of lines */
  57.  
  58.       lLine++;
  59.       strcpy (szString, szTemp);
  60.  
  61.       /* Convert to upper if needed */
  62.  
  63.       if (iIgnore)
  64.     for (i = 0; i < iLen; i++)
  65.       szTemp[i] = toupper (szTemp[i]);
  66.  
  67.       /* Locate the substring */
  68.       /* strstr() returns a pointer to the first occurrence in the
  69.        string of the substring */
  70.  
  71.       if ((((c = strstr (szTemp, sz)) != NULL) &&
  72.        (!iNot)) || ((c == NULL) && (iNot)))
  73.     {
  74.  
  75.       if (!iCount)
  76.         {
  77.           if (iNum)
  78.         printf ("%ld:", lLine);
  79.  
  80.           /* Print the line of text */
  81.  
  82.           puts (szString);
  83.         }
  84.  
  85.       else
  86.         lTotal++;
  87.     }
  88.     }
  89.  
  90.   if (iCount)
  91.     printf ("Number of lines: %ld\n", lTotal);
  92.  
  93.   return;
  94. }
  95.