2 * <pwd.h> wrapper functions.
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004-2005 Jonathan Pryor
23 offsetof (struct passwd
, pw_name
),
24 offsetof (struct passwd
, pw_passwd
),
25 offsetof (struct passwd
, pw_gecos
),
26 offsetof (struct passwd
, pw_dir
),
27 offsetof (struct passwd
, pw_shell
)
31 mph_passwd_offsets
[] = {
32 offsetof (struct Mono_Posix_Syscall__Passwd
, pw_name
),
33 offsetof (struct Mono_Posix_Syscall__Passwd
, pw_passwd
),
34 offsetof (struct Mono_Posix_Syscall__Passwd
, pw_gecos
),
35 offsetof (struct Mono_Posix_Syscall__Passwd
, pw_dir
),
36 offsetof (struct Mono_Posix_Syscall__Passwd
, pw_shell
)
40 * Copy the native `passwd' structure to it's managed representation.
42 * To minimize separate mallocs, all the strings are allocated within the same
43 * memory block (stored in _pw_buf_).
46 copy_passwd (struct Mono_Posix_Syscall__Passwd
*to
, struct passwd
*from
)
49 buf
= _mph_copy_structure_strings (to
, mph_passwd_offsets
,
50 from
, passwd_offsets
, sizeof(passwd_offsets
)/sizeof(passwd_offsets
[0]));
52 to
->pw_uid
= from
->pw_uid
;
53 to
->pw_gid
= from
->pw_gid
;
64 Mono_Posix_Syscall_getpwnam (const char *name
, struct Mono_Posix_Syscall__Passwd
*pwbuf
)
78 if (copy_passwd (pwbuf
, pw
) == -1) {
86 Mono_Posix_Syscall_getpwuid (mph_uid_t uid
, struct Mono_Posix_Syscall__Passwd
*pwbuf
)
101 if (copy_passwd (pwbuf
, pw
) == -1) {
108 #ifdef HAVE_GETPWNAM_R
110 Mono_Posix_Syscall_getpwnam_r (const char *name
,
111 struct Mono_Posix_Syscall__Passwd
*pwbuf
,
117 struct passwd _pwbuf
;
128 buf2
= realloc (buf
, buflen
*= 2);
136 } while ((r
= getpwnam_r (name
, &_pwbuf
, buf
, buflen
, (struct passwd
**) pwbufp
)) &&
139 if (r
== 0 && !(*pwbufp
))
140 /* On solaris, this function returns 0 even if the entry was not found */
143 if (r
== 0 && copy_passwd (pwbuf
, &_pwbuf
) == -1)
149 #endif /* ndef HAVE_GETPWNAM_R */
151 #ifdef HAVE_GETPWUID_R
153 Mono_Posix_Syscall_getpwuid_r (mph_uid_t uid
,
154 struct Mono_Posix_Syscall__Passwd
*pwbuf
,
160 struct passwd _pwbuf
;
171 buf2
= realloc (buf
, buflen
*= 2);
179 } while ((r
= getpwuid_r (uid
, &_pwbuf
, buf
, buflen
, (struct passwd
**) pwbufp
)) &&
182 if (r
== 0 && copy_passwd (pwbuf
, &_pwbuf
) == -1)
188 #endif /* ndef HAVE_GETPWUID_R */
191 Mono_Posix_Syscall_getpwent (struct Mono_Posix_Syscall__Passwd
*pwbuf
)
205 if (copy_passwd (pwbuf
, pw
) == -1) {
212 #ifdef HAVE_FGETPWENT
214 Mono_Posix_Syscall_fgetpwent (void *stream
, struct Mono_Posix_Syscall__Passwd
*pwbuf
)
224 pw
= fgetpwent ((FILE*) stream
);
228 if (copy_passwd (pwbuf
, pw
) == -1) {
234 #endif /* ndef HAVE_FGETPWENT */
237 Mono_Posix_Syscall_setpwent (void)
241 return errno
== 0 ? 0 : -1;
245 Mono_Posix_Syscall_endpwent (void)
249 return errno
== 0 ? 0 : -1;