Blame SOURCES/coreutils-getgrouplist.patch

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