diff options
author | Rob Landley <rob@landley.net> | 2015-10-25 16:50:03 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2015-10-25 16:50:03 -0500 |
commit | eb1bbc245dd6554a8fbe879a4efb903f6e7788cb (patch) | |
tree | 14ed879a8b0ea527a9a79c7aa8e4bdfc2b2f300c /lib/xwrap.c | |
parent | 5535a2695642dcf54051af2b654ab37a89ece72f (diff) | |
download | toybox-eb1bbc245dd6554a8fbe879a4efb903f6e7788cb.tar.gz |
Add xcount_cpus()
Diffstat (limited to 'lib/xwrap.c')
-rw-r--r-- | lib/xwrap.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/xwrap.c b/lib/xwrap.c index 4880bbe0..69f891b9 100644 --- a/lib/xwrap.c +++ b/lib/xwrap.c @@ -723,3 +723,21 @@ void xsignal(int signal, void *handler) if (sigaction(signal, sa, 0)) perror_exit("xsignal %d", signal); } + +unsigned xcount_cpus(void) +{ + int len = 0, i, fd = xopen("/proc/stat", O_RDONLY); + unsigned cpus = 0; + + for (;;) { + if (1>(i = xread(fd, libbuf, sizeof(libbuf)-len))) break; + len += i; + // Each cpu# line has data after it, so last 5 bytes of file can't match + for (i = 0; i<len-5; i++) + if (!strncmp(libbuf+i, "\ncpu", 4) && isdigit(libbuf[i+4])) cpus++; + memmove(libbuf, libbuf+i, 5); + } + close(fd); + + return cpus; +} |