diff --git a/Cargo.lock b/Cargo.lock index 82b190c..ae279da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,29 @@ name = "coreutils" version = "0.1.0" dependencies = [ "boxutils", + "num_cpus", +] + +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + +[[package]] +name = "libc" +version = "0.2.171" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" + +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", ] [[package]] diff --git a/coreutils/Cargo.lock b/coreutils/Cargo.lock index a5075b7..5befca4 100644 --- a/coreutils/Cargo.lock +++ b/coreutils/Cargo.lock @@ -11,4 +11,27 @@ name = "coreutils" version = "0.1.0" dependencies = [ "boxutils", + "num_cpus", +] + +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + +[[package]] +name = "libc" +version = "0.2.171" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" + +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", ] diff --git a/coreutils/Cargo.toml b/coreutils/Cargo.toml index 5c88adc..90f2bdc 100644 --- a/coreutils/Cargo.toml +++ b/coreutils/Cargo.toml @@ -5,3 +5,4 @@ edition = "2024" [dependencies] boxutils = { path = "../utils" } +num_cpus = "1.16.0" diff --git a/coreutils/src/commands/dd.rs b/coreutils/src/commands/dd.rs index 43fac19..c3da024 100644 --- a/coreutils/src/commands/dd.rs +++ b/coreutils/src/commands/dd.rs @@ -25,7 +25,7 @@ impl Command for Dd { if v.parse::().is_ok() { // assume the bs is specified in bytes, // because the last char is a number - blocksize = bs.parse::().unwrap() + blocksize = bs.parse::().unwrap() } else { match v { "K" | "k" => blocksize = k.parse::().unwrap() * 1024, @@ -59,8 +59,8 @@ impl Command for Dd { let _ = io::stdout().write_all(&vecbuf); } - let duration = start.elapsed().as_millis() / 1000; - + let duration = start.elapsed().as_secs_f64(); + let kb_per_sec = (vecbuf.len() as f64 / 1024.0) / duration; let out_blocks = vecbuf.len() as u64 / blocksize; let out_remainder = vecbuf.len() as u64 % blocksize; @@ -77,9 +77,12 @@ impl Command for Dd { ); println!( - "{} bytes copied, ca. {} seconds", + "{} bytes ({}B) copied, {:.6} seconds, {:.2}KB/s", vecbuf.len(), - duration + vecbuf.len(), + duration, + kb_per_sec + ) } } diff --git a/coreutils/src/commands/mod.rs b/coreutils/src/commands/mod.rs index 2a57ec5..d1967a4 100644 --- a/coreutils/src/commands/mod.rs +++ b/coreutils/src/commands/mod.rs @@ -12,3 +12,6 @@ pub use mkdir::Mkdir; mod dd; pub use dd::Dd; + +mod nproc; +pub use nproc::Nproc; diff --git a/coreutils/src/commands/nproc.rs b/coreutils/src/commands/nproc.rs new file mode 100644 index 0000000..eaf58f0 --- /dev/null +++ b/coreutils/src/commands/nproc.rs @@ -0,0 +1,53 @@ +use boxutils::commands::Command; +use boxutils::args::ArgParser; +use std::env; +use num_cpus::{get, get_physical}; + +pub struct Nproc; + +impl Command for Nproc { + fn execute(&self) { + let raw_args = env::args().collect::>(); + let args = ArgParser::builder() + .add_flag("--help") + .add_flag("--ignore") + .add_flag("--all") + .parse("nproc", raw_args); + let mut ignore: u64 = 0; + let mut all = false; + if args.get_flag("--help") { + println!( +" +Usage: nproc [--all] [ignore=N] +Prints the number of available CPUs to stdout. + + --all List all installed CPUs + --ignore=N, --ignore N Ignore N CPUs +" + ); + return + } + + if args.get_flag("--all") { + all = true; + } + + if args.get_flag("--ignore") { + ignore = args.get_option("--ignore").unwrap().parse().unwrap(); + } + + for argument in args.get_normal_args() { + if let Some((k, v)) = argument.split_once('=') { + if k == "--ignore" { + ignore = v.parse().unwrap(); + } + } + } + + if all { + println!("{}", get() as u64 - ignore) // TODO: actually make `--all` do something + } else { + println!("{}", get() as u64 - ignore) + } + } +} diff --git a/src/registry.rs b/src/registry.rs index 6967e7d..8a18606 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -10,6 +10,7 @@ pub fn get_registry() -> CommandRegistry { registry.register("mkdir", Box::new(coreutils::commands::Mkdir)); registry.register("ash", Box::new(shell::ash::Ash)); registry.register("dd", Box::new(coreutils::commands::Dd)); + registry.register("nproc", Box::new(coreutils::commands::Nproc)); registry.register("box", Box::new(Boxcmd)); registry