3 /* Copyright (C) 1994-2002, Jim Hall <jhall@freedos.org> */
5 /* Adapted for ReactOS -Edited for Findstr.exe K'Williams */
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 along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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.
40 /* Symbol definition */
44 /* This function prints out all lines containing a substring. There are some
45 * conditions that may be passed to the function.
47 * RETURN: If the string was found at least once, returns 1.
48 * If the string was not found at all, returns 0.
51 find_str (char *sz
, FILE *p
, int invert_search
,
52 int count_lines
, int number_output
, int ignore_case
, int at_start
, int literal_search
,
53 int at_end
, int reg_express
, int exact_match
, int sub_dirs
, int only_fname
)
56 long line_number
= 0, total_lines
= 0;
57 char *c
, temp_str
[MAX_STR
], this_line
[MAX_STR
];
59 /* Convert to upper if needed */
63 for (i
= 0; i
< length
; i
++)
64 sz
[i
] = toupper (sz
[i
]);
67 /* Scan the file until EOF */
68 while (fgets (temp_str
, MAX_STR
, p
) != NULL
)
70 /* Remove the trailing newline */
71 length
= strlen (temp_str
);
72 if (temp_str
[length
-1] == '\n')
74 temp_str
[length
-1] = '\0';
77 /* Increment number of lines */
79 strcpy (this_line
, temp_str
);
81 /* Convert to upper if needed */
84 for (i
= 0; i
< length
; i
++)
86 temp_str
[i
] = toupper (temp_str
[i
]);
90 /* Locate the substring */
92 /* strstr() returns a pointer to the first occurrence in the
93 string of the substring */
94 c
= strstr (temp_str
, sz
);
96 if ( ((invert_search
) ? (c
== NULL
) : (c
!= NULL
)) )
101 printf ("%ld:", line_number
);
103 /* Print the line of text */
113 /* Just show num. lines that contain the string */
114 printf ("%ld\n", total_lines
);
118 /* RETURN: If the string was found at least once, returns 1.
119 * If the string was not found at all, returns 0.
121 return (total_lines
> 0 ? 1 : 0);
130 LoadString( GetModuleHandle(NULL
), IDS_USAGE
, (LPTSTR
)lpUsage
, 4096);
131 CharToOem(lpUsage
, lpUsage
);
138 main (int argc
, char **argv
)
140 char *opt
, *needle
= NULL
;
142 TCHAR lpMessage
[4096];
144 int invert_search
= 0; /* flag to invert the search */
145 int count_lines
= 0; /* flag to whether/not count lines */
146 int number_output
= 0; /* flag to print line numbers */
147 int ignore_case
= 0; /* flag to be case insensitive */
148 int at_start
= 0; /* flag to Match if at the beginning of a line. */
149 int at_end
= 0; /* flag to Match if at the beginning of a line. */
150 int reg_express
= 0; /* flag to use/not use regular expressions */
151 int exact_match
= 0; /* flag to be exact match */
152 int sub_dirs
= 0; /* this and all subdirectories */
153 int only_fname
= 0; /* print only the name of the file*/
154 int literal_search
=0;
156 FILE *pfile
; /* file pointer */
157 int hfind
; /* search handle */
158 struct _finddata_t finddata
; /* _findfirst, filenext block */
160 /* Scan the command line */
161 while ((--argc
) && (needle
== NULL
))
163 if (*(opt
= *++argv
) == '/')
168 case 'B': /* Matches pattern if at the beginning of a line */
173 //case 'C': /* Literal? */
174 // literal_search = 1;
178 case 'E': /* matches pattern if at end of line */
183 case 'I': /* Ignore */
188 case 'M': /* only filename */
193 case 'N': /* Number */
198 case 'R': /* search strings as regular expressions */
203 case 'S': /* search files in child directory too*/
208 case 'V': /* Not with */
213 case 'X': /* exact match */
219 exit (2); /* syntax error .. return error 2 */
228 /* Assign the string to find */
234 /* Check for search string */
242 /* Scan the files for the string */
245 ret
= find_str (needle
, stdin
, invert_search
, count_lines
,
246 number_output
, ignore_case
, at_start
, literal_search
, at_end
, reg_express
, exact_match
,
247 sub_dirs
, only_fname
);
252 hfind
= _findfirst (*++argv
, &finddata
);
255 /* We were not able to find a file. Display a message and
256 set the exit status. */
257 LoadString( GetModuleHandle(NULL
), IDS_NO_SUCH_FILE
, (LPTSTR
)lpMessage
, 4096);
258 CharToOem(lpMessage
, lpMessage
);
259 fprintf (stderr
, lpMessage
, *argv
);//
263 /* repeat find next file to match the filemask */
266 /* We have found a file, so try to open it */
267 if ((pfile
= fopen (finddata
.name
, "r")) != NULL
)
269 printf ("---------------- %s\n", finddata
.name
);
270 ret
= find_str (needle
, pfile
, invert_search
, count_lines
,
271 number_output
, ignore_case
, at_start
, literal_search
, at_end
, reg_express
, exact_match
,
272 sub_dirs
, only_fname
);
277 LoadString(GetModuleHandle(NULL
), IDS_CANNOT_OPEN
, (LPTSTR
)lpMessage
, 4096);
278 CharToOem(lpMessage
, lpMessage
);
279 fprintf (stderr
, lpMessage
,
283 while (_findnext(hfind
, &finddata
) > 0);
286 } /* for each argv */
288 /* RETURN: If the string was found at least once, returns 0.
289 * If the string was not found at all, returns 1.
290 * (Note that find_str.c returns the exact opposite values.)
292 exit ( (ret
? 0 : 1) );