1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 13:37:48 +00:00

arch: use utsname in libc instead

This commit is contained in:
Knight 2016-08-20 03:53:26 +08:00
parent c63aa19cd1
commit ac6bc5886b
2 changed files with 15 additions and 43 deletions

View file

@ -7,9 +7,10 @@ authors = []
name = "uu_arch" name = "uu_arch"
path = "arch.rs" path = "arch.rs"
[dependencies] [dependencies.uucore]
libc = "*" path = "../uucore"
uucore = { path="../uucore" } default-features = false
features = ["utsname"]
[[bin]] [[bin]]
name = "arch" name = "arch"

View file

@ -1,54 +1,25 @@
#![crate_name = "uu_arch"] #![crate_name = "uu_arch"]
/* // This file is part of the uutils coreutils package.
* This file is part of the uutils coreutils package. //
* // (c) Smigle00 <smigle00@gmail.com>
* (c) Smigle00 <smigle00@gmail.com> // (c) Jian Zeng <anonymousknight96 AT gmail.com>
* //
* For the full copyright and license information, please view the LICENSE // For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. // file that was distributed with this source code.
*/ //
extern crate libc;
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
use uucore::utsname::Uname;
use std::ffi::CStr;
use std::mem::uninitialized;
use uucore::c_types::utsname;
static SYNTAX: &'static str = ""; static SYNTAX: &'static str = "";
static SUMMARY: &'static str = "Determine architecture name for current machine."; static SUMMARY: &'static str = "Determine architecture name for current machine.";
static LONG_HELP: &'static str = ""; static LONG_HELP: &'static str = "";
struct Arch {
arch_name: String
}
extern {
fn uname(uts: *mut utsname);
}
unsafe fn string_from_c_str(ptr: *const i8) -> String {
String::from_utf8_lossy(CStr::from_ptr(ptr as *const std::os::raw::c_char).to_bytes()).to_string()
}
unsafe fn get_machine_arch() -> Arch {
let mut uts: utsname = uninitialized();
uname(&mut uts);
Arch {
arch_name: string_from_c_str(uts.machine.as_ptr() as *const i8)
}
}
pub fn uumain(args: Vec<String>) -> i32 { pub fn uumain(args: Vec<String>) -> i32 {
new_coreopts!(SYNTAX, SUMMARY, LONG_HELP).parse(args); new_coreopts!(SYNTAX, SUMMARY, LONG_HELP).parse(args);
let uts = Uname::new();
let machine_arch = unsafe { get_machine_arch() }; println!("{}", uts.machine().trim());
let mut output = String::new();
output.push_str(machine_arch.arch_name.as_ref());
println!("{}", output.trim());
0 0
} }