OSDN Git Service

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