mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
mktemp: rustfmt
This commit is contained in:
parent
c6897da7f0
commit
c53fa53c75
1 changed files with 36 additions and 33 deletions
|
@ -1,13 +1,13 @@
|
||||||
#![crate_name = "uu_mktemp"]
|
#![crate_name = "uu_mktemp"]
|
||||||
|
|
||||||
/*
|
// This file is part of the uutils coreutils package.
|
||||||
* This file is part of the uutils coreutils package.
|
//
|
||||||
*
|
// (c) Sunrin SHIMURA
|
||||||
* (c) Sunrin SHIMURA
|
// Collaborator: Jian Zeng
|
||||||
*
|
//
|
||||||
* For the full copyright and license information, please view the LICENSE
|
// For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
*/
|
//
|
||||||
|
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
@ -36,7 +36,9 @@ static DEFAULT_TEMPLATE: &'static str = "tmp.XXXXXXXXXX";
|
||||||
pub fn uumain(args: Vec<String>) -> i32 {
|
pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let mut opts = getopts::Options::new();
|
let mut opts = getopts::Options::new();
|
||||||
opts.optflag("d", "directory", "Make a directory instead of a file");
|
opts.optflag("d", "directory", "Make a directory instead of a file");
|
||||||
opts.optflag("u", "dry-run", "do not create anything; merely print a name (unsafe)");
|
opts.optflag("u",
|
||||||
|
"dry-run",
|
||||||
|
"do not create anything; merely print a name (unsafe)");
|
||||||
opts.optflag("q", "quiet", "Fail silently if an error occurs.");
|
opts.optflag("q", "quiet", "Fail silently if an error occurs.");
|
||||||
opts.optopt("", "suffix", "append SUFF to TEMPLATE; SUFF must not contain a path separator. This option is implied if TEMPLATE does not end with X.", "SUFF");
|
opts.optopt("", "suffix", "append SUFF to TEMPLATE; SUFF must not contain a path separator. This option is implied if TEMPLATE does not end with X.", "SUFF");
|
||||||
opts.optopt("p", "tmpdir", "interpret TEMPLATE relative to DIR; if DIR is not specified, use $TMPDIR if set, else /tmp. With this option, TEMPLATE must not be an absolute name; unlike with -t, TEMPLATE may contain slashes, but mktemp creates only the final component", "DIR");
|
opts.optopt("p", "tmpdir", "interpret TEMPLATE relative to DIR; if DIR is not specified, use $TMPDIR if set, else /tmp. With this option, TEMPLATE must not be an absolute name; unlike with -t, TEMPLATE may contain slashes, but mktemp creates only the final component", "DIR");
|
||||||
|
@ -49,12 +51,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
// >> early return options
|
// >> early return options
|
||||||
let matches = match opts.parse(&args[1..]) {
|
let matches = match opts.parse(&args[1..]) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => crash!(1, "Invalid options\n{}", f)
|
Err(f) => crash!(1, "Invalid options\n{}", f),
|
||||||
};
|
|
||||||
|
|
||||||
if matches.opt_present("quiet") {
|
|
||||||
// TODO: close stderror. `crash!` macro always write output to stderror
|
|
||||||
crash!(1, "quiet option is not supported yet.");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if matches.opt_present("help") {
|
if matches.opt_present("help") {
|
||||||
|
@ -83,15 +80,20 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
};
|
};
|
||||||
|
|
||||||
let (prefix, rand, suffix) = match parse_template(template) {
|
let (prefix, rand, suffix) = match parse_template(template) {
|
||||||
Some((p, r, s)) => match suffix_opt {
|
Some((p, r, s)) => {
|
||||||
Some(suf) => if s == "" {
|
match suffix_opt {
|
||||||
|
Some(suf) => {
|
||||||
|
if s == "" {
|
||||||
(p, r, suf)
|
(p, r, suf)
|
||||||
} else {
|
} else {
|
||||||
crash!(1, "Template should end with 'X' when you specify suffix option.")
|
crash!(1,
|
||||||
},
|
"Template should end with 'X' when you specify suffix option.")
|
||||||
None => (p, r, s.to_owned())
|
}
|
||||||
},
|
}
|
||||||
None => ("",0, "".to_owned())
|
None => (p, r, s.to_owned()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => ("", 0, "".to_owned()),
|
||||||
};
|
};
|
||||||
|
|
||||||
if rand < 3 {
|
if rand < 3 {
|
||||||
|
@ -106,12 +108,12 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let tmpdir = match matches.opt_str("tmpdir") {
|
let tmpdir = match matches.opt_str("tmpdir") {
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
if PathBuf::from(prefix).is_absolute() {
|
if PathBuf::from(prefix).is_absolute() {
|
||||||
crash!(1, "template must not be an absolute path when tempdir is specified.");
|
show_info!("invalid template, ‘{}’; with --tmpdir, it may not be absolute", template);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
PathBuf::from(s)
|
PathBuf::from(s)
|
||||||
|
}
|
||||||
},
|
None => env::temp_dir(),
|
||||||
None => env::temp_dir()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if dry_run {
|
if dry_run {
|
||||||
|
@ -125,7 +127,8 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
fn print_help(opts: &getopts::Options) {
|
fn print_help(opts: &getopts::Options) {
|
||||||
let usage = format!(" Create a temporary file or directory, safely, and print its name.
|
let usage = format!(" Create a temporary file or directory, safely, and print its name.
|
||||||
TEMPLATE must contain at least 3 consecutive 'X's in last component.
|
TEMPLATE must contain at least 3 consecutive 'X's in last component.
|
||||||
If TEMPLATE is not specified, use {}, and --tmpdir is implied", DEFAULT_TEMPLATE);
|
If TEMPLATE is not specified, use {}, and --tmpdir is implied",
|
||||||
|
DEFAULT_TEMPLATE);
|
||||||
|
|
||||||
println!("{} {}", NAME, VERSION);
|
println!("{} {}", NAME, VERSION);
|
||||||
println!("SYNOPSIS");
|
println!("SYNOPSIS");
|
||||||
|
@ -137,7 +140,7 @@ If TEMPLATE is not specified, use {}, and --tmpdir is implied", DEFAULT_TEMPLATE
|
||||||
fn parse_template(temp: &str) -> Option<(&str, usize, &str)> {
|
fn parse_template(temp: &str) -> Option<(&str, usize, &str)> {
|
||||||
let right = match temp.rfind('X') {
|
let right = match temp.rfind('X') {
|
||||||
Some(r) => r + 1,
|
Some(r) => r + 1,
|
||||||
None => return None
|
None => return None,
|
||||||
};
|
};
|
||||||
let left = temp[..right].rfind(|c| c != 'X').map_or(0, |i| i + 1);
|
let left = temp[..right].rfind(|c| c != 'X').map_or(0, |i| i + 1);
|
||||||
let prefix = &temp[..left];
|
let prefix = &temp[..left];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue