2 * Copyright (c) 2014 by David I. Bell
3 * Permission is granted to use, distribute, or modify this source,
4 * provided that this copyright notice remains intact.
6 * The "grep" built-in command.
15 (const char * string, const char * word, BOOL ignoreCase);
19 do_grep(int argc, const char ** argv)
44 while (*++cp) switch (*cp)
55 fprintf(stderr, "Unknown option\n");
64 tellName = (argc > 1);
70 fp = fopen(name, "r");
82 while (fgets(buf, sizeof(buf), fp))
93 cp = &buf[strlen(buf) - 1];
96 fprintf(stderr, "%s: Line too long\n", name);
98 if (search(buf, word, ignoreCase))
102 printf("%s: ", name);
105 printf("%ld: ", line);
122 * See if the specified word is found in the specified string.
125 search(const char * string, const char * word, BOOL ignoreCase)
140 string = strchr(string, word[0]);
145 if (memcmp(string, word, len) == 0)
153 * Here if we need to check case independence.
154 * Do the search by lower casing both strings.
158 if (isupper(lowFirst))
159 lowFirst = tolower(lowFirst);
163 while (*string && (*string != lowFirst) &&
164 (!isupper(*string) || (tolower(*string) != lowFirst)))