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 - 重建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