build: remove automake/autoconf build system
[oweals/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 /* 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)
32 {
33         static const mode_t group_set[] = {
34                 S_ISUID | S_IRWXU,      /* u */
35                 S_ISGID | S_IRWXG,      /* g */
36                 S_IRWXO,        /* o */
37                 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO       /* a */
38         };
39
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 */
45                 S_ISVTX         /* t */
46         };
47
48         static const char group_chars[] = "ugoa";
49         static const char mode_chars[] = "rwxst";
50
51         const char *p;
52
53         mode_t andMode =
54             S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
55         mode_t orMode = 0;
56         mode_t mode;
57         mode_t groups;
58         char type;
59         char c;
60
61         if (s == NULL) {
62                 return (FALSE);
63         }
64
65         do {
66                 mode = 0;
67                 groups = 0;
68 NEXT_GROUP:
69                 if ((c = *s++) == '\0') {
70                         return -1;
71                 }
72                 for (p = group_chars; *p; p++) {
73                         if (*p == c) {
74                                 groups |= group_set[(int)(p - group_chars)];
75                                 goto NEXT_GROUP;
76                         }
77                 }
78                 switch (c) {
79                 case '=':
80                 case '+':
81                 case '-':
82                         type = c;
83                         if (groups == 0) {      /* The default is "all" */
84                                 groups |= S_ISUID | S_ISGID | S_ISVTX
85                                     | S_IRWXU | S_IRWXG | S_IRWXO;
86                         }
87                         break;
88                 default:
89                         if ((c < '0') || (c > '7') || (mode | groups)) {
90                                 return (FALSE);
91                         } else {
92                                 *theMode = strtol(--s, NULL, 8);
93                                 return (TRUE);
94                         }
95                 }
96
97 NEXT_MODE:
98                 if (((c = *s++) != '\0') && (c != ',')) {
99                         for (p = mode_chars; *p; p++) {
100                                 if (*p == c) {
101                                         mode |= mode_set[(int)(p - mode_chars)];
102                                         goto NEXT_MODE;
103                                 }
104                         }
105                         break;  /* We're done so break out of loop. */
106                 }
107                 switch (type) {
108                 case '=':
109                         andMode &= ~(groups);   /* Now fall through. */
110                 case '+':
111                         orMode |= mode & groups;
112                         break;
113                 case '-':
114                         andMode &= ~(mode & groups);
115                         orMode &= ~(mode & groups);
116                         break;
117                 }
118         } while (c == ',');
119
120         *theMode &= andMode;
121         *theMode |= orMode;
122
123         return TRUE;
124 }
125
126 /* END CODE */
127 /*
128 Local Variables:
129 c-file-style: "linux"
130 c-basic-offset: 4
131 tab-width: 4
132 End:
133 */