( standard c libraries translation )fgetgrent

fgetpwent - get password file entry
fgetpwent - 获取密码文件的入口

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

struct passwd *fgetpwent(FILE *stream);

The  fgetpwent()  function returns a pointer to a structure containing the broken out fields of a line in the file stream.  The first time it is called it returns the first entry; thereafter, it returns successive entries.  The file referred to by stream must have the same format  as  /etc/passwd  (see passwd(5)).
fgetpwent函数返回一个指向包含文件流一行信息的结构体指针,第一次调用的时候返回第一个入口,从那以后每调用成功就会返回一个成功的入口,流所对应的文件的格式必须跟/etc/passwd一样

       The passwd structure is defined in <pwd.h> as follows:

           struct passwd {
               char   *pw_name;       /* username */
               char   *pw_passwd;     /* user password */
               uid_t   pw_uid;        /* user ID */
               gid_t   pw_gid;        /* group ID */
               char   *pw_gecos;      /* real name */
               char   *pw_dir;        /* home directory */
               char   *pw_shell;      /* shell program */
           };

The fgetpwent() function returns a pointer to a passwd structure, or NULL if there are no more entries or an error occurs.
fgetpwent函数返回一个指向组结构体的指针,如果没有更多的入口或者发生错误则返回NULL
ENOMEM Insufficient memory to allocate passwd structure.
分配passwd结构体的时候内存不足


fgetgrent - get group file entry
fgetgrent - 获取组文件的入口

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

struct group *fgetgrent(FILE *stream);

The  fgetgrent()  function returns a pointer to a structure containing the group information from the file referred to by stream.  The first time it is called it returns the first entry; thereafter, it returns successive entries.  The file referred to by stream must have the same format  as  /etc/group(see group(5)).
fgetgrent函数返回一个指向包含文件流group信息结构体的指针,第一次调用的时候返回第一个入口,从那以后每调用成功就会返回一个成功的入口,流所对应的文件的格式必须跟/etc/group一样

       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 */
           };

The fgetgrent() function returns a pointer to a group structure, or NULL if there are no more entries or an error occurs.
fgetgrent函数返回一个指向组结构体的指针,如果没有更多的入口或者发生错误则返回NULL

ENOMEM Insufficient memory to allocate group structure.

分配group结构体的时候内存不足


testcase如下:

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

int main(void)
{
	FILE *fpw, *fgr;
	struct passwd *pw;
	struct group *gr;

	fpw = fopen("/etc/passwd", "r");
	fgr = fopen("/etc/group", "r");

	pw = fgetpwent(fpw);
	printf("pw_name = %s\n", pw->pw_name);

	gr = fgetgrent(fgr);
	printf("gr_name = %s\n", gr->gr_name);
	return 0;
}

运行结果如下:

cheny.le@cheny-ThinkPad-T420:~/cheny/testCode$ ./a.out
pw_name = root
gr_name = root

查看/etc/group和/etc/passwd文件的第一个entry都是root

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值