Skip to content

Commit 6234d4b

Browse files
committed
Use libc::uname and libc::utsname
1 parent b184317 commit 6234d4b

File tree

1 file changed

+9
-30
lines changed

1 file changed

+9
-30
lines changed

src/sys/utsname.rs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,41 @@
11
use std::mem;
2-
use libc::{c_char};
2+
use libc::{self, c_char};
33
use std::ffi::CStr;
4-
use std::str::from_utf8_unchecked;
5-
6-
mod ffi {
7-
use libc::c_int;
8-
use super::UtsName;
9-
10-
extern {
11-
pub fn uname(buf: *mut UtsName) -> c_int;
12-
}
13-
}
14-
15-
16-
const UTSNAME_LEN: usize = 65;
4+
use std::str::from_utf8_unchecked;
175

186
#[repr(C)]
197
#[derive(Copy)]
20-
pub struct UtsName {
21-
sysname: [c_char; UTSNAME_LEN],
22-
nodename: [c_char; UTSNAME_LEN],
23-
release: [c_char; UTSNAME_LEN],
24-
version: [c_char; UTSNAME_LEN],
25-
machine: [c_char; UTSNAME_LEN],
26-
// ifdef _GNU_SOURCE
27-
#[allow(dead_code)]
28-
domainname: [c_char; UTSNAME_LEN]
29-
}
8+
pub struct UtsName(libc::utsname);
309

3110
// workaround for `derive(Clone)` not working for fixed-length arrays
3211
impl Clone for UtsName { fn clone(&self) -> UtsName { *self } }
3312

3413
impl UtsName {
3514
pub fn sysname<'a>(&'a self) -> &'a str {
36-
to_str(&(&self.sysname as *const c_char ) as *const *const c_char)
15+
to_str(&(&self.0.sysname as *const c_char ) as *const *const c_char)
3716
}
3817

3918
pub fn nodename<'a>(&'a self) -> &'a str {
40-
to_str(&(&self.nodename as *const c_char ) as *const *const c_char)
19+
to_str(&(&self.0.nodename as *const c_char ) as *const *const c_char)
4120
}
4221

4322
pub fn release<'a>(&'a self) -> &'a str {
44-
to_str(&(&self.release as *const c_char ) as *const *const c_char)
23+
to_str(&(&self.0.release as *const c_char ) as *const *const c_char)
4524
}
4625

4726
pub fn version<'a>(&'a self) -> &'a str {
48-
to_str(&(&self.version as *const c_char ) as *const *const c_char)
27+
to_str(&(&self.0.version as *const c_char ) as *const *const c_char)
4928
}
5029

5130
pub fn machine<'a>(&'a self) -> &'a str {
52-
to_str(&(&self.machine as *const c_char ) as *const *const c_char)
31+
to_str(&(&self.0.machine as *const c_char ) as *const *const c_char)
5332
}
5433
}
5534

5635
pub fn uname() -> UtsName {
5736
unsafe {
5837
let mut ret: UtsName = mem::uninitialized();
59-
ffi::uname(&mut ret as *mut UtsName);
38+
libc::uname(&mut ret.0);
6039
ret
6140
}
6241
}

0 commit comments

Comments
 (0)