1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) many different people. If you wrote this, please
6 * acknowledge your work.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 /* This function parses the sort of string you might pass
29 * to chmod (i.e., [ugoa]{+|-|=}[rwxst] ) and returns the
30 * correct mode described by the string. */
31 extern int parse_mode(const char *s
, mode_t
* theMode
)
33 static const mode_t group_set
[] = {
34 S_ISUID
| S_IRWXU
, /* u */
35 S_ISGID
| S_IRWXG
, /* g */
37 S_ISUID
| S_ISGID
| S_ISVTX
| S_IRWXU
| S_IRWXG
| S_IRWXO
/* a */
40 static const mode_t mode_set
[] = {
41 S_IRUSR
| S_IRGRP
| S_IROTH
, /* r */
42 S_IWUSR
| S_IWGRP
| S_IWOTH
, /* w */
43 S_IXUSR
| S_IXGRP
| S_IXOTH
, /* x */
44 S_ISUID
| S_ISGID
, /* s */
48 static const char group_chars
[] = "ugoa";
49 static const char mode_chars
[] = "rwxst";
54 S_ISVTX
| S_ISUID
| S_ISGID
| S_IRWXU
| S_IRWXG
| S_IRWXO
;
69 if ((c
= *s
++) == '\0') {
72 for (p
= group_chars
; *p
; p
++) {
74 groups
|= group_set
[(int)(p
- group_chars
)];
83 if (groups
== 0) { /* The default is "all" */
84 groups
|= S_ISUID
| S_ISGID
| S_ISVTX
85 | S_IRWXU
| S_IRWXG
| S_IRWXO
;
89 if ((c
< '0') || (c
> '7') || (mode
| groups
)) {
92 *theMode
= strtol(--s
, NULL
, 8);
98 if (((c
= *s
++) != '\0') && (c
!= ',')) {
99 for (p
= mode_chars
; *p
; p
++) {
101 mode
|= mode_set
[(int)(p
- mode_chars
)];
105 break; /* We're done so break out of loop. */
109 andMode
&= ~(groups
); /* Now fall through. */
111 orMode
|= mode
& groups
;
114 andMode
&= ~(mode
& groups
);
115 orMode
&= ~(mode
& groups
);
129 c-file-style: "linux"