]>
Commit | Line | Data |
---|---|---|
fea681da | 1 | .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) |
c6ec3685 MK |
2 | .\" and Copyright 2008, Linux Foundation, written by Michael Kerrisk |
3 | .\" <mtk.manpages@gmail.com> | |
fea681da MK |
4 | .\" |
5 | .\" Permission is granted to make and distribute verbatim copies of this | |
6 | .\" manual provided the copyright notice and this permission notice are | |
7 | .\" preserved on all copies. | |
8 | .\" | |
9 | .\" Permission is granted to copy and distribute modified versions of this | |
10 | .\" manual under the conditions for verbatim copying, provided that the | |
11 | .\" entire resulting derived work is distributed under the terms of a | |
12 | .\" permission notice identical to this one. | |
c13182ef | 13 | .\" |
fea681da MK |
14 | .\" Since the Linux kernel and libraries are constantly changing, this |
15 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
16 | .\" responsibility for errors or omissions, or for damages resulting from | |
17 | .\" the use of the information contained herein. The author(s) may not | |
18 | .\" have taken the same level of care in the production of this manual, | |
19 | .\" which is licensed free of charge, as they might when working | |
20 | .\" professionally. | |
c13182ef | 21 | .\" |
fea681da MK |
22 | .\" Formatted or processed versions of this manual, if unaccompanied by |
23 | .\" the source, must acknowledge the copyright and authors of this work. | |
24 | .\" | |
25 | .\" References consulted: | |
26 | .\" Linux libc source code | |
6b2fc294 | 27 | .\" Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991) |
fea681da MK |
28 | .\" 386BSD man pages |
29 | .\" | |
30 | .\" Modified 1993-07-24 by Rik Faith (faith@cs.unc.edu) | |
31 | .\" Modified 1996-05-27 by Martin Schulze (joey@linux.de) | |
32 | .\" Modified 2003-11-15 by aeb | |
c6ec3685 | 33 | .\" 2008-11-07, mtk, Added an example program for getpwnam_r(). |
fea681da | 34 | .\" |
48e4cfa0 | 35 | .TH GETPWNAM 3 2009-03-30 "GNU" "Linux Programmer's Manual" |
fea681da MK |
36 | .SH NAME |
37 | getpwnam, getpwnam_r, getpwuid, getpwuid_r \- get password file entry | |
38 | .SH SYNOPSIS | |
39 | .nf | |
40 | .B #include <sys/types.h> | |
41 | .B #include <pwd.h> | |
42 | .sp | |
43 | .BI "struct passwd *getpwnam(const char *" name ); | |
44 | .sp | |
45 | .BI "struct passwd *getpwuid(uid_t " uid ); | |
46 | .sp | |
3adc12e9 | 47 | .BI "int getpwnam_r(const char *" name ", struct passwd *" pwd , |
fea681da | 48 | .br |
3adc12e9 | 49 | .BI " char *" buf ", size_t " buflen ", struct passwd **" result ); |
fea681da | 50 | .sp |
3adc12e9 | 51 | .BI "int getpwuid_r(uid_t " uid ", struct passwd *" pwd , |
fea681da | 52 | .br |
3adc12e9 | 53 | .BI " char *" buf ", size_t " buflen ", struct passwd **" result ); |
fea681da | 54 | .fi |
cc4615cc MK |
55 | .sp |
56 | .in -4n | |
57 | Feature Test Macro Requirements for glibc (see | |
58 | .BR feature_test_macros (7)): | |
59 | .in | |
60 | .sp | |
61 | .ad l | |
62 | .BR getpwnam_r (), | |
63 | .BR getpwuid_r (): | |
0f200f07 MK |
64 | _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE || |
65 | _SVID_SOURCE || _POSIX_SOURCE | |
cc4615cc | 66 | .ad b |
fea681da MK |
67 | .SH DESCRIPTION |
68 | The | |
63aa9df0 | 69 | .BR getpwnam () |
fea681da | 70 | function returns a pointer to a structure containing |
f936cf26 | 71 | the broken-out fields of the record in the password database |
c13182ef | 72 | (e.g., the local password file |
f2738b39 MK |
73 | .IR /etc/passwd , |
74 | NIS, and LDAP) | |
18701562 | 75 | that matches the username |
fea681da MK |
76 | .IR name . |
77 | .PP | |
78 | The | |
63aa9df0 | 79 | .BR getpwuid () |
fea681da | 80 | function returns a pointer to a structure containing |
f2738b39 | 81 | the broken-out fields of the record in the password database |
f936cf26 | 82 | that matches the user ID |
fea681da MK |
83 | .IR uid . |
84 | .PP | |
85 | The | |
63aa9df0 | 86 | .BR getpwnam_r () |
fea681da | 87 | and |
63aa9df0 | 88 | .BR getpwuid_r () |
f2738b39 MK |
89 | functions obtain the same information, but store the retrieved |
90 | .I passwd | |
91 | structure in the space pointed to by | |
3adc12e9 | 92 | .IR pwd . |
f2738b39 MK |
93 | This |
94 | .I passwd | |
95 | structure contains pointers to strings, and these strings | |
fea681da MK |
96 | are stored in the buffer |
97 | .I buf | |
98 | of size | |
99 | .IR buflen . | |
100 | A pointer to the result (in case of success) or NULL (in case no entry | |
101 | was found or an error occurred) is stored in | |
3adc12e9 | 102 | .IR *result . |
fea681da MK |
103 | .PP |
104 | The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows: | |
105 | .sp | |
bd191423 | 106 | .in +4n |
fea681da MK |
107 | .nf |
108 | struct passwd { | |
18701562 | 109 | char *pw_name; /* username */ |
f2738b39 | 110 | char *pw_passwd; /* user password */ |
34c97781 MK |
111 | uid_t pw_uid; /* user ID */ |
112 | gid_t pw_gid; /* group ID */ | |
f2738b39 MK |
113 | char *pw_gecos; /* real name */ |
114 | char *pw_dir; /* home directory */ | |
115 | char *pw_shell; /* shell program */ | |
fea681da MK |
116 | }; |
117 | .fi | |
bd191423 | 118 | .in |
fea681da MK |
119 | .PP |
120 | The maximum needed size for | |
121 | .I buf | |
122 | can be found using | |
123 | .BR sysconf (3) | |
c4bb193f MK |
124 | with the argument |
125 | .BR _SC_GETPW_R_SIZE_MAX . | |
fea681da | 126 | .SH "RETURN VALUE" |
60a90ecd MK |
127 | The |
128 | .BR getpwnam () | |
129 | and | |
130 | .BR getpwuid () | |
131 | functions return a pointer to a | |
f2738b39 MK |
132 | .I passwd |
133 | structure, or NULL if the matching entry is not found or | |
134 | an error occurs. | |
135 | If an error occurs, | |
fea681da | 136 | .I errno |
f2738b39 MK |
137 | is set appropriately. |
138 | If one wants to check | |
fea681da MK |
139 | .I errno |
140 | after the call, it should be set to zero before the call. | |
141 | .LP | |
57c1b002 | 142 | The return value may point to a static area, and may be overwritten |
fea681da | 143 | by subsequent calls to |
3a72373c | 144 | .BR getpwent (3), |
63aa9df0 | 145 | .BR getpwnam (), |
fea681da | 146 | or |
63aa9df0 | 147 | .BR getpwuid (). |
48e4cfa0 MK |
148 | (Do not pass the returned pointer to |
149 | .BR free (3).) | |
fea681da | 150 | .LP |
bbab485d | 151 | On success, |
60a90ecd MK |
152 | .BR getpwnam_r () |
153 | and | |
154 | .BR getpwuid_r () | |
bbab485d | 155 | return zero, and set |
3adc12e9 | 156 | .IR *result |
bbab485d | 157 | to |
3adc12e9 | 158 | .IR pwd . |
bbab485d MK |
159 | If no matching password record was found, |
160 | these functions return 0 and store NULL in | |
3adc12e9 | 161 | .IR *result . |
bbab485d | 162 | In case of error, an error number is returned, and NULL is stored in |
3adc12e9 | 163 | .IR *result . |
fea681da MK |
164 | .SH ERRORS |
165 | .TP | |
166 | .BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ... " | |
167 | The given | |
168 | .I name | |
169 | or | |
170 | .I uid | |
171 | was not found. | |
172 | .TP | |
173 | .B EINTR | |
174 | A signal was caught. | |
175 | .TP | |
176 | .B EIO | |
177 | I/O error. | |
178 | .TP | |
179 | .B EMFILE | |
2f0af33b MK |
180 | The maximum number |
181 | .RB ( OPEN_MAX ) | |
182 | of files was open already in the calling process. | |
fea681da MK |
183 | .TP |
184 | .B ENFILE | |
185 | The maximum number of files was open already in the system. | |
186 | .TP | |
187 | .B ENOMEM | |
f2738b39 | 188 | .\" not in POSIX |
b2133414 MK |
189 | Insufficient memory to allocate |
190 | .I passwd | |
191 | structure. | |
fea681da MK |
192 | .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45) |
193 | .TP | |
194 | .B ERANGE | |
195 | Insufficient buffer space supplied. | |
196 | .SH FILES | |
197 | .TP | |
198 | .I /etc/passwd | |
f2738b39 | 199 | local password database file |
fea681da | 200 | .SH "CONFORMING TO" |
44a2c328 | 201 | SVr4, 4.3BSD, POSIX.1-2001. |
fea681da | 202 | .SH NOTES |
68e1685c | 203 | The formulation given above under "RETURN VALUE" is from POSIX.1-2001. |
6b2fc294 | 204 | It does not call "not found" an error, and hence does not specify what value |
fea681da | 205 | .I errno |
c13182ef MK |
206 | might have in this situation. |
207 | But that makes it impossible to recognize | |
208 | errors. | |
209 | One might argue that according to POSIX | |
fea681da | 210 | .I errno |
c13182ef MK |
211 | should be left unchanged if an entry is not found. |
212 | Experiments on various | |
6b2fc294 | 213 | Unix-like systems show that lots of different values occur in this |
fea681da MK |
214 | situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM and probably others. |
215 | .\" more precisely: | |
216 | .\" AIX 5.1 - gives ESRCH | |
217 | .\" OSF1 4.0g - gives EWOULDBLOCK | |
db85058b MK |
218 | .\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT |
219 | .\" glibc since version 2.7 - give 0 | |
fea681da MK |
220 | .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM |
221 | .\" SunOS 5.8 - gives EBADF | |
222 | .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0 | |
6b2fc294 | 223 | |
c13182ef MK |
224 | The |
225 | .I pw_dir | |
226 | field contains the name of the initial working directory of the user. | |
2f0af33b MK |
227 | Login programs use the value of this field to initialize the |
228 | .B HOME | |
229 | environment variable for the login shell. | |
6b2fc294 | 230 | An application that wants to determine its user's home directory |
2f0af33b MK |
231 | should inspect the value of |
232 | .B HOME | |
233 | (rather than the value | |
94e9d9fe | 234 | .IR getpwuid(getuid())\->pw_dir ) |
6b2fc294 MK |
235 | since this allows the user to modify their notion of |
236 | "the home directory" during a login session. | |
237 | To determine the (initial) home directory of another user, | |
c13182ef | 238 | it is necessary to use |
94e9d9fe | 239 | .I getpwnam("username")\->pw_dir |
6b2fc294 | 240 | or similar. |
c6ec3685 MK |
241 | .SH EXAMPLE |
242 | The program below demonstrates the use of | |
243 | .BR getpwnam_r () | |
244 | to find the full username and user ID for the username | |
245 | supplied as a command-line argument. | |
246 | ||
247 | .nf | |
248 | #include <pwd.h> | |
249 | #include <stdio.h> | |
250 | #include <stdlib.h> | |
251 | #include <unistd.h> | |
252 | #include <errno.h> | |
253 | ||
254 | int | |
255 | main(int argc, char *argv[]) | |
256 | { | |
257 | struct passwd pwd; | |
258 | struct passwd *result; | |
259 | char *buf; | |
260 | size_t bufsize; | |
261 | int s; | |
262 | ||
263 | if (argc != 2) { | |
264 | fprintf(stderr, "Usage: %s username\\n", argv[0]); | |
265 | exit(EXIT_FAILURE); | |
266 | } | |
267 | ||
268 | bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); | |
269 | if (bufsize == \-1) /* Value was indeterminate */ | |
270 | bufsize = 16384; /* Should be more than enough */ | |
271 | ||
272 | buf = malloc(bufsize); | |
273 | if (buf == NULL) { | |
274 | perror("malloc"); | |
275 | exit(EXIT_FAILURE); | |
276 | } | |
277 | ||
278 | s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result); | |
279 | if (result == NULL) { | |
280 | if (s == 0) | |
281 | printf("Not found\\n"); | |
282 | else { | |
283 | errno = s; | |
284 | perror("getpwnam_r"); | |
285 | } | |
286 | exit(EXIT_FAILURE); | |
287 | } | |
288 | ||
289 | printf("Name: %s; UID: %ld\n", pwd.pw_gecos, (long) pwd.pw_uid); | |
290 | exit(EXIT_SUCCESS); | |
291 | } | |
292 | .fi | |
fea681da MK |
293 | .SH "SEE ALSO" |
294 | .BR endpwent (3), | |
295 | .BR fgetpwent (3), | |
296 | .BR getgrnam (3), | |
297 | .BR getpw (3), | |
298 | .BR getpwent (3), | |
1ed50462 | 299 | .BR getspnam (3), |
fea681da MK |
300 | .BR putpwent (3), |
301 | .BR setpwent (3), | |
302 | .BR passwd (5) |