fileis(1): various changes to make the code more efficient and idiomatic
This commit is contained in:
parent
6d7173e438
commit
f4bd4de2e4
@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2024 DTB <trinity@trinity.moe>
|
* Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||||
|
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
|
||||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify it under
|
* This program is free software: you can redistribute it and/or modify it under
|
||||||
@ -24,23 +25,27 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
extern crate getopt;
|
extern crate getopt;
|
||||||
use getopt::GetOpt;
|
extern crate strerror;
|
||||||
|
|
||||||
extern crate sysexits;
|
extern crate sysexits;
|
||||||
|
|
||||||
|
use getopt::GetOpt;
|
||||||
|
use strerror::StrError;
|
||||||
use sysexits::EX_USAGE;
|
use sysexits::EX_USAGE;
|
||||||
|
|
||||||
fn usage(s: &str) -> ExitCode {
|
const OPTS: &str = "bcdefgkprsuwxLS";
|
||||||
eprintln!("Usage: {} [-bcdefgkprsuwxLS] file...", s);
|
|
||||||
ExitCode::from(EX_USAGE as u8)
|
fn usage(argv0: &str) -> ExitCode {
|
||||||
|
eprintln!("Usage: {} [-{}] file...", argv0, OPTS);
|
||||||
|
ExitCode::from(EX_USAGE)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> ExitCode {
|
fn main() -> ExitCode {
|
||||||
let argv = args().collect::<Vec<String>>();
|
let argv = args().collect::<Vec<_>>();
|
||||||
|
|
||||||
let mut sel = String::from(""); // selected options
|
let mut sel = String::with_capacity(OPTS.len()); // selected options
|
||||||
let mut optind: usize = 1; // argv[0]
|
let mut optind: usize = 1; // argv[0]
|
||||||
|
|
||||||
while let Some(opt) = argv.getopt("bcdefgkprsuwxLS") {
|
while let Some(opt) = argv.getopt(OPTS) {
|
||||||
if let Ok(optchr) = opt.opt() { sel.push_str(optchr); }
|
if let Ok(optchr) = opt.opt() { sel.push_str(optchr); }
|
||||||
else { return usage(&argv[0]); }
|
else { return usage(&argv[0]); }
|
||||||
|
|
||||||
@ -50,17 +55,16 @@ fn main() -> ExitCode {
|
|||||||
if optind == argv.len() { return usage(&argv[0]); }
|
if optind == argv.len() { return usage(&argv[0]); }
|
||||||
|
|
||||||
for arg in argv.iter().skip(optind) {
|
for arg in argv.iter().skip(optind) {
|
||||||
let fmeta; // file metadata
|
let fmeta = match metadata(arg) {
|
||||||
let fmode; // file mode; see chmod(1p)
|
Ok(m) => m,
|
||||||
let ftype; // file type
|
Err(e) => { // no perms or nonexistent
|
||||||
|
eprintln!("{}: {}: {}", argv[0], arg, e.strerror());
|
||||||
|
return ExitCode::FAILURE;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
match metadata(arg) {
|
let fmode = fmeta.mode();
|
||||||
Ok(m) => fmeta = m,
|
let ftype = fmeta.file_type();
|
||||||
_ => { return ExitCode::FAILURE; } // no perms or nonexistent
|
|
||||||
}
|
|
||||||
|
|
||||||
fmode = fmeta.mode();
|
|
||||||
ftype = fmeta.file_type();
|
|
||||||
|
|
||||||
for selection in sel.chars() { // run all selected tests
|
for selection in sel.chars() { // run all selected tests
|
||||||
match selection {
|
match selection {
|
||||||
|
Loading…
Reference in New Issue
Block a user