Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/uu/hostname/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ normal = ["uucore_procs"]

[lib]
path = "src/hostname.rs"
test = false
doctest = false

[target.'cfg(any(all(unix, not(any(target_os = "aix", target_os = "redox"))), windows))'.dependencies]
Expand Down
60 changes: 50 additions & 10 deletions src/uu/hostname/src/hostname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ pub fn uu_app() -> Command {
)
}

/// DNS domain part of a host name (everything after the first `.`).
///
/// Matches net-tools `hostname -d` / `dnsdomainname`: no trailing domain yields
/// `None` (print nothing). The NIS/`domainname` value `(none)` comes from
/// `getdomainname(2)`, not from this DNS derivation.
fn dns_domain(hostname: &str) -> Option<&str> {
hostname
.char_indices()
.find(|&(_, c)| c == '.')
.map(|(i, _)| &hostname[i + 1..])
}

/// Short host name (everything before the first `.`), or the whole name.
fn short_hostname(hostname: &str) -> &str {
hostname
.char_indices()
.find(|&(_, c)| c == '.')
.map_or(hostname, |(i, _)| &hostname[..i])
}

fn display_hostname(matches: &ArgMatches) -> UResult<()> {
let hostname = hostname::get()
.map_err_context(|| "failed to get hostname".to_owned())?
Expand Down Expand Up @@ -118,16 +138,13 @@ fn display_hostname(matches: &ArgMatches) -> UResult<()> {

Ok(())
} else {
if matches.get_flag(OPT_SHORT) || matches.get_flag(OPT_DOMAIN) {
let mut it = hostname.char_indices().filter(|&ci| ci.1 == '.');
if let Some(ci) = it.next() {
if matches.get_flag(OPT_SHORT) {
writeln!(stdout(), "{}", &hostname[0..ci.0])?;
} else {
writeln!(stdout(), "{}", &hostname[ci.0 + 1..])?;
}
} else if matches.get_flag(OPT_SHORT) {
writeln!(stdout(), "{hostname}")?;
if matches.get_flag(OPT_SHORT) {
writeln!(stdout(), "{}", short_hostname(&hostname))?;
return Ok(());
}
if matches.get_flag(OPT_DOMAIN) {
if let Some(domain) = dns_domain(&hostname) {
writeln!(stdout(), "{domain}")?;
}
return Ok(());
}
Expand All @@ -137,3 +154,26 @@ fn display_hostname(matches: &ArgMatches) -> UResult<()> {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::{dns_domain, short_hostname};

#[test]
fn dns_domain_none_without_dot() {
assert_eq!(dns_domain("localhost"), None);
assert_eq!(dns_domain("myhost"), None);
}

#[test]
fn dns_domain_after_first_dot() {
assert_eq!(dns_domain("host.example.com"), Some("example.com"));
assert_eq!(dns_domain("a.b.c"), Some("b.c"));
}

#[test]
fn short_hostname_before_first_dot() {
assert_eq!(short_hostname("host.example.com"), "host");
assert_eq!(short_hostname("localhost"), "localhost");
}
}
13 changes: 0 additions & 13 deletions tests/by-util/test_hostname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,3 @@ fn test_hostname_full() {
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
}

#[test]
fn test_hostname_domain_empty() {
let fqdn = new_ucmd!().arg("-f").succeeds();
let short = new_ucmd!().arg("-s").succeeds();
let domain = new_ucmd!().arg("-d").succeeds();
let domain_short = new_ucmd!().arg("-sd").succeeds();

if fqdn.stdout() == short.stdout() {
assert!(domain.stdout().is_empty());
assert!(domain_short.stdout().is_empty());
}
}
Loading