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.
运行结果如下:
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