( standard c libraries translation )getgrent

getgrent, setgrent, endgrent - get group file entry
getgrent, setgrent, endgrent - 获取group文件的入口

所需头文件
#include <sys/types.h>
#include <grp.h>

struct group *getgrent(void);
void setgrent(void);
void endgrent(void);

The getgrent() function returns a pointer to a structure containing the broken-out fields of a record in the group database (e.g., the local group file /etc/group, NIS, and LDAP).  The first time getgrent() is called, it returns the first entry; thereafter, it returns successive entries.
The setgrent() function rewinds to the beginning of the group database, to allow repeated scans.
The endgrent() function is used to close the group database after all processing has been performed.
getgrent函数返回指向包含group datebase结构体的指针(本地group文件一般是/etc/group),第一次调用getgrent的话,返回第一个入口,从那以后,返回成功的入口
setgrent函数回滚到group datebase的开始处,这样子可以允许重复扫描
endgrent函数用来关闭group datebase,当所有程序都已经用完之后

The group structure is defined in <grp.h> as follows:
           struct group {
               char   *gr_name;       /* group name */
               char   *gr_passwd;     /* group password */
               gid_t   gr_gid;        /* group ID */
               char  **gr_mem;        /* group members */
           };

       For more information about the fields of this structure, see group(5).


The getgrent() function returns a pointer to a group structure, or NULL if there are no more entries or an error occurs.
Upon error, errno may be set.  If one wants to check errno after the call, it should be set to zero before the call.
The return value may point to a static area, and may be overwritten by subsequent calls to getgrent(), getgrgid(3), or getgrnam(3).  (Do not  pass  the returned pointer to free(3).)
getgrent函数返回一个指向group结构体的指针,如果没有更多的入口,则返回NULL
出错的时候errno会被设置,如果需要在调用之后检测errno,它必须在调用之前设置为0
返回一个指向静态数据区的指针,可能会被后续调用getgrent,getgrgid,getgrnam所覆盖(不要把返回值传递给free,静态数据区的数据不能free,这个是常识)

ERRORS
EINTR  A signal was caught.
信号量被捕捉到了
EIO    I/O error.
I/O错误
EMFILE The calling process already has too many open files.
调用的进程已经有太多打开的文件
ENFILE Too many open files in the system.
系统已经有太多打开的文件
ENOMEM Insufficient memory to allocate group structure.
创建group结构体的时候内存不足
ERANGE Insufficient buffer space supplied.
提供buffer的空间不足

/etc/group local group database file
/etc/group 本地gourp datebase文件


testcase如下:

#include <sys/types.h>
#include <grp.h>
#include <stdio.h>

int main(void)
{
	struct group *grp;

	grp = getgrent();
	printf("group name = %s\n", grp->gr_name);

	grp = getgrent();
	printf("group name = %s\n", grp->gr_name);

	setgrent();
	grp = getgrent();
	printf("group name = %s\n", grp->gr_name);
	endgrent();
	return 0;
}

运行结果如下:

cheny.le@cheny-ThinkPad-T420:~/cheny/testCode$ ./a.out
group name = root
group name = daemon
group name = root

通过这个testcase应该可以看到setgrent函数的rewind作用了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值