mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
commit
279656426b
45 changed files with 212 additions and 243 deletions
|
@ -111,6 +111,7 @@ fn basename(fullname: &str, suffix: &str) -> String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::manual_strip)] // can be replaced with strip_suffix once the minimum rust version is 1.45
|
||||||
fn strip_suffix(name: &str, suffix: &str) -> String {
|
fn strip_suffix(name: &str, suffix: &str) -> String {
|
||||||
if name == suffix {
|
if name == suffix {
|
||||||
return name.to_owned();
|
return name.to_owned();
|
||||||
|
|
|
@ -286,7 +286,7 @@ impl Chgrper {
|
||||||
|
|
||||||
ret = match wrap_chgrp(path, &meta, self.dest_gid, follow, self.verbosity.clone()) {
|
ret = match wrap_chgrp(path, &meta, self.dest_gid, follow, self.verbosity.clone()) {
|
||||||
Ok(n) => {
|
Ok(n) => {
|
||||||
if n != "" {
|
if !n.is_empty() {
|
||||||
show_info!("{}", n);
|
show_info!("{}", n);
|
||||||
}
|
}
|
||||||
0
|
0
|
||||||
|
|
|
@ -171,13 +171,13 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
// of a prefix '-' if it's associated with MODE
|
// of a prefix '-' if it's associated with MODE
|
||||||
// e.g. "chmod -v -xw -R FILE" -> "chmod -v xw -R FILE"
|
// e.g. "chmod -v -xw -R FILE" -> "chmod -v xw -R FILE"
|
||||||
pub fn strip_minus_from_mode(args: &mut Vec<String>) -> bool {
|
pub fn strip_minus_from_mode(args: &mut Vec<String>) -> bool {
|
||||||
for i in 0..args.len() {
|
for arg in args {
|
||||||
if args[i].starts_with("-") {
|
if arg.starts_with('-') {
|
||||||
if let Some(second) = args[i].chars().nth(1) {
|
if let Some(second) = arg.chars().nth(1) {
|
||||||
match second {
|
match second {
|
||||||
'r' | 'w' | 'x' | 'X' | 's' | 't' | 'u' | 'g' | 'o' | '0'..='7' => {
|
'r' | 'w' | 'x' | 'X' | 's' | 't' | 'u' | 'g' | 'o' | '0'..='7' => {
|
||||||
// TODO: use strip_prefix() once minimum rust version reaches 1.45.0
|
// TODO: use strip_prefix() once minimum rust version reaches 1.45.0
|
||||||
args[i] = args[i][1..args[i].len()].to_string();
|
*arg = arg[1..arg.len()].to_string();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
|
@ -391,7 +391,7 @@ impl Chowner {
|
||||||
self.verbosity.clone(),
|
self.verbosity.clone(),
|
||||||
) {
|
) {
|
||||||
Ok(n) => {
|
Ok(n) => {
|
||||||
if n != "" {
|
if !n.is_empty() {
|
||||||
show_info!("{}", n);
|
show_info!("{}", n);
|
||||||
}
|
}
|
||||||
0
|
0
|
||||||
|
@ -446,7 +446,7 @@ impl Chowner {
|
||||||
self.verbosity.clone(),
|
self.verbosity.clone(),
|
||||||
) {
|
) {
|
||||||
Ok(n) => {
|
Ok(n) => {
|
||||||
if n != "" {
|
if !n.is_empty() {
|
||||||
show_info!("{}", n);
|
show_info!("{}", n);
|
||||||
}
|
}
|
||||||
0
|
0
|
||||||
|
|
|
@ -104,7 +104,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
_ => {
|
_ => {
|
||||||
let mut vector: Vec<&str> = Vec::new();
|
let mut vector: Vec<&str> = Vec::new();
|
||||||
for (&k, v) in matches.args.iter() {
|
for (&k, v) in matches.args.iter() {
|
||||||
vector.push(k.clone());
|
vector.push(k);
|
||||||
vector.push(&v.vals[0].to_str().unwrap());
|
vector.push(&v.vals[0].to_str().unwrap());
|
||||||
}
|
}
|
||||||
vector
|
vector
|
||||||
|
@ -133,7 +133,7 @@ fn set_context(root: &Path, options: &clap::ArgMatches) {
|
||||||
let userspec = match userspec_str {
|
let userspec = match userspec_str {
|
||||||
Some(ref u) => {
|
Some(ref u) => {
|
||||||
let s: Vec<&str> = u.split(':').collect();
|
let s: Vec<&str> = u.split(':').collect();
|
||||||
if s.len() != 2 || s.iter().any(|&spec| spec == "") {
|
if s.len() != 2 || s.iter().any(|&spec| spec.is_empty()) {
|
||||||
crash!(1, "invalid userspec: `{}`", u)
|
crash!(1, "invalid userspec: `{}`", u)
|
||||||
};
|
};
|
||||||
s
|
s
|
||||||
|
@ -142,16 +142,16 @@ fn set_context(root: &Path, options: &clap::ArgMatches) {
|
||||||
};
|
};
|
||||||
|
|
||||||
let (user, group) = if userspec.is_empty() {
|
let (user, group) = if userspec.is_empty() {
|
||||||
(&user_str[..], &group_str[..])
|
(user_str, group_str)
|
||||||
} else {
|
} else {
|
||||||
(&userspec[0][..], &userspec[1][..])
|
(userspec[0], userspec[1])
|
||||||
};
|
};
|
||||||
|
|
||||||
enter_chroot(root);
|
enter_chroot(root);
|
||||||
|
|
||||||
set_groups_from_str(&groups_str[..]);
|
set_groups_from_str(groups_str);
|
||||||
set_main_group(&group[..]);
|
set_main_group(group);
|
||||||
set_user(&user[..]);
|
set_user(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enter_chroot(root: &Path) {
|
fn enter_chroot(root: &Path) {
|
||||||
|
|
|
@ -132,7 +132,9 @@ macro_rules! prompt_yes(
|
||||||
|
|
||||||
pub type CopyResult<T> = Result<T, Error>;
|
pub type CopyResult<T> = Result<T, Error>;
|
||||||
pub type Source = PathBuf;
|
pub type Source = PathBuf;
|
||||||
|
pub type SourceSlice = Path;
|
||||||
pub type Target = PathBuf;
|
pub type Target = PathBuf;
|
||||||
|
pub type TargetSlice = Path;
|
||||||
|
|
||||||
/// Specifies whether when overwrite files
|
/// Specifies whether when overwrite files
|
||||||
#[derive(Clone, Eq, PartialEq)]
|
#[derive(Clone, Eq, PartialEq)]
|
||||||
|
@ -547,14 +549,13 @@ impl FromStr for Attribute {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_all_attributes() -> Vec<Attribute> {
|
fn add_all_attributes() -> Vec<Attribute> {
|
||||||
let mut attr = Vec::new();
|
use Attribute::*;
|
||||||
|
|
||||||
|
let mut attr = vec![Ownership, Timestamps, Context, Xattr, Links];
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
attr.push(Attribute::Mode);
|
attr.insert(0, Mode);
|
||||||
attr.push(Attribute::Ownership);
|
|
||||||
attr.push(Attribute::Timestamps);
|
|
||||||
attr.push(Attribute::Context);
|
|
||||||
attr.push(Attribute::Xattr);
|
|
||||||
attr.push(Attribute::Links);
|
|
||||||
attr
|
attr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -665,7 +666,7 @@ impl TargetType {
|
||||||
///
|
///
|
||||||
/// Treat target as a dir if we have multiple sources or the target
|
/// Treat target as a dir if we have multiple sources or the target
|
||||||
/// exists and already is a directory
|
/// exists and already is a directory
|
||||||
fn determine(sources: &[Source], target: &Target) -> TargetType {
|
fn determine(sources: &[Source], target: &TargetSlice) -> TargetType {
|
||||||
if sources.len() > 1 || target.is_dir() {
|
if sources.len() > 1 || target.is_dir() {
|
||||||
TargetType::Directory
|
TargetType::Directory
|
||||||
} else {
|
} else {
|
||||||
|
@ -714,7 +715,7 @@ fn parse_path_args(path_args: &[String], options: &Options) -> CopyResult<(Vec<S
|
||||||
|
|
||||||
fn preserve_hardlinks(
|
fn preserve_hardlinks(
|
||||||
hard_links: &mut Vec<(String, u64)>,
|
hard_links: &mut Vec<(String, u64)>,
|
||||||
source: &std::path::PathBuf,
|
source: &std::path::Path,
|
||||||
dest: std::path::PathBuf,
|
dest: std::path::PathBuf,
|
||||||
found_hard_link: &mut bool,
|
found_hard_link: &mut bool,
|
||||||
) -> CopyResult<()> {
|
) -> CopyResult<()> {
|
||||||
|
@ -788,7 +789,7 @@ fn preserve_hardlinks(
|
||||||
/// Behavior depends on `options`, see [`Options`] for details.
|
/// Behavior depends on `options`, see [`Options`] for details.
|
||||||
///
|
///
|
||||||
/// [`Options`]: ./struct.Options.html
|
/// [`Options`]: ./struct.Options.html
|
||||||
fn copy(sources: &[Source], target: &Target, options: &Options) -> CopyResult<()> {
|
fn copy(sources: &[Source], target: &TargetSlice, options: &Options) -> CopyResult<()> {
|
||||||
let target_type = TargetType::determine(sources, target);
|
let target_type = TargetType::determine(sources, target);
|
||||||
verify_target_type(target, &target_type)?;
|
verify_target_type(target, &target_type)?;
|
||||||
|
|
||||||
|
@ -840,7 +841,7 @@ fn copy(sources: &[Source], target: &Target, options: &Options) -> CopyResult<()
|
||||||
|
|
||||||
fn construct_dest_path(
|
fn construct_dest_path(
|
||||||
source_path: &Path,
|
source_path: &Path,
|
||||||
target: &Target,
|
target: &TargetSlice,
|
||||||
target_type: &TargetType,
|
target_type: &TargetType,
|
||||||
options: &Options,
|
options: &Options,
|
||||||
) -> CopyResult<PathBuf> {
|
) -> CopyResult<PathBuf> {
|
||||||
|
@ -870,8 +871,8 @@ fn construct_dest_path(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn copy_source(
|
fn copy_source(
|
||||||
source: &Source,
|
source: &SourceSlice,
|
||||||
target: &Target,
|
target: &TargetSlice,
|
||||||
target_type: &TargetType,
|
target_type: &TargetType,
|
||||||
options: &Options,
|
options: &Options,
|
||||||
) -> CopyResult<()> {
|
) -> CopyResult<()> {
|
||||||
|
@ -912,7 +913,7 @@ fn adjust_canonicalization(p: &Path) -> Cow<Path> {
|
||||||
///
|
///
|
||||||
/// Any errors encountered copying files in the tree will be logged but
|
/// Any errors encountered copying files in the tree will be logged but
|
||||||
/// will not cause a short-circuit.
|
/// will not cause a short-circuit.
|
||||||
fn copy_directory(root: &Path, target: &Target, options: &Options) -> CopyResult<()> {
|
fn copy_directory(root: &Path, target: &TargetSlice, options: &Options) -> CopyResult<()> {
|
||||||
if !options.recursive {
|
if !options.recursive {
|
||||||
return Err(format!("omitting directory '{}'", root.display()).into());
|
return Err(format!("omitting directory '{}'", root.display()).into());
|
||||||
}
|
}
|
||||||
|
@ -1068,6 +1069,7 @@ fn copy_attribute(source: &Path, dest: &Path, attribute: &Attribute) -> CopyResu
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
|
#[allow(clippy::unnecessary_wraps)] // needed for windows version
|
||||||
fn symlink_file(source: &Path, dest: &Path, context: &str) -> CopyResult<()> {
|
fn symlink_file(source: &Path, dest: &Path, context: &str) -> CopyResult<()> {
|
||||||
match std::os::unix::fs::symlink(source, dest).context(context) {
|
match std::os::unix::fs::symlink(source, dest).context(context) {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
|
|
|
@ -406,7 +406,7 @@ fn cut_files(mut filenames: Vec<String>, mode: Mode) -> i32 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !path.metadata().is_ok() {
|
if path.metadata().is_err() {
|
||||||
show_error!("{}: No such file or directory", filename);
|
show_error!("{}: No such file or directory", filename);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -535,40 +535,36 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
matches.value_of(options::CHARACTERS),
|
matches.value_of(options::CHARACTERS),
|
||||||
matches.value_of(options::FIELDS),
|
matches.value_of(options::FIELDS),
|
||||||
) {
|
) {
|
||||||
(Some(byte_ranges), None, None) => {
|
(Some(byte_ranges), None, None) => list_to_ranges(byte_ranges, complement).map(|ranges| {
|
||||||
list_to_ranges(&byte_ranges[..], complement).map(|ranges| {
|
Mode::Bytes(
|
||||||
Mode::Bytes(
|
ranges,
|
||||||
ranges,
|
Options {
|
||||||
Options {
|
out_delim: Some(
|
||||||
out_delim: Some(
|
matches
|
||||||
matches
|
.value_of(options::OUTPUT_DELIMITER)
|
||||||
.value_of(options::OUTPUT_DELIMITER)
|
.unwrap_or_default()
|
||||||
.unwrap_or_default()
|
.to_owned(),
|
||||||
.to_owned(),
|
),
|
||||||
),
|
zero_terminated: matches.is_present(options::ZERO_TERMINATED),
|
||||||
zero_terminated: matches.is_present(options::ZERO_TERMINATED),
|
},
|
||||||
},
|
)
|
||||||
)
|
}),
|
||||||
})
|
(None, Some(char_ranges), None) => list_to_ranges(char_ranges, complement).map(|ranges| {
|
||||||
}
|
Mode::Characters(
|
||||||
(None, Some(char_ranges), None) => {
|
ranges,
|
||||||
list_to_ranges(&char_ranges[..], complement).map(|ranges| {
|
Options {
|
||||||
Mode::Characters(
|
out_delim: Some(
|
||||||
ranges,
|
matches
|
||||||
Options {
|
.value_of(options::OUTPUT_DELIMITER)
|
||||||
out_delim: Some(
|
.unwrap_or_default()
|
||||||
matches
|
.to_owned(),
|
||||||
.value_of(options::OUTPUT_DELIMITER)
|
),
|
||||||
.unwrap_or_default()
|
zero_terminated: matches.is_present(options::ZERO_TERMINATED),
|
||||||
.to_owned(),
|
},
|
||||||
),
|
)
|
||||||
zero_terminated: matches.is_present(options::ZERO_TERMINATED),
|
}),
|
||||||
},
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
(None, None, Some(field_ranges)) => {
|
(None, None, Some(field_ranges)) => {
|
||||||
list_to_ranges(&field_ranges[..], complement).and_then(|ranges| {
|
list_to_ranges(field_ranges, complement).and_then(|ranges| {
|
||||||
let out_delim = match matches.value_of(options::OUTPUT_DELIMITER) {
|
let out_delim = match matches.value_of(options::OUTPUT_DELIMITER) {
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
if s.is_empty() {
|
if s.is_empty() {
|
||||||
|
|
|
@ -116,7 +116,6 @@ struct Options {
|
||||||
show_listed_fs: bool,
|
show_listed_fs: bool,
|
||||||
show_fs_type: bool,
|
show_fs_type: bool,
|
||||||
show_inode_instead: bool,
|
show_inode_instead: bool,
|
||||||
print_grand_total: bool,
|
|
||||||
// block_size: usize,
|
// block_size: usize,
|
||||||
human_readable_base: i64,
|
human_readable_base: i64,
|
||||||
fs_selector: FsSelector,
|
fs_selector: FsSelector,
|
||||||
|
@ -286,7 +285,6 @@ impl Options {
|
||||||
show_listed_fs: false,
|
show_listed_fs: false,
|
||||||
show_fs_type: false,
|
show_fs_type: false,
|
||||||
show_inode_instead: false,
|
show_inode_instead: false,
|
||||||
print_grand_total: false,
|
|
||||||
// block_size: match env::var("BLOCKSIZE") {
|
// block_size: match env::var("BLOCKSIZE") {
|
||||||
// Ok(size) => size.parse().unwrap(),
|
// Ok(size) => size.parse().unwrap(),
|
||||||
// Err(_) => 512,
|
// Err(_) => 512,
|
||||||
|
@ -871,9 +869,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
if matches.is_present(OPT_ALL) {
|
if matches.is_present(OPT_ALL) {
|
||||||
opt.show_all_fs = true;
|
opt.show_all_fs = true;
|
||||||
}
|
}
|
||||||
if matches.is_present(OPT_TOTAL) {
|
|
||||||
opt.print_grand_total = true;
|
|
||||||
}
|
|
||||||
if matches.is_present(OPT_INODES) {
|
if matches.is_present(OPT_INODES) {
|
||||||
opt.show_inode_instead = true;
|
opt.show_inode_instead = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -322,7 +322,7 @@ fn convert_size_human(size: u64, multiplier: u64, _block_size: u64) -> String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if size == 0 {
|
if size == 0 {
|
||||||
return format!("0");
|
return "0".to_string();
|
||||||
}
|
}
|
||||||
format!("{}B", size)
|
format!("{}B", size)
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,7 @@ fn print_expr_error(expr_error: &str) -> ! {
|
||||||
crash!(2, "{}", expr_error)
|
crash!(2, "{}", expr_error)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn evaluate_ast(maybe_ast: Result<Box<syntax_tree::ASTNode>, String>) -> Result<String, String> {
|
fn evaluate_ast(maybe_ast: Result<Box<syntax_tree::AstNode>, String>) -> Result<String, String> {
|
||||||
if maybe_ast.is_err() {
|
if maybe_ast.is_err() {
|
||||||
Err(maybe_ast.err().unwrap())
|
Err(maybe_ast.err().unwrap())
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -17,10 +17,10 @@ use onig::{Regex, RegexOptions, Syntax};
|
||||||
use crate::tokens::Token;
|
use crate::tokens::Token;
|
||||||
|
|
||||||
type TokenStack = Vec<(usize, Token)>;
|
type TokenStack = Vec<(usize, Token)>;
|
||||||
pub type OperandsList = Vec<Box<ASTNode>>;
|
pub type OperandsList = Vec<Box<AstNode>>;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ASTNode {
|
pub enum AstNode {
|
||||||
Leaf {
|
Leaf {
|
||||||
token_idx: usize,
|
token_idx: usize,
|
||||||
value: String,
|
value: String,
|
||||||
|
@ -31,7 +31,7 @@ pub enum ASTNode {
|
||||||
operands: OperandsList,
|
operands: OperandsList,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
impl ASTNode {
|
impl AstNode {
|
||||||
fn debug_dump(&self) {
|
fn debug_dump(&self) {
|
||||||
self.debug_dump_impl(1);
|
self.debug_dump_impl(1);
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ impl ASTNode {
|
||||||
print!("\t",);
|
print!("\t",);
|
||||||
}
|
}
|
||||||
match *self {
|
match *self {
|
||||||
ASTNode::Leaf {
|
AstNode::Leaf {
|
||||||
ref token_idx,
|
ref token_idx,
|
||||||
ref value,
|
ref value,
|
||||||
} => println!(
|
} => println!(
|
||||||
|
@ -49,7 +49,7 @@ impl ASTNode {
|
||||||
token_idx,
|
token_idx,
|
||||||
self.evaluate()
|
self.evaluate()
|
||||||
),
|
),
|
||||||
ASTNode::Node {
|
AstNode::Node {
|
||||||
ref token_idx,
|
ref token_idx,
|
||||||
ref op_type,
|
ref op_type,
|
||||||
ref operands,
|
ref operands,
|
||||||
|
@ -67,23 +67,23 @@ impl ASTNode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_node(token_idx: usize, op_type: &str, operands: OperandsList) -> Box<ASTNode> {
|
fn new_node(token_idx: usize, op_type: &str, operands: OperandsList) -> Box<AstNode> {
|
||||||
Box::new(ASTNode::Node {
|
Box::new(AstNode::Node {
|
||||||
token_idx,
|
token_idx,
|
||||||
op_type: op_type.into(),
|
op_type: op_type.into(),
|
||||||
operands,
|
operands,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
fn new_leaf(token_idx: usize, value: &str) -> Box<ASTNode> {
|
fn new_leaf(token_idx: usize, value: &str) -> Box<AstNode> {
|
||||||
Box::new(ASTNode::Leaf {
|
Box::new(AstNode::Leaf {
|
||||||
token_idx,
|
token_idx,
|
||||||
value: value.into(),
|
value: value.into(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn evaluate(&self) -> Result<String, String> {
|
pub fn evaluate(&self) -> Result<String, String> {
|
||||||
match *self {
|
match *self {
|
||||||
ASTNode::Leaf { ref value, .. } => Ok(value.clone()),
|
AstNode::Leaf { ref value, .. } => Ok(value.clone()),
|
||||||
ASTNode::Node { ref op_type, .. } => match self.operand_values() {
|
AstNode::Node { ref op_type, .. } => match self.operand_values() {
|
||||||
Err(reason) => Err(reason),
|
Err(reason) => Err(reason),
|
||||||
Ok(operand_values) => match op_type.as_ref() {
|
Ok(operand_values) => match op_type.as_ref() {
|
||||||
"+" => infix_operator_two_ints(
|
"+" => infix_operator_two_ints(
|
||||||
|
@ -161,7 +161,7 @@ impl ASTNode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn operand_values(&self) -> Result<Vec<String>, String> {
|
pub fn operand_values(&self) -> Result<Vec<String>, String> {
|
||||||
if let ASTNode::Node { ref operands, .. } = *self {
|
if let AstNode::Node { ref operands, .. } = *self {
|
||||||
let mut out = Vec::with_capacity(operands.len());
|
let mut out = Vec::with_capacity(operands.len());
|
||||||
for operand in operands {
|
for operand in operands {
|
||||||
match operand.evaluate() {
|
match operand.evaluate() {
|
||||||
|
@ -178,7 +178,7 @@ impl ASTNode {
|
||||||
|
|
||||||
pub fn tokens_to_ast(
|
pub fn tokens_to_ast(
|
||||||
maybe_tokens: Result<Vec<(usize, Token)>, String>,
|
maybe_tokens: Result<Vec<(usize, Token)>, String>,
|
||||||
) -> Result<Box<ASTNode>, String> {
|
) -> Result<Box<AstNode>, String> {
|
||||||
if maybe_tokens.is_err() {
|
if maybe_tokens.is_err() {
|
||||||
Err(maybe_tokens.err().unwrap())
|
Err(maybe_tokens.err().unwrap())
|
||||||
} else {
|
} else {
|
||||||
|
@ -212,7 +212,7 @@ pub fn tokens_to_ast(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn maybe_dump_ast(result: &Result<Box<ASTNode>, String>) {
|
fn maybe_dump_ast(result: &Result<Box<AstNode>, String>) {
|
||||||
use std::env;
|
use std::env;
|
||||||
if let Ok(debug_var) = env::var("EXPR_DEBUG_AST") {
|
if let Ok(debug_var) = env::var("EXPR_DEBUG_AST") {
|
||||||
if debug_var == "1" {
|
if debug_var == "1" {
|
||||||
|
@ -238,11 +238,11 @@ fn maybe_dump_rpn(rpn: &TokenStack) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ast_from_rpn(rpn: &mut TokenStack) -> Result<Box<ASTNode>, String> {
|
fn ast_from_rpn(rpn: &mut TokenStack) -> Result<Box<AstNode>, String> {
|
||||||
match rpn.pop() {
|
match rpn.pop() {
|
||||||
None => Err("syntax error (premature end of expression)".to_owned()),
|
None => Err("syntax error (premature end of expression)".to_owned()),
|
||||||
|
|
||||||
Some((token_idx, Token::Value { value })) => Ok(ASTNode::new_leaf(token_idx, &value)),
|
Some((token_idx, Token::Value { value })) => Ok(AstNode::new_leaf(token_idx, &value)),
|
||||||
|
|
||||||
Some((token_idx, Token::InfixOp { value, .. })) => {
|
Some((token_idx, Token::InfixOp { value, .. })) => {
|
||||||
maybe_ast_node(token_idx, &value, 2, rpn)
|
maybe_ast_node(token_idx, &value, 2, rpn)
|
||||||
|
@ -262,7 +262,7 @@ fn maybe_ast_node(
|
||||||
op_type: &str,
|
op_type: &str,
|
||||||
arity: usize,
|
arity: usize,
|
||||||
rpn: &mut TokenStack,
|
rpn: &mut TokenStack,
|
||||||
) -> Result<Box<ASTNode>, String> {
|
) -> Result<Box<AstNode>, String> {
|
||||||
let mut operands = Vec::with_capacity(arity);
|
let mut operands = Vec::with_capacity(arity);
|
||||||
for _ in 0..arity {
|
for _ in 0..arity {
|
||||||
match ast_from_rpn(rpn) {
|
match ast_from_rpn(rpn) {
|
||||||
|
@ -271,7 +271,7 @@ fn maybe_ast_node(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
operands.reverse();
|
operands.reverse();
|
||||||
Ok(ASTNode::new_node(token_idx, op_type, operands))
|
Ok(AstNode::new_node(token_idx, op_type, operands))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_rest_of_ops_to_out(
|
fn move_rest_of_ops_to_out(
|
||||||
|
|
|
@ -267,7 +267,7 @@ impl<'a> ParagraphStream<'a> {
|
||||||
#[allow(clippy::match_like_matches_macro)]
|
#[allow(clippy::match_like_matches_macro)]
|
||||||
// `matches!(...)` macro not stabilized until rust v1.42
|
// `matches!(...)` macro not stabilized until rust v1.42
|
||||||
l_slice[..colon_posn].chars().all(|x| match x as usize {
|
l_slice[..colon_posn].chars().all(|x| match x as usize {
|
||||||
y if y < 33 || y > 126 => false,
|
y if !(33..=126).contains(&y) => false,
|
||||||
_ => true,
|
_ => true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,7 +66,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
.takes_value(true),
|
.takes_value(true),
|
||||||
)
|
)
|
||||||
.arg(Arg::with_name(options::FILE).hidden(true).multiple(true))
|
.arg(Arg::with_name(options::FILE).hidden(true).multiple(true))
|
||||||
.get_matches_from(args.clone());
|
.get_matches_from(args);
|
||||||
|
|
||||||
let bytes = matches.is_present(options::BYTES);
|
let bytes = matches.is_present(options::BYTES);
|
||||||
let spaces = matches.is_present(options::SPACES);
|
let spaces = matches.is_present(options::SPACES);
|
||||||
|
|
|
@ -78,7 +78,7 @@ fn detect_algo<'a>(
|
||||||
"sha512sum" => ("SHA512", Box::new(Sha512::new()) as Box<dyn Digest>, 512),
|
"sha512sum" => ("SHA512", Box::new(Sha512::new()) as Box<dyn Digest>, 512),
|
||||||
"b2sum" => ("BLAKE2", Box::new(Blake2b::new(64)) as Box<dyn Digest>, 512),
|
"b2sum" => ("BLAKE2", Box::new(Blake2b::new(64)) as Box<dyn Digest>, 512),
|
||||||
"sha3sum" => match matches.value_of("bits") {
|
"sha3sum" => match matches.value_of("bits") {
|
||||||
Some(bits_str) => match usize::from_str_radix(&bits_str, 10) {
|
Some(bits_str) => match (&bits_str).parse::<usize>() {
|
||||||
Ok(224) => (
|
Ok(224) => (
|
||||||
"SHA3-224",
|
"SHA3-224",
|
||||||
Box::new(Sha3_224::new()) as Box<dyn Digest>,
|
Box::new(Sha3_224::new()) as Box<dyn Digest>,
|
||||||
|
@ -128,7 +128,7 @@ fn detect_algo<'a>(
|
||||||
512,
|
512,
|
||||||
),
|
),
|
||||||
"shake128sum" => match matches.value_of("bits") {
|
"shake128sum" => match matches.value_of("bits") {
|
||||||
Some(bits_str) => match usize::from_str_radix(&bits_str, 10) {
|
Some(bits_str) => match (&bits_str).parse::<usize>() {
|
||||||
Ok(bits) => (
|
Ok(bits) => (
|
||||||
"SHAKE128",
|
"SHAKE128",
|
||||||
Box::new(Shake128::new()) as Box<dyn Digest>,
|
Box::new(Shake128::new()) as Box<dyn Digest>,
|
||||||
|
@ -139,7 +139,7 @@ fn detect_algo<'a>(
|
||||||
None => crash!(1, "--bits required for SHAKE-128"),
|
None => crash!(1, "--bits required for SHAKE-128"),
|
||||||
},
|
},
|
||||||
"shake256sum" => match matches.value_of("bits") {
|
"shake256sum" => match matches.value_of("bits") {
|
||||||
Some(bits_str) => match usize::from_str_radix(&bits_str, 10) {
|
Some(bits_str) => match (&bits_str).parse::<usize>() {
|
||||||
Ok(bits) => (
|
Ok(bits) => (
|
||||||
"SHAKE256",
|
"SHAKE256",
|
||||||
Box::new(Shake256::new()) as Box<dyn Digest>,
|
Box::new(Shake256::new()) as Box<dyn Digest>,
|
||||||
|
@ -182,7 +182,7 @@ fn detect_algo<'a>(
|
||||||
}
|
}
|
||||||
if matches.is_present("sha3") {
|
if matches.is_present("sha3") {
|
||||||
match matches.value_of("bits") {
|
match matches.value_of("bits") {
|
||||||
Some(bits_str) => match usize::from_str_radix(&bits_str, 10) {
|
Some(bits_str) => match (&bits_str).parse::<usize>() {
|
||||||
Ok(224) => set_or_crash(
|
Ok(224) => set_or_crash(
|
||||||
"SHA3-224",
|
"SHA3-224",
|
||||||
Box::new(Sha3_224::new()) as Box<dyn Digest>,
|
Box::new(Sha3_224::new()) as Box<dyn Digest>,
|
||||||
|
@ -226,7 +226,7 @@ fn detect_algo<'a>(
|
||||||
}
|
}
|
||||||
if matches.is_present("shake128") {
|
if matches.is_present("shake128") {
|
||||||
match matches.value_of("bits") {
|
match matches.value_of("bits") {
|
||||||
Some(bits_str) => match usize::from_str_radix(&bits_str, 10) {
|
Some(bits_str) => match (&bits_str).parse::<usize>() {
|
||||||
Ok(bits) => set_or_crash("SHAKE128", Box::new(Shake128::new()), bits),
|
Ok(bits) => set_or_crash("SHAKE128", Box::new(Shake128::new()), bits),
|
||||||
Err(err) => crash!(1, "{}", err),
|
Err(err) => crash!(1, "{}", err),
|
||||||
},
|
},
|
||||||
|
@ -235,7 +235,7 @@ fn detect_algo<'a>(
|
||||||
}
|
}
|
||||||
if matches.is_present("shake256") {
|
if matches.is_present("shake256") {
|
||||||
match matches.value_of("bits") {
|
match matches.value_of("bits") {
|
||||||
Some(bits_str) => match usize::from_str_radix(&bits_str, 10) {
|
Some(bits_str) => match (&bits_str).parse::<usize>() {
|
||||||
Ok(bits) => set_or_crash("SHAKE256", Box::new(Shake256::new()), bits),
|
Ok(bits) => set_or_crash("SHAKE256", Box::new(Shake256::new()), bits),
|
||||||
Err(err) => crash!(1, "{}", err),
|
Err(err) => crash!(1, "{}", err),
|
||||||
},
|
},
|
||||||
|
@ -253,7 +253,7 @@ fn detect_algo<'a>(
|
||||||
|
|
||||||
// TODO: return custom error type
|
// TODO: return custom error type
|
||||||
fn parse_bit_num(arg: &str) -> Result<usize, ParseIntError> {
|
fn parse_bit_num(arg: &str) -> Result<usize, ParseIntError> {
|
||||||
usize::from_str_radix(arg, 10)
|
arg.parse()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_valid_bit_num(arg: String) -> Result<(), String> {
|
fn is_valid_bit_num(arg: String) -> Result<(), String> {
|
||||||
|
|
|
@ -625,7 +625,7 @@ mod tests {
|
||||||
assert_eq!(arg_outputs("head"), Ok("head".to_owned()));
|
assert_eq!(arg_outputs("head"), Ok("head".to_owned()));
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(linux)]
|
#[cfg(target_os = "linux")]
|
||||||
fn test_arg_iterate_bad_encoding() {
|
fn test_arg_iterate_bad_encoding() {
|
||||||
let invalid = unsafe { std::str::from_utf8_unchecked(b"\x80\x81") };
|
let invalid = unsafe { std::str::from_utf8_unchecked(b"\x80\x81") };
|
||||||
// this arises from a conversion from OsString to &str
|
// this arises from a conversion from OsString to &str
|
||||||
|
|
|
@ -302,7 +302,7 @@ fn behavior(matches: &ArgMatches) -> Result<Behavior, i32> {
|
||||||
|
|
||||||
let specified_mode: Option<u32> = if matches.is_present(OPT_MODE) {
|
let specified_mode: Option<u32> = if matches.is_present(OPT_MODE) {
|
||||||
match matches.value_of(OPT_MODE) {
|
match matches.value_of(OPT_MODE) {
|
||||||
Some(x) => match mode::parse(&x[..], considering_dir) {
|
Some(x) => match mode::parse(x, considering_dir) {
|
||||||
Ok(y) => Some(y),
|
Ok(y) => Some(y),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
show_error!("Invalid mode string: {}", err);
|
show_error!("Invalid mode string: {}", err);
|
||||||
|
@ -429,7 +429,7 @@ fn standard(paths: Vec<String>, b: Behavior) -> i32 {
|
||||||
/// _files_ must all exist as non-directories.
|
/// _files_ must all exist as non-directories.
|
||||||
/// _target_dir_ must be a directory.
|
/// _target_dir_ must be a directory.
|
||||||
///
|
///
|
||||||
fn copy_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) -> i32 {
|
fn copy_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> i32 {
|
||||||
if !target_dir.is_dir() {
|
if !target_dir.is_dir() {
|
||||||
show_error!("target '{}' is not a directory", target_dir.display());
|
show_error!("target '{}' is not a directory", target_dir.display());
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -453,7 +453,7 @@ fn copy_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) ->
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut targetpath = target_dir.clone().to_path_buf();
|
let mut targetpath = target_dir.to_path_buf();
|
||||||
let filename = sourcepath.components().last().unwrap();
|
let filename = sourcepath.components().last().unwrap();
|
||||||
targetpath.push(filename);
|
targetpath.push(filename);
|
||||||
|
|
||||||
|
@ -478,7 +478,7 @@ fn copy_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) ->
|
||||||
/// _file_ must exist as a non-directory.
|
/// _file_ must exist as a non-directory.
|
||||||
/// _target_ must be a non-directory
|
/// _target_ must be a non-directory
|
||||||
///
|
///
|
||||||
fn copy_file_to_file(file: &PathBuf, target: &PathBuf, b: &Behavior) -> i32 {
|
fn copy_file_to_file(file: &Path, target: &Path, b: &Behavior) -> i32 {
|
||||||
if copy(file, &target, b).is_err() {
|
if copy(file, &target, b).is_err() {
|
||||||
1
|
1
|
||||||
} else {
|
} else {
|
||||||
|
@ -497,7 +497,7 @@ fn copy_file_to_file(file: &PathBuf, target: &PathBuf, b: &Behavior) -> i32 {
|
||||||
///
|
///
|
||||||
/// If the copy system call fails, we print a verbose error and return an empty error value.
|
/// If the copy system call fails, we print a verbose error and return an empty error value.
|
||||||
///
|
///
|
||||||
fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> {
|
fn copy(from: &Path, to: &Path, b: &Behavior) -> Result<(), ()> {
|
||||||
if b.compare && !need_copy(from, to, b) {
|
if b.compare && !need_copy(from, to, b) {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
@ -556,7 +556,7 @@ fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> {
|
||||||
};
|
};
|
||||||
let gid = meta.gid();
|
let gid = meta.gid();
|
||||||
match wrap_chown(
|
match wrap_chown(
|
||||||
to.as_path(),
|
to,
|
||||||
&meta,
|
&meta,
|
||||||
Some(owner_id),
|
Some(owner_id),
|
||||||
Some(gid),
|
Some(gid),
|
||||||
|
@ -582,7 +582,7 @@ fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> {
|
||||||
Ok(g) => g,
|
Ok(g) => g,
|
||||||
_ => crash!(1, "no such group: {}", b.group),
|
_ => crash!(1, "no such group: {}", b.group),
|
||||||
};
|
};
|
||||||
match wrap_chgrp(to.as_path(), &meta, group_id, false, Verbosity::Normal) {
|
match wrap_chgrp(to, &meta, group_id, false, Verbosity::Normal) {
|
||||||
Ok(n) => {
|
Ok(n) => {
|
||||||
if !n.is_empty() {
|
if !n.is_empty() {
|
||||||
show_info!("{}", n);
|
show_info!("{}", n);
|
||||||
|
@ -601,7 +601,7 @@ fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> {
|
||||||
let modified_time = FileTime::from_last_modification_time(&meta);
|
let modified_time = FileTime::from_last_modification_time(&meta);
|
||||||
let accessed_time = FileTime::from_last_access_time(&meta);
|
let accessed_time = FileTime::from_last_access_time(&meta);
|
||||||
|
|
||||||
match set_file_times(to.as_path(), accessed_time, modified_time) {
|
match set_file_times(to, accessed_time, modified_time) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => show_info!("{}", e),
|
Err(e) => show_info!("{}", e),
|
||||||
}
|
}
|
||||||
|
@ -630,7 +630,7 @@ fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> {
|
||||||
///
|
///
|
||||||
/// Crashes the program if a nonexistent owner or group is specified in _b_.
|
/// Crashes the program if a nonexistent owner or group is specified in _b_.
|
||||||
///
|
///
|
||||||
fn need_copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> bool {
|
fn need_copy(from: &Path, to: &Path, b: &Behavior) -> bool {
|
||||||
let from_meta = match fs::metadata(from) {
|
let from_meta = match fs::metadata(from) {
|
||||||
Ok(meta) => meta,
|
Ok(meta) => meta,
|
||||||
Err(_) => return true,
|
Err(_) => return true,
|
||||||
|
|
|
@ -303,7 +303,7 @@ fn exec(files: &[PathBuf], settings: &Settings) -> i32 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn link_files_in_dir(files: &[PathBuf], target_dir: &PathBuf, settings: &Settings) -> i32 {
|
fn link_files_in_dir(files: &[PathBuf], target_dir: &Path, settings: &Settings) -> i32 {
|
||||||
if !target_dir.is_dir() {
|
if !target_dir.is_dir() {
|
||||||
show_error!("target '{}' is not a directory", target_dir.display());
|
show_error!("target '{}' is not a directory", target_dir.display());
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -329,7 +329,7 @@ fn link_files_in_dir(files: &[PathBuf], target_dir: &PathBuf, settings: &Setting
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
target_dir.clone()
|
target_dir.to_path_buf()
|
||||||
} else {
|
} else {
|
||||||
match srcpath.as_os_str().to_str() {
|
match srcpath.as_os_str().to_str() {
|
||||||
Some(name) => {
|
Some(name) => {
|
||||||
|
@ -370,7 +370,7 @@ fn link_files_in_dir(files: &[PathBuf], target_dir: &PathBuf, settings: &Setting
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn relative_path<'a>(src: &PathBuf, dst: &PathBuf) -> Result<Cow<'a, Path>> {
|
fn relative_path<'a>(src: &Path, dst: &Path) -> Result<Cow<'a, Path>> {
|
||||||
let abssrc = canonicalize(src, CanonicalizeMode::Normal)?;
|
let abssrc = canonicalize(src, CanonicalizeMode::Normal)?;
|
||||||
let absdst = canonicalize(dst, CanonicalizeMode::Normal)?;
|
let absdst = canonicalize(dst, CanonicalizeMode::Normal)?;
|
||||||
let suffix_pos = abssrc
|
let suffix_pos = abssrc
|
||||||
|
@ -390,7 +390,7 @@ fn relative_path<'a>(src: &PathBuf, dst: &PathBuf) -> Result<Cow<'a, Path>> {
|
||||||
Ok(result.into())
|
Ok(result.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn link(src: &PathBuf, dst: &PathBuf, settings: &Settings) -> Result<()> {
|
fn link(src: &Path, dst: &Path, settings: &Settings) -> Result<()> {
|
||||||
let mut backup_path = None;
|
let mut backup_path = None;
|
||||||
let source: Cow<'_, Path> = if settings.relative {
|
let source: Cow<'_, Path> = if settings.relative {
|
||||||
relative_path(&src, dst)?
|
relative_path(&src, dst)?
|
||||||
|
@ -453,13 +453,13 @@ fn read_yes() -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn simple_backup_path(path: &PathBuf, suffix: &str) -> PathBuf {
|
fn simple_backup_path(path: &Path, suffix: &str) -> PathBuf {
|
||||||
let mut p = path.as_os_str().to_str().unwrap().to_owned();
|
let mut p = path.as_os_str().to_str().unwrap().to_owned();
|
||||||
p.push_str(suffix);
|
p.push_str(suffix);
|
||||||
PathBuf::from(p)
|
PathBuf::from(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn numbered_backup_path(path: &PathBuf) -> PathBuf {
|
fn numbered_backup_path(path: &Path) -> PathBuf {
|
||||||
let mut i: u64 = 1;
|
let mut i: u64 = 1;
|
||||||
loop {
|
loop {
|
||||||
let new_path = simple_backup_path(path, &format!(".~{}~", i));
|
let new_path = simple_backup_path(path, &format!(".~{}~", i));
|
||||||
|
@ -470,7 +470,7 @@ fn numbered_backup_path(path: &PathBuf) -> PathBuf {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn existing_backup_path(path: &PathBuf, suffix: &str) -> PathBuf {
|
fn existing_backup_path(path: &Path, suffix: &str) -> PathBuf {
|
||||||
let test_path = simple_backup_path(path, &".~1~".to_owned());
|
let test_path = simple_backup_path(path, &".~1~".to_owned());
|
||||||
if test_path.exists() {
|
if test_path.exists() {
|
||||||
return numbered_backup_path(path);
|
return numbered_backup_path(path);
|
||||||
|
|
|
@ -370,6 +370,7 @@ impl Config {
|
||||||
})
|
})
|
||||||
.or_else(|| termsize::get().map(|s| s.cols));
|
.or_else(|| termsize::get().map(|s| s.cols));
|
||||||
|
|
||||||
|
#[allow(clippy::needless_bool)]
|
||||||
let show_control = if options.is_present(options::HIDE_CONTROL_CHARS) {
|
let show_control = if options.is_present(options::HIDE_CONTROL_CHARS) {
|
||||||
false
|
false
|
||||||
} else if options.is_present(options::SHOW_CONTROL_CHARS) {
|
} else if options.is_present(options::SHOW_CONTROL_CHARS) {
|
||||||
|
@ -1042,7 +1043,7 @@ fn sort_entries(entries: &mut Vec<PathBuf>, config: &Config) {
|
||||||
.sort_by_key(|k| Reverse(get_metadata(k, config).map(|md| md.len()).unwrap_or(0))),
|
.sort_by_key(|k| Reverse(get_metadata(k, config).map(|md| md.len()).unwrap_or(0))),
|
||||||
// The default sort in GNU ls is case insensitive
|
// The default sort in GNU ls is case insensitive
|
||||||
Sort::Name => entries.sort_by_key(|k| k.to_string_lossy().to_lowercase()),
|
Sort::Name => entries.sort_by_key(|k| k.to_string_lossy().to_lowercase()),
|
||||||
Sort::Version => entries.sort_by(version_cmp::version_cmp),
|
Sort::Version => entries.sort_by(|a, b| version_cmp::version_cmp(a, b)),
|
||||||
Sort::None => {}
|
Sort::None => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1076,7 +1077,7 @@ fn should_display(entry: &DirEntry, config: &Config) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enter_directory(dir: &PathBuf, config: &Config) {
|
fn enter_directory(dir: &Path, config: &Config) {
|
||||||
let mut entries: Vec<_> = safe_unwrap!(fs::read_dir(dir).and_then(Iterator::collect));
|
let mut entries: Vec<_> = safe_unwrap!(fs::read_dir(dir).and_then(Iterator::collect));
|
||||||
|
|
||||||
entries.retain(|e| should_display(e, config));
|
entries.retain(|e| should_display(e, config));
|
||||||
|
@ -1101,7 +1102,7 @@ fn enter_directory(dir: &PathBuf, config: &Config) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_metadata(entry: &PathBuf, config: &Config) -> std::io::Result<Metadata> {
|
fn get_metadata(entry: &Path, config: &Config) -> std::io::Result<Metadata> {
|
||||||
if config.dereference {
|
if config.dereference {
|
||||||
entry.metadata().or_else(|_| entry.symlink_metadata())
|
entry.metadata().or_else(|_| entry.symlink_metadata())
|
||||||
} else {
|
} else {
|
||||||
|
@ -1109,7 +1110,7 @@ fn get_metadata(entry: &PathBuf, config: &Config) -> std::io::Result<Metadata> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn display_dir_entry_size(entry: &PathBuf, config: &Config) -> (usize, usize) {
|
fn display_dir_entry_size(entry: &Path, config: &Config) -> (usize, usize) {
|
||||||
if let Ok(md) = get_metadata(entry, config) {
|
if let Ok(md) = get_metadata(entry, config) {
|
||||||
(
|
(
|
||||||
display_symlink_count(&md).len(),
|
display_symlink_count(&md).len(),
|
||||||
|
@ -1204,7 +1205,7 @@ fn display_grid(names: impl Iterator<Item = Cell>, width: u16, direction: Direct
|
||||||
use uucore::fs::display_permissions;
|
use uucore::fs::display_permissions;
|
||||||
|
|
||||||
fn display_item_long(
|
fn display_item_long(
|
||||||
item: &PathBuf,
|
item: &Path,
|
||||||
strip: Option<&Path>,
|
strip: Option<&Path>,
|
||||||
max_links: usize,
|
max_links: usize,
|
||||||
max_size: usize,
|
max_size: usize,
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use std::{cmp::Ordering, path::PathBuf};
|
use std::cmp::Ordering;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
/// Compare pathbufs in a way that matches the GNU version sort, meaning that
|
/// Compare paths in a way that matches the GNU version sort, meaning that
|
||||||
/// numbers get sorted in a natural way.
|
/// numbers get sorted in a natural way.
|
||||||
pub(crate) fn version_cmp(a: &PathBuf, b: &PathBuf) -> Ordering {
|
pub(crate) fn version_cmp(a: &Path, b: &Path) -> Ordering {
|
||||||
let a_string = a.to_string_lossy();
|
let a_string = a.to_string_lossy();
|
||||||
let b_string = b.to_string_lossy();
|
let b_string = b.to_string_lossy();
|
||||||
let mut a = a_string.chars().peekable();
|
let mut a = a_string.chars().peekable();
|
||||||
|
|
|
@ -335,7 +335,7 @@ fn exec(files: &[PathBuf], b: Behavior) -> i32 {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) -> i32 {
|
fn move_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> i32 {
|
||||||
if !target_dir.is_dir() {
|
if !target_dir.is_dir() {
|
||||||
show_error!("target ‘{}’ is not a directory", target_dir.display());
|
show_error!("target ‘{}’ is not a directory", target_dir.display());
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -373,7 +373,7 @@ fn move_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) ->
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rename(from: &PathBuf, to: &PathBuf, b: &Behavior) -> io::Result<()> {
|
fn rename(from: &Path, to: &Path, b: &Behavior) -> io::Result<()> {
|
||||||
let mut backup_path = None;
|
let mut backup_path = None;
|
||||||
|
|
||||||
if to.exists() {
|
if to.exists() {
|
||||||
|
@ -429,7 +429,7 @@ fn rename(from: &PathBuf, to: &PathBuf, b: &Behavior) -> io::Result<()> {
|
||||||
|
|
||||||
/// A wrapper around `fs::rename`, so that if it fails, we try falling back on
|
/// A wrapper around `fs::rename`, so that if it fails, we try falling back on
|
||||||
/// copying and removing.
|
/// copying and removing.
|
||||||
fn rename_with_fallback(from: &PathBuf, to: &PathBuf) -> io::Result<()> {
|
fn rename_with_fallback(from: &Path, to: &Path) -> io::Result<()> {
|
||||||
if fs::rename(from, to).is_err() {
|
if fs::rename(from, to).is_err() {
|
||||||
// Get metadata without following symlinks
|
// Get metadata without following symlinks
|
||||||
let metadata = from.symlink_metadata()?;
|
let metadata = from.symlink_metadata()?;
|
||||||
|
@ -464,7 +464,7 @@ fn rename_with_fallback(from: &PathBuf, to: &PathBuf) -> io::Result<()> {
|
||||||
/// Move the given symlink to the given destination. On Windows, dangling
|
/// Move the given symlink to the given destination. On Windows, dangling
|
||||||
/// symlinks return an error.
|
/// symlinks return an error.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn rename_symlink_fallback(from: &PathBuf, to: &PathBuf) -> io::Result<()> {
|
fn rename_symlink_fallback(from: &Path, to: &Path) -> io::Result<()> {
|
||||||
let path_symlink_points_to = fs::read_link(from)?;
|
let path_symlink_points_to = fs::read_link(from)?;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
{
|
{
|
||||||
|
@ -507,20 +507,20 @@ fn read_yes() -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn simple_backup_path(path: &PathBuf, suffix: &str) -> PathBuf {
|
fn simple_backup_path(path: &Path, suffix: &str) -> PathBuf {
|
||||||
let mut p = path.to_string_lossy().into_owned();
|
let mut p = path.to_string_lossy().into_owned();
|
||||||
p.push_str(suffix);
|
p.push_str(suffix);
|
||||||
PathBuf::from(p)
|
PathBuf::from(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn numbered_backup_path(path: &PathBuf) -> PathBuf {
|
fn numbered_backup_path(path: &Path) -> PathBuf {
|
||||||
(1_u64..)
|
(1_u64..)
|
||||||
.map(|i| path.with_extension(format!("~{}~", i)))
|
.map(|i| path.with_extension(format!("~{}~", i)))
|
||||||
.find(|p| !p.exists())
|
.find(|p| !p.exists())
|
||||||
.expect("cannot create backup")
|
.expect("cannot create backup")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn existing_backup_path(path: &PathBuf, suffix: &str) -> PathBuf {
|
fn existing_backup_path(path: &Path, suffix: &str) -> PathBuf {
|
||||||
let test_path = path.with_extension("~1~");
|
let test_path = path.with_extension("~1~");
|
||||||
if test_path.exists() {
|
if test_path.exists() {
|
||||||
numbered_backup_path(path)
|
numbered_backup_path(path)
|
||||||
|
@ -529,7 +529,7 @@ fn existing_backup_path(path: &PathBuf, suffix: &str) -> PathBuf {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_empty_dir(path: &PathBuf) -> bool {
|
fn is_empty_dir(path: &Path) -> bool {
|
||||||
match fs::read_dir(path) {
|
match fs::read_dir(path) {
|
||||||
Ok(contents) => contents.peekable().peek().is_none(),
|
Ok(contents) => contents.peekable().peek().is_none(),
|
||||||
Err(_e) => false,
|
Err(_e) => false,
|
||||||
|
|
|
@ -118,7 +118,7 @@ struct OdOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OdOptions {
|
impl OdOptions {
|
||||||
fn new<'a>(matches: ArgMatches<'a>, args: Vec<String>) -> Result<OdOptions, String> {
|
fn new(matches: ArgMatches, args: Vec<String>) -> Result<OdOptions, String> {
|
||||||
let byte_order = match matches.value_of(options::ENDIAN) {
|
let byte_order = match matches.value_of(options::ENDIAN) {
|
||||||
None => ByteOrder::Native,
|
None => ByteOrder::Native,
|
||||||
Some("little") => ByteOrder::Little,
|
Some("little") => ByteOrder::Little,
|
||||||
|
|
|
@ -63,7 +63,7 @@ pub fn parse_inputs(matches: &dyn CommandLineOpts) -> Result<CommandLineInputs,
|
||||||
}
|
}
|
||||||
if input_strings.len() == 2 {
|
if input_strings.len() == 2 {
|
||||||
return Ok(CommandLineInputs::FileAndOffset((
|
return Ok(CommandLineInputs::FileAndOffset((
|
||||||
input_strings[0].clone().to_owned(),
|
input_strings[0].to_string(),
|
||||||
n,
|
n,
|
||||||
None,
|
None,
|
||||||
)));
|
)));
|
||||||
|
@ -106,7 +106,7 @@ pub fn parse_inputs_traditional(input_strings: Vec<&str>) -> Result<CommandLineI
|
||||||
Some(m),
|
Some(m),
|
||||||
))),
|
))),
|
||||||
(_, Ok(m)) => Ok(CommandLineInputs::FileAndOffset((
|
(_, Ok(m)) => Ok(CommandLineInputs::FileAndOffset((
|
||||||
input_strings[0].clone().to_owned(),
|
input_strings[0].to_string(),
|
||||||
m,
|
m,
|
||||||
None,
|
None,
|
||||||
))),
|
))),
|
||||||
|
@ -118,7 +118,7 @@ pub fn parse_inputs_traditional(input_strings: Vec<&str>) -> Result<CommandLineI
|
||||||
let label = parse_offset_operand(&input_strings[2]);
|
let label = parse_offset_operand(&input_strings[2]);
|
||||||
match (offset, label) {
|
match (offset, label) {
|
||||||
(Ok(n), Ok(m)) => Ok(CommandLineInputs::FileAndOffset((
|
(Ok(n), Ok(m)) => Ok(CommandLineInputs::FileAndOffset((
|
||||||
input_strings[0].clone().to_owned(),
|
input_strings[0].to_string(),
|
||||||
n,
|
n,
|
||||||
Some(m),
|
Some(m),
|
||||||
))),
|
))),
|
||||||
|
|
|
@ -15,7 +15,6 @@ use uucore::utmpx::{self, time, Utmpx};
|
||||||
|
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
use std::io::Result as IOResult;
|
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::os::unix::fs::MetadataExt;
|
use std::os::unix::fs::MetadataExt;
|
||||||
|
@ -136,12 +135,8 @@ The utmp file will be {}",
|
||||||
};
|
};
|
||||||
|
|
||||||
if do_short_format {
|
if do_short_format {
|
||||||
if let Err(e) = pk.short_pinky() {
|
pk.short_pinky();
|
||||||
show_usage_error!("{}", e);
|
0
|
||||||
1
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
pk.long_pinky()
|
pk.long_pinky()
|
||||||
}
|
}
|
||||||
|
@ -282,7 +277,7 @@ impl Pinky {
|
||||||
println!();
|
println!();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn short_pinky(&self) -> IOResult<()> {
|
fn short_pinky(&self) {
|
||||||
if self.include_heading {
|
if self.include_heading {
|
||||||
self.print_heading();
|
self.print_heading();
|
||||||
}
|
}
|
||||||
|
@ -295,7 +290,6 @@ impl Pinky {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn long_pinky(&self) -> i32 {
|
fn long_pinky(&self) -> i32 {
|
||||||
|
|
|
@ -199,8 +199,7 @@ pub fn arrnum_int_add(arrnum: &[u8], basenum: u8, base_ten_int_term: u8) -> Vec<
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn base_conv_vec(src: &[u8], radix_src: u8, radix_dest: u8) -> Vec<u8> {
|
pub fn base_conv_vec(src: &[u8], radix_src: u8, radix_dest: u8) -> Vec<u8> {
|
||||||
let mut result: Vec<u8> = Vec::new();
|
let mut result = vec![0];
|
||||||
result.push(0);
|
|
||||||
for i in src {
|
for i in src {
|
||||||
result = arrnum_int_mult(&result, radix_dest, radix_src);
|
result = arrnum_int_mult(&result, radix_dest, radix_src);
|
||||||
result = arrnum_int_add(&result, radix_dest, *i);
|
result = arrnum_int_add(&result, radix_dest, *i);
|
||||||
|
@ -226,8 +225,7 @@ pub fn base_conv_float(src: &[u8], radix_src: u8, radix_dest: u8) -> f64 {
|
||||||
// to implement this for arbitrary string input.
|
// to implement this for arbitrary string input.
|
||||||
// until then, the below operates as an outline
|
// until then, the below operates as an outline
|
||||||
// of how it would work.
|
// of how it would work.
|
||||||
let mut result: Vec<u8> = Vec::new();
|
let result: Vec<u8> = vec![0];
|
||||||
result.push(0);
|
|
||||||
let mut factor: f64 = 1_f64;
|
let mut factor: f64 = 1_f64;
|
||||||
let radix_src_float: f64 = f64::from(radix_src);
|
let radix_src_float: f64 = f64::from(radix_src);
|
||||||
let mut r: f64 = 0_f64;
|
let mut r: f64 = 0_f64;
|
||||||
|
|
|
@ -263,9 +263,5 @@ pub fn num_format(field: &FormatField, in_str_opt: Option<&String>) -> Option<St
|
||||||
};
|
};
|
||||||
// if we have a formatPrimitive, print its results
|
// if we have a formatPrimitive, print its results
|
||||||
// according to the field-char appropriate Formatter
|
// according to the field-char appropriate Formatter
|
||||||
if let Some(prim) = prim_opt {
|
prim_opt.map(|prim| fmtr.primitive_to_str(&prim, field.clone()))
|
||||||
Some(fmtr.primitive_to_str(&prim, field.clone()))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -177,14 +177,14 @@ fn get_config(matches: &clap::ArgMatches) -> Config {
|
||||||
}
|
}
|
||||||
if matches.is_present(options::WIDTH) {
|
if matches.is_present(options::WIDTH) {
|
||||||
let width_str = matches.value_of(options::WIDTH).expect(err_msg).to_string();
|
let width_str = matches.value_of(options::WIDTH).expect(err_msg).to_string();
|
||||||
config.line_width = crash_if_err!(1, usize::from_str_radix(&width_str, 10));
|
config.line_width = crash_if_err!(1, (&width_str).parse::<usize>());
|
||||||
}
|
}
|
||||||
if matches.is_present(options::GAP_SIZE) {
|
if matches.is_present(options::GAP_SIZE) {
|
||||||
let gap_str = matches
|
let gap_str = matches
|
||||||
.value_of(options::GAP_SIZE)
|
.value_of(options::GAP_SIZE)
|
||||||
.expect(err_msg)
|
.expect(err_msg)
|
||||||
.to_string();
|
.to_string();
|
||||||
config.gap_size = crash_if_err!(1, usize::from_str_radix(&gap_str, 10));
|
config.gap_size = crash_if_err!(1, (&gap_str).parse::<usize>());
|
||||||
}
|
}
|
||||||
if matches.is_present(options::FORMAT_ROFF) {
|
if matches.is_present(options::FORMAT_ROFF) {
|
||||||
config.format = OutFormat::Roff;
|
config.format = OutFormat::Roff;
|
||||||
|
|
|
@ -13,7 +13,7 @@ extern crate uucore;
|
||||||
use clap::{App, Arg};
|
use clap::{App, Arg};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::{stdout, Write};
|
use std::io::{stdout, Write};
|
||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
use uucore::fs::{canonicalize, CanonicalizeMode};
|
use uucore::fs::{canonicalize, CanonicalizeMode};
|
||||||
|
|
||||||
const NAME: &str = "readlink";
|
const NAME: &str = "readlink";
|
||||||
|
@ -160,8 +160,8 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn show(path: &PathBuf, no_newline: bool, use_zero: bool) {
|
fn show(path: &Path, no_newline: bool, use_zero: bool) {
|
||||||
let path = path.as_path().to_str().unwrap();
|
let path = path.to_str().unwrap();
|
||||||
if use_zero {
|
if use_zero {
|
||||||
print!("{}\0", path);
|
print!("{}\0", path);
|
||||||
} else if no_newline {
|
} else if no_newline {
|
||||||
|
|
|
@ -12,7 +12,7 @@ extern crate uucore;
|
||||||
|
|
||||||
use clap::{App, Arg};
|
use clap::{App, Arg};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
use uucore::fs::{canonicalize, CanonicalizeMode};
|
use uucore::fs::{canonicalize, CanonicalizeMode};
|
||||||
|
|
||||||
static ABOUT: &str = "print the resolved path";
|
static ABOUT: &str = "print the resolved path";
|
||||||
|
@ -82,7 +82,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
retcode
|
retcode
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_path(p: &PathBuf, strip: bool, zero: bool, quiet: bool) -> bool {
|
fn resolve_path(p: &Path, strip: bool, zero: bool, quiet: bool) -> bool {
|
||||||
let abs = canonicalize(p, CanonicalizeMode::Normal).unwrap();
|
let abs = canonicalize(p, CanonicalizeMode::Normal).unwrap();
|
||||||
|
|
||||||
if strip {
|
if strip {
|
||||||
|
|
|
@ -176,7 +176,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
} else if matches.is_present(OPT_PROMPT_MORE) {
|
} else if matches.is_present(OPT_PROMPT_MORE) {
|
||||||
InteractiveMode::Once
|
InteractiveMode::Once
|
||||||
} else if matches.is_present(OPT_INTERACTIVE) {
|
} else if matches.is_present(OPT_INTERACTIVE) {
|
||||||
match &matches.value_of(OPT_INTERACTIVE).unwrap()[..] {
|
match matches.value_of(OPT_INTERACTIVE).unwrap() {
|
||||||
"none" => InteractiveMode::None,
|
"none" => InteractiveMode::None,
|
||||||
"once" => InteractiveMode::Once,
|
"once" => InteractiveMode::Once,
|
||||||
"always" => InteractiveMode::Always,
|
"always" => InteractiveMode::Always,
|
||||||
|
|
|
@ -102,7 +102,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
let mut largest_dec = 0;
|
let mut largest_dec = 0;
|
||||||
let mut padding = 0;
|
let mut padding = 0;
|
||||||
let first = if numbers.len() > 1 {
|
let first = if numbers.len() > 1 {
|
||||||
let slice = &numbers[0][..];
|
let slice = numbers[0];
|
||||||
let len = slice.len();
|
let len = slice.len();
|
||||||
let dec = slice.find('.').unwrap_or(len);
|
let dec = slice.find('.').unwrap_or(len);
|
||||||
largest_dec = len - dec;
|
largest_dec = len - dec;
|
||||||
|
@ -118,7 +118,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
1.0
|
1.0
|
||||||
};
|
};
|
||||||
let increment = if numbers.len() > 2 {
|
let increment = if numbers.len() > 2 {
|
||||||
let slice = &numbers[1][..];
|
let slice = numbers[1];
|
||||||
let len = slice.len();
|
let len = slice.len();
|
||||||
let dec = slice.find('.').unwrap_or(len);
|
let dec = slice.find('.').unwrap_or(len);
|
||||||
largest_dec = cmp::max(largest_dec, len - dec);
|
largest_dec = cmp::max(largest_dec, len - dec);
|
||||||
|
@ -134,11 +134,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
1.0
|
1.0
|
||||||
};
|
};
|
||||||
if increment == 0.0 {
|
if increment == 0.0 {
|
||||||
show_error!("increment value: '{}'", &numbers[1][..]);
|
show_error!("increment value: '{}'", numbers[1]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
let last = {
|
let last = {
|
||||||
let slice = &numbers[numbers.len() - 1][..];
|
let slice = numbers[numbers.len() - 1];
|
||||||
padding = cmp::max(padding, slice.find('.').unwrap_or_else(|| slice.len()));
|
padding = cmp::max(padding, slice.find('.').unwrap_or_else(|| slice.len()));
|
||||||
match parse_float(slice) {
|
match parse_float(slice) {
|
||||||
Ok(n) => n,
|
Ok(n) => n,
|
||||||
|
|
|
@ -363,10 +363,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
|
|
||||||
let force = matches.is_present(options::FORCE);
|
let force = matches.is_present(options::FORCE);
|
||||||
let remove = matches.is_present(options::REMOVE);
|
let remove = matches.is_present(options::REMOVE);
|
||||||
let size_arg = match matches.value_of(options::SIZE) {
|
let size_arg = matches.value_of(options::SIZE).map(|s| s.to_string());
|
||||||
Some(s) => Some(s.to_string()),
|
|
||||||
None => None,
|
|
||||||
};
|
|
||||||
let size = get_size(size_arg);
|
let size = get_size(size_arg);
|
||||||
let exact = matches.is_present(options::EXACT) && size.is_none(); // if -s is given, ignore -x
|
let exact = matches.is_present(options::EXACT) && size.is_none(); // if -s is given, ignore -x
|
||||||
let zero = matches.is_present(options::ZERO);
|
let zero = matches.is_present(options::ZERO);
|
||||||
|
@ -439,6 +436,7 @@ fn pass_name(pass_type: PassType) -> String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
fn wipe_file(
|
fn wipe_file(
|
||||||
path_str: &str,
|
path_str: &str,
|
||||||
n_passes: usize,
|
n_passes: usize,
|
||||||
|
@ -472,12 +470,9 @@ fn wipe_file(
|
||||||
|
|
||||||
let mut perms = metadata.permissions();
|
let mut perms = metadata.permissions();
|
||||||
perms.set_readonly(false);
|
perms.set_readonly(false);
|
||||||
match fs::set_permissions(path, perms) {
|
if let Err(e) = fs::set_permissions(path, perms) {
|
||||||
Err(e) => {
|
show_error!("{}", e);
|
||||||
show_error!("{}", e);
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,8 +35,8 @@ extern "C" {
|
||||||
|
|
||||||
fn set_buffer(stream: *mut FILE, value: &str) {
|
fn set_buffer(stream: *mut FILE, value: &str) {
|
||||||
let (mode, size): (c_int, size_t) = match value {
|
let (mode, size): (c_int, size_t) = match value {
|
||||||
"0" => (_IONBF, 0 as size_t),
|
"0" => (_IONBF, 0_usize),
|
||||||
"L" => (_IOLBF, 0 as size_t),
|
"L" => (_IOLBF, 0_usize),
|
||||||
input => {
|
input => {
|
||||||
let buff_size: usize = match input.parse() {
|
let buff_size: usize = match input.parse() {
|
||||||
Ok(num) => num,
|
Ok(num) => num,
|
||||||
|
|
|
@ -141,12 +141,10 @@ fn parse_size(size: &str) -> Option<u64> {
|
||||||
|
|
||||||
fn check_option(matches: &ArgMatches, name: &str) -> Result<BufferType, ProgramOptionsError> {
|
fn check_option(matches: &ArgMatches, name: &str) -> Result<BufferType, ProgramOptionsError> {
|
||||||
match matches.value_of(name) {
|
match matches.value_of(name) {
|
||||||
Some(value) => match &value[..] {
|
Some(value) => match value {
|
||||||
"L" => {
|
"L" => {
|
||||||
if name == options::INPUT {
|
if name == options::INPUT {
|
||||||
Err(ProgramOptionsError(format!(
|
Err(ProgramOptionsError("line buffering stdin is meaningless".to_string()))
|
||||||
"line buffering stdin is meaningless"
|
|
||||||
)))
|
|
||||||
} else {
|
} else {
|
||||||
Ok(BufferType::Line)
|
Ok(BufferType::Line)
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,7 +75,7 @@ fn open(name: &str) -> Result<Box<dyn Read>> {
|
||||||
"Is a directory",
|
"Is a directory",
|
||||||
));
|
));
|
||||||
};
|
};
|
||||||
if !path.metadata().is_ok() {
|
if path.metadata().is_err() {
|
||||||
return Err(std::io::Error::new(
|
return Err(std::io::Error::new(
|
||||||
std::io::ErrorKind::NotFound,
|
std::io::ErrorKind::NotFound,
|
||||||
"No such file or directory",
|
"No such file or directory",
|
||||||
|
|
|
@ -90,7 +90,7 @@ fn tac(filenames: Vec<String>, before: bool, _: bool, separator: &str) -> i32 {
|
||||||
Box::new(stdin()) as Box<dyn Read>
|
Box::new(stdin()) as Box<dyn Read>
|
||||||
} else {
|
} else {
|
||||||
let path = Path::new(filename);
|
let path = Path::new(filename);
|
||||||
if path.is_dir() || !path.metadata().is_ok() {
|
if path.is_dir() || path.metadata().is_err() {
|
||||||
show_error!(
|
show_error!(
|
||||||
"failed to open '{}' for reading: No such file or directory",
|
"failed to open '{}' for reading: No such file or directory",
|
||||||
filename
|
filename
|
||||||
|
|
|
@ -55,16 +55,16 @@ fn two(args: &[&[u8]], error: &mut bool) -> bool {
|
||||||
b"-d" => path(args[1], PathCondition::Directory),
|
b"-d" => path(args[1], PathCondition::Directory),
|
||||||
b"-e" => path(args[1], PathCondition::Exists),
|
b"-e" => path(args[1], PathCondition::Exists),
|
||||||
b"-f" => path(args[1], PathCondition::Regular),
|
b"-f" => path(args[1], PathCondition::Regular),
|
||||||
b"-g" => path(args[1], PathCondition::GroupIDFlag),
|
b"-g" => path(args[1], PathCondition::GroupIdFlag),
|
||||||
b"-h" => path(args[1], PathCondition::SymLink),
|
b"-h" => path(args[1], PathCondition::SymLink),
|
||||||
b"-L" => path(args[1], PathCondition::SymLink),
|
b"-L" => path(args[1], PathCondition::SymLink),
|
||||||
b"-n" => one(&args[1..]),
|
b"-n" => one(&args[1..]),
|
||||||
b"-p" => path(args[1], PathCondition::FIFO),
|
b"-p" => path(args[1], PathCondition::Fifo),
|
||||||
b"-r" => path(args[1], PathCondition::Readable),
|
b"-r" => path(args[1], PathCondition::Readable),
|
||||||
b"-S" => path(args[1], PathCondition::Socket),
|
b"-S" => path(args[1], PathCondition::Socket),
|
||||||
b"-s" => path(args[1], PathCondition::NonEmpty),
|
b"-s" => path(args[1], PathCondition::NonEmpty),
|
||||||
b"-t" => isatty(args[1]),
|
b"-t" => isatty(args[1]),
|
||||||
b"-u" => path(args[1], PathCondition::UserIDFlag),
|
b"-u" => path(args[1], PathCondition::UserIdFlag),
|
||||||
b"-w" => path(args[1], PathCondition::Writable),
|
b"-w" => path(args[1], PathCondition::Writable),
|
||||||
b"-x" => path(args[1], PathCondition::Executable),
|
b"-x" => path(args[1], PathCondition::Executable),
|
||||||
b"-z" => !one(&args[1..]),
|
b"-z" => !one(&args[1..]),
|
||||||
|
@ -322,13 +322,13 @@ enum PathCondition {
|
||||||
Directory,
|
Directory,
|
||||||
Exists,
|
Exists,
|
||||||
Regular,
|
Regular,
|
||||||
GroupIDFlag,
|
GroupIdFlag,
|
||||||
SymLink,
|
SymLink,
|
||||||
FIFO,
|
Fifo,
|
||||||
Readable,
|
Readable,
|
||||||
Socket,
|
Socket,
|
||||||
NonEmpty,
|
NonEmpty,
|
||||||
UserIDFlag,
|
UserIdFlag,
|
||||||
Writable,
|
Writable,
|
||||||
Executable,
|
Executable,
|
||||||
}
|
}
|
||||||
|
@ -390,13 +390,13 @@ fn path(path: &[u8], cond: PathCondition) -> bool {
|
||||||
PathCondition::Directory => file_type.is_dir(),
|
PathCondition::Directory => file_type.is_dir(),
|
||||||
PathCondition::Exists => true,
|
PathCondition::Exists => true,
|
||||||
PathCondition::Regular => file_type.is_file(),
|
PathCondition::Regular => file_type.is_file(),
|
||||||
PathCondition::GroupIDFlag => metadata.mode() & S_ISGID != 0,
|
PathCondition::GroupIdFlag => metadata.mode() & S_ISGID != 0,
|
||||||
PathCondition::SymLink => metadata.file_type().is_symlink(),
|
PathCondition::SymLink => metadata.file_type().is_symlink(),
|
||||||
PathCondition::FIFO => file_type.is_fifo(),
|
PathCondition::Fifo => file_type.is_fifo(),
|
||||||
PathCondition::Readable => perm(metadata, Permission::Read),
|
PathCondition::Readable => perm(metadata, Permission::Read),
|
||||||
PathCondition::Socket => file_type.is_socket(),
|
PathCondition::Socket => file_type.is_socket(),
|
||||||
PathCondition::NonEmpty => metadata.size() > 0,
|
PathCondition::NonEmpty => metadata.size() > 0,
|
||||||
PathCondition::UserIDFlag => metadata.mode() & S_ISUID != 0,
|
PathCondition::UserIdFlag => metadata.mode() & S_ISUID != 0,
|
||||||
PathCondition::Writable => perm(metadata, Permission::Write),
|
PathCondition::Writable => perm(metadata, Permission::Write),
|
||||||
PathCondition::Executable => perm(metadata, Permission::Execute),
|
PathCondition::Executable => perm(metadata, Permission::Execute),
|
||||||
}
|
}
|
||||||
|
@ -416,13 +416,13 @@ fn path(path: &[u8], cond: PathCondition) -> bool {
|
||||||
PathCondition::Directory => stat.is_dir(),
|
PathCondition::Directory => stat.is_dir(),
|
||||||
PathCondition::Exists => true,
|
PathCondition::Exists => true,
|
||||||
PathCondition::Regular => stat.is_file(),
|
PathCondition::Regular => stat.is_file(),
|
||||||
PathCondition::GroupIDFlag => false,
|
PathCondition::GroupIdFlag => false,
|
||||||
PathCondition::SymLink => false,
|
PathCondition::SymLink => false,
|
||||||
PathCondition::FIFO => false,
|
PathCondition::Fifo => false,
|
||||||
PathCondition::Readable => false, // TODO
|
PathCondition::Readable => false, // TODO
|
||||||
PathCondition::Socket => false,
|
PathCondition::Socket => false,
|
||||||
PathCondition::NonEmpty => stat.len() > 0,
|
PathCondition::NonEmpty => stat.len() > 0,
|
||||||
PathCondition::UserIDFlag => false,
|
PathCondition::UserIdFlag => false,
|
||||||
PathCondition::Writable => false, // TODO
|
PathCondition::Writable => false, // TODO
|
||||||
PathCondition::Executable => false, // TODO
|
PathCondition::Executable => false, // TODO
|
||||||
}
|
}
|
||||||
|
|
|
@ -137,7 +137,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
|
|
||||||
let (mut atime, mut mtime) = if matches.is_present(options::sources::REFERENCE) {
|
let (mut atime, mut mtime) = if matches.is_present(options::sources::REFERENCE) {
|
||||||
stat(
|
stat(
|
||||||
&matches.value_of(options::sources::REFERENCE).unwrap()[..],
|
matches.value_of(options::sources::REFERENCE).unwrap(),
|
||||||
!matches.is_present(options::NO_DEREF),
|
!matches.is_present(options::NO_DEREF),
|
||||||
)
|
)
|
||||||
} else if matches.is_present(options::sources::DATE)
|
} else if matches.is_present(options::sources::DATE)
|
||||||
|
|
|
@ -24,7 +24,7 @@ use std::ops::RangeInclusive;
|
||||||
fn parse_sequence(s: &str) -> (char, usize) {
|
fn parse_sequence(s: &str) -> (char, usize) {
|
||||||
let c = s.chars().next().expect("invalid escape: empty string");
|
let c = s.chars().next().expect("invalid escape: empty string");
|
||||||
|
|
||||||
if '0' <= c && c <= '7' {
|
if ('0'..='7').contains(&c) {
|
||||||
let mut v = c.to_digit(8).unwrap();
|
let mut v = c.to_digit(8).unwrap();
|
||||||
let mut consumed = 1;
|
let mut consumed = 1;
|
||||||
let bits_per_digit = 3;
|
let bits_per_digit = 3;
|
||||||
|
|
|
@ -32,13 +32,16 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
.version(VERSION)
|
.version(VERSION)
|
||||||
.usage(USAGE)
|
.usage(USAGE)
|
||||||
.about(SUMMARY)
|
.about(SUMMARY)
|
||||||
.arg(Arg::with_name(options::FILE).hidden(true))
|
.arg(
|
||||||
|
Arg::with_name(options::FILE)
|
||||||
|
.default_value("-")
|
||||||
|
.hidden(true),
|
||||||
|
)
|
||||||
.get_matches_from(args);
|
.get_matches_from(args);
|
||||||
|
|
||||||
let input = match matches.value_of(options::FILE) {
|
let input = matches
|
||||||
Some(v) => v,
|
.value_of(options::FILE)
|
||||||
None => "-",
|
.expect("Value is required by clap");
|
||||||
};
|
|
||||||
|
|
||||||
let mut stdin_buf;
|
let mut stdin_buf;
|
||||||
let mut file_buf;
|
let mut file_buf;
|
||||||
|
|
|
@ -65,9 +65,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return if is_stdin_interactive() {
|
if is_stdin_interactive() {
|
||||||
libc::EXIT_SUCCESS
|
libc::EXIT_SUCCESS
|
||||||
} else {
|
} else {
|
||||||
libc::EXIT_FAILURE
|
libc::EXIT_FAILURE
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,10 +149,8 @@ fn next_tabstop(tabstops: &[usize], col: usize) -> Option<usize> {
|
||||||
Some(tabstops[0] - col % tabstops[0])
|
Some(tabstops[0] - col % tabstops[0])
|
||||||
} else {
|
} else {
|
||||||
// find next larger tab
|
// find next larger tab
|
||||||
match tabstops.iter().find(|&&t| t > col) {
|
// if there isn't one in the list, tab becomes a single space
|
||||||
Some(t) => Some(t - col),
|
tabstops.iter().find(|&&t| t > col).map(|t| t-col)
|
||||||
None => None, // if there isn't one in the list, tab becomes a single space
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -72,30 +72,27 @@ pub(crate) fn count_bytes_fast<T: WordCountable>(handle: &mut T) -> WcResult<usi
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
{
|
{
|
||||||
let fd = handle.as_raw_fd();
|
let fd = handle.as_raw_fd();
|
||||||
match fstat(fd) {
|
if let Ok(stat) = fstat(fd) {
|
||||||
Ok(stat) => {
|
// If the file is regular, then the `st_size` should hold
|
||||||
// If the file is regular, then the `st_size` should hold
|
// the file's size in bytes.
|
||||||
// the file's size in bytes.
|
if (stat.st_mode & S_IFREG) != 0 {
|
||||||
if (stat.st_mode & S_IFREG) != 0 {
|
return Ok(stat.st_size as usize);
|
||||||
return Ok(stat.st_size as usize);
|
}
|
||||||
}
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
||||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
{
|
||||||
{
|
// Else, if we're on Linux and our file is a FIFO pipe
|
||||||
// Else, if we're on Linux and our file is a FIFO pipe
|
// (or stdin), we use splice to count the number of bytes.
|
||||||
// (or stdin), we use splice to count the number of bytes.
|
if (stat.st_mode & S_IFIFO) != 0 {
|
||||||
if (stat.st_mode & S_IFIFO) != 0 {
|
if let Ok(n) = count_bytes_using_splice(fd) {
|
||||||
if let Ok(n) = count_bytes_using_splice(fd) {
|
return Ok(n);
|
||||||
return Ok(n);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fall back on `read`, but without the overhead of counting words and lines.
|
// Fall back on `read`, but without the overhead of counting words and lines.
|
||||||
let mut buf = [0 as u8; BUF_SIZE];
|
let mut buf = [0_u8; BUF_SIZE];
|
||||||
let mut byte_count = 0;
|
let mut byte_count = 0;
|
||||||
loop {
|
loop {
|
||||||
match handle.read(&mut buf) {
|
match handle.read(&mut buf) {
|
||||||
|
|
|
@ -138,11 +138,8 @@ impl AddAssign for WordCount {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WordCount {
|
impl WordCount {
|
||||||
fn with_title<'a>(self, title: &'a str) -> TitledWordCount<'a> {
|
fn with_title(self, title: &str) -> TitledWordCount {
|
||||||
return TitledWordCount {
|
TitledWordCount { title, count: self }
|
||||||
title: title,
|
|
||||||
count: self,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -251,7 +248,7 @@ fn is_word_separator(byte: u8) -> bool {
|
||||||
fn word_count_from_reader<T: WordCountable>(
|
fn word_count_from_reader<T: WordCountable>(
|
||||||
mut reader: T,
|
mut reader: T,
|
||||||
settings: &Settings,
|
settings: &Settings,
|
||||||
path: &String,
|
path: &str,
|
||||||
) -> WcResult<WordCount> {
|
) -> WcResult<WordCount> {
|
||||||
let only_count_bytes = settings.show_bytes
|
let only_count_bytes = settings.show_bytes
|
||||||
&& (!(settings.show_chars
|
&& (!(settings.show_chars
|
||||||
|
@ -333,18 +330,18 @@ fn word_count_from_reader<T: WordCountable>(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn word_count_from_path(path: &String, settings: &Settings) -> WcResult<WordCount> {
|
fn word_count_from_path(path: &str, settings: &Settings) -> WcResult<WordCount> {
|
||||||
if path == "-" {
|
if path == "-" {
|
||||||
let stdin = io::stdin();
|
let stdin = io::stdin();
|
||||||
let stdin_lock = stdin.lock();
|
let stdin_lock = stdin.lock();
|
||||||
return Ok(word_count_from_reader(stdin_lock, settings, path)?);
|
word_count_from_reader(stdin_lock, settings, path)
|
||||||
} else {
|
} else {
|
||||||
let path_obj = Path::new(path);
|
let path_obj = Path::new(path);
|
||||||
if path_obj.is_dir() {
|
if path_obj.is_dir() {
|
||||||
return Err(WcError::IsDirectory(path.clone()));
|
Err(WcError::IsDirectory(path.to_owned()))
|
||||||
} else {
|
} else {
|
||||||
let file = File::open(path)?;
|
let file = File::open(path)?;
|
||||||
return Ok(word_count_from_reader(file, settings, path)?);
|
word_count_from_reader(file, settings, path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -425,7 +422,7 @@ fn print_stats(
|
||||||
}
|
}
|
||||||
|
|
||||||
if result.title == "-" {
|
if result.title == "-" {
|
||||||
writeln!(stdout_lock, "")?;
|
writeln!(stdout_lock)?;
|
||||||
} else {
|
} else {
|
||||||
writeln!(stdout_lock, " {}", result.title)?;
|
writeln!(stdout_lock, " {}", result.title)?;
|
||||||
}
|
}
|
||||||
|
|
|
@ -222,7 +222,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
need_runlevel,
|
need_runlevel,
|
||||||
need_users,
|
need_users,
|
||||||
my_line_only,
|
my_line_only,
|
||||||
has_records: false,
|
|
||||||
args: matches.free,
|
args: matches.free,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -247,7 +246,6 @@ struct Who {
|
||||||
need_runlevel: bool,
|
need_runlevel: bool,
|
||||||
need_users: bool,
|
need_users: bool,
|
||||||
my_line_only: bool,
|
my_line_only: bool,
|
||||||
has_records: bool,
|
|
||||||
args: Vec<String>,
|
args: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,8 +319,7 @@ impl Who {
|
||||||
println!("{}", users.join(" "));
|
println!("{}", users.join(" "));
|
||||||
println!("# users={}", users.len());
|
println!("# users={}", users.len());
|
||||||
} else {
|
} else {
|
||||||
let mut records = Utmpx::iter_all_records().read_from(f).peekable();
|
let records = Utmpx::iter_all_records().read_from(f).peekable();
|
||||||
self.has_records = records.peek().is_some();
|
|
||||||
|
|
||||||
if self.include_heading {
|
if self.include_heading {
|
||||||
self.print_heading()
|
self.print_heading()
|
||||||
|
|
|
@ -31,9 +31,9 @@ fn chgrp<P: AsRef<Path>>(path: P, dgid: gid_t, follow: bool) -> IOResult<()> {
|
||||||
let s = CString::new(path.as_os_str().as_bytes()).unwrap();
|
let s = CString::new(path.as_os_str().as_bytes()).unwrap();
|
||||||
let ret = unsafe {
|
let ret = unsafe {
|
||||||
if follow {
|
if follow {
|
||||||
libc::chown(s.as_ptr(), (0 as gid_t).wrapping_sub(1), dgid)
|
libc::chown(s.as_ptr(), 0_u32.wrapping_sub(1), dgid)
|
||||||
} else {
|
} else {
|
||||||
lchown(s.as_ptr(), (0 as gid_t).wrapping_sub(1), dgid)
|
lchown(s.as_ptr(), 0_u32.wrapping_sub(1), dgid)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if ret == 0 {
|
if ret == 0 {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue