2 * Copyright (c) 2023 Mateusz Guzik
4 * Redistribution and use in source and binary forms, with
5 * or without modification, are permitted provided that the
6 * following conditions are met:
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the
10 * following disclaimer.
11 * 2. Redistributions in binary form must reproduce the
12 * above copyright notice, this list of conditions and
13 * the following disclaimer in the documentation and/or
14 * other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
17 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
29 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 * _XOPEN_SOURCE hides _SC_NPROCESSORS_* on some BSDs
36 /* #define _XOPEN_SOURCE 700 */
47 #ifndef _SC_NPROCESSORS_CONF
48 #error "_SC_NPROCESSORS_CONF not defined, system incompatible"
51 #ifndef _SC_NPROCESSORS_ONLN
52 #error "_SC_NPROCESSORS_ONLN not defined, system incompatible"
56 usage(const char* progname
)
58 fprintf(stderr
, "usage: %s [-a] [-i #]\n", progname
);
63 main(int argc
, char *argv
[])
74 while ((ch
= getopt(argc
, argv
, "ai:")) >= 0) {
80 ignore
= strtol(optarg
, &endptr
, 10);
81 if (ignore
< 0 || *endptr
|| errno
) {
82 fprintf(stderr
, "%s: bad ignore count: %s\n",
99 cpus
= sysconf(_SC_NPROCESSORS_CONF
);
101 cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
104 fprintf(stderr
, "%s: sysconf: %s", progname
, strerror(errno
));
113 printf("%li\n", cpus
);