2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1998-1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid
[] = "$Id: getpwent_r.c,v 1.5.2.1 2004/03/09 09:17:31 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
22 #include <port_before.h>
23 #if !defined(_REENTRANT) || !defined(DO_PTHREADS) || !defined(WANT_IRS_PW)
24 static int getpwent_r_not_required
= 0;
29 #include <sys/types.h>
30 #if (defined(POSIX_GETPWNAM_R) || defined(POSIX_GETPWUID_R))
31 #if defined(_POSIX_PTHREAD_SEMANTICS)
32 /* turn off solaris remapping in <grp.h> */
33 #undef _POSIX_PTHREAD_SEMANTICS
35 #define _POSIX_PTHREAD_SEMANTICS 1
43 #include <port_after.h>
48 copy_passwd(struct passwd
*, struct passwd
*, char *buf
, int buflen
);
51 #ifdef POSIX_GETPWNAM_R
53 __posix_getpwnam_r(const char *login
, struct passwd
*pwptr
,
54 char *buf
, size_t buflen
, struct passwd
**result
) {
57 getpwnam_r(const char *login
, struct passwd
*pwptr
,
58 char *buf
, size_t buflen
, struct passwd
**result
) {
60 struct passwd
*pw
= getpwnam(login
);
68 res
= copy_passwd(pw
, pwptr
, buf
, buflen
);
69 *result
= res
? NULL
: pwptr
;
73 #ifdef POSIX_GETPWNAM_R
75 getpwnam_r(const char *login
, struct passwd
*pwptr
, char *buf
, int buflen
) {
76 struct passwd
*pw
= getpwnam(login
);
82 res
= copy_passwd(pw
, pwptr
, buf
, buflen
);
83 return (res
? NULL
: pwptr
);
88 #ifdef POSIX_GETPWUID_R
90 __posix_getpwuid_r(uid_t uid
, struct passwd
*pwptr
,
91 char *buf
, int buflen
, struct passwd
**result
) {
94 getpwuid_r(uid_t uid
, struct passwd
*pwptr
,
95 char *buf
, size_t buflen
, struct passwd
**result
) {
97 struct passwd
*pw
= getpwuid(uid
);
105 res
= copy_passwd(pw
, pwptr
, buf
, buflen
);
106 *result
= res
? NULL
: pwptr
;
110 #ifdef POSIX_GETPWUID_R
112 getpwuid_r(uid_t uid
, struct passwd
*pwptr
, char *buf
, int buflen
) {
113 struct passwd
*pw
= getpwuid(uid
);
119 res
= copy_passwd(pw
, pwptr
, buf
, buflen
);
120 return (res
? NULL
: pwptr
);
125 * These assume a single context is in operation per thread.
126 * If this is not the case we will need to call irs directly
127 * rather than through the base functions.
131 getpwent_r(struct passwd
*pwptr
, PASS_R_ARGS
) {
132 struct passwd
*pw
= getpwent();
138 res
= copy_passwd(pw
, pwptr
, buf
, buflen
);
139 return (res
? PASS_R_BAD
: PASS_R_OK
);
143 #ifdef PASS_R_ENT_ARGS
144 setpassent_r(int stayopen
, PASS_R_ENT_ARGS
)
146 setpassent_r(int stayopen
)
150 setpassent(stayopen
);
151 #ifdef PASS_R_SET_RESULT
152 return (PASS_R_SET_RESULT
);
157 #ifdef PASS_R_ENT_ARGS
158 setpwent_r(PASS_R_ENT_ARGS
)
165 #ifdef PASS_R_SET_RESULT
166 return (PASS_R_SET_RESULT
);
171 #ifdef PASS_R_ENT_ARGS
172 endpwent_r(PASS_R_ENT_ARGS
)
179 PASS_R_END_RESULT(PASS_R_OK
);
185 fgetpwent_r(FILE *f
, struct passwd
*pwptr
, PASS_R_COPY_ARGS
) {
186 struct passwd
*pw
= fgetpwent(f
);
192 res
= copy_passwd(pw
, pwptr
, PASS_R_COPY
);
193 return (res
? PASS_R_BAD
: PASS_R_OK
);
200 copy_passwd(struct passwd
*pw
, struct passwd
*pwptr
, char *buf
, int buflen
) {
205 /* Find out the amount of space required to store the answer. */
206 len
= strlen(pw
->pw_name
) + 1;
207 len
+= strlen(pw
->pw_passwd
) + 1;
209 len
+= strlen(pw
->pw_class
) + 1;
211 len
+= strlen(pw
->pw_gecos
) + 1;
212 len
+= strlen(pw
->pw_dir
) + 1;
213 len
+= strlen(pw
->pw_shell
) + 1;
220 /* copy fixed atomic values*/
221 pwptr
->pw_uid
= pw
->pw_uid
;
222 pwptr
->pw_gid
= pw
->pw_gid
;
223 #ifdef HAVE_PW_CHANGE
224 pwptr
->pw_change
= pw
->pw_change
;
226 #ifdef HAVE_PW_EXPIRE
227 pwptr
->pw_expire
= pw
->pw_expire
;
232 /* copy official name */
233 n
= strlen(pw
->pw_name
) + 1;
234 strcpy(cp
, pw
->pw_name
);
239 n
= strlen(pw
->pw_passwd
) + 1;
240 strcpy(cp
, pw
->pw_passwd
);
241 pwptr
->pw_passwd
= cp
;
246 n
= strlen(pw
->pw_class
) + 1;
247 strcpy(cp
, pw
->pw_class
);
248 pwptr
->pw_class
= cp
;
253 n
= strlen(pw
->pw_gecos
) + 1;
254 strcpy(cp
, pw
->pw_gecos
);
255 pwptr
->pw_gecos
= cp
;
259 n
= strlen(pw
->pw_dir
) + 1;
260 strcpy(cp
, pw
->pw_dir
);
264 /* copy login shell */
265 n
= strlen(pw
->pw_shell
) + 1;
266 strcpy(cp
, pw
->pw_shell
);
267 pwptr
->pw_shell
= cp
;
272 #else /* PASS_R_RETURN */
273 static int getpwent_r_unknown_system
= 0;
274 #endif /* PASS_R_RETURN */
275 #endif /* !def(_REENTRANT) || !def(DO_PTHREADS) || !def(WANT_IRS_PW) */