diff --git a/src/uu/nohup/src/nohup.rs b/src/uu/nohup/src/nohup.rs index 6280d44e1..c4d5707dd 100644 --- a/src/uu/nohup/src/nohup.rs +++ b/src/uu/nohup/src/nohup.rs @@ -150,12 +150,12 @@ fn find_stdout() -> UResult { Ok(t) } Err(e1) => { - let Ok(home) = env::var("HOME") else { + let Some(home) = env::var_os("HOME") else { return Err(NohupError::OpenFailed(internal_failure_code, e1).into()); }; let mut homeout = PathBuf::from(home); homeout.push(NOHUP_OUT); - let homeout_str = homeout.to_str().unwrap(); + let homeout_str = homeout.to_string_lossy(); match OpenOptions::new().create(true).append(true).open(&homeout) { Ok(t) => { show_error!( diff --git a/tests/by-util/test_nohup.rs b/tests/by-util/test_nohup.rs index f3fa0bc94..f11f0609d 100644 --- a/tests/by-util/test_nohup.rs +++ b/tests/by-util/test_nohup.rs @@ -247,3 +247,23 @@ fn test_nohup_stderr_to_stdout() { assert!(content.contains("stdout message")); assert!(content.contains("stderr message")); } + +#[test] +#[cfg(all(unix, not(target_os = "macos")))] +fn test_nohup_non_utf8_home() { + use std::{ffi::OsStr, os::unix::ffi::OsStrExt}; + let ts = TestScenario::new(util_name!()); + let at = &ts.fixtures; + + at.mkdir("nohup.out"); + at.mkdir(OsStr::from_bytes(b"nohup_home\x80")); + let path = at.plus(OsStr::from_bytes(b"nohup_home\x80")); + + ts.ucmd() + .terminal_simulation(true) + .env("HOME", path.as_os_str()) + .args(&["true"]) + .succeeds(); + + assert!(at.file_exists(path.join("nohup.out"))); +}