From 08e09354b2b3904f36b79a9029dd0d29c5f7c965 Mon Sep 17 00:00:00 2001 From: nekral-guest Date: Fri, 28 Dec 2007 10:30:39 +0000 Subject: [PATCH] find_new_gid is never called when an GID is specified with -g. Simplify find_new_gid accordingly. --- ChangeLog | 2 ++ src/groupadd.c | 39 +++++++++++---------------------------- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/ChangeLog b/ChangeLog index 33f573e2..edd2f08e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,8 @@ cannot exist at that time. Remove the check. * src/groupadd.c (find_new_gid): If oflg is set, gflg is also set. Use (!gflg), which is clearer than (!gflg || !oflg). + * src/groupadd.c (find_new_gid): find_new_gid is never called when an + GID is specified with -g. Simplify find_new_gid accordingly. 2007-12-28 Nicolas François diff --git a/src/groupadd.c b/src/groupadd.c index 1eb56f76..aba4e361 100644 --- a/src/groupadd.c +++ b/src/groupadd.c @@ -31,6 +31,7 @@ #ident "$Id$" +#include #include #include #include @@ -190,51 +191,33 @@ static void grp_update (void) * find_new_gid - find the next available GID * * find_new_gid() locates the next highest unused GID in the group - * file, or checks the given group ID against the existing ones for - * uniqueness. + * file. */ static void find_new_gid (void) { const struct group *grp; gid_t gid_min, gid_max; + /* + * It doesn't make sense to use find_new_uid(), + * if a GID is specified via "-g" option. + */ + assert (!gflg); + gid_min = getdef_unum ("GID_MIN", 1000); gid_max = getdef_unum ("GID_MAX", 60000); /* - * Start with some GID value if the user didn't provide us with - * one already. - */ - - if (!gflg) { - group_id = gid_min; - } - - /* - * Search the entire group file, either looking for this GID (if the - * user specified one with -g) or looking for the largest unused + * Search the entire group file, looking for the largest unused * value. */ setgrent (); while ((grp = getgrent ())) { - if (gflg && (group_id == grp->gr_gid)) { - if (fflg) { - /* turn off -g and search again */ - gflg = 0; - setgrent (); - continue; - } - fprintf (stderr, _("%s: GID %u is not unique\n"), - Prog, (unsigned int) group_id); - fail_exit (E_GID_IN_USE); - } - if (!gflg && (grp->gr_gid >= group_id)) { - if (grp->gr_gid > gid_max) - continue; + if ((grp->gr_gid >= group_id) && (grp->gr_gid <= gid_max)) { group_id = grp->gr_gid + 1; } } - if (!gflg && (group_id == (gid_max + 1))) { + if (group_id == (gid_max + 1)) { for (group_id = gid_min; group_id < gid_max; group_id++) { /* local, no need for xgetgrgid */ if (!getgrgid (group_id)) {