#include <grp.h>
int getgrouplist(const char *user, gid_t group,
gid_t *groups, int *ngroups);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
/* これは glibc 2.3.2 ではクラッシュする。*/
#include <stdio.h>
#include <stdlib.h>
#include <grp.h>
#include <pwd.h>
int
main(void)
{
int i, ng = 0;
char *user = "who"; /* ユーザ名をここに書く。 */
gid_t *groups = NULL;
struct passwd *pw = getpwnam(user);
if (pw == NULL)
exit(EXIT_SUCCESS);
if (getgrouplist(user, pw->pw_gid, NULL, &ng) < 0) {
groups = (gid_t *) malloc(ng * sizeof (gid_t));
getgrouplist(user, pw->pw_gid, groups, &ng);
}
for (i = 0; i < ng; i++)
printf("%d\n", groups[i]);
exit(EXIT_SUCCESS);
}