The compiler almost certainly knows better.
[project/opkg-lede.git] / libbb / parse_mode.c
1 /* vi: set sw=4 ts=4: */
2 /*
3 * Utility routines.
4 *
5 * Copyright (C) many different people. If you wrote this, please
6 * acknowledge your work.
7 *
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.
12 *
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.
17 *
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
21 * USA
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "libbb.h"
27
28
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)
33 {
34 static const mode_t group_set[] = {
35 S_ISUID | S_IRWXU, /* u */
36 S_ISGID | S_IRWXG, /* g */
37 S_IRWXO, /* o */
38 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO /* a */
39 };
40
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 */
46 S_ISVTX /* t */
47 };
48
49 static const char group_chars[] = "ugoa";
50 static const char mode_chars[] = "rwxst";
51
52 const char *p;
53
54 mode_t andMode =
55 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
56 mode_t orMode = 0;
57 mode_t mode;
58 mode_t groups;
59 char type;
60 char c;
61
62 if (s==NULL) {
63 return (FALSE);
64 }
65
66 do {
67 mode = 0;
68 groups = 0;
69 NEXT_GROUP:
70 if ((c = *s++) == '\0') {
71 return -1;
72 }
73 for (p=group_chars ; *p ; p++) {
74 if (*p == c) {
75 groups |= group_set[(int)(p-group_chars)];
76 goto NEXT_GROUP;
77 }
78 }
79 switch (c) {
80 case '=':
81 case '+':
82 case '-':
83 type = c;
84 if (groups == 0) { /* The default is "all" */
85 groups |= S_ISUID | S_ISGID | S_ISVTX
86 | S_IRWXU | S_IRWXG | S_IRWXO;
87 }
88 break;
89 default:
90 if ((c < '0') || (c > '7') || (mode | groups)) {
91 return (FALSE);
92 } else {
93 *theMode = strtol(--s, NULL, 8);
94 return (TRUE);
95 }
96 }
97
98 NEXT_MODE:
99 if (((c = *s++) != '\0') && (c != ',')) {
100 for (p=mode_chars ; *p ; p++) {
101 if (*p == c) {
102 mode |= mode_set[(int)(p-mode_chars)];
103 goto NEXT_MODE;
104 }
105 }
106 break; /* We're done so break out of loop.*/
107 }
108 switch (type) {
109 case '=':
110 andMode &= ~(groups); /* Now fall through. */
111 case '+':
112 orMode |= mode & groups;
113 break;
114 case '-':
115 andMode &= ~(mode & groups);
116 orMode &= ~(mode & groups);
117 break;
118 }
119 } while (c == ',');
120
121 *theMode &= andMode;
122 *theMode |= orMode;
123
124 return TRUE;
125 }
126
127 /* END CODE */
128 /*
129 Local Variables:
130 c-file-style: "linux"
131 c-basic-offset: 4
132 tab-width: 4
133 End:
134 */