Created
November 8, 2025 00:17
-
-
Save su8/890939ddf96c6456cc9f3eddf4804086 to your computer and use it in GitHub Desktop.
main12.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::fs; | |
| use std::fs::DirEntry; | |
| use std::fs::File; | |
| use std::io; | |
| use std::io::BufRead; | |
| use std::io::BufReader; | |
| use std::path::Path; | |
| use std::path::PathBuf; | |
| use std::sync::Mutex; | |
| use std::thread; | |
| use std::thread::JoinHandle; | |
| use std::collections::HashMap; | |
| static mut mtx: Mutex<()>; | |
| static mut cur_dir_num: HashMap<String, u64> = HashMap::new(); | |
| fn walk_multiple_dirs(folder: &str, opt: char) { | |
| let folder_path = Path::new(folder); | |
| let entries = fs::read_dir(folder_path).unwrap(); | |
| let mut lock = mtx.lock().unwrap(); | |
| fs::set_current_dir(folder_path).unwrap(); | |
| if opt == 'b' { | |
| for entry in entries { | |
| let entry = entry.unwrap(); | |
| let file_path = entry.path(); | |
| let file_str = file_path.file_name().unwrap().to_str().unwrap(); | |
| if fs::metadata(file_path).unwrap().is_dir() { | |
| continue; | |
| } | |
| println!("{} {} bytes", file_str, file_path.metadata().unwrap().len()); | |
| } | |
| } | |
| cur_dir_num.insert(folder.to_string(), entries.count()); | |
| println!("{} {} items", folder, cur_dir_num[&folder.to_string()]); | |
| } | |
| fn main() { | |
| let mut args = std::env::args(); | |
| let opt = args.nth(1).unwrap_or_default(); | |
| if opt.chars().nth(1).unwrap_or(' ') == '' || opt.chars().nth(1).unwrap_or(' ') == 'b' { | |
| let mut threads: Vec<JoinHandle<()>> = Vec::new(); | |
| for arg in args { | |
| let folder = arg.to_string(); | |
| threads.push(thread::spawn(move || walk_multiple_dirs(folder.as_str(), opt.chars().nth(1).unwrap_or(' ')))); | |
| cur_dir_num.insert(folder, 0); | |
| } | |
| for thread in threads { | |
| thread.join().unwrap(); | |
| } | |
| } else if opt.chars().nth(1).unwrap_or(' ') == 'l' { | |
| let mut count = 0; | |
| for line in io::stdin().lines() { | |
| count += 1; | |
| } | |
| println!("{} items", count); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment