From 3cd785c492a4de625ded3c1fd2bdaf1822d68297 Mon Sep 17 00:00:00 2001 From: Bryant Mairs Date: Wed, 9 Aug 2017 22:36:31 -0700 Subject: Use libc::uname and libc::utsname --- src/sys/utsname.rs | 39 +++++++++------------------------------ 1 file changed, 9 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/sys/utsname.rs b/src/sys/utsname.rs index 7eeb323b..104249d8 100644 --- a/src/sys/utsname.rs +++ b/src/sys/utsname.rs @@ -1,62 +1,41 @@ use std::mem; -use libc::{c_char}; +use libc::{self, c_char}; use std::ffi::CStr; -use std::str::from_utf8_unchecked; - -mod ffi { - use libc::c_int; - use super::UtsName; - - extern { - pub fn uname(buf: *mut UtsName) -> c_int; - } -} - - -const UTSNAME_LEN: usize = 65; +use std::str::from_utf8_unchecked; #[repr(C)] #[derive(Copy)] -pub struct UtsName { - sysname: [c_char; UTSNAME_LEN], - nodename: [c_char; UTSNAME_LEN], - release: [c_char; UTSNAME_LEN], - version: [c_char; UTSNAME_LEN], - machine: [c_char; UTSNAME_LEN], - // ifdef _GNU_SOURCE - #[allow(dead_code)] - domainname: [c_char; UTSNAME_LEN] -} +pub struct UtsName(libc::utsname); // workaround for `derive(Clone)` not working for fixed-length arrays impl Clone for UtsName { fn clone(&self) -> UtsName { *self } } impl UtsName { pub fn sysname<'a>(&'a self) -> &'a str { - to_str(&(&self.sysname as *const c_char ) as *const *const c_char) + to_str(&(&self.0.sysname as *const c_char ) as *const *const c_char) } pub fn nodename<'a>(&'a self) -> &'a str { - to_str(&(&self.nodename as *const c_char ) as *const *const c_char) + to_str(&(&self.0.nodename as *const c_char ) as *const *const c_char) } pub fn release<'a>(&'a self) -> &'a str { - to_str(&(&self.release as *const c_char ) as *const *const c_char) + to_str(&(&self.0.release as *const c_char ) as *const *const c_char) } pub fn version<'a>(&'a self) -> &'a str { - to_str(&(&self.version as *const c_char ) as *const *const c_char) + to_str(&(&self.0.version as *const c_char ) as *const *const c_char) } pub fn machine<'a>(&'a self) -> &'a str { - to_str(&(&self.machine as *const c_char ) as *const *const c_char) + to_str(&(&self.0.machine as *const c_char ) as *const *const c_char) } } pub fn uname() -> UtsName { unsafe { let mut ret: UtsName = mem::uninitialized(); - ffi::uname(&mut ret as *mut UtsName); + libc::uname(&mut ret.0); ret } } -- cgit v1.2.3