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