Created
September 28, 2016 15:16
-
-
Save philippkeller/89a8a0b47362e86570958dc7a14e84d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate libc; | |
extern crate itertools; | |
use libc::{utsname, uname}; | |
use std::ffi::CStr; | |
use itertools::Itertools; | |
fn array_to_string(slice:&[i8]) -> String { | |
slice.iter().take_while(|&x| *x != 0).map(|&a| a as u8 as char).join("") | |
} | |
unsafe fn u8_to_i8(a:&[i8]) -> &[u8] { | |
std::slice::from_raw_parts(a.as_ptr() as *const u8, a.len()) | |
} | |
fn main() { | |
unsafe { | |
let mut u:utsname = std::mem::uninitialized(); | |
if uname(&mut u) == 0 { | |
println!("sysname = {:?}", CStr::from_bytes_with_nul(u8_to_i8(&u.sysname))); | |
println!("nodename = {:?}", CStr::from_bytes_with_nul(u8_to_i8(&u.nodename))); | |
println!("release = {:?}", array_to_string(&u.release)); | |
println!("version = {:?}", array_to_string(&u.version)); | |
println!("machine = {:?}", array_to_string(&u.machine)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment