From owner-svn-src-all@FreeBSD.ORG Thu Dec 27 14:30:20 2012 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 6042D3B4; Thu, 27 Dec 2012 14:30:20 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id 2B04C8FC0A; Thu, 27 Dec 2012 14:30:20 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.5/8.14.5) with ESMTP id qBREUKY7095328; Thu, 27 Dec 2012 14:30:20 GMT (envelope-from bapt@svn.freebsd.org) Received: (from bapt@localhost) by svn.freebsd.org (8.14.5/8.14.5/Submit) id qBREUJp1095325; Thu, 27 Dec 2012 14:30:19 GMT (envelope-from bapt@svn.freebsd.org) Message-Id: <201212271430.qBREUJp1095325@svn.freebsd.org> From: Baptiste Daroussin Date: Thu, 27 Dec 2012 14:30:19 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r244736 - head/lib/libutil X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 27 Dec 2012 14:30:20 -0000 Author: bapt Date: Thu Dec 27 14:30:19 2012 New Revision: 244736 URL: http://svnweb.freebsd.org/changeset/base/244736 Log: New gr_add function to provide a clean and safe method to append a new member into an existing group. Submitted by: db Modified: head/lib/libutil/gr_util.c head/lib/libutil/libutil.h Modified: head/lib/libutil/gr_util.c ============================================================================== --- head/lib/libutil/gr_util.c Thu Dec 27 14:09:50 2012 (r244735) +++ head/lib/libutil/gr_util.c Thu Dec 27 14:30:19 2012 (r244736) @@ -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 Modified: head/lib/libutil/libutil.h ============================================================================== --- head/lib/libutil/libutil.h Thu Dec 27 14:09:50 2012 (r244735) +++ head/lib/libutil/libutil.h Thu Dec 27 14:30:19 2012 (r244736) @@ -166,6 +166,8 @@ int gr_copy(int __ffd, int _tfd, const 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);