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