-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnproc.cpp
More file actions
52 lines (44 loc) · 1.43 KB
/
nproc.cpp
File metadata and controls
52 lines (44 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <cstdio>
#include <cstdlib>
#include <string>
#include <thread>
#include <unistd.h>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "nproc",
.version = CFBOX_VERSION_STRING,
.one_line = "print number of available processors",
.usage = "nproc [--all] [--ignore=N]",
.options = " --all print installed processors\n"
" --ignore=N subtract N from the count",
.extra = "",
};
} // namespace
auto nproc_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'\0', false, "all"},
cfbox::args::OptSpec{'\0', true, "ignore"},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
long count;
if (parsed.has_long("all")) {
count = sysconf(_SC_NPROCESSORS_CONF);
} else {
count = sysconf(_SC_NPROCESSORS_ONLN);
}
if (count <= 0) {
count = static_cast<long>(std::thread::hardware_concurrency());
}
if (count <= 0) count = 1;
auto ignore_val = parsed.get_long("ignore");
if (ignore_val) {
long ignore = std::strtol(std::string{*ignore_val}.c_str(), nullptr, 10);
count -= ignore;
if (count < 1) count = 1;
}
std::printf("%ld\n", count);
return 0;
}