Debian Codegraph @

src/platform/unix.rs

raw symbols SCIP
1 // This file is part of the uutils coreutils package.
2 //
3 // (c) Jian Zeng <anonymousknight96 AT gmail.com>
4 // (c) Alex Lyon <[email protected]>
5 //
6 // For the full copyright and license information, please view the LICENSE file
7 // that was distributed with this source code.
8
9 // spell-checker:ignore (API) domainname nodename osname sysname
10 // spell-checker:ignore (libc) libc utsname
11 // spell-checker:ignore (jargon) hasher
12 // spell-checker:ignore (names) Jian Zeng * anonymousknight96
13 // spell-checker:ignore (rust) uninit
14 // spell-checker:ignore (uutils) coreutils uutils
15 // spell-checker:ignore (VSCode) endregion
16
17 // refs:
18 // [Byte-to/from-String Conversions](https://nicholasbishop.github.io/rust-conversions) @@ <https://archive.is/AnDCY>
19
20 #![warn(unused_results)] // enable warnings for unused results
21
22 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::{OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#, OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#};
23 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/;
24 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::{Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#};
25
26 use craterust-analyzer cargo platform-info 2.0.5 crate/::{PlatformInfoAPIrust-analyzer cargo platform-info 2.0.5 PlatformInfoAPI#, PlatformInfoErrorrust-analyzer cargo platform-info 2.0.5 lib_impl/BoxedThreadSafeStdError#, UNameAPIrust-analyzer cargo platform-info 2.0.5 UNameAPI#};
27
28 use unix_saferust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/::{oss_from_cstrrust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/oss_from_cstr()., utsnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/utsname().};
29
30 // PlatformInfo
31 /// Handles initial retrieval and holds cached information for the current platform (a Unix-like OS in this case).
32 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)]
33 pub struct PlatformInforust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo# {
34 /// Contains the cached results of the `utsname()` system call.
35 // ref: <https://docs.rs/libc/latest/i686-unknown-linux-gnu/libc/struct.utsname.html>
36 pub utsnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#utsname.: UTSNamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#, /* aka "Unix Time-sharing System Name"; ref: <https://stackoverflow.com/questions/41669397/whats-the-meaning-of-utsname-in-linux> */
37 // * private-use fields
38 sysnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#sysname.: OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#,
39 nodenamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#nodename.: OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#,
40 releaserust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#release.: OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#,
41 versionrust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#version.: OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#,
42 machinerust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#machine.: OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#,
43 osnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#osname.: OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#,
44 }
45
46 impl PlatformInfoAPIrust-analyzer cargo platform-info 2.0.5 PlatformInfoAPI# for PlatformInforust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo# {
47 // * note: this function *should* never fail
48 fn newrust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[PlatformInfo][PlatformInfoAPI]new().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[PlatformInfo][PlatformInfoAPI], PlatformInfoErrorrust-analyzer cargo platform-info 2.0.5 lib_impl/BoxedThreadSafeStdError#> {
49 let utsname = UTSNamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#(utsnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/utsname().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().);
50 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[PlatformInfo][PlatformInfoAPI] {
51 utsnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#utsname.,
52 sysnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#sysname.: oss_from_cstrrust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/oss_from_cstr().(&utsname.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..sysnamerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#sysname.),
53 nodenamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#nodename.: oss_from_cstrrust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/oss_from_cstr().(&utsname.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..nodenamerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#nodename.),
54 releaserust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#release.: oss_from_cstrrust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/oss_from_cstr().(&utsname.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..releaserust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#release.),
55 versionrust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#version.: oss_from_cstrrust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/oss_from_cstr().(&utsname.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..versionrust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#version.),
56 machinerust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#machine.: oss_from_cstrrust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/oss_from_cstr().(&utsname.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..machinerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#machine.),
57 osnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#osname.: OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`From<&T>`]from().(craterust-analyzer cargo platform-info 2.0.5 crate/::lib_implrust-analyzer cargo platform-info 2.0.5 lib_impl/::HOST_OS_NAMErust-analyzer cargo platform-info 2.0.5 lib_impl/HOST_OS_NAME.),
58 })
59 }
60 }
61
62 impl UNameAPIrust-analyzer cargo platform-info 2.0.5 UNameAPI# for PlatformInforust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo# {
63 fn sysnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[PlatformInfo][UNameAPI]sysname().(&self) -> &OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr# {
64 &self.sysnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#sysname.
65 }
66
67 fn nodenamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[PlatformInfo][UNameAPI]nodename().(&self) -> &OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr# {
68 &self.nodenamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#nodename.
69 }
70
71 fn releaserust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[PlatformInfo][UNameAPI]release().(&self) -> &OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr# {
72 &self.releaserust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#release.
73 }
74
75 fn versionrust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[PlatformInfo][UNameAPI]version().(&self) -> &OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr# {
76 &self.versionrust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#version.
77 }
78
79 fn machinerust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[PlatformInfo][UNameAPI]machine().(&self) -> &OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr# {
80 &self.machinerust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#machine.
81 }
82
83 fn osnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[PlatformInfo][UNameAPI]osname().(&self) -> &OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr# {
84 &self.osnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#osname.
85 }
86 }
87
88 //===
89
90 // UTSName
91 /// Contains information about the current computer system.
92 ///
93 /// Wraps [`libc::utsname`].
94 // ref: <https://docs.rs/libc/latest/i686-unknown-linux-gnu/libc/struct.utsname.html>
95 /*
96 pub struct utsname {
97 pub sysname: [::c_char; 65],
98 pub nodename: [::c_char; 65],
99 pub release: [::c_char; 65],
100 pub version: [::c_char; 65],
101 pub machine: [::c_char; 65],
102 pub domainname: [::c_char; 65]
103 }
104 */
105 // aka "Unix Time-sharing System Name"; ref: <https://stackoverflow.com/questions/41669397/whats-the-meaning-of-utsname-in-linux>
106 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy: /* , Debug, PartialEq, Eq */)]
107 pub struct UTSNamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#(libcrust-analyzer cargo libc 0.2.159 crate/::utsnamerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#);
108
109 impl Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for UTSNamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName# {
110 fn fmtrust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[UTSName][Debug]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
111 let mut debug_struct = &mut f.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("UTSName");
112 debug_struct = debug_struct
113 .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("sysname", &oss_from_cstrrust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/oss_from_cstr().(&self.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..sysnamerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#sysname.))
114 .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("nodename", &oss_from_cstrrust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/oss_from_cstr().(&self.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..nodenamerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#nodename.))
115 .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("release", &oss_from_cstrrust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/oss_from_cstr().(&self.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..releaserust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#release.))
116 .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("version", &oss_from_cstrrust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/oss_from_cstr().(&self.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..versionrust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#version.))
117 .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("machine", &oss_from_cstrrust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/oss_from_cstr().(&self.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..machinerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#machine.));
118 // The domainname field is not part of the POSIX standard but a GNU extension. Therefor
119 // BSD-like platforms and solaris/illumos are missing the domainname field.
120 #[cfg(not(any(
121 target_os = "aix",
122 target_os = "illumos",
123 target_os = "solaris",
124 target_os = "macos",
125 target_os = "dragonfly",
126 target_os = "freebsd",
127 target_os = "openbsd",
128 target_os = "netbsd",
129 target_os = "haiku"
130 )))]
131 {
132 debug_struct = debug_struct.fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("domainname", &oss_from_cstrrust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/oss_from_cstr().(&self.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..domainnamerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#domainname.));
133 }
134 debug_struct.finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish().()
135 }
136 }
137
138 impl PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq# for UTSNamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName# {
139 fn eqrust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[UTSName][`PartialEq<Self>`]eq().(&self, other: &Selfrust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[UTSName][`PartialEq<Self>`]) -> bool {
140 let mut equal = true; // avoid 'unused-mut' and 'clippy::let-and-return' warnings on MacOS
141 equal = equal
142 && (
143 self.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..sysnamerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#sysname.,
144 self.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..nodenamerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#nodename.,
145 self.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..releaserust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#release.,
146 self.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..versionrust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#version.,
147 self.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..machinerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#machine.,
148 ) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(X, W, V, U, T)`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(X, W, V, U, T)`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(X, W, V, U, T)`][`PartialEq<Self>`]eq().(
149 other.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..sysnamerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#sysname.,
150 other.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..nodenamerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#nodename.,
151 other.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..releaserust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#release.,
152 other.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..versionrust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#version.,
153 other.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..machinerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#machine.,
154 );
155 // The domainname field is not part of the POSIX standard but a GNU extension. Therefor
156 // BSD-like platforms and solaris/illumos are missing the domainname field.
157 #[cfg(not(any(
158 target_os = "aix",
159 target_os = "illumos",
160 target_os = "solaris",
161 target_os = "macos",
162 target_os = "dragonfly",
163 target_os = "freebsd",
164 target_os = "openbsd",
165 target_os = "netbsd",
166 target_os = "haiku"
167 )))]
168 {
169 equal = equal && (self.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..domainnamerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#domainname. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/equality/impl#[`[T; N]`][`PartialEq<[U; N]>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/equality/impl#[`[T; N]`][`PartialEq<[U; N]>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/equality/impl#[`[T; N]`][`PartialEq<[U; N]>`]eq().other.0rust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName#0..domainnamerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#domainname.);
170 }
171 equal
172 }
173 }
174
175 impl Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# for UTSNamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/UTSName# {}
176
177 //===
178
179 //#region unsafe code
180 mod unix_saferust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/ {
181 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#;
182 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::{CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#, OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#, OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#};
183 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/;
184 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#;
185 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/::OsStrExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/OsStrExt#;
186
187 // oss_from_str()
188 /// *Returns* an `OsString` created from a `libc::c_char` slice.
189 pub fn oss_from_cstrrust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/oss_from_cstr().(slice: &[libcrust-analyzer cargo libc 0.2.159 crate/::c_charrust-analyzer cargo libc 0.2.159 unix/linux_like/linux/gnu/b64/x86_64/c_char#]) -> OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString# {
190 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(slice.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). usize::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[usize][`TryFrom<isize>`]try_from().(isize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[isize]MAX.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
191 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(slice.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]position().(|&c| c == 0 /* NUL */).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). slice.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().());
192 OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`From<&T>`]from().(OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#::from_bytesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsStr][OsStrExt]from_bytes().(
193 unsafe { CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#::from_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]from_ptr().(slice.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().()) }.to_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]to_bytes().(),
194 ))
195 }
196
197 // utsname()
198 /// *Returns* a `libc::utsname` structure containing `uname`-like OS system information.
199 pub fn utsnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/unix_safe/utsname().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<libcrust-analyzer cargo libc 0.2.159 crate/::utsnamerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#, stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> {
200 // ref: <https://docs.rs/libc/latest/i686-unknown-linux-gnu/libc/fn.uname.html>
201 // ref: <https://docs.rs/libc/latest/i686-unknown-linux-gnu/libc/struct.utsname.html>
202 let mut uts = MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#::<libcrust-analyzer cargo libc 0.2.159 crate/::utsnamerust-analyzer cargo libc 0.2.159 unix/linux_like/utsname#>::uninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]uninit().();
203 let result = unsafe { libcrust-analyzer cargo libc 0.2.159 crate/::unamerust-analyzer cargo libc 0.2.159 uname().(uts.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]as_mut_ptr().()) };
204 if result 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().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1 {
205 // SAFETY: `libc::uname()` succeeded => `uts` was initialized
206 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(unsafe { uts.assume_initrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]assume_init().() })
207 } else {
208 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::last_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]last_os_error().())
209 }
210 }
211 }
212 //#endregion (unsafe code)
213
214 //=== Tests
215
216 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
217 fn test_osnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/test_osname().() {
218 let info = PlatformInforust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#::newrust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[PlatformInfo][PlatformInfoAPI]new().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
219 let osname = info.osnamerust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[PlatformInfo][UNameAPI]osname().().to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().();
220 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(osname.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(craterust-analyzer cargo platform-info 2.0.5 crate/::lib_implrust-analyzer cargo platform-info 2.0.5 lib_impl/::HOST_OS_NAMErust-analyzer cargo platform-info 2.0.5 lib_impl/HOST_OS_NAME.));
221 }
222
223 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
224 fn structure_clonerust-analyzer cargo platform-info 2.0.5 lib_impl/target/structure_clone().() {
225 let info = PlatformInforust-analyzer cargo platform-info 2.0.5 lib_impl/target/PlatformInfo#::newrust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[PlatformInfo][PlatformInfoAPI]new().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
226 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{info:?}");
227 #[allow(clippy::redundant_clone)] // ignore `clippy::redundant_clone` warning for direct testing
228 let info_copy = info.clonerust-analyzer cargo platform-info 2.0.5 lib_impl/target/impl#[PlatformInfo][Clone]clone().();
229 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(info_copy, info);
230 }