1GETPWNAM(3) Linux Programmer's Manual GETPWNAM(3)
2
3
4
6 getpwnam, getpwnam_r, getpwuid, getpwuid_r - get password file entry
7
9 #include <sys/types.h>
10 #include <pwd.h>
11
12 struct passwd *getpwnam(const char *name);
13
14 struct passwd *getpwuid(uid_t uid);
15
16 int getpwnam_r(const char *name, struct passwd *pwbuf,
17 char *buf, size_t buflen, struct passwd **pwbufp);
18
19 int getpwuid_r(uid_t uid, struct passwd *pwbuf,
20 char *buf, size_t buflen, struct passwd **pwbufp);
21
23 The getpwnam() function returns a pointer to a structure containing the
24 broken-out fields of the record in the password database (e.g., the
25 local password file /etc/passwd, NIS, and LDAP) that matches the user
26 name name.
27
28 The getpwuid() function returns a pointer to a structure containing the
29 broken-out fields of the record in the password database that matches
30 the user ID uid.
31
32 The getpwnam_r() and getpwuid_r() functions obtain the same informa‐
33 tion, but store the retrieved passwd structure in the space pointed to
34 by pwbuf. This passwd structure contains pointers to strings, and
35 these strings are stored in the buffer buf of size buflen. A pointer
36 to the result (in case of success) or NULL (in case no entry was found
37 or an error occurred) is stored in *pwbufp.
38
39 The passwd structure is defined in <pwd.h> as follows:
40
41 struct passwd {
42 char *pw_name; /* user name */
43 char *pw_passwd; /* user password */
44 uid_t pw_uid; /* user ID */
45 gid_t pw_gid; /* group ID */
46 char *pw_gecos; /* real name */
47 char *pw_dir; /* home directory */
48 char *pw_shell; /* shell program */
49 };
50
51 The maximum needed size for buf can be found using sysconf(3) with the
52 _SC_GETPW_R_SIZE_MAX parameter.
53
55 The getpwnam() and getpwuid() functions return a pointer to a passwd
56 structure, or NULL if the matching entry is not found or an error
57 occurs. If an error occurs, errno is set appropriately. If one wants
58 to check errno after the call, it should be set to zero before the
59 call.
60
61 The return value may point to static area, and may be overwritten by
62 subsequent calls to getpwent(), getpwnam(), or getpwuid().
63
64 The getpwnam_r() and getpwuid_r() functions return zero on success. In
65 case of error, an error number is returned.
66
68 0 or ENOENT or ESRCH or EBADF or EPERM or ...
69 The given name or uid was not found.
70
71 EINTR A signal was caught.
72
73 EIO I/O error.
74
75 EMFILE The maximum number (OPEN_MAX) of files was open already in the
76 calling process.
77
78 ENFILE The maximum number of files was open already in the system.
79
80 ENOMEM Insufficient memory to allocate passwd structure.
81
82 ERANGE Insufficient buffer space supplied.
83
85 /etc/passwd
86 local password database file
87
89 SVr4, 4.3BSD, POSIX.1-2001
90
92 The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
93 It does not call "not found" an error, and hence does not specify what
94 value errno might have in this situation. But that makes it impossible
95 to recognize errors. One might argue that according to POSIX errno
96 should be left unchanged if an entry is not found. Experiments on vari‐
97 ous Unix-like systems show that lots of different values occur in this
98 situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM and probably
99 others.
100
101 The pw_dir field contains the name of the initial working directory of
102 the user. Login programs use the value of this field to initialize the
103 HOME environment variable for the login shell. An application that
104 wants to determine its user's home directory should inspect the value
105 of HOME (rather than the value getpwuid(getuid())->pw_dir) since this
106 allows the user to modify their notion of "the home directory" during a
107 login session. To determine the (initial) home directory of another
108 user, it is necessary to use getpwnam("username")->pw_dir or similar.
109
111 endpwent(3), fgetpwent(3), getgrnam(3), getpw(3), getpwent(3), putp‐
112 went(3), setpwent(3), passwd(5)
113
114
115
116GNU 1996-05-27 GETPWNAM(3)