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 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
)
55 long line_number
= 0, total_lines
= 0;
56 char *c
, temp_str
[MAX_STR
], this_line
[MAX_STR
];
58 /* Convert to upper if needed */
62 for (i
= 0; i
< length
; i
++)
63 sz
[i
] = toupper (sz
[i
]);
66 /* Scan the file until EOF */
67 while (fgets (temp_str
, MAX_STR
, p
) != NULL
)
69 /* Remove the trailing newline */
70 length
= strlen (temp_str
);
71 if (temp_str
[length
-1] == '\n')
73 temp_str
[length
-1] = '\0';
76 /* Increment number of lines */
78 strcpy (this_line
, temp_str
);
80 /* Convert to upper if needed */
83 for (i
= 0; i
< length
; i
++)
85 temp_str
[i
] = toupper (temp_str
[i
]);
89 /* Locate the substring */
91 /* strstr() returns a pointer to the first occurrence in the
92 string of the substring */
93 c
= strstr (temp_str
, sz
);
95 if ( ((invert_search
) ? (c
== NULL
) : (c
!= NULL
)) )
100 printf ("%ld:", line_number
);
102 /* Print the line of text */
112 /* Just show num. lines that contain the string */
113 printf ("%ld\n", total_lines
);
117 /* RETURN: If the string was found at least once, returns 1.
118 * If the string was not found at all, returns 0.
120 return (total_lines
> 0 ? 1 : 0);
129 LoadString( GetModuleHandle(NULL
), IDS_USAGE
, (LPTSTR
)lpUsage
, 4096);
130 CharToOem(lpUsage
, lpUsage
);
137 main (int argc
, char **argv
)
139 char *opt
, *needle
= NULL
;
141 TCHAR lpMessage
[4096];
143 int invert_search
= 0; /* flag to invert the search */
144 int count_lines
= 0; /* flag to whether/not count lines */
145 int number_output
= 0; /* flag to print line numbers */
146 int ignore_case
= 0; /* flag to be case insensitive */
148 FILE *pfile
; /* file pointer */
149 int hfind
; /* search handle */
150 struct _finddata_t finddata
; /* _findfirst, filenext block */
152 /* Scan the command line */
153 while ((--argc
) && (needle
== NULL
))
155 if (*(opt
= *++argv
) == '/')
160 case 'C': /* Count */
165 case 'I': /* Ignore */
170 case 'N': /* Number */
175 case 'V': /* Not with */
181 exit (2); /* syntax error .. return error 2 */
190 /* Assign the string to find */
196 /* Check for search string */
204 /* Scan the files for the string */
207 ret
= find_str (needle
, stdin
, invert_search
, count_lines
,
208 number_output
, ignore_case
);
213 hfind
= _findfirst (*++argv
, &finddata
);
216 /* We were not able to find a file. Display a message and
217 set the exit status. */
218 LoadString( GetModuleHandle(NULL
), IDS_NO_SUCH_FILE
, (LPTSTR
)lpMessage
, 4096);
219 CharToOem(lpMessage
, lpMessage
);
220 fprintf (stderr
, lpMessage
, *argv
);//
224 /* repeat find next file to match the filemask */
227 /* We have found a file, so try to open it */
228 if ((pfile
= fopen (finddata
.name
, "r")) != NULL
)
230 printf ("---------------- %s\n", finddata
.name
);
231 ret
= find_str (needle
, pfile
, invert_search
, count_lines
,
232 number_output
, ignore_case
);
237 LoadString(GetModuleHandle(NULL
), IDS_CANNOT_OPEN
, (LPTSTR
)lpMessage
, 4096);
238 CharToOem(lpMessage
, lpMessage
);
239 fprintf (stderr
, lpMessage
,
243 while (_findnext(hfind
, &finddata
) > 0);
246 } /* for each argv */
248 /* RETURN: If the string was found at least once, returns 0.
249 * If the string was not found at all, returns 1.
250 * (Note that find_str.c returns the exact opposite values.)
252 exit ( (ret
? 0 : 1) );