Skip to content

Instantly share code, notes, and snippets.

@su8
Created November 6, 2025 13:24
Show Gist options
  • Select an option

  • Save su8/f8ff83a546cb48cbf494a098289bb48c to your computer and use it in GitHub Desktop.

Select an option

Save su8/f8ff83a546cb48cbf494a098289bb48c to your computer and use it in GitHub Desktop.
main8huge.rs
use std::fs::{self, DirEntry, Metadata};
use std::io::{self, BufRead, BufReader, Write};
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::thread;
static mut CURRENT_DIR_NUM: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
fn walk_multiple_dirs(folder: &str, opt: char) {
let mut count = 0;
for entry in fs::read_dir(folder).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if opt == 'b' {
if path.is_dir() {
continue;
}
let metadata: Metadata = fs::metadata(&path).unwrap();
println!("{} {} bytes", path.file_name().unwrap().to_str().unwrap(), metadata.len());
}
unsafe {
CURRENT_DIR_NUM.entry(folder.to_string()).and_modify(|e| *e += 1).or_insert(1);
}
count += 1;
}
let current_path = Path::new(folder);
if folder.starts_with(".") {
println!("{} {} items", fs::current_dir().unwrap().join(current_path).to_str().unwrap(), count);
} else {
println!("{} {} items", current_path.to_str().unwrap(), count);
}
}
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() > 2 && (args[1].chars().nth(1) == Some('m') || args[1].chars().nth(1) == Some('b')) {
let mut threads = Vec::new();
for i in 2..args.len() {
let folder = args[i].clone();
let opt = args[1].chars().nth(1).unwrap();
threads.push(thread::spawn(move || walk_multiple_dirs(&folder, opt)));
}
for thread in threads {
thread.join().unwrap();
}
} else if args.len() > 2 && args[1].chars().nth(1) == Some('l') {
let mut count = 0;
let stdin = io::stdin();
let reader = BufReader::new(stdin.lock());
for _ in reader.lines() {
count += 1;
}
println!("{} items", count);
}
}
use std::collections::HashMap;
use std::fs::{self, DirEntry};
use std::io::{self, BufRead};
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::thread;
static mut CUR_DIR_NUM: HashMap<String, u64> = HashMap::new();
lazy_static::lazy_static! {
static ref MUTE: Mutex<()> = Mutex::new(());
}
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() > 1 && (args[1].chars().nth(1).unwrap() == 'm' || args[1].chars().nth(1).unwrap() == 'b') {
let mut handles = vec![];
for folder in &args[2..] {
let opt = args[1].chars().nth(1).unwrap();
let folder_clone = folder.clone();
handles.push(thread::spawn(move || {
walk_multiple_dirs(&folder_clone, opt);
}));
unsafe {
CUR_DIR_NUM.insert(folder_clone.clone(), 0);
}
}
for handle in handles {
let _ = handle.join();
}
return;
}
if args.len() > 1 && args[1].chars().nth(1).unwrap() == 'l' {
let stdin = io::stdin();
let count = stdin.lock().lines().count();
println!("{} items", count);
}
}
fn walk_multiple_dirs(folder: &str, opt: char) {
let entries = fs::read_dir(folder);
match entries {
Ok(entries) => {
for entry in entries.filter_map(Result::ok) {
let lock = MUTE.lock().unwrap();
let file_str = entry.file_name().into_string().unwrap();
if opt == 'b' {
let path = Path::new(&file_str);
if path.exists() && path.is_dir() {
continue;
}
let size = fs::metadata(&file_str).map(|m| m.len()).unwrap_or(0);
println!("{} {} bytes", file_str, size);
}
unsafe {
*CUR_DIR_NUM.get_mut(folder).unwrap() += 1;
}
}
let cur_folder = if folder.starts_with('.') {
std::env::current_dir().unwrap()
} else {
Path::new(folder).to_path_buf()
};
println!("{} {} items", cur_folder.display(), unsafe { CUR_DIR_NUM[folder] });
}
Err(e) => {
let _lock = MUTE.lock().unwrap();
eprintln!("Error: {}", e);
}
}
}
use std::env;
fn variants(word: &str) -> Vec<String> {
let mut result = Vec::new();
result.push(word.to_string());
let mut capitalized = word.to_string();
if let Some(first_char) = capitalized.get_mut(0..1) {
capitalized.replace_range(0..1, &first_char.to_uppercase());
}
result.push(capitalized);
let upper = word.to_uppercase();
result.push(upper);
result
}
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 6 {
std::process::exit(1);
}
let mut usernames = vec!["frost".to_string()];
let mut roles = Vec::new();
let separators = vec!["".to_string(), "_".to_string()];
if args[1].chars().nth(1) == Some('f') {
usernames = vec![args[2].clone()];
}
if args[3].chars().nth(1) == Some('o') {
for x in (4..args.len()).rev() {
roles.push(args[x].clone());
}
}
for x in &usernames {
for z in &roles {
for sep in &separators {
for user in variants(x) {
for role in variants(z) {
println!("{}{}{}", user, sep, role);
}
}
}
}
}
}
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);
}
use std::collections::HashMap;
use std::fs;
use std::io::{self, BufRead};
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::thread;
fn walk_multiple_dirs(folder: &str, opt: char, cur_dir_num: Arc<Mutex<HashMap<String, u64>>>, mtx: Arc<Mutex<()>>) {
let folder_path = PathBuf::from(folder);
let res = (|| -> Result<(), Box<dyn std::error::Error>> {
for entry in fs::read_dir(&folder_path)? {
let entry = entry?;
let _lock = mtx.lock().unwrap();
std::env::set_current_dir(&folder_path)?;
if opt == 'b' {
let file_name = entry.file_name();
let file_str = file_name.to_string_lossy();
let file_path = PathBuf::from(&file_str);
if file_path.exists() && file_path.is_dir() {
continue;
}
let file_size = fs::metadata(&file_str)?.len();
println!("{} {} bytes", file_str, file_size);
}
let mut map = cur_dir_num.lock().unwrap();
*map.entry(folder.to_string()).or_insert(0) += 1;
}
let cur_folder = if folder.starts_with('.') {
std::env::current_dir()?
} else {
folder_path
};
let map = cur_dir_num.lock().unwrap();
let count = map.get(folder).copied().unwrap_or(0);
println!("{} {} items", cur_folder.display(), count);
Ok(())
})();
if let Err(e) = res {
let _lock = mtx.lock().unwrap();
eprintln!("Error: {}", e);
}
}
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() > 1 {
let opt = args[1].chars().nth(1);
if let Some(opt_char) = opt {
if opt_char == 'm' || opt_char == 'b' {
let cur_dir_num = Arc::new(Mutex::new(HashMap::new()));
let mtx = Arc::new(Mutex::new(()));
let mut handles = Vec::new();
for folder in &args[2..] {
{
let mut map = cur_dir_num.lock().unwrap();
map.insert(folder.clone(), 0);
}
let cur_dir_num_clone = Arc::clone(&cur_dir_num);
let mtx_clone = Arc::clone(&mtx);
let folder_clone = folder.clone();
let handle = thread::spawn(move || {
walk_multiple_dirs(&folder_clone, opt_char, cur_dir_num_clone, mtx_clone);
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
std::process::exit(0);
} else if opt_char == 'l' {
let stdin = io::stdin();
let mut count: u64 = 0;
for line in stdin.lock().lines() {
if line.is_err() {
break;
}
count += 1;
}
println!("{} items", count);
std::process::exit(0);
}
}
}
std::process::exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment