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