Created
November 7, 2025 23:07
-
-
Save su8/c500af76472c978ffb2363ea6513c8fe to your computer and use it in GitHub Desktop.
main10.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::File; | |
| use std::fs::DirEntry; | |
| use std::io; | |
| use std::io::Write; | |
| use std::path::Path; | |
| use std::path::PathBuf; | |
| use std::sync::Mutex; | |
| use std::thread; | |
| 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) { | |
| for entry in fs::read_dir(folder).unwrap() { | |
| let entry = entry.unwrap(); | |
| let path = entry.path(); | |
| let mut lock = mtx.lock().unwrap(); | |
| fs::set_current_dir(folder).unwrap(); | |
| if opt == 'b' { | |
| if path.is_file() { | |
| println!("{} {} bytes", path.file_name().unwrap(), path.metadata().unwrap().len()); | |
| } | |
| } | |
| *cur_dir_num.entry(folder.to_string()).or_insert(0) += 1; | |
| } | |
| let cur_folder = if folder.starts_with('.') { fs::current_dir().unwrap().to_str().unwrap().to_string() } else { folder.to_string() }; | |
| println!("{} {} items", cur_folder, *cur_dir_num.get(folder).unwrap()); | |
| } | |
| fn main() { | |
| let args: Vec<String> = std::env::args().collect(); | |
| if args.len() > 1 && (args[1].chars().next().unwrap() == '' || args[1].chars().next().unwrap() == 'b') { | |
| let mut threads = vec![]; | |
| for x in 2..args.len() { | |
| threads.push(thread::spawn(move || walk_multiple_dirs(&args[x], args[1].chars().next().unwrap()))); | |
| *cur_dir_num.entry(args[x].clone()).or_insert(0); | |
| } | |
| for thread in threads { | |
| thread.join().unwrap(); | |
| } | |
| } else if args.len() > 1 && args[1].chars().next().unwrap() == '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