implement getgrouplist (for initgroups), formerly dummied-out v0.7.8
authorRich Felker <dalias@aerifal.cx>
Wed, 13 Apr 2011 13:39:47 +0000 (09:39 -0400)
committerRich Felker <dalias@aerifal.cx>
Wed, 13 Apr 2011 13:39:47 +0000 (09:39 -0400)
src/misc/getgrouplist.c

index 88f273d77871d662f225786f264888a2771a7d8f..63557afe87681b07de17faa1804ea356bf15820e 100644 (file)
@@ -1,11 +1,23 @@
 #include <grp.h>
-
-/* FIXME */
+#include <string.h>
+#include <limits.h>
 
 int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
 {
+       size_t n, i;
+       struct group *gr;
        if (*ngroups<1) return -1;
-       *groups = gid;
+       n = *ngroups;
+       *groups++ = gid;
        *ngroups = 1;
-       return 0;
+
+       setgrent();
+       while ((gr = getgrent()) && *ngroups < INT_MAX) {
+               for (i=0; gr->gr_mem[i] && strcmp(user, gr->gr_mem[i]); i++);
+               if (!gr->gr_mem[i]) continue;
+               if (++*ngroups <= n) *groups++ = gr->gr_gid;
+       }
+       endgrent();
+
+       return *ngroups > n ? -1 : *ngroups;
 }