2 * <pwd.h> wrapper functions.
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004-2005 Jonathan Pryor
20 struct Mono_Posix_Syscall__Passwd
{
21 /* string */ char *pw_name
;
22 /* string */ char *pw_passwd
;
23 /* uid_t */ mph_uid_t pw_uid
;
24 /* gid_t */ mph_gid_t pw_gid
;
25 /* string */ char *pw_gecos
;
26 /* string */ char *pw_dir
;
27 /* string */ char *pw_shell
;
28 /* string */ char *_pw_buf_
;
33 offsetof (struct passwd
, pw_name
),
34 offsetof (struct passwd
, pw_passwd
),
35 offsetof (struct passwd
, pw_gecos
),
36 offsetof (struct passwd
, pw_dir
),
37 offsetof (struct passwd
, pw_shell
)
41 mph_passwd_offsets
[] = {
42 offsetof (struct Mono_Posix_Syscall__Passwd
, pw_name
),
43 offsetof (struct Mono_Posix_Syscall__Passwd
, pw_passwd
),
44 offsetof (struct Mono_Posix_Syscall__Passwd
, pw_gecos
),
45 offsetof (struct Mono_Posix_Syscall__Passwd
, pw_dir
),
46 offsetof (struct Mono_Posix_Syscall__Passwd
, pw_shell
)
50 * Copy the native `passwd' structure to it's managed representation.
52 * To minimize separate mallocs, all the strings are allocated within the same
53 * memory block (stored in _pw_buf_).
56 copy_passwd (struct Mono_Posix_Syscall__Passwd
*to
, struct passwd
*from
)
59 buf
= _mph_copy_structure_strings (to
, mph_passwd_offsets
,
60 from
, passwd_offsets
, sizeof(passwd_offsets
)/sizeof(passwd_offsets
[0]));
62 to
->pw_uid
= from
->pw_uid
;
63 to
->pw_gid
= from
->pw_gid
;
74 Mono_Posix_Syscall_getpwnam (const char *name
, struct Mono_Posix_Syscall__Passwd
*pwbuf
)
88 if (copy_passwd (pwbuf
, pw
) == -1) {
96 Mono_Posix_Syscall_getpwuid (mph_uid_t uid
, struct Mono_Posix_Syscall__Passwd
*pwbuf
)
111 if (copy_passwd (pwbuf
, pw
) == -1) {
118 #ifdef HAVE_GETPWNAM_R
120 Mono_Posix_Syscall_getpwnam_r (const char *name
,
121 struct Mono_Posix_Syscall__Passwd
*pwbuf
,
127 struct passwd _pwbuf
;
138 buf2
= realloc (buf
, buflen
*= 2);
146 } while ((r
= getpwnam_r (name
, &_pwbuf
, buf
, buflen
, (struct passwd
**) pwbufp
)) &&
149 if (r
== 0 && !(*pwbufp
))
150 /* On solaris, this function returns 0 even if the entry was not found */
153 if (r
== 0 && copy_passwd (pwbuf
, &_pwbuf
) == -1)
159 #endif /* ndef HAVE_GETPWNAM_R */
161 #ifdef HAVE_GETPWUID_R
163 Mono_Posix_Syscall_getpwuid_r (mph_uid_t uid
,
164 struct Mono_Posix_Syscall__Passwd
*pwbuf
,
170 struct passwd _pwbuf
;
181 buf2
= realloc (buf
, buflen
*= 2);
189 } while ((r
= getpwuid_r (uid
, &_pwbuf
, buf
, buflen
, (struct passwd
**) pwbufp
)) &&
192 if (r
== 0 && copy_passwd (pwbuf
, &_pwbuf
) == -1)
198 #endif /* ndef HAVE_GETPWUID_R */
201 Mono_Posix_Syscall_getpwent (struct Mono_Posix_Syscall__Passwd
*pwbuf
)
215 if (copy_passwd (pwbuf
, pw
) == -1) {
222 #ifdef HAVE_FGETPWENT
224 Mono_Posix_Syscall_fgetpwent (void *stream
, struct Mono_Posix_Syscall__Passwd
*pwbuf
)
234 pw
= fgetpwent ((FILE*) stream
);
238 if (copy_passwd (pwbuf
, pw
) == -1) {
244 #endif /* ndef HAVE_FGETPWENT */
247 Mono_Posix_Syscall_setpwent (void)
251 return errno
== 0 ? 0 : -1;
255 Mono_Posix_Syscall_endpwent (void)
259 return errno
== 0 ? 0 : -1;