aboutsummaryrefslogtreecommitdiff
path: root/util2.c
diff options
context:
space:
mode:
Diffstat (limited to 'util2.c')
-rw-r--r--util2.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/util2.c b/util2.c
new file mode 100644
index 0000000..1ce2c55
--- /dev/null
+++ b/util2.c
@@ -0,0 +1,53 @@
+#include <stdio.h>
+#include <fcntl.h>
+#include <err.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <regex.h>
+
+int
+readEnv(FILE *fh)
+{
+ int ret;
+ char name[1024], value[8192];
+
+ while (1) {
+ if ((ret = fscanf(fh, "%1023[^=]=%8191[^\n]\n", &name, &value)) == EOF)
+ break;
+ else if (ret == 0)
+ break;
+
+ fprintf(stderr, "Setting: '%s' = '%s'\n", name, value);
+ setenv(name, value, 1);
+ }
+
+ return 0;
+}
+
+int
+reMatch(const char *regex, const char *str) {
+ char reErr[1024] = {0};
+ regex_t *re = calloc(1, sizeof(regex_t));
+ int rc = 0;
+
+ if (!re)
+ return -1;
+
+ rc = regcomp(re, regex, REG_EXTENDED|REG_ICASE|REG_NOSUB);
+ if (rc != 0) {
+ regerror(rc, re, reErr, 1024);
+ fprintf(stderr, "Regex compile err: %s %s\n", regex, reErr);
+ return -1;
+ }
+
+ rc = regexec(re, str, 0, NULL, 0);
+ regfree(re);
+ if (rc != 0) {
+ regerror(rc, re, reErr, 1024);
+ fprintf(stderr, "Regex match error: %s -> %s : %s\n",
+ regex, str, reErr);
+ return -1;
+ }
+
+ return rc;
+}