mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 11:07:44 +00:00
Merge pull request #3604 from weijunji/mktemp-fix
mktemp: respect POSIXLY_CORRECT env var when parsing args
This commit is contained in:
commit
95de5f6494
2 changed files with 45 additions and 2 deletions
|
@ -53,9 +53,14 @@ enum MkTempError {
|
||||||
/// The template suffix contains a path separator (e.g. `"XXXa/b"`).
|
/// The template suffix contains a path separator (e.g. `"XXXa/b"`).
|
||||||
SuffixContainsDirSeparator(String),
|
SuffixContainsDirSeparator(String),
|
||||||
InvalidTemplate(String),
|
InvalidTemplate(String),
|
||||||
|
TooManyTemplates,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UError for MkTempError {}
|
impl UError for MkTempError {
|
||||||
|
fn usage(&self) -> bool {
|
||||||
|
matches!(self, Self::TooManyTemplates)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Error for MkTempError {}
|
impl Error for MkTempError {}
|
||||||
|
|
||||||
|
@ -85,6 +90,9 @@ impl Display for MkTempError {
|
||||||
"invalid template, {}; with --tmpdir, it may not be absolute",
|
"invalid template, {}; with --tmpdir, it may not be absolute",
|
||||||
s.quote()
|
s.quote()
|
||||||
),
|
),
|
||||||
|
TooManyTemplates => {
|
||||||
|
write!(f, "too many templates")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -308,11 +316,24 @@ impl Params {
|
||||||
|
|
||||||
#[uucore::main]
|
#[uucore::main]
|
||||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
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
|
// Parse command-line options into a format suitable for the
|
||||||
// application logic.
|
// application logic.
|
||||||
let options = Options::from(&matches);
|
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 dry_run = options.dry_run;
|
||||||
let suppress_file_err = options.quiet;
|
let suppress_file_err = options.quiet;
|
||||||
let make_dir = options.directory;
|
let make_dir = options.directory;
|
||||||
|
|
|
@ -641,3 +641,25 @@ fn test_suffix_empty_template() {
|
||||||
.fails()
|
.fails()
|
||||||
.stderr_is("mktemp: with --suffix, template '' must end in X\n");
|
.stderr_is("mktemp: with --suffix, template '' must end in X\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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();
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue