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