diff --git a/base/applications/findstr/findstr.c b/base/applications/findstr/findstr.c index 0e726351bb..bcb4cc0593 100644 --- a/base/applications/findstr/findstr.c +++ b/base/applications/findstr/findstr.c @@ -49,8 +49,9 @@ */ int find_str (char *sz, FILE *p, int invert_search, - int count_lines, int number_output, int ignore_case, int at_start, int literal_search, - int at_end, int reg_express, int exact_match, int sub_dirs, int only_fname) + int number_output, int ignore_case, int at_start, int literal_search, + int at_end, int reg_express, int exact_match, int sub_dirs, + int only_fname, char *fn) { int i, length; long line_number = 0, total_lines = 0; @@ -95,25 +96,27 @@ find_str (char *sz, FILE *p, int invert_search, if ( ((invert_search) ? (c == NULL) : (c != NULL)) ) { - if (!count_lines) + if ((at_start) && (c != temp_str)) continue; + if ((at_end) && (strcmp(sz, temp_str + length - strlen(sz) - 1) != 0)) continue; + if ((exact_match) && ((c != temp_str) || (strlen(sz) != strlen(temp_str)))) continue; { - if (number_output) + if (fn) printf ("%s:", fn); + if (only_fname == 0) + { + if (number_output) printf ("%ld:", line_number); - /* Print the line of text */ - puts (this_line); + /* Print the line of text */ + puts (this_line); + } + else + if (fn) printf("\n"); } 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. @@ -137,12 +140,12 @@ usage (void) int main (int argc, char **argv) { - char *opt, *needle = NULL; + char *opt, *pch = NULL, *needle = NULL; int ret = 0; TCHAR lpMessage[4096]; + char *colon = ":"; 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 */ int at_start = 0; /* flag to Match if at the beginning of a line. */ @@ -169,10 +172,10 @@ main (int argc, char **argv) at_start = 1; break; - //case 'c': - //case 'C': /* Literal? */ - // literal_search = 1; - // break; + case 'c': + case 'C': /* Literal? */ + literal_search = 1; + break; case 'e': case 'E': /* matches pattern if at end of line */ @@ -229,8 +232,20 @@ main (int argc, char **argv) needle = *argv; } } + + if (literal_search == 1) + /* If literal seach then find the colon */ + pch = strstr(*argv, colon); + + if (pch) + { + /* If it was found then advance past it and set the needle here */ + pch++; + if (pch) needle = pch; + } } + /* Check for search string */ if (needle == NULL) { @@ -242,9 +257,9 @@ main (int argc, char **argv) /* Scan the files for the string */ if (argc == 0) { - ret = find_str (needle, stdin, invert_search, count_lines, - number_output, ignore_case, at_start, literal_search, at_end, reg_express, exact_match, - sub_dirs, only_fname); + ret = find_str (needle, stdin, invert_search, number_output, + ignore_case, at_start, literal_search, at_end, reg_express, exact_match, + sub_dirs, only_fname, NULL); } while (--argc >= 0) @@ -264,13 +279,12 @@ main (int argc, char **argv) do { /* We have found a file, so try to open it */ - if ((pfile = fopen (finddata.name, "r")) != NULL) + 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, at_start, literal_search, at_end, reg_express, exact_match, - sub_dirs, only_fname); - fclose (pfile); + ret = find_str (needle, pfile, invert_search, number_output, + ignore_case, at_start, literal_search, at_end, reg_express, exact_match, + sub_dirs, only_fname, finddata.name) || ret; + fclose (pfile); } else { @@ -280,14 +294,13 @@ main (int argc, char **argv) finddata.name); } } - while (_findnext(hfind, &finddata) > 0); + 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) ); } diff --git a/base/applications/findstr/lang/en-US.rc b/base/applications/findstr/lang/en-US.rc index 93ffd529b3..c99f1dbede 100644 --- a/base/applications/findstr/lang/en-US.rc +++ b/base/applications/findstr/lang/en-US.rc @@ -2,12 +2,17 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US STRINGTABLE BEGIN - IDS_USAGE "FINDSTR: Prints all lines of a file that contain a string.\n\n\ - FIND [ /C ] [ /I ] [ /N ] [ /V ] ""string"" [ file... ]\n\ - /C Count the number of lines that contain string\n\ - /I Ignore case\n\ - /N Number the displayed lines, starting at 1\n\ - /V Print lines that do not contain the string" + IDS_USAGE "Searches for lines of a file that contain a string.\n\n\ +FINDSTR [/B] [E] [/I] [/M] [/N] [/V] [/X]\n\ + [/C:string] ""string"" [filespec...]\n\ + /B Matches pattern at beginning of line\n\ + /C:string Does a literal search for this string\n\ + /E Matches pattern at end of line\n\ + /I Ignore case\n\ + /M Displays filename it if contains a match\n\ + /N Number the displayed lines, starting at 1\n\ + /V Print lines that do not contain the string\n\ + /X Print only exact matching lines" IDS_NO_SUCH_FILE "FINDSTR: %s: No such file\n" IDS_CANNOT_OPEN "FINDSTR: %s: Cannot open file\n" END