2 * Copyright (c) 2023 Mateusz Guzik
4 * SPDX-License-Identifier: BSD-2-Clause
8 * This program is intended to be compatible with nproc as found in GNU
11 * In order to maintain that, do not add any features here if they are not
12 * present in said program. If you are looking for anything more advanced you
13 * probably should patch cpuset(1) instead.
16 #include <sys/param.h>
17 #include <sys/cpuset.h>
29 #define OPT_ALL (CHAR_MAX + 1)
30 #define OPT_IGNORE (CHAR_MAX + 2)
31 #define OPT_VERSION (CHAR_MAX + 3)
32 #define OPT_HELP (CHAR_MAX + 4)
34 static struct option long_opts[] = {
35 { "all", no_argument, NULL, OPT_ALL },
36 { "ignore", required_argument, NULL, OPT_IGNORE },
37 { "version", no_argument, NULL, OPT_VERSION },
38 { "help", no_argument, NULL, OPT_HELP },
46 "usage: nproc [--all] [--ignore=count]\n");
50 " nproc --version\n");
61 * GNU variant ships with the --version switch.
63 * While we don't have anything to put there, print something which is
64 * whitespace-compatible with the original. Version number was taken
65 * from coreutils this code is in sync with.
70 printf("nproc (neither_GNU nor_coreutils) 8.32\n");
75 main(int argc, char *argv[])
85 while ((ch = getopt_long(argc, argv, "", long_opts, NULL)) != -1) {
91 ignore = strtonum(optarg, 0, INT_MAX, &errstr);
93 errx(1, "bad ignore count: %s", errstr);
113 cpus = sysconf(_SC_NPROCESSORS_CONF);
118 if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
119 sizeof(mask), &mask) != 0)
120 err(1, "cpuset_getaffinity");
121 cpus = CPU_COUNT(&mask);
129 printf("%u\n", cpus);