1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

Merge pull request #1184 from jiangjun1983/hostname_xgethostname_fix

hostname: bug fix for non-windows xgethostname
This commit is contained in:
Alex Lyon 2018-05-02 08:30:03 -07:00 committed by GitHub
commit bc8ad30eef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 5 deletions

View file

@ -157,13 +157,12 @@ fn xgethostname() -> io::Result<String> {
}; };
if err == 0 { if err == 0 {
let mut last_char = name.iter().position(|byte| *byte == 0).unwrap_or(namelen); let null_pos = name.iter().position(|byte| *byte == 0).unwrap_or(namelen);
if last_char == name.len() { if null_pos == namelen {
name.push(0); name.push(0);
last_char += 1;
} }
Ok(CStr::from_bytes_with_nul(&name[..last_char]) Ok(CStr::from_bytes_with_nul(&name[..null_pos + 1])
.unwrap() .unwrap()
.to_string_lossy() .to_string_lossy()
.into_owned()) .into_owned())

12
tests/test_hostname.rs Normal file
View file

@ -0,0 +1,12 @@
use common::util::*;
#[test]
fn test_hostname() {
let ls_default_res = new_ucmd!().succeeds();
let ls_short_res = new_ucmd!().arg("-s").succeeds();
let ls_domain_res = new_ucmd!().arg("-d").succeeds();
assert!(ls_default_res.stdout.len() >= ls_short_res.stdout.len());
assert!(ls_default_res.stdout.len() >= ls_domain_res.stdout.len());
}

View file

@ -91,6 +91,7 @@ generic! {
"tsort", test_tsort; "tsort", test_tsort;
"unexpand", test_unexpand; "unexpand", test_unexpand;
"uniq", test_uniq; "uniq", test_uniq;
"wc", test_wc;
// Be aware of the trailing semicolon after the last item // Be aware of the trailing semicolon after the last item
"wc", test_wc "hostname", test_hostname
} }