aboutsummaryrefslogtreecommitdiff
path: root/util2.c
blob: 1ce2c55e00073f8485459e09f08e60d898023fa0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;
}