fileis(1): various changes to make the code more efficient and idiomatic

This commit is contained in:
Emma Tebibyte 2024-09-10 02:42:55 -06:00
parent 6d7173e438
commit f4bd4de2e4
Signed by: emma
GPG Key ID: 06FA419A1698C270

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2024 DTB <trinity@trinity.moe>
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify it under
@ -24,23 +25,27 @@ use std::{
};
extern crate getopt;
use getopt::GetOpt;
extern crate strerror;
extern crate sysexits;
use getopt::GetOpt;
use strerror::StrError;
use sysexits::EX_USAGE;
fn usage(s: &str) -> ExitCode {
eprintln!("Usage: {} [-bcdefgkprsuwxLS] file...", s);
ExitCode::from(EX_USAGE as u8)
const OPTS: &str = "bcdefgkprsuwxLS";
fn usage(argv0: &str) -> ExitCode {
eprintln!("Usage: {} [-{}] file...", argv0, OPTS);
ExitCode::from(EX_USAGE)
}
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]
while let Some(opt) = argv.getopt("bcdefgkprsuwxLS") {
while let Some(opt) = argv.getopt(OPTS) {
if let Ok(optchr) = opt.opt() { sel.push_str(optchr); }
else { return usage(&argv[0]); }
@ -50,17 +55,16 @@ fn main() -> ExitCode {
if optind == argv.len() { return usage(&argv[0]); }
for arg in argv.iter().skip(optind) {
let fmeta; // file metadata
let fmode; // file mode; see chmod(1p)
let ftype; // file type
let fmeta = match metadata(arg) {
Ok(m) => m,
Err(e) => { // no perms or nonexistent
eprintln!("{}: {}: {}", argv[0], arg, e.strerror());
return ExitCode::FAILURE;
},
};
match metadata(arg) {
Ok(m) => fmeta = m,
_ => { return ExitCode::FAILURE; } // no perms or nonexistent
}
fmode = fmeta.mode();
ftype = fmeta.file_type();
let fmode = fmeta.mode();
let ftype = fmeta.file_type();
for selection in sel.chars() { // run all selected tests
match selection {