diff options
author | haroon maqsood <maqsood3525@live.com> | 2018-07-06 16:17:57 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-07-06 16:19:04 +0200 |
commit | 4a85d56c4cda90d0c175f29eb320375d14ead507 (patch) | |
tree | fe8054ff79285c09ddd7dca3f30aa952f1a3e8fa | |
parent | 6f09785b7e4a6cc7a9ede623d51aa8f6dc5e9a03 (diff) | |
download | busybox-4a85d56c4cda90d0c175f29eb320375d14ead507.tar.gz |
nproc: implement --all --ignore=N
function old new delta
nproc_main 98 242 +144
packed_usage 32799 32816 +17
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 161/0) Total: 161 bytes
Signed-off-by: haroon maqsood <maqsood3525@live.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/nproc.c | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/coreutils/nproc.c b/coreutils/nproc.c index 336b176ca..0ea8d1001 100644 --- a/coreutils/nproc.c +++ b/coreutils/nproc.c @@ -14,10 +14,14 @@ //kbuild:lib-$(CONFIG_NPROC) += nproc.o //usage:#define nproc_trivial_usage -//usage: "" -//TODO: "[--all] [--ignore=N]" +//usage: ""IF_LONG_OPTS("--all --ignore=N") //usage:#define nproc_full_usage "\n\n" -//usage: "Print number of CPUs" +//usage: "Print number of available CPUs" +//usage: IF_LONG_OPTS( +//usage: "\n" +//usage: "\n --all Number of installed CPUs" +//usage: "\n --ignore=N Exclude N CPUs" +//usage: ) #include <sched.h> #include "libbb.h" @@ -26,13 +30,30 @@ int nproc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) { unsigned long mask[1024]; - unsigned i, count = 0; - - //getopt32(argv, ""); - - //if --all, count /sys/devices/system/cpu/cpuN dirs, else: + int count = 0; +#if ENABLE_LONG_OPTS + int ignore = 0; + int opts = getopt32long(argv, "\xfe:+", + "ignore\0" Required_argument "\xfe" + "all\0" No_argument "\xff" + , &ignore + ); + if (opts & (1 << 1)) { + DIR *cpusd = opendir("/sys/devices/system/cpu"); + if (cpusd) { + struct dirent *de; + while (NULL != (de = readdir(cpusd))) { + char *cpuid = strstr(de->d_name, "cpu"); + if (cpuid && isdigit(cpuid[strlen(cpuid) - 1])) + count++; + } + closedir(cpusd); + } + } else +#endif if (sched_getaffinity(0, sizeof(mask), (void*)mask) == 0) { + int i; for (i = 0; i < ARRAY_SIZE(mask); i++) { unsigned long m = mask[i]; while (m) { @@ -42,8 +63,11 @@ int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) } } } - if (count == 0) - count++; + + IF_LONG_OPTS(count -= ignore;) + if (count <= 0) + count = 1; + printf("%u\n", count); return 0; |