getgrnam和getgrgid函数得到用户组信息

tech2023-02-13  100

#include <stdio.h> #include <sys/types.h> #include <sys/types.h> #include <grp.h> #include <unistd.h> #if 0 struct group { char *gr_name; /* group name */ char *gr_passwd; /* group password */ gid_t gr_gid; /* group ID */ char **gr_mem; /* NULL-terminated array of pointers to names of group members */ }; #endif int main(int argc, const char *argv[]) { int ret = -1; struct group * gp = NULL; int i = 0; gp = getgrnam("root"); printf("%s\n", gp->gr_name); printf("%s\n", gp->gr_passwd); printf("%d\n", gp->gr_gid); while(gp->gr_mem[i]) printf("%s\n", gp->gr_mem[i++]); puts("*****************************************************"); gp = getgrgid(getgid()); printf("%s\n", gp->gr_name); printf("%s\n", gp->gr_passwd); printf("%d\n", gp->gr_gid); while(gp->gr_mem[i]) printf("%s\n", gp->gr_mem[i++]); return 0; }

测试结果

最新回复(0)