OSDN Git Service

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