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