aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBaptiste Daroussin <bapt@FreeBSD.org>2012-12-27 14:30:19 +0000
committerBaptiste Daroussin <bapt@FreeBSD.org>2012-12-27 14:30:19 +0000
commitbe49c8301192a7ade39537264ab2db0e88436f40 (patch)
treeaa053e8a4e86f98b45a33e762c770d0c49973a35 /lib
parent98e79fb122f0aa6d7efd8af4cd338ea4b52e0dcf (diff)
downloadsrc-be49c8301192a7ade39537264ab2db0e88436f40.tar.gz
src-be49c8301192a7ade39537264ab2db0e88436f40.zip
Notes
Diffstat (limited to 'lib')
-rw-r--r--lib/libutil/gr_util.c40
-rw-r--r--lib/libutil/libutil.h2
2 files changed, 42 insertions, 0 deletions
diff --git a/lib/libutil/gr_util.c b/lib/libutil/gr_util.c
index 6bf102f3c4f3..90062eb033ee 100644
--- a/lib/libutil/gr_util.c
+++ b/lib/libutil/gr_util.c
@@ -479,6 +479,46 @@ gr_dup(const struct group *gr)
}
/*
+ * Add a new member name to a struct group.
+ */
+struct group *
+gr_add(struct group *gr, const char *newmember)
+{
+ size_t mlen;
+ int num_mem=0;
+ char **members;
+ struct group *newgr;
+
+ if (newmember == NULL)
+ return(gr_dup(gr));
+
+ if (gr->gr_mem != NULL) {
+ for (num_mem = 0; gr->gr_mem[num_mem] != NULL; num_mem++) {
+ if (strcmp(gr->gr_mem[num_mem], newmember) == 0) {
+ errno = EEXIST;
+ return (NULL);
+ }
+ }
+ }
+ /* Allocate enough for current pointers + 1 more and NULL marker */
+ mlen = (num_mem + 2) * sizeof(*gr->gr_mem);
+ if ((members = calloc(1, mlen )) == NULL) {
+ errno = ENOMEM;
+ return (NULL);
+ }
+ memcpy(members, gr->gr_mem, num_mem * sizeof(*gr->gr_mem));
+ members[num_mem++] = (char *)newmember;
+ members[num_mem] = NULL;
+ gr->gr_mem = members;
+ newgr = gr_dup(gr);
+ if (newgr == NULL)
+ errno = ENOMEM;
+
+ free(members);
+ return (newgr);
+}
+
+/*
* Scan a line and place it into a group structure.
*/
static bool
diff --git a/lib/libutil/libutil.h b/lib/libutil/libutil.h
index bf42766a128c..fcd74e1fe9ed 100644
--- a/lib/libutil/libutil.h
+++ b/lib/libutil/libutil.h
@@ -166,6 +166,8 @@ int gr_copy(int __ffd, int _tfd, const struct group *_gr,
struct group *_old_gr);
struct group *
gr_dup(const struct group *_gr);
+struct group *
+ gr_add(struct group *_gr, const char *_newmember);
int gr_equal(const struct group *_gr1, const struct group *_gr2);
void gr_fini(void);
int gr_init(const char *_dir, const char *_master);