Add support for OpenWRT
[pulga.git] / src / util.rs
blobb2d4417bcde30dab1b4c0749bf1bf37263705503
1 use libc::{self, c_char};
3 use std::{
4     ffi::{CStr, OsStr},
5     os::unix::ffi::OsStrExt,
6     ptr,
7 };
9 pub(crate) unsafe fn char_ptr_to_string(ptr: *mut c_char) -> String {
10     let cstr = CStr::from_ptr(ptr);
11     let os_str = OsStr::from_bytes(cstr.to_bytes());
12     os_str_to_string(os_str)
15 pub(crate) fn os_str_to_string(os_str: &OsStr) -> String {
16     let string = String::from_utf8_lossy(os_str.as_bytes());
17     string.into()
20 /// Simple rand function, wraps over libc::rand
21 /// It isn't super secure, but we don't really need security
22 pub(crate) fn get_rand(max: i32) -> i32 {
23     unsafe {
24         libc::srand(libc::time(ptr::null_mut()) as u32);
25         libc::rand() % max
26     }
29 // Extracts the last element of a path.
30 // Example: "/foo/bar/" -> "bar"
31 pub(crate) fn get_base(path: &str) -> String {
32     path.rsplit(|a| a == '/')
33             .next()
34             .unwrap()
35             .to_string()