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