home *** CD-ROM | disk | FTP | other *** search
/ Zodiac Super OZ / MEDIADEPOT.ISO / FILES / 16 / FREEDOS.ZIP / FD_A4PRE.ZIP / SOURCE / MICROC.ZIP / FIND.C < prev    next >
C/C++ Source or Header  |  1995-06-21  |  5KB  |  187 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. /************************************************************************
  18.  * This program locates a string in a text file and prints those lines
  19.  * that contain the string.  Multiple files are clearly separated.
  20.  *
  21.  * Author: James Hall
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include "freedos.h"
  26. #include "getopt.c"
  27.  
  28. void ffind (char sz[], FILE * p, int iNot, int iCount, int iNum, int iIgnore);
  29.  
  30. main (int argc, char **argv)
  31. {
  32.  
  33.     int c, i, iNot, iCount, iNum, iIgnore, iQuiet;
  34.     char szBuf[MAX_STR];
  35.     FILE *p;
  36.  
  37.     iNot = FALSE, iCount = FALSE, iNum = FALSE, iIgnore = FALSE;
  38.     iQuiet = FALSE;
  39.  
  40.     /* Scan the command line */
  41.  
  42.     while ((c = getopt (argc, argv, "CINQV?", "")) != EOF)
  43.     {
  44.         switch (c)
  45.         {
  46.         case 'C':        /* Count */
  47.             iCount = TRUE;
  48.             break;
  49.         case 'I':        /* Ignore */
  50.             iIgnore = TRUE;
  51.             break;
  52.         case 'N':        /* Number */
  53.             iNum = TRUE;
  54.             break;
  55.     case 'Q':        /* suppress header lines for each file */
  56.         iQuiet = TRUE;
  57.         break;
  58.         case 'V':        /* Not with */
  59.             iNot = TRUE;
  60.             break;
  61.         default:
  62.             usage ();
  63.             break;
  64.         }
  65.     }
  66.  
  67.     /* Get the string */
  68.  
  69.     if ((c = optind) >= argc)
  70.         usage ();
  71.  
  72.     else
  73.         strcpy (szBuf, argv[c++]);
  74.  
  75.     /* Read the files */
  76.  
  77.     if ((argc - c) == 0)
  78.         ffind (szBuf, stdin, iNot, iCount, iNum, iIgnore);
  79.  
  80.     for (i = c; i < argc; i++)
  81.     {
  82.         if ((p = fopen (argv[i], "r")) != NULL)
  83.         {
  84.         if (!iQuiet)
  85.             printf ("=================== %s\n", argv[i]);
  86.  
  87.         ffind (szBuf, p, iNot, iCount, iNum, iIgnore);
  88.         fclose (p);
  89.         }
  90.  
  91.         else
  92.             fprintf (stderr, "Could not open %s\n", argv[i]);
  93.     }
  94.  
  95.     exit (0);
  96. }
  97.  
  98.  
  99. void 
  100. usage (void)
  101. {
  102.     printp ("FIND", "Prints all lines of a file that contain a string.");
  103.     printc ("1995", "James Hall");
  104.     printu ("FIND", "[/C] [/I] [/N] [/V] \"string\" [file..]");
  105.     printo ("C", "Count the number of occurrences of the string");
  106.     printo ("I", "Ignore case");
  107.     printo ("N", "Number the displayed lines, starting with 1");
  108.     printo ("V", "Just print the lines that don\'t contain the string");
  109.     exit (1);
  110. }
  111.  
  112. /*****************************************************************************
  113.  * This function prints out all lines containing a substring.  There are some
  114.  * conditions that may be passed to the function.
  115.  *
  116.  * Author: James Hall
  117.  */
  118.  
  119. void ffind (char sz[], FILE * p, int iNot, int iCount, int iNum, int iIgnore)
  120.  
  121. {
  122.     int i, iLen, lLine, lTotal;
  123.     char *c, szTemp[MAX_STR], szString[MAX_STR];
  124.  
  125.  
  126.     lLine = 0, lTotal = 0;
  127.  
  128.     /* Convert to upper if needed */
  129.  
  130.     if (iIgnore)
  131.     {
  132.         iLen = strlen (sz);
  133.         for (i = 0; i < iLen; i++)
  134.             sz[i] = toupper (sz[i]);
  135.     }
  136.  
  137.     /* Scan the file until EOF */
  138.  
  139.     while (fgets (szTemp, MAX_STR, p) != NULL)
  140.     {
  141.  
  142.         /* Remove the trailing newline */
  143.  
  144.         iLen = strlen (szTemp);
  145.         if (szTemp[iLen - 1] == '\n')
  146.             szTemp[iLen - 1] = '\0';
  147.  
  148.         /* Increment number of lines */
  149.  
  150.         lLine++;
  151.         strcpy (szString, szTemp);
  152.  
  153.         /* Convert to upper if needed */
  154.  
  155.         if (iIgnore)
  156.             for (i = 0; i < iLen; i++)
  157.                 szTemp[i] = toupper (szTemp[i]);
  158.  
  159.         /* Locate the substring */
  160.         /* strstr() returns a pointer to the first occurrence in the
  161.                string of the substring */
  162.  
  163.         if ((((c = strstr (szTemp, sz)) != NULL) &&
  164.             (!iNot)) || ((c == NULL) && (iNot)))
  165.         {
  166.  
  167.             if (!iCount)
  168.             {
  169.                 if (iNum)
  170.                     printf ("%d:", lLine);
  171.  
  172.                 /* Print the line of text */
  173.  
  174.                 puts (szString);
  175.             }
  176.  
  177.             else
  178.                 lTotal++;
  179.         }
  180.     }
  181.  
  182.     if (iCount)
  183.         printf ("Number of lines: %d\n", lTotal);
  184.  
  185.     return;
  186. }
  187.