4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
29 unsigned requested: 1;
33 static const char prefix_of_permissions[] = PREFIXPERMISSION;
35 static int nrpermissions = 0;
36 static struct permission *permissions = NULL;
37 static int indexiter = 0;
39 /* check is the name has the correct prefix for permissions */
40 int is_standard_permission(const char *name)
42 return 0 == memcmp(name, prefix_of_permissions, sizeof(prefix_of_permissions) - 1);
45 /* retrieves the permission of name */
46 static struct permission *get_permission(const char *name)
50 for (i = 0 ; i < nrpermissions ; i++)
51 if (0 == strcmp(permissions[i].name, name))
56 /* add a permission of name */
57 static struct permission *add_permission(const char *name)
59 struct permission *p = get_permission(name);
61 p = realloc(permissions, ((nrpermissions + 8) & ~7) * sizeof(*p));
64 p = permissions + nrpermissions;
65 memset(p, 0, sizeof(*p));
66 p->name = strdup(name);
74 /* remove any granting */
75 void reset_permissions()
78 for (i = 0 ; i < nrpermissions ; i++)
79 permissions[i].granted = 0;
82 /* remove any granting */
83 void crop_permissions(unsigned level)
86 for (i = 0 ; i < nrpermissions ; i++)
87 if (permissions[i].level < level)
88 permissions[i].granted = 0;
91 /* add permissions granted for installation */
92 void grant_permission_list(const char *list)
97 static const char separators[] = " \t\n\r,";
100 iter += strspn(iter, separators);
102 n = strcspn(iter, separators);
105 p = add_permission(iter);
107 ERROR("Can't allocate permission");
113 iter += strspn(iter, separators);
117 /* checks if the permission 'name' is recorded */
118 int permission_exists(const char *name)
120 return !!get_permission(name);
123 /* request the permission, returns 1 if granted or 0 otherwise */
124 int request_permission(const char *name)
126 struct permission *p = get_permission(name);
135 /* iteration over granted and requested permissions */
136 const char *first_usable_permission()
139 return next_usable_permission();
142 const char *next_usable_permission()
144 while(indexiter < nrpermissions) {
145 struct permission *p = &permissions[indexiter++];
146 if (p->granted && p->requested)