/* Output every line of standard input that matches the regular expression. */ #include #include #include #include int main(int argc, char **argv) { regex_t expression; const int error = regcomp(&expression, "^moe[0-9]", 0); char line[1024]; if (error != 0) { const size_t length = regerror(error, &expression, NULL, 0); if (length == 0) { fprintf(stderr, "%s: regerror not implemented\n", argv[0]); } else { char *const p = malloc(length); if (p == NULL) { fprintf(stderr, "%s: can't malloc space for error message\n", argv[0]); } else { regerror(error, &expression, p, length); fprintf(stderr, "%s: %s\n", argv[0], p); free(p); } } return EXIT_FAILURE; } while (fgets(line, sizeof line, stdin) != NULL) { if (regexec(&expression, line, 0, NULL, 0) == 0) { fputs(line, stdout); } } regfree(&expression); return EXIT_SUCCESS; }