2023-01-07 18:50:18 -05:00
|
|
|
use super::Cmd;
|
2023-01-08 01:12:36 -05:00
|
|
|
use clap::{Arg, ArgAction, Command};
|
2023-01-07 18:50:18 -05:00
|
|
|
|
2023-01-13 01:08:32 -05:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct Nproc;
|
2023-01-07 18:50:18 -05:00
|
|
|
|
|
|
|
impl Cmd for Nproc {
|
|
|
|
fn cli(&self) -> clap::Command {
|
2023-01-13 01:08:32 -05:00
|
|
|
Command::new("nproc")
|
2023-01-07 18:50:18 -05:00
|
|
|
.author("Nathan Fisher")
|
|
|
|
.about("Print the number of processing units available")
|
|
|
|
.arg(
|
|
|
|
Arg::new("ALL")
|
|
|
|
.help("Print the number of installed processors")
|
|
|
|
.short('a')
|
|
|
|
.long("all")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-04 08:54:27 -05:00
|
|
|
fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
|
2023-01-07 18:50:18 -05:00
|
|
|
if matches.get_flag("ALL") {
|
2023-02-02 23:34:37 -05:00
|
|
|
println!("{}", unsafe { get_nprocs_conf() });
|
2023-01-07 18:50:18 -05:00
|
|
|
} else {
|
2023-02-02 23:34:37 -05:00
|
|
|
println!("{}", unsafe { get_nprocs() });
|
2023-01-07 18:50:18 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path(&self) -> Option<crate::Path> {
|
2023-01-13 01:08:32 -05:00
|
|
|
Some(crate::Path::UsrBin)
|
2023-01-07 18:50:18 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-02 23:34:37 -05:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
fn get_nprocs() -> libc::c_int;
|
|
|
|
fn get_nprocs_conf() -> libc::c_int;
|
|
|
|
}
|