aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2021-08-10 09:28:10 +0100
committerRon Yorston <rmy@pobox.com>2021-08-10 09:28:10 +0100
commite8bc6bbc96d4cb0b3b169dd88d3ac77b185a4f0f (patch)
tree78cb5a345bc2fb63beb831e9333ac6c46dcfd53a
parent56e1d04ae71a05586fa3414aabdef0de720d0720 (diff)
downloadbusybox-w32-e8bc6bbc96d4cb0b3b169dd88d3ac77b185a4f0f.tar.gz
busybox-w32-e8bc6bbc96d4cb0b3b169dd88d3ac77b185a4f0f.tar.bz2
busybox-w32-e8bc6bbc96d4cb0b3b169dd88d3ac77b185a4f0f.zip
nproc: code shrink
Avoid duplication of call to getopt(). Simplify WIN32 code. Saves 80 bytes.
-rw-r--r--coreutils/nproc.c36
1 files changed, 12 insertions, 24 deletions
diff --git a/coreutils/nproc.c b/coreutils/nproc.c
index b345255a7..fb673a210 100644
--- a/coreutils/nproc.c
+++ b/coreutils/nproc.c
@@ -31,6 +31,9 @@ int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
31{ 31{
32#if !ENABLE_PLATFORM_MINGW32 32#if !ENABLE_PLATFORM_MINGW32
33 unsigned long mask[1024]; 33 unsigned long mask[1024];
34#else
35 DWORD_PTR affinity, process_affinity, system_affinity;
36#endif
34 int count = 0; 37 int count = 0;
35#if ENABLE_LONG_OPTS 38#if ENABLE_LONG_OPTS
36 int ignore = 0; 39 int ignore = 0;
@@ -39,7 +42,10 @@ int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
39 "all\0" No_argument "\xff" 42 "all\0" No_argument "\xff"
40 , &ignore 43 , &ignore
41 ); 44 );
45#endif
42 46
47#if !ENABLE_PLATFORM_MINGW32
48#if ENABLE_LONG_OPTS
43 if (opts & (1 << 1)) { 49 if (opts & (1 << 1)) {
44 DIR *cpusd = opendir("/sys/devices/system/cpu"); 50 DIR *cpusd = opendir("/sys/devices/system/cpu");
45 if (cpusd) { 51 if (cpusd) {
@@ -65,31 +71,13 @@ int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
65 } 71 }
66 } 72 }
67#else /* ENABLE_PLATFORM_MINGW32 */ 73#else /* ENABLE_PLATFORM_MINGW32 */
68 int count = 0; 74 if (GetProcessAffinityMask(GetCurrentProcess(), &process_affinity,
69 DWORD_PTR process_affinity, system_affinity;
70#if ENABLE_LONG_OPTS
71 int ignore = 0;
72 int opts = getopt32long(argv, "\xfe:+",
73 "ignore\0" Required_argument "\xfe"
74 "all\0" No_argument "\xff"
75 , &ignore
76 );
77#endif
78 if (!GetProcessAffinityMask(GetCurrentProcess(), &process_affinity,
79 &system_affinity)) { 75 &system_affinity)) {
80 process_affinity = system_affinity = 0; 76 affinity = (ENABLE_LONG_OPTS && opts & (1 << 1)) ?
81 } 77 system_affinity : process_affinity;
82 78 while (affinity) {
83#if ENABLE_LONG_OPTS 79 count += affinity & 1;
84 if (opts & (1 << 1)) { 80 affinity >>= 1;
85 for (count = 0; system_affinity; system_affinity >>= 1) {
86 count += system_affinity & 1;
87 }
88 } else
89#endif
90 {
91 for (count = 0; process_affinity; process_affinity >>= 1) {
92 count += process_affinity & 1;
93 } 81 }
94 } 82 }
95#endif /* ENABLE_PLATFORM_MINGW32 */ 83#endif /* ENABLE_PLATFORM_MINGW32 */