/rust/registry/src/index.crates.io-1949cf8c6b5b557f/sysinfo-0.33.1/src/unix/utils.rs
Line | Count | Source |
1 | | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | | |
3 | | #[cfg(feature = "user")] |
4 | 0 | pub(crate) fn cstr_to_rust(c: *const libc::c_char) -> Option<String> { |
5 | 0 | cstr_to_rust_with_size(c, None) |
6 | 0 | } |
7 | | |
8 | | #[cfg(any(feature = "disk", feature = "system", feature = "user"))] |
9 | | #[allow(dead_code)] |
10 | 0 | pub(crate) fn cstr_to_rust_with_size( |
11 | 0 | c: *const libc::c_char, |
12 | 0 | size: Option<usize>, |
13 | 0 | ) -> Option<String> { |
14 | 0 | if c.is_null() { |
15 | 0 | return None; |
16 | 0 | } |
17 | 0 | let (mut s, max) = match size { |
18 | 0 | Some(len) => (Vec::with_capacity(len), len as isize), |
19 | 0 | None => (Vec::new(), isize::MAX), |
20 | | }; |
21 | 0 | let mut i = 0; |
22 | | unsafe { |
23 | | loop { |
24 | 0 | let value = *c.offset(i) as u8; |
25 | 0 | if value == 0 { |
26 | 0 | break; |
27 | 0 | } |
28 | 0 | s.push(value); |
29 | 0 | i += 1; |
30 | 0 | if i >= max { |
31 | 0 | break; |
32 | 0 | } |
33 | | } |
34 | 0 | String::from_utf8(s).ok() |
35 | | } |
36 | 0 | } |