diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 80540d222..3ccff0303 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -1498,6 +1498,7 @@ fn copy_file( options.overwrite, OverwriteMode::Clobber(ClobberMode::RemoveDestination) ) + && std::env::var_os("POSIXLY_CORRECT").is_none() { return Err(Error::Error(format!( "not writing through dangling symlink '{}'", diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index 7a0743bd1..b77ad474e 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -1903,6 +1903,17 @@ fn test_copy_through_dangling_symlink() { .stderr_only("cp: not writing through dangling symlink 'target'\n"); } +#[test] +fn test_copy_through_dangling_symlink_posixly_correct() { + let (at, mut ucmd) = at_and_ucmd!(); + at.touch("file"); + at.symlink_file("nonexistent", "target"); + ucmd.arg("file") + .arg("target") + .env("POSIXLY_CORRECT", "1") + .succeeds(); +} + #[test] fn test_copy_through_dangling_symlink_no_dereference() { let (at, mut ucmd) = at_and_ucmd!();