Blame SOURCES/coreutils-getgrouplist.patch

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