backlog of changes from the uconsole
This commit is contained in:
122
walk/walk.rs
Normal file
122
walk/walk.rs
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU Affero General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see https://www.gnu.org/licenses/.
|
||||
*
|
||||
* This file incorporates work covered by the following copyright and permission
|
||||
* notice:
|
||||
*
|
||||
* Copyright 2019 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use std::{
|
||||
env::{args, current_dir},
|
||||
fs::{read_dir, DirEntry},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
extern crate getopt;
|
||||
use getopt::{Opt, Parser};
|
||||
|
||||
extern crate sysexits;
|
||||
use sysexits::{EX_SOFTWARE, EX_USAGE};
|
||||
|
||||
enum Terminator {
|
||||
USER,
|
||||
NUL,
|
||||
NEWL,
|
||||
ASV,
|
||||
}
|
||||
|
||||
/* Recursively walks d, yielding all contained files and folders. */
|
||||
fn walk(d: &Path, l: i8) -> impl Iterator<Item = DirEntry> {
|
||||
assert!(d.is_dir());
|
||||
|
||||
// TODO: read_dir(d)??
|
||||
for entry in read_dir(d)? {
|
||||
let e = entry?;
|
||||
yield e;
|
||||
if e.is_dir() {
|
||||
for f in walk(e.path(), l - 1)? {
|
||||
yield f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn usage(s: &str) -> ExitCode {
|
||||
eprintln!("Usage: {} (-0n) (-d [delimiter]) (-l [levels])", s);
|
||||
ExitCode::from(EX_USAGE as u8)
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let argv = args().collect::<Vec<String>>();
|
||||
|
||||
let mut opts = Parser::new(&argv, "0d:l:nq");
|
||||
let mut levels: i8 = -1;
|
||||
let mut t: (Terminator, Option<String>) = (ASV, None);
|
||||
|
||||
loop {
|
||||
match opts.next() {
|
||||
None => break,
|
||||
Some(opt) => match opt {
|
||||
Ok(Opt('0', None)) => t = (NUL, None),
|
||||
Ok(Opt('n', None)) => t = (NEWL, None),
|
||||
Ok(Opt('d', Some(arg))) => t = (USER, arg),
|
||||
Ok(Opt('l', Some(arg))) => match arg.parse::<u8>() {
|
||||
Ok(l) => levels = l,
|
||||
_ => {
|
||||
return usage(&argv[0]);
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
return usage(&argv[0]);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
/* t.2 can only be Some if t.1 is USER */
|
||||
assert_eq!(t.1 == USER, !t.2.is_none());
|
||||
|
||||
// if(*argv == NULL)
|
||||
// argv = dot;
|
||||
//
|
||||
// {
|
||||
// int retval;
|
||||
//
|
||||
// for(retval = 0; *argv != NULL; ++argv)
|
||||
// if((retval = walk(*argv, levels, verbosity >= 2 ? argv0 : NULL))
|
||||
// != EX_OK){
|
||||
// if(verbosity >= 1)
|
||||
// fprintf(stderr, "%s: %s: %s\n",
|
||||
// argv0, *argv, strerror(errno));
|
||||
// return retval;
|
||||
// }
|
||||
// }
|
||||
|
||||
EX_OK as u8
|
||||
}
|
||||
Reference in New Issue
Block a user