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