Unix Not sure how to use passwd structure

I have done some research and am still struggling with the passwd structure.

http://www.opengroup.org/onlinepubs/000095399/basedefs/pwd.h.html

I need to get the user id, but I donโ€™t think I understand it at all.

int getpwuid_r (uid_t, struct passwd *, char *, size_t, struct passwd **);

This method call returns a point in the structure that will contain all the data I need. I'm pretty confused about the options.

struct passwd. Should I announce this first? struct passwd passwd?

I just completely lost how to use it.

Finally, as soon as I type in my pointer. What challenges will I use to receive data? Thanks for any help.

+5
source share
3 answers

In the signature getpwuid_r:

int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, 
    size_t buflen, struct passwd **pwbufp);

uid- The input parameter is the UID of the user you want to find. The rest are essentially output parameters: the structure that it points pwbufto will be filled with password information, and the pointer that it points pwbufpto will be set to pwbufif the call was successful (and NULLif it hadnโ€™t). The pair of parameters bufand buflenindicates the user buffer that is used to store strings, which indicate the members of the structure struct passwdthat are returned.

You would use it like this (this is looking for a 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);
    }

, , getpwnam_r pw_uid .

+6

-, UID, , getpwnam_r(). , getpwuid_r(), , (char *) .

-, `struct passwd '. pwd.h.

-, :

int getpwnam_r(const char *name, struct passwd *pwd,
               char *buf, size_t buflen, struct passwd **result);

pwd output. , .

, getpwnam_r Linux getpwnam_r, .

1: http://manpages.ubuntu.com/manpages/jaunty/en/man3/getpwnam.3.html

+2

, -, , - , uid, getpwuid , uid. , getpwnam, ? uid ?

, , getpwnam, getpwuid - uid username.

. man:

struct passwd *getpwuid(uid_t uid);

int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t buflen, struct passwd **pwbufp);

getpwuid() , , ID .

getpwuid_r() , passwd , pwbuf.

, . , , , , :

struct passwd * my_passwd;
my_passwd = getpwuid(uid);
// or:
// my_passwd = getpwnam(username);

if (my_passwd == NULL) {
    // the lookup failed - handle the error!
} else {
    // the lookup succeeded - do your thing 
    printf("User name: %s\n", my_passwd->pw_name);
    printf("User password: %s\n", my_passwd->pw_passwd);
    ...
}

, , getpwuid .

( ) .

getpwuid_r , , , , .

+1

All Articles