mirror of
https://github.com/uutils/coreutils
synced 2024-12-19 09:33:25 +00:00
b90d253584
For coreutils, there are two build artifacts: 1. multicall executable (each utility is a separate static library) 2. individual utilities (still separate library with main wrapper) To avoid namespace collision, each utility crate is defined as "uu_{CMD}". The end user only sees the original utility name. This simplifies build.rs. Also, the thin wrapper for the main() function is no longer contained in the crate. It has been separated into a dedicated file. This was necessary to work around Cargo's need for the crate name attribute to match the name in the respective Cargo.toml.
89 lines
1.9 KiB
Rust
89 lines
1.9 KiB
Rust
#![crate_name = "uu_hostid"]
|
|
|
|
/*
|
|
* This file is part of the uutils coreutils package.
|
|
*
|
|
* (c) Maciej Dziardziel <fiedzia@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE file
|
|
* that was distributed with this source code.
|
|
*/
|
|
|
|
extern crate getopts;
|
|
extern crate libc;
|
|
|
|
#[macro_use]
|
|
extern crate uucore;
|
|
|
|
use libc::c_long;
|
|
|
|
static NAME: &'static str = "hostid";
|
|
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
|
|
|
static EXIT_ERR: i32 = 1;
|
|
|
|
pub enum Mode {
|
|
HostId,
|
|
Help,
|
|
Version,
|
|
}
|
|
|
|
// currently rust libc interface doesn't include gethostid
|
|
extern {
|
|
pub fn gethostid() -> c_long;
|
|
}
|
|
|
|
pub fn uumain(args: Vec<String>) -> i32 {
|
|
let mut opts = getopts::Options::new();
|
|
opts.optflag("", "help", "display this help and exit");
|
|
opts.optflag("", "version", "output version information and exit");
|
|
|
|
let matches = match opts.parse(&args[1..]) {
|
|
Ok(m) => m,
|
|
Err(_) => {
|
|
help(&opts);
|
|
return EXIT_ERR;
|
|
},
|
|
};
|
|
|
|
let mode = if matches.opt_present("version") {
|
|
Mode::Version
|
|
} else if matches.opt_present("help") {
|
|
Mode::Help
|
|
} else {
|
|
Mode::HostId
|
|
};
|
|
|
|
match mode {
|
|
Mode::HostId => hostid(),
|
|
Mode::Help => help(&opts),
|
|
Mode::Version => version(),
|
|
}
|
|
|
|
0
|
|
}
|
|
|
|
fn version() {
|
|
println!("{} {}", NAME, VERSION);
|
|
}
|
|
|
|
fn help(opts: &getopts::Options) {
|
|
let msg = format!("Usage:\n {} [options]", NAME);
|
|
print!("{}", opts.usage(&msg));
|
|
}
|
|
|
|
fn hostid() {
|
|
/*
|
|
* POSIX says gethostid returns a "32-bit identifier" but is silent
|
|
* whether it's sign-extended. Turn off any sign-extension. This
|
|
* is a no-op unless unsigned int is wider than 32 bits.
|
|
*/
|
|
|
|
let mut result:c_long;
|
|
unsafe {
|
|
result = gethostid();
|
|
}
|
|
|
|
result &= 0xffffffff;
|
|
println!("{:0>8x}", result);
|
|
}
|