在系统文件中,跟口令文件类似,存在这组文件,基本结构跟口令文件相同。
/* The group structure. */ struct group { char *gr_name; /* Group name. */ char *gr_passwd; /* Password. */ gid_t gr_gid; /* Group ID. */ char **gr_mem; /* Member list. */ };
/**** 函数功能:获取组文件信息 返回值:若成功返回指针,出错则返回NULL; 函数原型: *****/ /* Search for an entry with a matching group ID. */ struct group *getgrgid (gid_t gid); /* Search for an entry with a matching group name. */ struct group *getgrnam (const char *name);
测试程序
#include <grp.h> #include <sys/types.h> #include <unistd.h> #include "apue.h" int main(void) { struct group *ptr; gid_t gi = getgid(); ptr = getgrgid(gi); if(NULL == ptr) perror("error."); printf("gr_name:%s\n",ptr->gr_name); printf("gr_gid:%d\n",ptr->gr_gid); printf("gr_pwd:%s\n",ptr->gr_passwd); exit(0); }
/**** * 函数功能:查看整个组文件信息; * 返回值:若成功则返回指针,若出错或达到文件尾则返回NULL; * 函数原型: * */ /* Read an entry from the group-file stream, opening it if necessary. */ struct group *getgrent (void); /* Rewind the group-file stream. */ void setgrent (void); /* Close the group-file stream. */ void endgrent (void);
测试程序
#include <grp.h> #include <sys/types.h> #include <unistd.h> #include "apue.h" int main(void) { struct group *ptr; setgrent(); ptr = getgrent(); if(NULL == ptr) perror("error."); printf("gr_name:%s\n",ptr->gr_name); printf("gr_gid:%d\n",ptr->gr_gid); printf("gr_passwd:%s\n",ptr->gr_passwd); endgrent(); exit(0); }参考资料:
《unix高级环境编程》