1 /* vi: set sw=4 ts=4: */
3 * parse_mode implementation for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
14 /* This function is used from NOFORK applets. It must not allocate anything */
16 #define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
18 int FAST_FUNC bb_parse_mode(const char *s, unsigned current_mode)
20 static const mode_t who_mask[] = {
21 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */
22 S_ISUID | S_IRWXU, /* u */
23 S_ISGID | S_IRWXG, /* g */
26 static const mode_t perm_mask[] = {
27 S_IRUSR | S_IRGRP | S_IROTH, /* r */
28 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
29 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
30 S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */
31 S_ISUID | S_ISGID, /* s */
34 static const char who_chars[] ALIGN1 = "augo";
35 static const char perm_chars[] ALIGN1 = "rwxXst";
43 if ((unsigned char)(*s - '0') < 8) {
47 tmp = strtoul(s, &e, 8);
48 if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */
54 new_mode = current_mode;
56 /* Note: we allow empty clauses, and hence empty modes.
57 * We treat an empty mode as no change to perms. */
59 while (*s) { /* Process clauses. */
60 if (*s == ',') { /* We allow empty clauses. */
71 wholist |= who_mask[(int)(p-who_chars)];
79 do { /* Process action list. */
80 if ((*s != '+') && (*s != '-')) {
84 /* Since op is '=', clear all bits corresponding to the
85 * wholist, or all file bits if wholist is empty. */
86 permlist = ~FILEMODEBITS;
94 /* Check for permcopy. */
95 p = who_chars + 1; /* Skip 'a' entry. */
99 permlist = who_mask[(int)(p-who_chars)]
100 & (S_IRWXU | S_IRWXG | S_IRWXO)
103 if (permlist & perm_mask[i]) {
104 permlist |= perm_mask[i];
112 /* It was not a permcopy, so get a permlist. */
119 || (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH))
121 permlist |= perm_mask[(int)(p-perm_chars)];
130 if (permlist) { /* The permlist was nonempty. */
131 mode_t tmp = wholist;
133 mode_t u_mask = umask(0);
139 new_mode &= ~permlist;
141 new_mode |= permlist;
144 } while (*s && (*s != ','));