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/wasi.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/wasi.rs')
-rw-r--r-- | vendor/errno/src/wasi.rs | 57 |
1 files changed, 26 insertions, 31 deletions
diff --git a/vendor/errno/src/wasi.rs b/vendor/errno/src/wasi.rs index 28e3d9f11..b18fa9b2c 100644 --- a/vendor/errno/src/wasi.rs +++ b/vendor/errno/src/wasi.rs @@ -12,54 +12,49 @@ // 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; +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 { - // libc_errno is thread-local, so simply read its value. - unsafe { - Errno(libc_errno) - } + unsafe { Errno(*__errno_location()) } } pub fn set_errno(Errno(new_errno): Errno) { - // libc_errno is thread-local, so simply assign to it. unsafe { - libc_errno = new_errno; + *__errno_location() = new_errno; } } -extern { - #[thread_local] - #[link_name = "errno"] - static mut libc_errno: c_int; - - #[cfg(feature = "std")] - fn strerror_r(errnum: c_int, buf: *mut c_char, - buflen: libc::size_t) -> c_int; +extern "C" { + fn __errno_location() -> *mut c_int; + fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: size_t) -> c_int; } |