| 1 | //! Implementation of `errno` functionality for Unix systems. |
| 2 | //! |
| 3 | //! Adapted from `src/libstd/sys/unix/os.rs` in the Rust distribution. |
| 4 | |
| 5 | // Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
| 6 | // file at the top-level directory of this distribution and at |
| 7 | // http://rust-lang.org/COPYRIGHT. |
| 8 | // |
| 9 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 10 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 11 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 12 | // option. This file may not be copied, modified, or distributed |
| 13 | // except according to those terms. |
| 14 | |
| 15 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/; |
| 16 | use libcrust-analyzer cargo libc 0.2.175 crate/::{selfrust-analyzer cargo libc 0.2.175 crate/, c_intrust-analyzer cargo libc 0.2.175 primitives/c_int#, size_trust-analyzer cargo libc 0.2.175 unix/size_t#, strerror_rrust-analyzer cargo libc 0.2.175 strerror_r()., strlenrust-analyzer cargo libc 0.2.175 strlen().}; |
| 17 | |
| 18 | use craterust-analyzer cargo errno 0.3.14 crate/::Errnorust-analyzer cargo errno 0.3.14 Errno#; |
| 19 | |
| 20 | fn from_utf8_lossyrust-analyzer cargo errno 0.3.14 sys/from_utf8_lossy().(input: &[u8]) -> &str { |
| 21 | match strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(input) { |
| 22 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(valid) => valid, |
| 23 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error) => unsafe { strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/::from_utf8_uncheckedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8_unchecked().(&input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#error.valid_up_torust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/error/impl#[Utf8Error]valid_up_to().()]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) }, |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | pub fn with_descriptionrust-analyzer cargo errno 0.3.14 sys/with_description().<F, T>(err: Errnorust-analyzer cargo errno 0.3.14 Errno#, callback: F) -> T |
| 28 | where |
| 29 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&str, Errnorust-analyzer cargo errno 0.3.14 Errno#>) -> T, |
| 30 | { |
| 31 | let mut buf = [0u8; 1024]; |
| 32 | let c_str = unsafe { |
| 33 | let rc = strerror_rrust-analyzer cargo libc 0.2.175 strerror_r().(err.0rust-analyzer cargo errno 0.3.14 Errno#0., buf.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().() as *mut _, buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() as size_trust-analyzer cargo libc 0.2.175 unix/size_t#); |
| 34 | if rc rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().0 { |
| 35 | // Handle negative return codes for compatibility with glibc < 2.13 |
| 36 | let fm_err = match rc rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt().0 { |
| 37 | true => errnorust-analyzer cargo errno 0.3.14 sys/errno().(), |
| 38 | false => Errnorust-analyzer cargo errno 0.3.14 Errno#(rc), |
| 39 | }; |
| 40 | if fm_err rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().Errnorust-analyzer cargo errno 0.3.14 Errno#(libcrust-analyzer cargo libc 0.2.175 crate/::ERANGErust-analyzer cargo libc 0.2.175 unix/linux_like/linux/ERANGE.) { |
| 41 | return callback(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(fm_err)); |
| 42 | } |
| 43 | } |
| 44 | let c_str_len = strlenrust-analyzer cargo libc 0.2.175 strlen().(buf.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().() as *const _); |
| 45 | &buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#c_str_len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). |
| 46 | }; |
| 47 | callback(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(from_utf8_lossyrust-analyzer cargo errno 0.3.14 sys/from_utf8_lossy().(c_str))) |
| 48 | } |
| 49 | |
| 50 | pub const STRERROR_NAMErust-analyzer cargo errno 0.3.14 sys/STRERROR_NAME.: &str = "strerror_r"; |
| 51 | |
| 52 | pub fn errnorust-analyzer cargo errno 0.3.14 sys/errno().() -> Errnorust-analyzer cargo errno 0.3.14 Errno# { |
| 53 | unsafe { Errnorust-analyzer cargo errno 0.3.14 Errno#(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().errno_locationrust-analyzer cargo errno 0.3.14 errno_location().()) } |
| 54 | } |
| 55 | |
| 56 | pub fn set_errnorust-analyzer cargo errno 0.3.14 sys/set_errno().(Errnorust-analyzer cargo errno 0.3.14 Errno#(errno): Errnorust-analyzer cargo errno 0.3.14 Errno#) { |
| 57 | unsafe { |
| 58 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().errno_locationrust-analyzer cargo errno 0.3.14 errno_location().() = errno; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | extern "C" { |
| 63 | #[cfg_attr( |
| 64 | any( |
| 65 | target_os = "macos", |
| 66 | target_os = "ios", |
| 67 | target_os = "tvos", |
| 68 | target_os = "watchos", |
| 69 | target_os = "visionos", |
| 70 | target_os = "freebsd" |
| 71 | ), |
| 72 | link_name = "__error" |
| 73 | )] |
| 74 | #[cfg_attr( |
| 75 | any( |
| 76 | target_os = "openbsd", |
| 77 | target_os = "netbsd", |
| 78 | target_os = "android", |
| 79 | target_os = "espidf", |
| 80 | target_os = "vxworks", |
| 81 | target_os = "cygwin", |
| 82 | target_env = "newlib" |
| 83 | ), |
| 84 | link_name = "__errno" |
| 85 | )] |
| 86 | #[cfg_attr( |
| 87 | any(target_os = "solaris", target_os = "illumos"), |
| 88 | link_name = "___errno" |
| 89 | )] |
| 90 | #[cfg_attr(target_os = "haiku", link_name = "_errnop")] |
| 91 | #[cfg_attr( |
| 92 | any( |
| 93 | target_os = "linux", |
| 94 | target_os = "hurd", |
| 95 | target_os = "redox", |
| 96 | target_os = "dragonfly", |
| 97 | target_os = "emscripten", |
| 98 | ), |
| 99 | link_name = "__errno_location" |
| 100 | )] |
| 101 | #[cfg_attr(target_os = "aix", link_name = "_Errno")] |
| 102 | #[cfg_attr(target_os = "nto", link_name = "__get_errno_ptr")] |
| 103 | fn errno_locationrust-analyzer cargo errno 0.3.14 errno_location().() -> *mut c_intrust-analyzer cargo libc 0.2.175 primitives/c_int#; |
| 104 | } |