mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
parent
18191f9212
commit
698924a20a
3 changed files with 28 additions and 39 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -2500,7 +2500,7 @@ dependencies = [
|
||||||
name = "uu_unlink"
|
name = "uu_unlink"
|
||||||
version = "0.0.6"
|
version = "0.0.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"getopts",
|
"clap",
|
||||||
"libc",
|
"libc",
|
||||||
"uucore",
|
"uucore",
|
||||||
"uucore_procs",
|
"uucore_procs",
|
||||||
|
|
|
@ -15,7 +15,7 @@ edition = "2018"
|
||||||
path = "src/unlink.rs"
|
path = "src/unlink.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
getopts = "0.2.18"
|
clap = "2.33"
|
||||||
libc = "0.2.42"
|
libc = "0.2.42"
|
||||||
uucore = { version=">=0.0.8", package="uucore", path="../../uucore" }
|
uucore = { version=">=0.0.8", package="uucore", path="../../uucore" }
|
||||||
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
||||||
|
|
|
@ -12,59 +12,53 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
use getopts::Options;
|
use clap::{App, Arg};
|
||||||
use libc::{lstat, stat, unlink};
|
use libc::{lstat, stat, unlink};
|
||||||
use libc::{S_IFLNK, S_IFMT, S_IFREG};
|
use libc::{S_IFLNK, S_IFMT, S_IFREG};
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::io::{Error, ErrorKind};
|
use std::io::{Error, ErrorKind};
|
||||||
|
|
||||||
static NAME: &str = "unlink";
|
|
||||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
static ABOUT: &str = "Unlink the file at [FILE].";
|
||||||
|
static OPT_PATH: &str = "FILE";
|
||||||
|
|
||||||
|
fn get_usage() -> String {
|
||||||
|
format!("{} [OPTION]... FILE", executable!())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
let args = args.collect_str();
|
let args = args.collect_str();
|
||||||
|
|
||||||
let mut opts = Options::new();
|
let usage = get_usage();
|
||||||
|
|
||||||
opts.optflag("h", "help", "display this help and exit");
|
let matches = App::new(executable!())
|
||||||
opts.optflag("V", "version", "output version information and exit");
|
.version(VERSION)
|
||||||
|
.about(ABOUT)
|
||||||
|
.usage(&usage[..])
|
||||||
|
.arg(Arg::with_name(OPT_PATH).hidden(true).multiple(true))
|
||||||
|
.get_matches_from(args);
|
||||||
|
|
||||||
let matches = match opts.parse(&args[1..]) {
|
let paths: Vec<String> = matches
|
||||||
Ok(m) => m,
|
.values_of(OPT_PATH)
|
||||||
Err(f) => crash!(1, "invalid options\n{}", f),
|
.map(|v| v.map(ToString::to_string).collect())
|
||||||
};
|
.unwrap_or_default();
|
||||||
|
|
||||||
if matches.opt_present("help") {
|
if paths.is_empty() {
|
||||||
println!("{} {}", NAME, VERSION);
|
|
||||||
println!();
|
|
||||||
println!("Usage:");
|
|
||||||
println!(" {} [FILE]... [OPTION]...", NAME);
|
|
||||||
println!();
|
|
||||||
println!("{}", opts.usage("Unlink the file at [FILE]."));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches.opt_present("version") {
|
|
||||||
println!("{} {}", NAME, VERSION);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches.free.is_empty() {
|
|
||||||
crash!(
|
crash!(
|
||||||
1,
|
1,
|
||||||
"missing operand\nTry '{0} --help' for more information.",
|
"missing operand\nTry '{0} --help' for more information.",
|
||||||
NAME
|
executable!()
|
||||||
);
|
);
|
||||||
} else if matches.free.len() > 1 {
|
} else if paths.len() > 1 {
|
||||||
crash!(
|
crash!(
|
||||||
1,
|
1,
|
||||||
"extra operand: '{1}'\nTry '{0} --help' for more information.",
|
"extra operand: '{1}'\nTry '{0} --help' for more information.",
|
||||||
NAME,
|
executable!(),
|
||||||
matches.free[1]
|
paths[1]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let c_string = CString::new(matches.free[0].clone()).unwrap(); // unwrap() cannot fail, the string comes from argv so it cannot contain a \0.
|
let c_string = CString::new(paths[0].clone()).unwrap(); // unwrap() cannot fail, the string comes from argv so it cannot contain a \0.
|
||||||
|
|
||||||
let st_mode = {
|
let st_mode = {
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
|
@ -72,12 +66,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
let result = unsafe { lstat(c_string.as_ptr(), &mut buf as *mut stat) };
|
let result = unsafe { lstat(c_string.as_ptr(), &mut buf as *mut stat) };
|
||||||
|
|
||||||
if result < 0 {
|
if result < 0 {
|
||||||
crash!(
|
crash!(1, "Cannot stat '{}': {}", paths[0], Error::last_os_error());
|
||||||
1,
|
|
||||||
"Cannot stat '{}': {}",
|
|
||||||
matches.free[0],
|
|
||||||
Error::last_os_error()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.st_mode & S_IFMT
|
buf.st_mode & S_IFMT
|
||||||
|
@ -101,7 +90,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
crash!(1, "cannot unlink '{0}': {1}", matches.free[0], e);
|
crash!(1, "cannot unlink '{0}': {1}", paths[0], e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue