Skip to content

qgetpwuid.3

Manvendra Bhangui edited this page Nov 22, 2023 · 5 revisions

NAME

qgetpwnam, qgetpwnam_r, qgetpwuid, qgetpwuid_r - get password file entry

SYNOPSIS

#include <sys/types.h>
#include <qgetpwgr.h>

struct passwd *qgetpwnam(const char *name);

struct passwd *qgetpwuid(uid_t uid);

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

int qgetpwuid_r(uid_t uid, struct passwd *pwd,
 char *buf, size_t buflen, struct passwd **result);

DESCRIPTION

The qgetpwnam() function returns a pointer to a structure containing the broken-out fields of the record in the password database, the local password file /etc/passwd, that matches the username name.

The qgetpwuid() function returns a pointer to a structure containing the broken-out fields of the record in the password database that matches the user ID uid.

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 */
};

See passwd(5) for more information about these fields.

The qgetpwnam_r() and qgetpwuid_r() functions obtain the same information as qgetpwnam() and qgetpwuid(), but store the retrieved passwd structure in the space pointed to by pwd. The string fields pointed to by the members of the passwd structure are stored in the buffer buf of size buflen. A pointer to the result (in case of success) or NULL (in case no entry was found or an error occurred) is stored in *result.

The call

sysconf(_SC_GETPW_R_SIZE_MAX)

returns either -1, without changing errno, or an initial suggested size for buf. (If this size is too small, the call fails with ERANGE, in which case the caller can retry with a larger buffer.)

RETURN VALUE

The qgetpwnam() and qgetpwuid() functions return a pointer to a passwd structure, or NULL if the matching entry is not found or an error occurs. If an error occurs, errno is set appropriately. If one wants to check errno after the call, it should be set to zero before the call.

The return value may point to a static area, and may be overwritten by subsequent calls to qgetpwent(3), qgetpwnam(), or qgetpwuid(). (Do not pass the returned pointer to free(3).)

On success, qgetpwnam_r() and qgetpwuid_r() return zero, and set *result to pwd. If no matching password record was found, these functions return 0 and store NULL in *result. In case of error, an error number is returned, and NULL is stored in *result.

ERRORS

0 or ENOENT"
The given name or uid was not found.

EINTR
A signal was caught; see signal(7).

ENOMEM
Insufficient memory to allocate passwd structure.

ERANGE
Insufficient buffer space supplied.

ETXTBUSY
Temporary error happened.

FILES

/etc/passwd
local password database file

The pw_dir field contains the name of the initial working directory of the user. Login programs use the value of this field to initialize the HOME environment variable for the login shell. An application that wants to determine its user's home directory should inspect the value of HOME (rather than the value qgetpwuid(getuid())->pw_dir) since this allows the user to modify their notion of "the home directory" during a login session. To determine the (initial) home directory of another user, it is necessary to use qgetpwnam("username")->pw_dir or similar.

SEE ALSO

qendpwent(3), qgetgrnam(3), qgetpwent(3), qsetpwent(3), passwd(5)

Clone this wiki locally