Unix的编程。不知道如何使用passwd结构 [英] Unix programming. Not sure how to use the passwd struct
问题描述
我已经做了一些研究,我仍然用passwd结构中的挣扎。
I've done some research and I'm still struggling with the passwd structure.
http://www.opengroup.org/onlinepubs/000095399/ basedefs / pwd.h.html
我不过需要获得用户ID我不认为我使用的是理解它。
I need to obtain the user ID however I dont think I'm using understanding it at all.
INT getpwuid_r(将uid_t,结构passwd文件*,字符*,为size_t,结构passwd文件**);
int getpwuid_r(uid_t, struct passwd *, char *, size_t, struct passwd **);
这个方法调用返回一个指向将包含所有我需要的数据结构。我在参数相当困惑。
This method call returns a point to a structure that will contain all the data I need. I'm fairly confused on the parameters.
结构passwd文件。我是否需要先声明这一点?结构passwd文件passwd文件?
struct passwd. Do I need to declare this first? struct passwd passwd?
我只是完全失去了对如何使用它。
I'm just totally lost on how to use this.
最后,当我补我的指针。什么叫我将用获得的数据?感谢您的帮助。
Lastly, once I fill my pointer. What calls will I use to get the data? Thanks for any help.
推荐答案
在 getpwuid_r
签名:
int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf,
size_t buflen, struct passwd **pwbufp);
UID
是输入参数 - 这是您要查找用户的UID。其余的基本上是输出参数:结构指向 pwbuf
将与密码信息填写,指针由 pwbufp $指了指C $ C>将被设置为
pwbuf
如果调用成功(的价值和 NULL
如果不是)。在 BUF
和 buflen
对参数指定将用于存储字符串指向用户提供的缓冲区在结构的passwd
结构返回的成员。
uid
is an input parameter - it is the UID of the user that you want to look up. The rest are essentially output parameters: the structure pointed to by pwbuf
will be filled with the password information, and the pointer pointed to by pwbufp
will be set to the value of pwbuf
if the call was successful (and NULL
if it was not). The buf
and buflen
pair of parameters specify a user-supplied buffer that will be used to store the strings pointed to by members of the struct passwd
structure that is returned.
您会使用它像这样(这查找用户的UID 101):
You would use it like so (this looks up the user with UID 101):
struct passwd pwent;
struct passwd *pwentp;
char buf[1024];
if (getpwuid_r(101, &pwent, buf, sizeof buf, &pwentp))
{
perror("getpwuid_r");
}
else
{
printf("Username: %s\n", pwent.pw_name);
printf("Real Name: %s\n", pwent.pw_gecos);
printf("Home Directory: %s\n", pwent.pw_dir);
}
如果instread你想通过名称来查找用户找到自己的ID,使用 getpwnam_r
并检查 pw_uid
返回结构的领域。
If instread you want to look up a user by name to find their ID, use getpwnam_r
and examine the pw_uid
field of the returned struct.
这篇关于Unix的编程。不知道如何使用passwd结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!