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 >
Wrap
C/C++ Source or Header
|
1995-06-21
|
5KB
|
187 lines
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/************************************************************************
* This program locates a string in a text file and prints those lines
* that contain the string. Multiple files are clearly separated.
*
* Author: James Hall
*/
#include <stdio.h>
#include "freedos.h"
#include "getopt.c"
void ffind (char sz[], FILE * p, int iNot, int iCount, int iNum, int iIgnore);
main (int argc, char **argv)
{
int c, i, iNot, iCount, iNum, iIgnore, iQuiet;
char szBuf[MAX_STR];
FILE *p;
iNot = FALSE, iCount = FALSE, iNum = FALSE, iIgnore = FALSE;
iQuiet = FALSE;
/* Scan the command line */
while ((c = getopt (argc, argv, "CINQV?", "")) != EOF)
{
switch (c)
{
case 'C': /* Count */
iCount = TRUE;
break;
case 'I': /* Ignore */
iIgnore = TRUE;
break;
case 'N': /* Number */
iNum = TRUE;
break;
case 'Q': /* suppress header lines for each file */
iQuiet = TRUE;
break;
case 'V': /* Not with */
iNot = TRUE;
break;
default:
usage ();
break;
}
}
/* Get the string */
if ((c = optind) >= argc)
usage ();
else
strcpy (szBuf, argv[c++]);
/* Read the files */
if ((argc - c) == 0)
ffind (szBuf, stdin, iNot, iCount, iNum, iIgnore);
for (i = c; i < argc; i++)
{
if ((p = fopen (argv[i], "r")) != NULL)
{
if (!iQuiet)
printf ("=================== %s\n", argv[i]);
ffind (szBuf, p, iNot, iCount, iNum, iIgnore);
fclose (p);
}
else
fprintf (stderr, "Could not open %s\n", argv[i]);
}
exit (0);
}
void
usage (void)
{
printp ("FIND", "Prints all lines of a file that contain a string.");
printc ("1995", "James Hall");
printu ("FIND", "[/C] [/I] [/N] [/V] \"string\" [file..]");
printo ("C", "Count the number of occurrences of the string");
printo ("I", "Ignore case");
printo ("N", "Number the displayed lines, starting with 1");
printo ("V", "Just print the lines that don\'t contain the string");
exit (1);
}
/*****************************************************************************
* This function prints out all lines containing a substring. There are some
* conditions that may be passed to the function.
*
* Author: James Hall
*/
void ffind (char sz[], FILE * p, int iNot, int iCount, int iNum, int iIgnore)
{
int i, iLen, lLine, lTotal;
char *c, szTemp[MAX_STR], szString[MAX_STR];
lLine = 0, lTotal = 0;
/* Convert to upper if needed */
if (iIgnore)
{
iLen = strlen (sz);
for (i = 0; i < iLen; i++)
sz[i] = toupper (sz[i]);
}
/* Scan the file until EOF */
while (fgets (szTemp, MAX_STR, p) != NULL)
{
/* Remove the trailing newline */
iLen = strlen (szTemp);
if (szTemp[iLen - 1] == '\n')
szTemp[iLen - 1] = '\0';
/* Increment number of lines */
lLine++;
strcpy (szString, szTemp);
/* Convert to upper if needed */
if (iIgnore)
for (i = 0; i < iLen; i++)
szTemp[i] = toupper (szTemp[i]);
/* Locate the substring */
/* strstr() returns a pointer to the first occurrence in the
string of the substring */
if ((((c = strstr (szTemp, sz)) != NULL) &&
(!iNot)) || ((c == NULL) && (iNot)))
{
if (!iCount)
{
if (iNum)
printf ("%d:", lLine);
/* Print the line of text */
puts (szString);
}
else
lTotal++;
}
}
if (iCount)
printf ("Number of lines: %d\n", lTotal);
return;
}