Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int rgrep_matches(char *line, char *pattern) {
- int i = 0;
- while (line[i] != 0)
- {
- if ((line[i] == pattern[i] || pattern[i] == '.' ) && pattern[i] != '+' && pattern[i] != '?' && pattern[i] != '\\')
- {
- char a[MAXSIZE], b[MAXSIZE];
- int k = 0, m = 0, n = 0;
- while (pattern[k] != 0)
- {
- a[k] == pattern[k+1];
- if (a[k] > 0)
- k++;
- }
- while (line[m] != 0)
- {
- b[m] == line[m+1];
- if (b[m] > 0)
- m++;
- }
- n = rgrep_matches(a, b);
- if (n == 0)
- return 0;
- }
- else if (pattern[i+1] == '+' && pattern[i] != '\\')
- {
- int k = 0, l = 0, n = 0;
- while (line[i] != 0)
- {
- char b[MAXSIZE];
- for (k = 0; k < i; k++)
- b[k] = pattern[0];
- while (pattern[l] != 0)
- {
- if (pattern[l] == '+')
- {
- l++;
- continue;
- }
- b[k] = pattern[l];
- k++;
- l++;
- }
- n = rgrep_matches(line, b);
- if (n == 0)
- return 0;
- }
- }
- else if (pattern[i+1] == '?' && pattern[i] != '\\' && pattern[i] != '+' && pattern[i] != 0)
- {
- char a[MAXSIZE], b[MAXSIZE];
- int j = 0, match = -1, match1 = -1;
- if (match != 0)
- {
- while (pattern[j+1] != 0)
- {
- a[j] = pattern[j+1];
- j++;
- }
- match = rgrep_matches(line, a);
- }
- if (match1 != 0)
- {
- while (pattern[j+2] != 0)
- {
- b[j] = pattern[j+2];
- j++;
- }
- match1 = rgrep_matches(line, b);
- }
- if (match == 0 && match1 == 0)
- return 0;
- }
- else if (pattern[i] == '\\')
- {
- char a[MAXSIZE], b[MAXSIZE];
- int j = 0, k = 0, n = 0;
- if (line[i] == pattern[i+1])
- {
- while (line[j] != 0)
- {
- a[j] = line[j+1];
- j++;
- }
- while (pattern[k+2] != 0)
- {
- b[k] = pattern[k+2];
- k++;
- }
- }
- n = rgrep_matches(a, b);
- if ( n == 0)
- return 0;
- }
- }
- return 1;
- }
- int main(int argc, char **argv) {
- if (argc != 2) {
- fprintf(stderr, "Usage: %s <PATTERN>\n", argv[0]);
- return 2;
- }
- /* we're not going to worry about long lines */
- char buf[MAXSIZE];
- while (!feof(stdin) && !ferror(stdin)) {
- if (!fgets(buf, sizeof(buf), stdin)) {
- break;
- }
- if (rgrep_matches(buf, argv[1])) {
- fputs(buf, stdout);
- fflush(stdout);
- }
- }
- if (ferror(stdin)) {
- perror(argv[0]);
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement