|
1 | 1 | use std::mem; |
2 | | -use libc::{c_char}; |
| 2 | +use libc::{self, c_char}; |
3 | 3 | 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; |
17 | 5 |
|
18 | 6 | #[repr(C)] |
19 | 7 | #[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); |
30 | 9 |
|
31 | 10 | // workaround for `derive(Clone)` not working for fixed-length arrays |
32 | 11 | impl Clone for UtsName { fn clone(&self) -> UtsName { *self } } |
33 | 12 |
|
34 | 13 | impl UtsName { |
35 | 14 | 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) |
37 | 16 | } |
38 | 17 |
|
39 | 18 | 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) |
41 | 20 | } |
42 | 21 |
|
43 | 22 | 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) |
45 | 24 | } |
46 | 25 |
|
47 | 26 | 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) |
49 | 28 | } |
50 | 29 |
|
51 | 30 | 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) |
53 | 32 | } |
54 | 33 | } |
55 | 34 |
|
56 | 35 | pub fn uname() -> UtsName { |
57 | 36 | unsafe { |
58 | 37 | let mut ret: UtsName = mem::uninitialized(); |
59 | | - ffi::uname(&mut ret as *mut UtsName); |
| 38 | + libc::uname(&mut ret.0); |
60 | 39 | ret |
61 | 40 | } |
62 | 41 | } |
|
0 commit comments