From e160756d55224543015bbe32fd3d9149b75138a8 Mon Sep 17 00:00:00 2001 From: Ezra Barrow Date: Tue, 14 Nov 2023 02:35:05 -0600 Subject: [PATCH] Add hostname --- Cargo.toml | 1 + usr/src/mei/hostname/Cargo.toml | 11 ++++ usr/src/mei/hostname/src/hostname.rs | 78 ++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 usr/src/mei/hostname/Cargo.toml create mode 100644 usr/src/mei/hostname/src/hostname.rs diff --git a/Cargo.toml b/Cargo.toml index 695f829..a28f711 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ members = [ "usr/src/mei/echo", "usr/src/mei/printf", "usr/src/mei/false", + "usr/src/mei/hostname", ] diff --git a/usr/src/mei/hostname/Cargo.toml b/usr/src/mei/hostname/Cargo.toml new file mode 100644 index 0000000..b691d54 --- /dev/null +++ b/usr/src/mei/hostname/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "hostname" +version = "0.1.0" +edition = "2021" + +[[bin]] +name = "hostname" +path = "src/hostname.rs" + +[dependencies] +nix = { version = "0.27", features = ["hostname"] } diff --git a/usr/src/mei/hostname/src/hostname.rs b/usr/src/mei/hostname/src/hostname.rs new file mode 100644 index 0000000..272ec2e --- /dev/null +++ b/usr/src/mei/hostname/src/hostname.rs @@ -0,0 +1,78 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License, Version 1.0 only + * (the "License"). You may not use this file except in compliance + * with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright 2023 Ezra Barrow All rights reserved. + * Use is subject to license terms. + */ + +use nix::unistd::{gethostname, sethostname}; + +fn usage() { + println!("usage: hostname [-s] [system_name]"); +} + +//TODO: should probably involve a lot more osstrings as bytes rather than strings +fn main() -> Result<(), ()> { + let mut shortflag = false; + let mut newhostname = None; + for arg in std::env::args().skip(1) { + if arg == "-?" { + usage(); + return Ok(()); + } else if arg == "-s" { + shortflag = true; + } else if !arg.starts_with("-") { + if newhostname.replace(arg).is_some() { + usage(); + return Ok(()); + } + } + } + if let Some(hostname) = newhostname { + match sethostname(hostname) { + Ok(_) => Ok(()), + Err(e) => { + eprintln!("hostname: error in setting name: {}", e.desc()); + Err(()) + } + } + } else { + match gethostname() { + Ok(s) => { + if shortflag { + println!( + "{}", + s.to_string_lossy().split('.').nth(0).unwrap_or_default() + ); + } else { + println!("{}", s.to_string_lossy()); + }; + Ok(()) + } + Err(_) => { + eprintln!("hostname: unable to obtain hostname"); + Err(()) + } + } + } +}