Blame SOURCES/coreutils-getgrouplist.patch

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