Whamcloud - gitweb
libext2fs: add threading support to the I/O manager abstraction
[tools/e2fsprogs.git] / lib / support / parse_qtype.c
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
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 #ifdef DEBUG_PROGRAM
67                 printf("word: %s\n", token);
68 #endif
69                 next = strtok_r(NULL, PARSE_DELIM, &tmp);
70         }
71         *qtype_bits = qtype;
72 errout:
73         free(buf);
74         return ret;
75 }
76
77 #ifdef DEBUG_PROGRAM
78 int main(int argc, char **argv)
79 {
80         unsigned int qtype_bits = 0;
81         int ret;
82         char *err_token = 0;
83
84         ret = parse_quota_types(argv[1], &qtype_bits, &err_token);
85         printf("parse_quota_types returns %d, %d\n", ret, qtype_bits);
86         if (err_token)
87                 printf("err_token is %s\n", err_token);
88         return 0;
89 }
90 #endif