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 #if HAVE_STRUCT_PASSWD_PW_GECOS
26 MPH_STRING_OFFSET (struct passwd
, pw_gecos
, MPH_STRING_OFFSET_PTR
),
27 #endif /* def HAVE_STRUCT_PASSWD_PW_GECOS */
28 MPH_STRING_OFFSET (struct passwd
, pw_dir
, MPH_STRING_OFFSET_PTR
),
29 MPH_STRING_OFFSET (struct passwd
, pw_shell
, MPH_STRING_OFFSET_PTR
)
32 static const mph_string_offset_t
33 mph_passwd_offsets
[] = {
34 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd
, pw_name
, MPH_STRING_OFFSET_PTR
),
35 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd
, pw_passwd
, MPH_STRING_OFFSET_PTR
),
36 #if HAVE_STRUCT_PASSWD_PW_GECOS
37 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd
, pw_gecos
, MPH_STRING_OFFSET_PTR
),
38 #endif /* def HAVE_STRUCT_PASSWD_PW_GECOS */
39 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd
, pw_dir
, MPH_STRING_OFFSET_PTR
),
40 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd
, pw_shell
, MPH_STRING_OFFSET_PTR
)
44 * Copy the native `passwd' structure to it's managed representation.
46 * To minimize separate mallocs, all the strings are allocated within the same
47 * memory block (stored in _pw_buf_).
50 copy_passwd (struct Mono_Posix_Syscall__Passwd
*to
, struct passwd
*from
)
53 buf
= _mph_copy_structure_strings (to
, mph_passwd_offsets
,
54 from
, passwd_offsets
, sizeof(passwd_offsets
)/sizeof(passwd_offsets
[0]));
56 to
->pw_uid
= from
->pw_uid
;
57 to
->pw_gid
= from
->pw_gid
;
68 Mono_Posix_Syscall_getpwnam (const char *name
, struct Mono_Posix_Syscall__Passwd
*pwbuf
)
82 if (copy_passwd (pwbuf
, pw
) == -1) {
90 Mono_Posix_Syscall_getpwuid (mph_uid_t uid
, struct Mono_Posix_Syscall__Passwd
*pwbuf
)
105 if (copy_passwd (pwbuf
, pw
) == -1) {
112 #ifdef HAVE_GETPWNAM_R
114 Mono_Posix_Syscall_getpwnam_r (const char *name
,
115 struct Mono_Posix_Syscall__Passwd
*pwbuf
,
121 struct passwd _pwbuf
;
132 buf2
= realloc (buf
, buflen
*= 2);
140 } while ((r
= getpwnam_r (name
, &_pwbuf
, buf
, buflen
, (struct passwd
**) pwbufp
)) &&
143 if (r
== 0 && !(*pwbufp
))
144 /* On solaris, this function returns 0 even if the entry was not found */
147 if (r
== 0 && copy_passwd (pwbuf
, &_pwbuf
) == -1)
153 #endif /* ndef HAVE_GETPWNAM_R */
155 #ifdef HAVE_GETPWUID_R
157 Mono_Posix_Syscall_getpwuid_r (mph_uid_t uid
,
158 struct Mono_Posix_Syscall__Passwd
*pwbuf
,
164 struct passwd _pwbuf
;
175 buf2
= realloc (buf
, buflen
*= 2);
183 } while ((r
= getpwuid_r (uid
, &_pwbuf
, buf
, buflen
, (struct passwd
**) pwbufp
)) &&
186 if (r
== 0 && !(*pwbufp
))
187 /* On solaris, this function returns 0 even if the entry was not found */
190 if (r
== 0 && copy_passwd (pwbuf
, &_pwbuf
) == -1)
196 #endif /* ndef HAVE_GETPWUID_R */
200 Mono_Posix_Syscall_getpwent (struct Mono_Posix_Syscall__Passwd
*pwbuf
)
214 if (copy_passwd (pwbuf
, pw
) == -1) {
220 #endif /* def HAVE_GETPWENT */
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 */
248 Mono_Posix_Syscall_setpwent (void)
253 } while (errno
== EINTR
);
254 mph_return_if_val_in_list5(errno
, EIO
, EMFILE
, ENFILE
, ENOMEM
, ERANGE
);
257 #endif /* def HAVE_SETPWENT */
261 Mono_Posix_Syscall_endpwent (void)
269 #endif /* def HAVE_ENDPWENT */