Blame SOURCES/coreutils-getgrouplist.patch

dd59ef
diff --git a/lib/getugroups.c b/lib/getugroups.c
dd59ef
index 299bae6..8ece29b 100644
dd59ef
--- a/lib/getugroups.c
dd59ef
+++ b/lib/getugroups.c
dd59ef
@@ -19,6 +19,9 @@
dd59ef
 
dd59ef
 #include <config.h>
dd59ef
 
dd59ef
+/* We do not need this code if getgrouplist(3) is available.  */
dd59ef
+#ifndef HAVE_GETGROUPLIST
dd59ef
+
dd59ef
 #include "getugroups.h"
dd59ef
 
dd59ef
 #include <errno.h>
dd59ef
@@ -123,3 +126,4 @@ getugroups (int maxcount, gid_t *grouplist, char const *username,
dd59ef
 }
dd59ef
 
dd59ef
 #endif /* HAVE_GRP_H */
dd59ef
+#endif	/* have getgrouplist */
dd59ef
diff --git a/lib/mgetgroups.c b/lib/mgetgroups.c
dd59ef
index 76474c2..0a9d221 100644
dd59ef
--- a/lib/mgetgroups.c
dd59ef
+++ b/lib/mgetgroups.c
dd59ef
@@ -115,9 +115,17 @@ mgetgroups (char const *username, gid_t gid, gid_t **groups)
dd59ef
   /* else no username, so fall through and use getgroups. */
dd59ef
 #endif
dd59ef
 
dd59ef
-  max_n_groups = (username
dd59ef
-                  ? getugroups (0, NULL, username, gid)
dd59ef
-                  : getgroups (0, NULL));
dd59ef
+  if (!username)
dd59ef
+    max_n_groups = getgroups(0, NULL);
dd59ef
+  else 
dd59ef
+  {
dd59ef
+#ifdef HAVE_GETGROUPLIST
dd59ef
+    max_n_groups = 0;
dd59ef
+    getgrouplist (username, gid, NULL, &max_n_groups);
dd59ef
+#else
dd59ef
+    max_n_groups = getugroups (0, NULL, username, gid);
dd59ef
+#endif
dd59ef
+  }
dd59ef
 
dd59ef
   /* If we failed to count groups because there is no supplemental
dd59ef
      group support, then return an array containing just GID.
dd59ef
@@ -139,10 +147,25 @@ mgetgroups (char const *username, gid_t gid, gid_t **groups)
dd59ef
   if (g == NULL)
dd59ef
     return -1;
dd59ef
 
dd59ef
-  ng = (username
dd59ef
-        ? getugroups (max_n_groups, g, username, gid)
dd59ef
-        : getgroups (max_n_groups - (gid != (gid_t) -1),
dd59ef
-                                g + (gid != (gid_t) -1)));
dd59ef
+  if (!username)
dd59ef
+    ng = getgroups (max_n_groups - (gid != (gid_t)-1), g + (gid != (gid_t)-1));
dd59ef
+  else
dd59ef
+  {
dd59ef
+#ifdef HAVE_GETGROUPLIST
dd59ef
+    int e;
dd59ef
+    ng = max_n_groups;
dd59ef
+    while ((e = getgrouplist (username, gid, g, &ng)) == -1
dd59ef
+	&& ng > max_n_groups)
dd59ef
+    {
dd59ef
+      max_n_groups = ng;
dd59ef
+      g = xrealloc (g, max_n_groups * sizeof (GETGROUPS_T));
dd59ef
+    }
dd59ef
+    if (e == -1)
dd59ef
+      ng = -1;
dd59ef
+#else
dd59ef
+    ng = getugroups (max_n_groups, g, username, gid);
dd59ef
+#endif
dd59ef
+  }
dd59ef
 
dd59ef
   if (ng < 0)
dd59ef
     {
dd59ef
diff --git a/m4/jm-macros.m4 b/m4/jm-macros.m4
dd59ef
index 62777c7..5180243 100644
dd59ef
--- a/m4/jm-macros.m4
dd59ef
+++ b/m4/jm-macros.m4
dd59ef
@@ -78,6 +78,7 @@ 
dd59ef
     fchown
dd59ef
     fchmod
dd59ef
     ftruncate
dd59ef
+    getgrouplist
dd59ef
     iswspace
dd59ef
     mkfifo
dd59ef
     mbrlen