1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <grp.h> #include <sys/types.h> #include <string.h> int initgroups(const char *user, gid_t group) { int n=0; gid_t grouplist[32]; struct group *g; setgrent(); while ((g=getgrent())) { char **duh=g->gr_mem; while (*duh) { if (!strcmp(*duh,user)) { grouplist[n]=g->gr_gid; if (++n>=32) break; } duh++; } } return setgroups(n,grouplist); }