Last active
November 6, 2025 13:21
-
-
Save su8/b585d41db11a9588eaf84a63e1a46b16 to your computer and use it in GitHub Desktop.
main7.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::io::prelude::*; | |
| 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>; | |
| fn walk_multiple_dirs(folder: &str, opt: char) { | |
| let mut path = Path::new(folder); | |
| if let Ok(entry) = fs::read_dir(path) { | |
| for entry in entry { | |
| if let Ok(entry) = entry { | |
| let entry_path = entry.path(); | |
| let file_name = entry_path.file_name().unwrap().to_str().unwrap(); | |
| let file_path = entry_path.to_str().unwrap(); | |
| mtx.lock().unwrap(); | |
| let file_size = fs::metadata(file_path).unwrap().len(); | |
| match opt { | |
| 'b' => { | |
| if fs::metadata(file_path).unwrap().is_file() { | |
| println!("{} {} bytes", file_name, file_size); | |
| } | |
| } | |
| _ => {} | |
| } | |
| *cur_dir_num.entry(folder.to_string()).or_insert(0) += 1; | |
| mtx.unlock(); | |
| } | |
| } | |
| } else { | |
| eprintln!("Error: {}", fs::errno().unwrap()); | |
| } | |
| } | |
| fn main() { | |
| let args: Vec<String> = std::env::args().collect(); | |
| if args.len() > 1 { | |
| let opt = args[1].chars().next().unwrap(); | |
| let mut threads: Vec<JoinHandle<()>> = Vec::new(); | |
| for arg in &args[2..] { | |
| let handle = thread::spawn(move || { | |
| walk_multiple_dirs(arg, opt); | |
| }); | |
| threads.push(handle); | |
| } | |
| for thread in threads { | |
| thread.join().unwrap(); | |
| } | |
| } | |
| } | |
| let args: Vec<String> = std::env::args().collect(); | |
| if args.len() > 1 && (args[1].as_str() == "m" || args[1].as_str() == "b") { | |
| let cur_dir_num = Arc::new(Mutex::new(CurDirNum::new())); | |
| let mut threads = Vec::new(); | |
| for arg in &args[2..] { | |
| threads.push(thread::spawn(move || { | |
| let cur_dir_num = Arc::clone(&cur_dir_num); | |
| walk_multiple_dirs(arg.as_str(), &args[1][1], &mut cur_dir_num); | |
| })); | |
| } | |
| for thread in threads { | |
| thread.join().unwrap(); | |
| } | |
| println!("EXIT_SUCCESS"); | |
| } else if args.len() > 1 && args[1].as_str() == "l" { | |
| let mut count = 0; | |
| for line in std::io::stdin().lines() { | |
| if let Ok(_) = line { | |
| count += 1; | |
| } | |
| } | |
| println!("{} items", count); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment