/* * Simple example to show the basic regexp(3) functions * in action (regexec() and regcomp()). * * Juan RP <-> 2005/09/22. */ #include #include #include struct compat_osver { const char *hosver_reg; /* host os version */ const char *bosver_reg; /* builder os version */ } compat_osver[] = { { "^3.99.[8-9]*", "3.99.[1-8]" }, /* COMPAT_30 for HEAD */ { "^3.99.[8-9]*", "3.[0-8]*", }, /* COMPAT_30 for 3.x */ { "^3.99.[8-9]*", "[2-3].[0-8]", }, /* COMPAT_[23]0 for HEAD */ { "^3.99.[8-9]*", "[1-2].*", }, /* COMPAT_[12]* for HEAD */ { "^3.[0-8]*", "[1-2].*", }, /* COMPAT_[12]* for 3.x */ { "^2.*", "^1.*$", }, /* COMPAT_1[1-6] for 2.x */ { NULL, NULL } }; void usage(void); void check_osver(const char *, const char *); void usage(void) { printf("pkgosver [host os version] [builder os version]\n"); exit(EXIT_FAILURE); } void check_osver(const char *h_os_ver, const char *b_os_ver) { struct compat_osver *c_osver; regex_t host_reg, builder_reg; int i, found; i = found = 0; for (c_osver = compat_osver; c_osver->hosver_reg != NULL; c_osver++) { i++; printf("%d: HOST OS VERSION: %s ", i, c_osver->hosver_reg); printf("%d: BUILDER OS VERSION: %s\n", i, c_osver->bosver_reg); regcomp(&host_reg, c_osver->hosver_reg, REG_NOSUB); /* check host os version string */ if (regexec(&host_reg, h_os_ver, 10, NULL, 0) == 0) { regfree(&host_reg); regcomp(&builder_reg, c_osver->bosver_reg, REG_NOSUB); /* check builder os version string */ if (regexec(&builder_reg, b_os_ver, 10, NULL, 0) == 0) { regfree(&builder_reg); /* found! */ printf("regexp %d matched!\n", i); found = 1; break; } regfree(&builder_reg); } regfree(&host_reg); } if (found != 1) printf("not mached!\n"); } int main(int argc, char **argv) { if (argc != 3) usage(); check_osver(argv[1], argv[2]); exit(EXIT_SUCCESS); }