svn commit: r244737 - head/usr.sbin/pw

Baptiste Daroussin bapt at FreeBSD.org
Thu Dec 27 14:35:07 UTC 2012


Author: bapt
Date: Thu Dec 27 14:35:06 2012
New Revision: 244737
URL: http://svnweb.freebsd.org/changeset/base/244737

Log:
  Simplify the code by using the new gr_add function

Modified:
  head/usr.sbin/pw/pw_user.c

Modified: head/usr.sbin/pw/pw_user.c
==============================================================================
--- head/usr.sbin/pw/pw_user.c	Thu Dec 27 14:30:19 2012	(r244736)
+++ head/usr.sbin/pw/pw_user.c	Thu Dec 27 14:35:06 2012	(r244737)
@@ -745,25 +745,19 @@ pw_user(struct userconf * cnf, int mode,
 	 */
 
 	if (mode == M_ADD || getarg(args, 'G') != NULL) {
-		int i, j;
+		int i;
 		for (i = 0; cnf->groups[i] != NULL; i++) {
-			char **members;
 			grp = GETGRNAM(cnf->groups[i]);
-			for (j = 0; grp->gr_mem[j] != NULL; j++) {
-				if (!strcmp(grp->gr_mem[j], pwd->pw_name))
-					break;
-			}
-			if (grp->gr_mem[j] != NULL) /* user already member of group */
+			grp = gr_add(grp, pwd->pw_name);
+			/*
+			 * grp can only be NULL in 2 cases:
+			 * - the new member is already a member
+			 * - a problem with memory occurs
+			 * in both cases we want to skip now.
+			 */
+			if (grp == NULL)
 				continue;
-
-			members = malloc(sizeof(char *) * (j + 2));
-			memcpy(members, grp->gr_mem, j * sizeof(*members));
-
-			members[j] = pwd->pw_name;
-			members[j+1] = NULL;
-			grp->gr_mem = members;
 			chggrent(cnf->groups[i], grp);
-			free(members);
 		}
 	}
 


More information about the svn-src-all mailing list