diff --git a/src/uu/mktemp/src/mktemp.rs b/src/uu/mktemp/src/mktemp.rs index 491814b268d..1faf0719a89 100644 --- a/src/uu/mktemp/src/mktemp.rs +++ b/src/uu/mktemp/src/mktemp.rs @@ -53,9 +53,14 @@ enum MkTempError { /// The template suffix contains a path separator (e.g. `"XXXa/b"`). SuffixContainsDirSeparator(String), InvalidTemplate(String), + TooManyTemplates, } -impl UError for MkTempError {} +impl UError for MkTempError { + fn usage(&self) -> bool { + matches!(self, Self::TooManyTemplates) + } +} impl Error for MkTempError {} @@ -85,6 +90,9 @@ impl Display for MkTempError { "invalid template, {}; with --tmpdir, it may not be absolute", s.quote() ), + TooManyTemplates => { + write!(f, "too many templates") + } } } } @@ -317,11 +325,24 @@ impl Params { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let matches = uu_app().try_get_matches_from(args)?; + let args = args.collect_str_lossy().accept_any(); + + let matches = uu_app().try_get_matches_from(&args)?; // Parse command-line options into a format suitable for the // application logic. let options = Options::from(&matches); + + if env::var("POSIXLY_CORRECT").is_ok() { + // If POSIXLY_CORRECT was set, template MUST be the last argument. + if is_tmpdir_argument_actually_the_template(&matches) || matches.is_present(ARG_TEMPLATE) { + // Template argument was provided, check if was the last one. + if args.last().unwrap() != &options.template { + return Err(Box::new(MkTempError::TooManyTemplates)); + } + } + } + let dry_run = options.dry_run; let suppress_file_err = options.quiet; let make_dir = options.directory; diff --git a/tests/by-util/test_mktemp.rs b/tests/by-util/test_mktemp.rs index 7d059aeab89..db40cd7f668 100644 --- a/tests/by-util/test_mktemp.rs +++ b/tests/by-util/test_mktemp.rs @@ -680,3 +680,25 @@ fn test_tmpdir_env_var() { assert_matches_template!(template, filename); assert!(at.file_exists(filename)); } + +#[test] +fn test_mktemp_with_posixly_correct() { + let scene = TestScenario::new(util_name!()); + + scene + .ucmd() + .env("POSIXLY_CORRECT", "1") + .args(&["aXXXX", "--suffix=b"]) + .fails() + .stderr_is(&format!( + "mktemp: too many templates\nTry '{} {} --help' for more information.\n", + scene.bin_path.to_string_lossy(), + scene.util_name + )); + + scene + .ucmd() + .env("POSIXLY_CORRECT", "1") + .args(&["--suffix=b", "aXXXX"]) + .succeeds(); +} diff --git a/tests/by-util/test_tail.rs b/tests/by-util/test_tail.rs index 3b809114f56..2f8ca4a7890 100644 --- a/tests/by-util/test_tail.rs +++ b/tests/by-util/test_tail.rs @@ -1877,10 +1877,10 @@ fn test_follow_name_move_create2() { "9", ]; - let delay = 300; + let mut delay = 500; for _ in 0..2 { let mut p = ts.ucmd().set_stdin(Stdio::null()).args(&args).run_no_wait(); - sleep(Duration::from_millis(100)); + sleep(Duration::from_millis(delay)); at.truncate("9", "x\n"); sleep(Duration::from_millis(delay)); @@ -1916,6 +1916,7 @@ fn test_follow_name_move_create2() { at.remove("f"); args.push("---disable-inotify"); + delay = 2000; } }