3 /* Copyright (C) 1994-2002, Jim Hall <jhall@freedos.org> */
5 /* Adapted for ReactOS */
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* This program locates a string in a text file and prints those lines
25 * that contain the string. Multiple files are clearly separated.
37 /* Symbol definition */
41 /* This function prints out all lines containing a substring. There are some
42 * conditions that may be passed to the function.
44 * RETURN: If the string was found at least once, returns 1.
45 * If the string was not found at all, returns 0.
48 find_str (char *sz
, FILE *p
, int invert_search
,
49 int count_lines
, int number_output
, int ignore_case
)
52 long line_number
= 0, total_lines
= 0;
53 char *c
, temp_str
[MAX_STR
], this_line
[MAX_STR
];
55 /* Convert to upper if needed */
59 for (i
= 0; i
< length
; i
++)
60 sz
[i
] = toupper (sz
[i
]);
63 /* Scan the file until EOF */
64 while (fgets (temp_str
, MAX_STR
, p
) != NULL
)
66 /* Remove the trailing newline */
67 length
= strlen (temp_str
);
68 if (temp_str
[length
-1] == '\n')
70 temp_str
[length
-1] = '\0';
73 /* Increment number of lines */
75 strcpy (this_line
, temp_str
);
77 /* Convert to upper if needed */
80 for (i
= 0; i
< length
; i
++)
82 temp_str
[i
] = toupper (temp_str
[i
]);
86 /* Locate the substring */
88 /* strstr() returns a pointer to the first occurrence in the
89 string of the substring */
90 c
= strstr (temp_str
, sz
);
92 if ( ((invert_search
) ? (c
== NULL
) : (c
!= NULL
)) )
97 printf ("%ld:", line_number
);
99 /* Print the line of text */
109 /* Just show num. lines that contain the string */
110 printf ("%ld\n", total_lines
);
114 /* RETURN: If the string was found at least once, returns 1.
115 * If the string was not found at all, returns 0.
117 return (total_lines
> 0 ? 1 : 0);
125 fprintf (stderr
, "FIND: Prints all lines of a file that contain a string\n");
126 fprintf (stderr
, "FIND [ /C ] [ /I ] [ /N ] [ /V ] \"string\" [ file... ]\n");
127 fprintf (stderr
, " /C Count the number of lines that contain string\n");
128 fprintf (stderr
, " /I Ignore case\n");
129 fprintf (stderr
, " /N Number the displayed lines, starting at 1\n");
130 fprintf (stderr
, " /V Print lines that do not contain the string\n");
136 main (int argc
, char **argv
)
138 char *opt
, *needle
= NULL
;
141 int invert_search
= 0; /* flag to invert the search */
142 int count_lines
= 0; /* flag to whether/not count lines */
143 int number_output
= 0; /* flag to print line numbers */
144 int ignore_case
= 0; /* flag to be case insensitive */
146 FILE *pfile
; /* file pointer */
147 int hfind
; /* search handle */
148 struct _finddata_t finddata
; /* _findfirst, filenext block */
150 /* Scan the command line */
151 while ((--argc
) && (needle
== NULL
))
153 if (*(opt
= *++argv
) == '/')
158 case 'C': /* Count */
163 case 'I': /* Ignore */
168 case 'N': /* Number */
173 case 'V': /* Not with */
179 exit (2); /* syntax error .. return error 2 */
188 /* Assign the string to find */
194 /* Check for search string */
202 /* Scan the files for the string */
205 ret
= find_str (needle
, stdin
, invert_search
, count_lines
,
206 number_output
, ignore_case
);
211 hfind
= _findfirst (*++argv
, &finddata
);
214 /* We were not able to find a file. Display a message and
215 set the exit status. */
216 fprintf (stderr
, "FIND: %s: No such file\n", *argv
);
220 /* repeat find next file to match the filemask */
223 /* We have found a file, so try to open it */
224 if ((pfile
= fopen (finddata
.name
, "r")) != NULL
)
226 printf ("---------------- %s\n", finddata
.name
);
227 ret
= find_str (needle
, pfile
, invert_search
, count_lines
,
228 number_output
, ignore_case
);
233 fprintf (stderr
, "FIND: %s: Cannot open file\n",
237 while (_findnext(hfind
, &finddata
) > 0);
240 } /* for each argv */
242 /* RETURN: If the string was found at least once, returns 0.
243 * If the string was not found at all, returns 1.
244 * (Note that find_str.c returns the exact opposite values.)
246 exit ( (ret
? 0 : 1) );