]>
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 (): | |
f496f649 | 64 | .RS 4 |
0f200f07 MK |
65 | _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE || |
66 | _SVID_SOURCE || _POSIX_SOURCE | |
f496f649 | 67 | .RE |
cc4615cc | 68 | .ad b |
fea681da MK |
69 | .SH DESCRIPTION |
70 | The | |
63aa9df0 | 71 | .BR getpwnam () |
fea681da | 72 | function returns a pointer to a structure containing |
f936cf26 | 73 | the broken-out fields of the record in the password database |
c13182ef | 74 | (e.g., the local password file |
f2738b39 MK |
75 | .IR /etc/passwd , |
76 | NIS, and LDAP) | |
18701562 | 77 | that matches the username |
fea681da MK |
78 | .IR name . |
79 | .PP | |
80 | The | |
63aa9df0 | 81 | .BR getpwuid () |
fea681da | 82 | function returns a pointer to a structure containing |
f2738b39 | 83 | the broken-out fields of the record in the password database |
f936cf26 | 84 | that matches the user ID |
fea681da MK |
85 | .IR uid . |
86 | .PP | |
87 | The | |
63aa9df0 | 88 | .BR getpwnam_r () |
fea681da | 89 | and |
63aa9df0 | 90 | .BR getpwuid_r () |
f2738b39 MK |
91 | functions obtain the same information, but store the retrieved |
92 | .I passwd | |
93 | structure in the space pointed to by | |
3adc12e9 | 94 | .IR pwd . |
f2738b39 MK |
95 | This |
96 | .I passwd | |
97 | structure contains pointers to strings, and these strings | |
fea681da MK |
98 | are stored in the buffer |
99 | .I buf | |
100 | of size | |
101 | .IR buflen . | |
102 | A pointer to the result (in case of success) or NULL (in case no entry | |
103 | was found or an error occurred) is stored in | |
3adc12e9 | 104 | .IR *result . |
fea681da MK |
105 | .PP |
106 | The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows: | |
107 | .sp | |
bd191423 | 108 | .in +4n |
fea681da MK |
109 | .nf |
110 | struct passwd { | |
18701562 | 111 | char *pw_name; /* username */ |
f2738b39 | 112 | char *pw_passwd; /* user password */ |
34c97781 MK |
113 | uid_t pw_uid; /* user ID */ |
114 | gid_t pw_gid; /* group ID */ | |
f2738b39 MK |
115 | char *pw_gecos; /* real name */ |
116 | char *pw_dir; /* home directory */ | |
117 | char *pw_shell; /* shell program */ | |
fea681da MK |
118 | }; |
119 | .fi | |
bd191423 | 120 | .in |
fea681da MK |
121 | .PP |
122 | The maximum needed size for | |
123 | .I buf | |
124 | can be found using | |
125 | .BR sysconf (3) | |
c4bb193f MK |
126 | with the argument |
127 | .BR _SC_GETPW_R_SIZE_MAX . | |
fea681da | 128 | .SH "RETURN VALUE" |
60a90ecd MK |
129 | The |
130 | .BR getpwnam () | |
131 | and | |
132 | .BR getpwuid () | |
133 | functions return a pointer to a | |
f2738b39 MK |
134 | .I passwd |
135 | structure, or NULL if the matching entry is not found or | |
136 | an error occurs. | |
137 | If an error occurs, | |
fea681da | 138 | .I errno |
f2738b39 MK |
139 | is set appropriately. |
140 | If one wants to check | |
fea681da MK |
141 | .I errno |
142 | after the call, it should be set to zero before the call. | |
143 | .LP | |
57c1b002 | 144 | The return value may point to a static area, and may be overwritten |
fea681da | 145 | by subsequent calls to |
3a72373c | 146 | .BR getpwent (3), |
63aa9df0 | 147 | .BR getpwnam (), |
fea681da | 148 | or |
63aa9df0 | 149 | .BR getpwuid (). |
48e4cfa0 MK |
150 | (Do not pass the returned pointer to |
151 | .BR free (3).) | |
fea681da | 152 | .LP |
bbab485d | 153 | On success, |
60a90ecd MK |
154 | .BR getpwnam_r () |
155 | and | |
156 | .BR getpwuid_r () | |
bbab485d | 157 | return zero, and set |
3adc12e9 | 158 | .IR *result |
bbab485d | 159 | to |
3adc12e9 | 160 | .IR pwd . |
bbab485d MK |
161 | If no matching password record was found, |
162 | these functions return 0 and store NULL in | |
3adc12e9 | 163 | .IR *result . |
bbab485d | 164 | In case of error, an error number is returned, and NULL is stored in |
3adc12e9 | 165 | .IR *result . |
fea681da MK |
166 | .SH ERRORS |
167 | .TP | |
168 | .BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ... " | |
169 | The given | |
170 | .I name | |
171 | or | |
172 | .I uid | |
173 | was not found. | |
174 | .TP | |
175 | .B EINTR | |
176 | A signal was caught. | |
177 | .TP | |
178 | .B EIO | |
179 | I/O error. | |
180 | .TP | |
181 | .B EMFILE | |
2f0af33b MK |
182 | The maximum number |
183 | .RB ( OPEN_MAX ) | |
184 | of files was open already in the calling process. | |
fea681da MK |
185 | .TP |
186 | .B ENFILE | |
187 | The maximum number of files was open already in the system. | |
188 | .TP | |
189 | .B ENOMEM | |
f2738b39 | 190 | .\" not in POSIX |
b2133414 MK |
191 | Insufficient memory to allocate |
192 | .I passwd | |
193 | structure. | |
fea681da MK |
194 | .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45) |
195 | .TP | |
196 | .B ERANGE | |
197 | Insufficient buffer space supplied. | |
198 | .SH FILES | |
199 | .TP | |
200 | .I /etc/passwd | |
f2738b39 | 201 | local password database file |
fea681da | 202 | .SH "CONFORMING TO" |
44a2c328 | 203 | SVr4, 4.3BSD, POSIX.1-2001. |
fea681da | 204 | .SH NOTES |
68e1685c | 205 | The formulation given above under "RETURN VALUE" is from POSIX.1-2001. |
6b2fc294 | 206 | It does not call "not found" an error, and hence does not specify what value |
fea681da | 207 | .I errno |
c13182ef MK |
208 | might have in this situation. |
209 | But that makes it impossible to recognize | |
210 | errors. | |
211 | One might argue that according to POSIX | |
fea681da | 212 | .I errno |
c13182ef MK |
213 | should be left unchanged if an entry is not found. |
214 | Experiments on various | |
008f1ecc | 215 | UNIX-like systems show that lots of different values occur in this |
fea681da MK |
216 | situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM and probably others. |
217 | .\" more precisely: | |
218 | .\" AIX 5.1 - gives ESRCH | |
219 | .\" OSF1 4.0g - gives EWOULDBLOCK | |
db85058b MK |
220 | .\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT |
221 | .\" glibc since version 2.7 - give 0 | |
fea681da MK |
222 | .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM |
223 | .\" SunOS 5.8 - gives EBADF | |
224 | .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0 | |
6b2fc294 | 225 | |
c13182ef MK |
226 | The |
227 | .I pw_dir | |
228 | field contains the name of the initial working directory of the user. | |
2f0af33b MK |
229 | Login programs use the value of this field to initialize the |
230 | .B HOME | |
231 | environment variable for the login shell. | |
6b2fc294 | 232 | An application that wants to determine its user's home directory |
2f0af33b MK |
233 | should inspect the value of |
234 | .B HOME | |
235 | (rather than the value | |
94e9d9fe | 236 | .IR getpwuid(getuid())\->pw_dir ) |
6b2fc294 MK |
237 | since this allows the user to modify their notion of |
238 | "the home directory" during a login session. | |
239 | To determine the (initial) home directory of another user, | |
c13182ef | 240 | it is necessary to use |
94e9d9fe | 241 | .I getpwnam("username")\->pw_dir |
6b2fc294 | 242 | or similar. |
c6ec3685 MK |
243 | .SH EXAMPLE |
244 | The program below demonstrates the use of | |
245 | .BR getpwnam_r () | |
246 | to find the full username and user ID for the username | |
247 | supplied as a command-line argument. | |
248 | ||
249 | .nf | |
250 | #include <pwd.h> | |
251 | #include <stdio.h> | |
252 | #include <stdlib.h> | |
253 | #include <unistd.h> | |
254 | #include <errno.h> | |
255 | ||
256 | int | |
257 | main(int argc, char *argv[]) | |
258 | { | |
259 | struct passwd pwd; | |
260 | struct passwd *result; | |
261 | char *buf; | |
262 | size_t bufsize; | |
263 | int s; | |
264 | ||
265 | if (argc != 2) { | |
266 | fprintf(stderr, "Usage: %s username\\n", argv[0]); | |
267 | exit(EXIT_FAILURE); | |
268 | } | |
269 | ||
270 | bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); | |
271 | if (bufsize == \-1) /* Value was indeterminate */ | |
272 | bufsize = 16384; /* Should be more than enough */ | |
273 | ||
274 | buf = malloc(bufsize); | |
275 | if (buf == NULL) { | |
276 | perror("malloc"); | |
277 | exit(EXIT_FAILURE); | |
278 | } | |
279 | ||
280 | s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result); | |
281 | if (result == NULL) { | |
282 | if (s == 0) | |
283 | printf("Not found\\n"); | |
284 | else { | |
285 | errno = s; | |
286 | perror("getpwnam_r"); | |
287 | } | |
288 | exit(EXIT_FAILURE); | |
289 | } | |
290 | ||
42afc407 | 291 | printf("Name: %s; UID: %ld\\n", pwd.pw_gecos, (long) pwd.pw_uid); |
c6ec3685 MK |
292 | exit(EXIT_SUCCESS); |
293 | } | |
294 | .fi | |
fea681da MK |
295 | .SH "SEE ALSO" |
296 | .BR endpwent (3), | |
297 | .BR fgetpwent (3), | |
298 | .BR getgrnam (3), | |
299 | .BR getpw (3), | |
300 | .BR getpwent (3), | |
1ed50462 | 301 | .BR getspnam (3), |
fea681da MK |
302 | .BR putpwent (3), |
303 | .BR setpwent (3), | |
304 | .BR passwd (5) |