summaryrefslogtreecommitdiffstats
path: root/lib/support/parse_qtype.c
blob: d8df07d399b5104eafdd80ea07809aab5026da0d (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * parse_qtype.c
 */

#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>

#include "quotaio.h"

#define PARSE_DELIM ":,"

int parse_quota_types(const char *in_str, unsigned int *qtype_bits,
		      char **err_token)
{
	char	*buf, *token, *next, *tmp;
	unsigned int qtype = *qtype_bits;
	int	len, ret = 0;

	if (!in_str)
		return 0;

	len = strlen(in_str);
	buf = malloc(len + 1);
	if (!buf)
		return ENOMEM;
	strcpy(buf, in_str);

	for (token = buf, next = strtok_r(buf, PARSE_DELIM, &tmp);
	     token && *token; token = next) {
		int	not = 0;
		char	*p = token;

		if (*p == '^') {
			not = 1;
			p++;
		}
		if (!strcmp(p, "usr") || !strcmp(p, "usrquota")) {
			if (not)
				qtype &= ~QUOTA_USR_BIT;
			else
				qtype |= QUOTA_USR_BIT;
		} else if (!strcmp(p, "grp") || !strcmp(p, "grpquota")) {
			if (not)
				qtype &= ~QUOTA_GRP_BIT;
			else
				qtype |= QUOTA_GRP_BIT;
		} else if (!strcmp(p, "prj") || !strcmp(p, "prjquota")) {
			if (not)
				qtype &= ~QUOTA_PRJ_BIT;
			else
				qtype |= QUOTA_PRJ_BIT;
		} else {
			if (err_token) {
				*err_token = malloc(strlen(token) + 1);
				if (*err_token)
					strcpy(*err_token, token);
			}
			ret = EINVAL;
			goto errout;
		}
#ifdef DEBUG_PROGRAM
		printf("word: %s\n", token);
#endif
		next = strtok_r(NULL, PARSE_DELIM, &tmp);
	}
	*qtype_bits = qtype;
errout:
	free(buf);
	return ret;
}

#ifdef DEBUG_PROGRAM
int main(int argc, char **argv)
{
	unsigned int qtype_bits = 0;
	int ret;
	char *err_token = 0;

	ret = parse_quota_types(argv[1], &qtype_bits, &err_token);
	printf("parse_quota_types returns %d, %d\n", ret, qtype_bits);
	if (err_token)
		printf("err_token is %s\n", err_token);
	return 0;
}
#endif