I've done some research and I'm still struggling with the passwd structure.
http://www.opengroup.org/onlinepubs/000095399/basedefs/pwd.h.html
I need to obtain the user ID however I dont think I'm using understanding it at all.
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.
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.
In the getpwuid_r
signature:
int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf,
size_t buflen, struct passwd **pwbufp);
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.
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);
}
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.