uu_hostid/
hostid.rs

1// This file is part of the uutils coreutils package.
2//
3// For the full copyright and license information, please view the LICENSE
4// file that was distributed with this source code.
5
6// spell-checker:ignore (ToDO) gethostid
7
8use clap::{crate_version, Command};
9use libc::c_long;
10use uucore::{error::UResult, format_usage, help_about, help_usage};
11
12const USAGE: &str = help_usage!("hostid.md");
13const ABOUT: &str = help_about!("hostid.md");
14
15// currently rust libc interface doesn't include gethostid
16extern "C" {
17    pub fn gethostid() -> c_long;
18}
19
20#[uucore::main]
21pub fn uumain(args: impl uucore::Args) -> UResult<()> {
22    uu_app().try_get_matches_from(args)?;
23    hostid();
24    Ok(())
25}
26
27pub fn uu_app() -> Command {
28    Command::new(uucore::util_name())
29        .version(crate_version!())
30        .about(ABOUT)
31        .override_usage(format_usage(USAGE))
32        .infer_long_args(true)
33}
34
35fn hostid() {
36    /*
37     * POSIX says gethostid returns a "32-bit identifier" but is silent
38     * whether it's sign-extended.  Turn off any sign-extension.  This
39     * is a no-op unless unsigned int is wider than 32 bits.
40     */
41
42    let mut result: c_long;
43    unsafe {
44        result = gethostid();
45    }
46
47    #[allow(overflowing_literals)]
48    let mask = 0xffff_ffff;
49
50    result &= mask;
51    println!("{result:0>8x}");
52}