1 /* vi: set sw=4 ts=4: */
3 * parse_mode implementation for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
30 #define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
32 extern int bb_parse_mode(const char *s, mode_t *current_mode)
34 static const mode_t who_mask[] = {
35 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */
36 S_ISUID | S_IRWXU, /* u */
37 S_ISGID | S_IRWXG, /* g */
41 static const mode_t perm_mask[] = {
42 S_IRUSR | S_IRGRP | S_IROTH, /* r */
43 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
44 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
45 S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */
46 S_ISUID | S_ISGID, /* s */
50 static const char who_chars[] = "augo";
51 static const char perm_chars[] = "rwxXst";
63 if (((unsigned int)(*s - '0')) < 8) {
67 tmp = strtol(s, &e, 8);
68 if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */
78 new_mode = *current_mode;
80 /* Note: We allow empty clauses, and hence empty modes.
81 * We treat an empty mode as no change to perms. */
83 while (*s) { /* Process clauses. */
85 if (*s == ',') { /* We allow empty clauses. */
97 wholist |= who_mask[(int)(p-who_chars)];
105 do { /* Process action list. */
106 if ((*s != '+') && (*s != '-')) {
110 /* Since op is '=', clear all bits corresponding to the
111 * wholist, of all file bits if wholist is empty. */
112 permlist = ~FILEMODEBITS;
116 new_mode &= permlist;
120 /* Check for permcopy. */
121 p = who_chars + 1; /* Skip 'a' entry. */
125 permlist = who_mask[(int)(p-who_chars)]
126 & (S_IRWXU | S_IRWXG | S_IRWXO)
129 if (permlist & perm_mask[i]) {
130 permlist |= perm_mask[i];
138 /* It was not a permcopy, so get a permlist. */
146 || (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH))
148 permlist |= perm_mask[(int)(p-perm_chars)];
158 if (permlist) { /* The permlist was nonempty. */
166 new_mode &= ~permlist;
168 new_mode |= permlist;
171 } while (*s && (*s != ','));
174 *current_mode = new_mode;