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.
28 unsigned requested: 1;
32 static const char prefix_of_permissions[] = FWK_PREFIX_PERMISSION;
34 static int nrpermissions = 0;
35 static struct permission *permissions = NULL;
36 static int indexiter = 0;
38 /* check is the name has the correct prefix for permissions */
39 int is_standard_permission(const char *name)
41 return 0 == memcmp(name, prefix_of_permissions, sizeof(prefix_of_permissions) - 1);
44 /* retrieves the permission of name */
45 static struct permission *get_permission(const char *name)
49 for (i = 0 ; i < nrpermissions ; i++)
50 if (0 == strcmp(permissions[i].name, name))
55 /* add a permission of name */
56 static struct permission *add_permission(const char *name)
58 struct permission *p = get_permission(name);
60 p = realloc(permissions, ((nrpermissions + 8) & ~7) * sizeof(*p));
63 p = permissions + nrpermissions;
64 memset(p, 0, sizeof(*p));
65 p->name = strdup(name);
73 /* remove any granting */
74 void reset_permissions()
77 for (i = 0 ; i < nrpermissions ; i++)
78 permissions[i].granted = 0;
81 /* remove any granting */
82 void crop_permissions(unsigned level)
85 for (i = 0 ; i < nrpermissions ; i++)
86 if (permissions[i].level < level)
87 permissions[i].granted = 0;
90 /* add permissions granted for installation */
91 void grant_permission_list(const char *list)
96 static const char separators[] = " \t\n\r,";
99 iter += strspn(iter, separators);
101 n = strcspn(iter, separators);
104 p = add_permission(iter);
106 ERROR("Can't allocate permission");
112 iter += strspn(iter, separators);
116 /* checks if the permission 'name' is recorded */
117 int permission_exists(const char *name)
119 return !!get_permission(name);
122 /* request the permission, returns 1 if granted or 0 otherwise */
123 int request_permission(const char *name)
125 struct permission *p = get_permission(name);
134 /* iteration over granted and requested permissions */
135 const char *first_usable_permission()
138 return next_usable_permission();
141 const char *next_usable_permission()
143 while(indexiter < nrpermissions) {
144 struct permission *p = &permissions[indexiter++];
145 if (p->granted && p->requested)