1 /*
2 * parse_qtype.c
3 */
4
5 #include "config.h"
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <string.h>
11 #include <fcntl.h>
12
13 #include "quotaio.h"
14
15 #define PARSE_DELIM ":,"
16
parse_quota_types(const char * in_str,unsigned int * qtype_bits,char ** err_token)17 int parse_quota_types(const char *in_str, unsigned int *qtype_bits,
18 char **err_token)
19 {
20 char *buf, *token, *next, *tmp;
21 unsigned int qtype = *qtype_bits;
22 int len, ret = 0;
23
24 if (!in_str)
25 return 0;
26
27 len = strlen(in_str);
28 buf = malloc(len + 1);
29 if (!buf)
30 return ENOMEM;
31 strcpy(buf, in_str);
32
33 for (token = buf, next = strtok_r(buf, PARSE_DELIM, &tmp);
34 token && *token; token = next) {
35 int not = 0;
36 char *p = token;
37
38 if (*p == '^') {
39 not = 1;
40 p++;
41 }
42 if (!strcmp(p, "usr") || !strcmp(p, "usrquota")) {
43 if (not)
44 qtype &= ~QUOTA_USR_BIT;
45 else
46 qtype |= QUOTA_USR_BIT;
47 } else if (!strcmp(p, "grp") || !strcmp(p, "grpquota")) {
48 if (not)
49 qtype &= ~QUOTA_GRP_BIT;
50 else
51 qtype |= QUOTA_GRP_BIT;
52 } else if (!strcmp(p, "prj") || !strcmp(p, "prjquota")) {
53 if (not)
54 qtype &= ~QUOTA_PRJ_BIT;
55 else
56 qtype |= QUOTA_PRJ_BIT;
57 } else {
58 if (err_token) {
59 *err_token = malloc(strlen(token) + 1);
60 if (*err_token)
61 strcpy(*err_token, token);
62 }
63 ret = EINVAL;
64 goto errout;
65 }
66 printf("word: %s\n", token);
67 next = strtok_r(NULL, PARSE_DELIM, &tmp);
68 }
69 *qtype_bits = qtype;
70 errout:
71 free(buf);
72 return ret;
73 }
74
75 #if 0
76 int main(int argc, char **argv)
77 {
78 unsigned int qtype_bits = 0;
79 int ret;
80 char *err_token = 0;
81
82 ret = parse_quota_types(argv[1], &qtype_bits, &err_token);
83 printf("parse_quota_types returns %d, %d\n", ret, qtype_bits);
84 if (err_token)
85 printf("err_token is %s\n", err_token);
86 return 0;
87 }
88 #endif
89