(standard c libraries translation )getpwent

getpwent, setpwent, endpwent - get password file entry
getpwent, setpwent, endpwent - 获取密码文件入口

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

struct passwd *getpwent(void);
void setpwent(void);
void endpwent(void);

The  getpwent()  function  returns  a  pointer to a structure containing the broken-out fields of a record from the password
database (e.g., the local password file /etc/passwd, NIS, and LDAP).  The first time getpwent() is called,  it  returns  the
first entry; thereafter, it returns successive entries.
The setpwent() function rewinds to the beginning of the password database.
The endpwent() function is used to close the password database after all processing has been performed.
getpwent函数返回一个指向包含密码数据库的文件的指针,第一次调用getpwent的时候,返回第一个入口,从那以后,将返回成功的入口
setpwent函数回滚到密码数据库的起始处
endpwent函数关闭密码数据库,在所有程序使用完毕之后

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;      /* user information */
       char   *pw_dir;        /* home directory */
       char   *pw_shell;      /* shell program */
   };

When  shadow(5) passwords are enabled (which is default on many GNU/Linux installations) the content of pw_passwd is usually
not very useful.  In such a case most passwords are stored in a separate file.
当shadow密码使用的时候,pw_passwd的内容通常没有什么用,在这种情况下,大多数密码都是单独存放的

The variable pw_shell may be empty, in which case the system will execute the default shell (/bin/sh) for the user.
pw_shell变量的值可能是空的,这种情况下system会使用默认的shell(/bin/sh)

For more information about the fields of this structure, see passwd(5).
如果需要更多关于这个结构体的信息,请参见passwd

The getpwent() function returns a pointer to a passwd structure, or NULL if there are no more entries or an  error  occured.
If an error occurs, errno is set appropriately.  If one wants to check errno after the call, it should be set to zero before
the call.
getpwent函数返回一直指向passwd结构体的指针,如果没有更多的入口或者发生了错误则返回NULL,如果发生了错误,errno会被设置成适当的值,如果需要在调用后检查errno的值,那么需要在调用前设置成0

The return value may point to a static area, and may be overwritten by subsequent calls to getpwent(), getpwnam(3), or getp‐
wuid(3).  (Do not pass the returned pointer to free(3).)
返回值指向一个静态区域,可能被后续的petpwent,getpwnam或者getpwuid调用覆盖

EINTR  A signal was caught.
一个信号量被捕获
EIO    I/O error.
I/0错误
EMFILE The maximum number (OPEN_MAX) of files was open already in the calling process.
调用程序已经打开了最多的文件
ENFILE The maximum number of files was open already in the system.
系统已经打开了最多的文件
ENOMEM Insufficient memory to allocate passwd structure.
分配passwd结构体的时候内存不足
ERANGE Insufficient buffer space supplied.
所提供的缓冲区不足

/etc/passwd      local password database file

本地密码数据库文件


testcase如下:

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

int main(void)
{
	struct passwd *pwd;

	pwd = getpwent();
	printf("pw_name = %s\npw_passwd = %s\n", pwd->pw_name, pwd->pw_passwd);

	pwd = getpwent();
	printf("pw_name = %s\npw_passwd = %s\n", pwd->pw_name, pwd->pw_passwd);
	setpwent();
	pwd = getpwent();
	printf("pw_name = %s\npw_passwd = %s\n", pwd->pw_name, pwd->pw_passwd);
	endpwent();
	return 0;
}

运行结果如下:

cheny@cheny-ThinkPad-T410:~/testCode/endpwent$ ./a.out
pw_name = root
pw_passwd = x
pw_name = daemon
pw_passwd = x
pw_name = root
pw_passwd = x

这个函数的用法跟getgrent类似,不过感觉都是属于用的比较少的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值