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
29 /* This function parses the sort of string you might pass
30 * to chmod (i.e., [ugoa]{+|-|=}[rwxst] ) and returns the
31 * correct mode described by the string. */
32 extern int parse_mode(const char *s
, mode_t
* theMode
)
34 static const mode_t group_set
[] = {
35 S_ISUID
| S_IRWXU
, /* u */
36 S_ISGID
| S_IRWXG
, /* g */
38 S_ISUID
| S_ISGID
| S_ISVTX
| S_IRWXU
| S_IRWXG
| S_IRWXO
/* a */
41 static const mode_t mode_set
[] = {
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_ISUID
| S_ISGID
, /* s */
49 static const char group_chars
[] = "ugoa";
50 static const char mode_chars
[] = "rwxst";
55 S_ISVTX
| S_ISUID
| S_ISGID
| S_IRWXU
| S_IRWXG
| S_IRWXO
;
70 if ((c
= *s
++) == '\0') {
73 for (p
=group_chars
; *p
; p
++) {
75 groups
|= group_set
[(int)(p
-group_chars
)];
84 if (groups
== 0) { /* The default is "all" */
85 groups
|= S_ISUID
| S_ISGID
| S_ISVTX
86 | S_IRWXU
| S_IRWXG
| S_IRWXO
;
90 if ((c
< '0') || (c
> '7') || (mode
| groups
)) {
93 *theMode
= strtol(--s
, NULL
, 8);
99 if (((c
= *s
++) != '\0') && (c
!= ',')) {
100 for (p
=mode_chars
; *p
; p
++) {
102 mode
|= mode_set
[(int)(p
-mode_chars
)];
106 break; /* We're done so break out of loop.*/
110 andMode
&= ~(groups
); /* Now fall through. */
112 orMode
|= mode
& groups
;
115 andMode
&= ~(mode
& groups
);
116 orMode
&= ~(mode
& groups
);
130 c-file-style: "linux"