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.
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
13 /* This function is used from NOFORK applets. It must not allocate anything */
15 #define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
17 int FAST_FUNC bb_parse_mode(const char *s, unsigned current_mode)
19 static const mode_t who_mask[] = {
20 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */
21 S_ISUID | S_IRWXU, /* u */
22 S_ISGID | S_IRWXG, /* g */
25 static const mode_t perm_mask[] = {
26 S_IRUSR | S_IRGRP | S_IROTH, /* r */
27 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
28 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
29 S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */
30 S_ISUID | S_ISGID, /* s */
33 static const char who_chars[] ALIGN1 = "augo";
34 static const char perm_chars[] ALIGN1 = "rwxXst";
42 if ((unsigned char)(*s - '0') < 8) {
46 tmp = strtoul(s, &e, 8);
47 if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */
53 new_mode = current_mode;
55 /* Note: we allow empty clauses, and hence empty modes.
56 * We treat an empty mode as no change to perms. */
58 while (*s) { /* Process clauses. */
59 if (*s == ',') { /* We allow empty clauses. */
70 wholist |= who_mask[(int)(p-who_chars)];
78 do { /* Process action list. */
79 if ((*s != '+') && (*s != '-')) {
83 /* Since op is '=', clear all bits corresponding to the
84 * wholist, or all file bits if wholist is empty. */
85 permlist = ~FILEMODEBITS;
93 /* Check for permcopy. */
94 p = who_chars + 1; /* Skip 'a' entry. */
98 permlist = who_mask[(int)(p-who_chars)]
99 & (S_IRWXU | S_IRWXG | S_IRWXO)
102 if (permlist & perm_mask[i]) {
103 permlist |= perm_mask[i];
111 /* It was not a permcopy, so get a permlist. */
118 || (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH))
120 permlist |= perm_mask[(int)(p-perm_chars)];
129 if (permlist) { /* The permlist was nonempty. */
130 mode_t tmp = wholist;
132 mode_t u_mask = umask(0);
138 new_mode &= ~permlist;
140 new_mode |= permlist;
143 } while (*s && (*s != ','));