Skip to content
Snippets Groups Projects
Commit 00bbb7e7 authored by Alex Lyon's avatar Alex Lyon
Browse files

Convert utsname from uucore into a separate crate

parents
No related branches found
No related tags found
No related merge requests found
/.vscode/
/target
**/*.rs.bk
Cargo.lock
[package]
name = "platform-info"
version = "0.1.0"
authors = ["Alex Lyon <arcterus@mail.com>"]
[dependencies]
libc = "0.2"
winapi = { version = "0.3", features = ["sysinfoapi"] }
LICENSE 0 → 100644
Copyright (c) Jordi Boggiano
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// This file is part of the uutils coreutils package.
//
// (c) Jian Zeng <anonymousknight96 AT gmail.com>
// (c) Alex Lyon <arcterus@mail.com>
//
// For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code.
//
pub use platform::*;
mod platform;
//pub trait Uname
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
// This file is part of the uutils coreutils package.
//
// (c) Alex Lyon <arcterus@mail.com>
//
// For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code.
//
#[cfg(unix)]
pub use self::unix::*;
#[cfg(windows)]
pub use self::windows::*;
#[cfg(target_os = "redox")]
pub use self::redox::*;
#[cfg(unix)]
mod unix;
#[cfg(windows)]
mod windows;
#[cfg(target_os = "redox")]
mod redox;
// This file is part of the uutils coreutils package.
//
// (c) Alex Lyon <arcterus@mail.com>
//
// For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code.
//
use std::borrow::Cow;
use std::io::{self, Read};
use std::fs::File;
pub struct Uname {
kernel_name: String,
nodename: String,
kernel_release: String,
kernel_version: String,
machine: String
}
impl Uname {
pub fn new() -> io::Result<Uname> {
let mut inner = Box::new(String::new());
File::open("sys:uname")?.read_to_string(&mut inner)?;
let mut lines = inner.lines();
let kernel_name = lines.next().unwrap();
let nodename = lines.next().unwrap();
let kernel_release = lines.next().unwrap();
let kernel_version = lines.next().unwrap();
let machine = lines.next().unwrap();
// FIXME: don't actually duplicate the data as doing so is wasteful
Ok(Uname {
kernel_name: kernel_name.to_owned(),
nodename: nodename.to_owned(),
kernel_release: kernel_release.to_owned(),
kernel_version: kernel_version.to_owned(),
machine: machine.to_owned()
})
}
pub fn sysname(&self) -> Cow<str> {
Cow::from(self.kernel_name.as_str())
}
pub fn nodename(&self) -> Cow<str> {
Cow::from(self.nodename.as_str())
}
pub fn release(&self) -> Cow<str> {
Cow::from(self.kernel_release.as_str())
}
pub fn version(&self) -> Cow<str> {
Cow::from(self.kernel_version.as_str())
}
pub fn machine(&self) -> Cow<str> {
Cow::from(self.machine.as_str())
}
}
\ No newline at end of file
// This file is part of the uutils coreutils package.
//
// (c) Jian Zeng <anonymousknight96 AT gmail.com>
// (c) Alex Lyon <arcterus@mail.com>
//
// For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code.
//
extern crate libc;
use self::libc::{uname, utsname};
use std::mem;
use std::ffi::CStr;
use std::borrow::Cow;
use std::io;
macro_rules! cstr2cow {
($v:expr) => (
unsafe { CStr::from_ptr($v.as_ref().as_ptr()).to_string_lossy() }
)
}
pub struct Uname {
inner: utsname,
}
impl Uname {
pub fn new() -> io::Result<Self> {
unsafe {
let mut uts: utsname = mem::uninitialized();
if uname(&mut uts) == 0 {
Ok(Uname { inner: uts })
} else {
Err(io::Error::last_os_error())
}
}
}
pub fn sysname(&self) -> Cow<str> {
cstr2cow!(self.inner.sysname)
}
pub fn nodename(&self) -> Cow<str> {
cstr2cow!(self.inner.nodename)
}
pub fn release(&self) -> Cow<str> {
cstr2cow!(self.inner.release)
}
pub fn version(&self) -> Cow<str> {
cstr2cow!(self.inner.version)
}
pub fn machine(&self) -> Cow<str> {
cstr2cow!(self.inner.machine)
}
}
// This file is part of the uutils coreutils package.
//
// (c) Alex Lyon <arcterus@mail.com>
//
// For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code.
//
extern crate winapi;
use self::winapi::um::sysinfoapi::{SYSTEM_INFO, GetSystemInfo};
use self::winapi::um::winnt::*;
use std::mem;
use std::borrow::Cow;
use std::io;
pub struct Uname {
inner: SYSTEM_INFO
}
impl Uname {
pub fn new() -> io::Result<Uname> {
unsafe {
let mut info = mem::uninitialized();
GetSystemInfo(&mut info);
Ok(Uname { inner: info })
}
}
// FIXME: need to implement more architectures (e.g. ARM)
pub fn machine(&self) -> Cow<str> {
let arch = unsafe {
match self.inner.u.s().wProcessorArchitecture {
PROCESSOR_ARCHITECTURE_AMD64 => "x86_64",
PROCESSOR_ARCHITECTURE_INTEL => "x86",
_ => unimplemented!()
}
};
Cow::from(arch)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment