Blame SOURCES/coreutils-getgrouplist.patch

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