about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorEzra Barrow <[email protected]>2023-11-14 02:35:05 -0600
committerEzra Barrow <[email protected]>2023-11-14 02:35:05 -0600
commite160756d55224543015bbe32fd3d9149b75138a8 (patch)
treef54b58b6282655d0b12c86a276f96e56afa3c57b
parent56f461df4ad1cd990f4ce6406e766891d5c2efc1 (diff)
downloadk2spice-e160756d55224543015bbe32fd3d9149b75138a8.tar.gz
k2spice-e160756d55224543015bbe32fd3d9149b75138a8.tar.bz2
k2spice-e160756d55224543015bbe32fd3d9149b75138a8.zip
Add hostname
-rw-r--r--Cargo.toml1
-rw-r--r--usr/src/mei/hostname/Cargo.toml11
-rw-r--r--usr/src/mei/hostname/src/hostname.rs78
3 files changed, 90 insertions, 0 deletions
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(())
+            }
+        }
+    }
+}