micro-utils/libmu/parse_mode.c

97 lines
1.4 KiB
C
Raw Normal View History

2024-07-09 19:33:45 +00:00
#include <stdlib.h>
2024-07-01 10:23:00 +00:00
#include <string.h>
2024-07-10 19:59:28 +00:00
#include "parse_mode.h"
2024-07-01 10:23:00 +00:00
#define U (S_ISUID | S_IRWXU)
#define G (S_ISGID | S_IRWXG)
#define O (S_IRWXO)
#define A (U | G | O)
#define WR_PERM (S_IWUSR | S_IWGRP | S_IWOTH)
#define EX_PERM (S_IXUSR | S_IXGRP | S_IXOTH)
#define RD_PERM (S_IRUSR | S_IRGRP | S_IROTH)
#define SU_PERM (S_ISUID | S_ISGID | S_ISVTX)
#define FULL_PERM (WR_PERM | EX_PERM | RD_PERM)
2024-07-09 19:33:45 +00:00
mode_t mu_parse_mode(const char *s, mode_t cur_mode) {
2024-07-01 10:23:00 +00:00
char *p = NULL;
mode_t mode = (mode_t)strtol(s, &p, 8);
if (!*p && mode < 07777U)
return mode;
else if (mode > 07777U)
return 0;
mode = 0;
/* Default + */
char append = 1;
mode_t mask = 0;
for (size_t i = 0; i < strlen(s); i++) {
switch (s[i]) {
case 'r':
mode |= RD_PERM;
break;
case 'w':
mode |= WR_PERM;
break;
case 'x':
mode |= EX_PERM;
break;
case 's':
mode |= SU_PERM;
break;
case '+':
append = 1;
break;
case '-':
append = 0;
break;
case '=':
append = 2;
break;
case 'g':
mask |= G;
break;
case 'u':
mask |= U;
break;
case 'o':
mask |= O;
break;
case 'a':
mask |= A;
break;
default:
return 0;
}
}
if (mask == 0)
mask = U;
mask &= mode;
if (append == 0)
mode = ~mode;
if (append == 2)
return mode & mask;
else
return (cur_mode & ~mask) | (mode & mask);
}