一、介绍
getgrent(),setgrent(),endgrent() 组合起来使用按顺序扫描密码文件(/etc/group)中的所有的各项记录。
#include <sys/types.h>
#include <grp.h>
// 调用会自动打开组文件,不需要执行open打开
// 每次调用会按顺序读取各项,有点类似文件操作的lseek
struct group *getgrent(void);
// 调用重返文件的起始处
void setgrent(void);
// 调用相当于关闭组文件,不需要执行close关闭
void endgrent(void);
二、实例
#include <sys/stat.h>
#include <sys/types.h>
#include <grp.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
void print_grp(struct group *grp)
{
printf("grp->gr_name : [%s]