source: MondoRescue/branches/3.3/mindi-busybox/libbb/parse_mode.c@ 3670

Last change on this file since 3670 was 3621, checked in by Bruno Cornec, 7 years ago

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

File size: 3.3 KB
RevLine 
[821]1/* vi: set sw=4 ts=4: */
2/*
3 * parse_mode implementation for busybox
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
[2725]7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
[821]8 */
9
10/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
11
12#include "libbb.h"
13
[1765]14/* This function is used from NOFORK applets. It must not allocate anything */
[821]15
[1765]16#define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
17
[3621]18int FAST_FUNC bb_parse_mode(const char *s, unsigned current_mode)
[821]19{
20 static const mode_t who_mask[] = {
21 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */
[1765]22 S_ISUID | S_IRWXU, /* u */
23 S_ISGID | S_IRWXG, /* g */
24 S_IRWXO /* o */
[821]25 };
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 */
[1765]31 S_ISUID | S_ISGID, /* s */
32 S_ISVTX /* t */
[821]33 };
[1765]34 static const char who_chars[] ALIGN1 = "augo";
35 static const char perm_chars[] ALIGN1 = "rwxXst";
[821]36
37 const char *p;
38 mode_t wholist;
39 mode_t permlist;
40 mode_t new_mode;
41 char op;
42
[2725]43 if ((unsigned char)(*s - '0') < 8) {
[821]44 unsigned long tmp;
45 char *e;
46
[1765]47 tmp = strtoul(s, &e, 8);
[821]48 if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */
[3621]49 return -1;
[821]50 }
[3621]51 return tmp;
[821]52 }
53
[3621]54 new_mode = current_mode;
[821]55
[1765]56 /* Note: we allow empty clauses, and hence empty modes.
[821]57 * We treat an empty mode as no change to perms. */
58
[2725]59 while (*s) { /* Process clauses. */
60 if (*s == ',') { /* We allow empty clauses. */
[821]61 ++s;
62 continue;
63 }
64
65 /* Get a wholist. */
66 wholist = 0;
[1765]67 WHO_LIST:
[821]68 p = who_chars;
69 do {
70 if (*p == *s) {
71 wholist |= who_mask[(int)(p-who_chars)];
72 if (!*++s) {
[3621]73 return -1;
[821]74 }
75 goto WHO_LIST;
76 }
77 } while (*++p);
78
[2725]79 do { /* Process action list. */
[821]80 if ((*s != '+') && (*s != '-')) {
81 if (*s != '=') {
[3621]82 return -1;
[821]83 }
84 /* Since op is '=', clear all bits corresponding to the
[1765]85 * wholist, or all file bits if wholist is empty. */
[821]86 permlist = ~FILEMODEBITS;
87 if (wholist) {
88 permlist = ~wholist;
89 }
90 new_mode &= permlist;
91 }
92 op = *s++;
93
94 /* Check for permcopy. */
[2725]95 p = who_chars + 1; /* Skip 'a' entry. */
[821]96 do {
97 if (*p == *s) {
98 int i = 0;
99 permlist = who_mask[(int)(p-who_chars)]
100 & (S_IRWXU | S_IRWXG | S_IRWXO)
101 & new_mode;
102 do {
103 if (permlist & perm_mask[i]) {
104 permlist |= perm_mask[i];
105 }
106 } while (++i < 3);
107 ++s;
108 goto GOT_ACTION;
109 }
110 } while (*++p);
111
112 /* It was not a permcopy, so get a permlist. */
113 permlist = 0;
[1765]114 PERM_LIST:
[821]115 p = perm_chars;
116 do {
117 if (*p == *s) {
118 if ((*p != 'X')
[1765]119 || (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH))
[821]120 ) {
121 permlist |= perm_mask[(int)(p-perm_chars)];
122 }
123 if (!*++s) {
124 break;
125 }
126 goto PERM_LIST;
127 }
128 } while (*++p);
[1765]129 GOT_ACTION:
[2725]130 if (permlist) { /* The permlist was nonempty. */
[1765]131 mode_t tmp = wholist;
132 if (!wholist) {
133 mode_t u_mask = umask(0);
134 umask(u_mask);
135 tmp = ~u_mask;
[821]136 }
137 permlist &= tmp;
138 if (op == '-') {
139 new_mode &= ~permlist;
140 } else {
141 new_mode |= permlist;
142 }
143 }
144 } while (*s && (*s != ','));
145 }
146
[3621]147 return new_mode;
[821]148}
Note: See TracBrowser for help on using the repository browser.