diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:20:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:20:39 +0000 |
commit | 1376c5a617be5c25655d0d7cb63e3beaa5a6e026 (patch) | |
tree | 3bb8d61aee02bc7a15eab3f36e3b921afc2075d0 /vendor/errno/src/unix.rs | |
parent | Releasing progress-linux version 1.69.0+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.tar.xz rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.zip |
Merging upstream version 1.70.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/errno/src/unix.rs')
-rw-r--r-- | vendor/errno/src/unix.rs | 82 |
1 files changed, 44 insertions, 38 deletions
diff --git a/vendor/errno/src/unix.rs b/vendor/errno/src/unix.rs index 820d89748..c22c587c2 100644 --- a/vendor/errno/src/unix.rs +++ b/vendor/errno/src/unix.rs @@ -12,40 +12,42 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[cfg(feature = "std")] -use std::ffi::CStr; -use libc::c_int; -#[cfg(feature = "std")] -use libc::{self, c_char}; +use core::str; #[cfg(target_os = "dragonfly")] use errno_dragonfly::errno_location; +use libc::{self, c_char, c_int, size_t, strlen}; -use Errno; +use crate::Errno; -#[cfg(feature = "std")] -pub fn with_description<F, T>(err: Errno, callback: F) -> T where - F: FnOnce(Result<&str, Errno>) -> T +fn from_utf8_lossy(input: &[u8]) -> &str { + match str::from_utf8(input) { + Ok(valid) => valid, + Err(error) => unsafe { str::from_utf8_unchecked(&input[..error.valid_up_to()]) }, + } +} + +pub fn with_description<F, T>(err: Errno, callback: F) -> T +where + F: FnOnce(Result<&str, Errno>) -> T, { - let mut buf = [0 as c_char; 1024]; - unsafe { - if strerror_r(err.0, buf.as_mut_ptr(), buf.len() as libc::size_t) < 0 { + let mut buf = [0u8; 1024]; + let c_str = unsafe { + if strerror_r(err.0, buf.as_mut_ptr() as *mut _, buf.len() as size_t) < 0 { let fm_err = errno(); if fm_err != Errno(libc::ERANGE) { return callback(Err(fm_err)); } } - } - let c_str = unsafe { CStr::from_ptr(buf.as_ptr()) }; - callback(Ok(&String::from_utf8_lossy(c_str.to_bytes()))) + let c_str_len = strlen(buf.as_ptr() as *const _); + &buf[..c_str_len] + }; + callback(Ok(from_utf8_lossy(c_str))) } -#[cfg(feature = "std")] -pub const STRERROR_NAME: &'static str = "strerror_r"; +pub const STRERROR_NAME: &str = "strerror_r"; pub fn errno() -> Errno { - unsafe { - Errno(*errno_location()) - } + unsafe { Errno(*errno_location()) } } pub fn set_errno(Errno(errno): Errno) { @@ -54,26 +56,30 @@ pub fn set_errno(Errno(errno): Errno) { } } -extern { +extern "C" { #[cfg(not(target_os = "dragonfly"))] - #[cfg_attr(any(target_os = "macos", - target_os = "ios", - target_os = "freebsd"), - link_name = "__error")] - #[cfg_attr(any(target_os = "openbsd", - target_os = "netbsd", - target_os = "bitrig", - target_os = "android"), - link_name = "__errno")] - #[cfg_attr(any(target_os = "solaris", - target_os = "illumos"), - link_name = "___errno")] - #[cfg_attr(target_os = "linux", - link_name = "__errno_location")] + #[cfg_attr( + any(target_os = "macos", target_os = "ios", target_os = "freebsd"), + link_name = "__error" + )] + #[cfg_attr( + any( + target_os = "openbsd", + target_os = "netbsd", + target_os = "bitrig", + target_os = "android" + ), + link_name = "__errno" + )] + #[cfg_attr( + any(target_os = "solaris", target_os = "illumos"), + link_name = "___errno" + )] + #[cfg_attr(target_os = "haiku", link_name = "_errnop")] + #[cfg_attr(target_os = "linux", link_name = "__errno_location")] + #[cfg_attr(target_os = "aix", link_name = "_Errno")] fn errno_location() -> *mut c_int; - #[cfg(feature = "std")] #[cfg_attr(target_os = "linux", link_name = "__xpg_strerror_r")] - fn strerror_r(errnum: c_int, buf: *mut c_char, - buflen: libc::size_t) -> c_int; + fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: size_t) -> c_int; } |