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 "includes.h"
23
24/****************************************************************************
25 Add a gid to an array of gids if it's not already there.
26****************************************************************************/
27
28bool add_gid_to_array_unique(TALLOC_CTX *mem_ctx, gid_t gid,
29			     gid_t **gids, size_t *num_gids)
30{
31	int i;
32
33	if ((*num_gids != 0) && (*gids == NULL)) {
34		/*
35		 * A former call to this routine has failed to allocate memory
36		 */
37		return false;
38	}
39
40	for (i=0; i<*num_gids; i++) {
41		if ((*gids)[i] == gid) {
42			return true;
43		}
44	}
45
46	*gids = talloc_realloc(mem_ctx, *gids, gid_t, *num_gids+1);
47	if (*gids == NULL) {
48		*num_gids = 0;
49		return false;
50	}
51
52	(*gids)[*num_gids] = gid;
53	*num_gids += 1;
54	return true;
55}
56
57/****************************************************************************
58 Add a uid to an array of uids if it's not already there.
59****************************************************************************/
60
61bool add_uid_to_array_unique(TALLOC_CTX *mem_ctx, uid_t uid,
62			     uid_t **uids, size_t *num_uids)
63{
64	int i;
65
66	if ((*num_uids != 0) && (*uids == NULL)) {
67		/*
68		 * A former call to this routine has failed to allocate memory
69		 */
70		return false;
71	}
72
73	for (i=0; i<*num_uids; i++) {
74		if ((*uids)[i] == uid) {
75			return true;
76		}
77	}
78
79	*uids = talloc_realloc(mem_ctx, *uids, uid_t, *num_uids+1);
80	if (*uids == NULL) {
81		*num_uids = 0;
82		return false;
83	}
84
85	(*uids)[*num_uids] = uid;
86	*num_uids += 1;
87	return true;
88}
89