From 8a914ca06587ecf74420f16448b8f4b32e8653d1 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Mon, 3 Oct 2022 17:47:44 +0800 Subject: [PATCH] adopt the get[pw/gr]ent_r def used by FreeBSD --- src/unix/solarish/compat.rs | 49 +++++++++++++++++++++++++++++++++++++ src/unix/solarish/mod.rs | 18 +++----------- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/unix/solarish/compat.rs b/src/unix/solarish/compat.rs index 4a232f0d83ace..cbf955a31edaa 100644 --- a/src/unix/solarish/compat.rs +++ b/src/unix/solarish/compat.rs @@ -1,6 +1,7 @@ // Common functions that are unfortunately missing on illumos and // Solaris, but often needed by other crates. +use core::cmp::min; use unix::solarish::*; const PTEM: &[u8] = b"ptem\0"; @@ -169,3 +170,51 @@ pub unsafe fn forkpty( 0 } + +pub unsafe fn getpwent_r( + pwd: *mut passwd, + buf: *mut ::c_char, + buflen: ::size_t, + result: *mut *mut passwd, +) -> ::c_int { + let old_errno = *::___errno(); + *::___errno() = 0; + *result = native_getpwent_r( + pwd, + buf, + min(buflen, ::c_int::max_value() as ::size_t) as ::c_int, + ); + + let ret = if (*result).is_null() { + *::___errno() + } else { + 0 + }; + *::___errno() = old_errno; + + ret +} + +pub unsafe fn getgrent_r( + grp: *mut ::group, + buf: *mut ::c_char, + buflen: ::size_t, + result: *mut *mut ::group, +) -> ::c_int { + let old_errno = *::___errno(); + *::___errno() = 0; + *result = native_getgrent_r( + grp, + buf, + min(buflen, ::c_int::max_value() as ::size_t) as ::c_int, + ); + + let ret = if (*result).is_null() { + *::___errno() + } else { + 0 + }; + *::___errno() = old_errno; + + ret +} diff --git a/src/unix/solarish/mod.rs b/src/unix/solarish/mod.rs index 4041c67cf88fd..bf9b42a512cf0 100644 --- a/src/unix/solarish/mod.rs +++ b/src/unix/solarish/mod.rs @@ -3016,24 +3016,14 @@ extern "C" { ) -> ::c_int; #[cfg_attr( any(target_os = "solaris", target_os = "illumos"), - link_name = "__posix_getpwent_r" + link_name = "getpwent_r" )] - pub fn getpwent_r( - pwd: *mut passwd, - buf: *mut ::c_char, - buflen: ::size_t, - result: *mut *mut passwd, - ) -> ::c_int; + fn native_getpwent_r(pwd: *mut passwd, buf: *mut ::c_char, buflen: ::c_int) -> *mut passwd; #[cfg_attr( any(target_os = "solaris", target_os = "illumos"), - link_name = "__posix_getgrent_r" + link_name = "getgrent_r" )] - pub fn getgrent_r( - grp: *mut ::group, - buf: *mut ::c_char, - buflen: ::size_t, - result: *mut *mut ::group, - ) -> ::c_int; + fn native_getgrent_r(grp: *mut ::group, buf: *mut ::c_char, buflen: ::c_int) -> *mut ::group; #[cfg_attr( any(target_os = "solaris", target_os = "illumos"), link_name = "__posix_sigwait"