/
find.c
256 lines (216 loc) · 6.28 KB
/
find.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* find.c */
/* Copyright (C) 1994-2002, Jim Hall <jhall@freedos.org> */
/* Adapted for ReactOS */
/*
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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* This program locates a string in a text file and prints those lines
* that contain the string. Multiple files are clearly separated.
*/
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
//#include <ctype.h>
#include <windef.h>
#include <winbase.h>
#include <winuser.h>
//#include <io.h>
#include <dos.h>
#include "resource.h"
/* Symbol definition */
#define MAX_STR 1024
/* This function prints out all lines containing a substring. There are some
* conditions that may be passed to the function.
*
* RETURN: If the string was found at least once, returns 1.
* If the string was not found at all, returns 0.
*/
int
find_str (char *sz, FILE *p, int invert_search,
int count_lines, int number_output, int ignore_case)
{
int i, length;
long line_number = 0, total_lines = 0;
char *c, temp_str[MAX_STR], this_line[MAX_STR];
/* Convert to upper if needed */
if (ignore_case)
{
length = strlen (sz);
for (i = 0; i < length; i++)
sz[i] = toupper (sz[i]);
}
/* Scan the file until EOF */
while (fgets (temp_str, MAX_STR, p) != NULL)
{
/* Remove the trailing newline */
length = strlen (temp_str);
if (temp_str[length-1] == '\n')
{
temp_str[length-1] = '\0';
}
/* Increment number of lines */
line_number++;
strcpy (this_line, temp_str);
/* Convert to upper if needed */
if (ignore_case)
{
for (i = 0; i < length; i++)
{
temp_str[i] = toupper (temp_str[i]);
}
}
/* Locate the substring */
/* strstr() returns a pointer to the first occurrence in the
string of the substring */
c = strstr (temp_str, sz);
if ( ((invert_search) ? (c == NULL) : (c != NULL)) )
{
if (!count_lines)
{
if (number_output)
printf ("%ld:", line_number);
/* Print the line of text */
puts (this_line);
}
total_lines++;
} /* long if */
} /* while fgets */
if (count_lines)
{
/* Just show num. lines that contain the string */
printf ("%ld\n", total_lines);
}
/* RETURN: If the string was found at least once, returns 1.
* If the string was not found at all, returns 0.
*/
return (total_lines > 0 ? 1 : 0);
}
/* Show usage */
void
usage (void)
{
WCHAR wszUsage[4096];
char oemUsage[4096];
LoadStringW (GetModuleHandleW (NULL), IDS_USAGE, wszUsage, sizeof(wszUsage) / sizeof(wszUsage[0]));
CharToOemW (wszUsage, oemUsage);
fputs (oemUsage, stdout);
}
/* Main program */
int
main (int argc, char **argv)
{
char *opt, *needle = NULL;
int ret = 0;
WCHAR wszMessage[4096];
char oemMessage[4096];
int invert_search = 0; /* flag to invert the search */
int count_lines = 0; /* flag to whether/not count lines */
int number_output = 0; /* flag to print line numbers */
int ignore_case = 0; /* flag to be case insensitive */
FILE *pfile; /* file pointer */
int hfind; /* search handle */
struct _finddata_t finddata; /* _findfirst, filenext block */
/* Scan the command line */
while ((--argc) && (needle == NULL))
{
if (*(opt = *++argv) == '/')
{
switch (opt[1])
{
case 'c':
case 'C': /* Count */
count_lines = 1;
break;
case 'i':
case 'I': /* Ignore */
ignore_case = 1;
break;
case 'n':
case 'N': /* Number */
number_output = 1;
break;
case 'v':
case 'V': /* Not with */
invert_search = 1;
break;
default:
usage ();
exit (2); /* syntax error .. return error 2 */
break;
}
}
else
{
/* Get the string */
if (needle == NULL)
{
/* Assign the string to find */
needle = *argv;
}
}
}
/* Check for search string */
if (needle == NULL)
{
/* No string? */
usage ();
exit (1);
}
/* Scan the files for the string */
if (argc == 0)
{
ret = find_str (needle, stdin, invert_search, count_lines,
number_output, ignore_case);
}
while (--argc >= 0)
{
hfind = _findfirst (*++argv, &finddata);
if (hfind < 0)
{
/* We were not able to find a file. Display a message and
set the exit status. */
LoadStringW (GetModuleHandleW (NULL), IDS_NO_SUCH_FILE, wszMessage, sizeof(wszMessage) / sizeof(wszMessage[0]));
CharToOemW (wszMessage, oemMessage);
fprintf (stderr, oemMessage, *argv);
}
else
{
/* repeat find next file to match the filemask */
do
{
/* We have found a file, so try to open it */
if ((pfile = fopen (finddata.name, "r")) != NULL)
{
printf ("---------------- %s\n", finddata.name);
ret = find_str (needle, pfile, invert_search, count_lines,
number_output, ignore_case);
fclose (pfile);
}
else
{
LoadStringW (GetModuleHandleW (NULL), IDS_CANNOT_OPEN, wszMessage, sizeof(wszMessage) / sizeof(wszMessage[0]));
CharToOemW (wszMessage, oemMessage);
fprintf (stderr, oemMessage,
finddata.name);
}
}
while (_findnext(hfind, &finddata) > 0);
}
_findclose(hfind);
} /* for each argv */
/* RETURN: If the string was found at least once, returns 0.
* If the string was not found at all, returns 1.
* (Note that find_str.c returns the exact opposite values.)
*/
exit ( (ret ? 0 : 1) );
}