2 * <pwd.h> wrapper functions.
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004-2005 Jonathan Pryor
21 static const mph_string_offset_t
23 MPH_STRING_OFFSET (struct passwd
, pw_name
, MPH_STRING_OFFSET_PTR
),
24 MPH_STRING_OFFSET (struct passwd
, pw_passwd
, MPH_STRING_OFFSET_PTR
),
25 MPH_STRING_OFFSET (struct passwd
, pw_gecos
, MPH_STRING_OFFSET_PTR
),
26 MPH_STRING_OFFSET (struct passwd
, pw_dir
, MPH_STRING_OFFSET_PTR
),
27 MPH_STRING_OFFSET (struct passwd
, pw_shell
, MPH_STRING_OFFSET_PTR
)
30 static const mph_string_offset_t
31 mph_passwd_offsets
[] = {
32 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd
, pw_name
, MPH_STRING_OFFSET_PTR
),
33 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd
, pw_passwd
, MPH_STRING_OFFSET_PTR
),
34 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd
, pw_gecos
, MPH_STRING_OFFSET_PTR
),
35 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd
, pw_dir
, MPH_STRING_OFFSET_PTR
),
36 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd
, pw_shell
, MPH_STRING_OFFSET_PTR
)
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)
242 } while (errno
== EINTR
);
243 mph_return_if_val_in_list5(errno
, EIO
, EMFILE
, ENFILE
, ENOMEM
, ERANGE
);
248 Mono_Posix_Syscall_endpwent (void)