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 tarball for details.
10 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
17 #define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
19 int bb_parse_mode(const char *s, mode_t *current_mode)
21 static const mode_t who_mask[] = {
22 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */
23 S_ISUID | S_IRWXU, /* u */
24 S_ISGID | S_IRWXG, /* g */
28 static const mode_t perm_mask[] = {
29 S_IRUSR | S_IRGRP | S_IROTH, /* r */
30 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
31 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
32 S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */
33 S_ISUID | S_ISGID, /* s */
37 static const char who_chars[] = "augo";
38 static const char perm_chars[] = "rwxXst";
50 if (((unsigned int)(*s - '0')) < 8) {
54 tmp = strtol(s, &e, 8);
55 if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */
65 new_mode = *current_mode;
67 /* Note: We allow empty clauses, and hence empty modes.
68 * We treat an empty mode as no change to perms. */
70 while (*s) { /* Process clauses. */
72 if (*s == ',') { /* We allow empty clauses. */
84 wholist |= who_mask[(int)(p-who_chars)];
92 do { /* Process action list. */
93 if ((*s != '+') && (*s != '-')) {
97 /* Since op is '=', clear all bits corresponding to the
98 * wholist, of all file bits if wholist is empty. */
99 permlist = ~FILEMODEBITS;
103 new_mode &= permlist;
107 /* Check for permcopy. */
108 p = who_chars + 1; /* Skip 'a' entry. */
112 permlist = who_mask[(int)(p-who_chars)]
113 & (S_IRWXU | S_IRWXG | S_IRWXO)
116 if (permlist & perm_mask[i]) {
117 permlist |= perm_mask[i];
125 /* It was not a permcopy, so get a permlist. */
133 || (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH))
135 permlist |= perm_mask[(int)(p-perm_chars)];
145 if (permlist) { /* The permlist was nonempty. */
153 new_mode &= ~permlist;
155 new_mode |= permlist;
158 } while (*s && (*s != ','));
161 *current_mode = new_mode;