#include #include #define MAX_ARGS 1000 int match_strings(const int n, const char * s []) { int idx[MAX_ARGS]; /* chars just matched for each parameter (string) */ int left_to_match; int i, c; left_to_match = 0; for(i = 0; i < n && i < MAX_ARGS; ++i) { if (s[i] == '\0') { idx[i] = -1; } else { idx[i] = 0; ++left_to_match; } } while(left_to_match > 0 && (c = getchar()) != EOF) { for(i = 0; i < n && i < MAX_ARGS; ++i) { if (idx[i] < 0) continue; if (c == s[i][idx[i]]) { idx[i] += 1; } else if (idx[i] > 0) { int j; /* try to backtrack by j positions */ for(j = 1; j < idx[i]; ++j) { if (s[i][idx[i] - j] == c && strncmp(s[i] + j, s[i], idx[i] - j) == 0) break; } idx[i] -= j; if (s[i][idx[i]] == c) idx[i] += 1; } if (s[i][idx[i]] == '\0') { idx[i] = -1; left_to_match -= 1; if (left_to_match == 0) return 0; } } } return left_to_match; } int main(int argc, const char * argv []) { if (argc < 2) { fprintf(stderr, "usage %s [ ...]\n", argv[0]); return 2; } int res = match_strings(argc - 1, argv + 1); printf("%d left to match.\n", res); return res == 0; }