Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : Helper routines for uid and gid arrays
5 :
6 : Copyright (C) Guenther Deschner 2009
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 3 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,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU 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, see <http://www.gnu.org/licenses/>.
20 : */
21 :
22 : #include "replace.h"
23 : #include "lib/util/samba_util.h"
24 :
25 : /****************************************************************************
26 : Add a gid to an array of gids if it's not already there.
27 : ****************************************************************************/
28 :
29 20122 : bool add_gid_to_array_unique(TALLOC_CTX *mem_ctx, gid_t gid,
30 : gid_t **gids, uint32_t *num_gids)
31 : {
32 : uint32_t i;
33 :
34 20122 : if ((*num_gids != 0) && (*gids == NULL)) {
35 : /*
36 : * A former call to this routine has failed to allocate memory
37 : */
38 0 : return false;
39 : }
40 :
41 72234 : for (i=0; i<*num_gids; i++) {
42 55195 : if ((*gids)[i] == gid) {
43 3083 : return true;
44 : }
45 : }
46 :
47 17039 : *gids = talloc_realloc(mem_ctx, *gids, gid_t, *num_gids+1);
48 17039 : if (*gids == NULL) {
49 0 : *num_gids = 0;
50 0 : return false;
51 : }
52 :
53 17039 : (*gids)[*num_gids] = gid;
54 17039 : *num_gids += 1;
55 17039 : return true;
56 : }
57 :
58 : /****************************************************************************
59 : Add a uid to an array of uids if it's not already there.
60 : ****************************************************************************/
61 :
62 0 : bool add_uid_to_array_unique(TALLOC_CTX *mem_ctx, uid_t uid,
63 : uid_t **uids, uint32_t *num_uids)
64 : {
65 : uint32_t i;
66 :
67 0 : if ((*num_uids != 0) && (*uids == NULL)) {
68 : /*
69 : * A former call to this routine has failed to allocate memory
70 : */
71 0 : return false;
72 : }
73 :
74 0 : for (i=0; i<*num_uids; i++) {
75 0 : if ((*uids)[i] == uid) {
76 0 : return true;
77 : }
78 : }
79 :
80 0 : *uids = talloc_realloc(mem_ctx, *uids, uid_t, *num_uids+1);
81 0 : if (*uids == NULL) {
82 0 : *num_uids = 0;
83 0 : return false;
84 : }
85 :
86 0 : (*uids)[*num_uids] = uid;
87 0 : *num_uids += 1;
88 0 : return true;
89 : }
|