(standard c libraries translation )getpw

getpw - reconstruct password line entry
getpw - 重建password行入口

所需头文件
#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <sys/types.h>
#include <pwd.h>

int getpw(uid_t uid, char *buf);

The getpw() function reconstructs the password line entry for the given user ID uid in the buffer buf.  The returned buffer contains a line of format
              name:passwd:uid:gid:gecos:dir:shell
getpw函数重构所给的uid的password行的入口到buf缓冲区中,返回的buffer一般是如下格式:
              name:passwd:uid:gid:gecos:dir:shell
       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 */
           };
       For more information about the fields of this structure, see passwd(5).

The getpw() function returns 0 on success; on error, it returns -1, and errno is set to indicate the error.
getpw函数成功的时候返回0,失败的时候返回-1,errno用来指代错误

EINVAL buf is NULL.
buf是NULL
ENOENT No user corresponding to uid.
不存在uid所关联的id
ENOMEM Insufficient memory to allocate passwd structure.
分配passwd结构体的时候内存不足

FILES /etc/passwd password database file
password数据库的文件

The getpw() function is dangerous as it may overflow the provided buffer buf.  It is obsoleted by getpwuid(3).

getpw函数有buf缓冲区溢出的危险,已经被getpwuid所替代


testcase如下:

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

int main(void)
{
	uid_t uid = 1001;
	char buf[100];

	getpw(uid, buf);
	printf("buf = %s\n", buf);
	return 0;
}

运行结果如下:

cheny.le@cheny-ThinkPad-T420:~/cheny/testCode$ ./a.out
buf = cheny.le:x:1001:1001:cheny.le,,,:/home/cheny.le:/bin/bash


验证结果如下:

cheny.le@cheny-ThinkPad-T420:~/cheny/testCode$ cat /etc/passwd |busybox grep 1001
cheny.le:x:1001:1001:cheny.le,,,:/home/cheny.le:/bin/bash

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值