add -Wpedantic to default CFLAGS
[baseutils.git] / nproc.c
1 /*-
2 * Copyright (c) 2023 Mateusz Guzik
3 *
4 * Redistribution and use in source and binary forms, with
5 * or without modification, are permitted provided that the
6 * following conditions are met:
7 *
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.
15 *
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
30 * OF SUCH DAMAGE.
31 */
32
33 /*
34 * _XOPEN_SOURCE hides _SC_NPROCESSORS_* on some BSDs
35 */
36 /* #define _XOPEN_SOURCE 700 */
37
38 #include <errno.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 #include <getopt.h>
46
47 #ifndef _SC_NPROCESSORS_CONF
48 #error "_SC_NPROCESSORS_CONF not defined, system incompatible"
49 #endif
50
51 #ifndef _SC_NPROCESSORS_ONLN
52 #error "_SC_NPROCESSORS_ONLN not defined, system incompatible"
53 #endif
54
55 static void
56 usage(const char* progname)
57 {
58 fprintf(stderr, "usage: %s [-a] [-i #]\n", progname);
59 exit(2);
60 }
61
62 int
63 main(int argc, char *argv[])
64 {
65 char *progname;
66 char *endptr;
67 int ch, all;
68 long ignore, cpus;
69
70 ignore = 0;
71 all = 0;
72
73 progname = argv[0];
74 while ((ch = getopt(argc, argv, "ai:")) >= 0) {
75 switch (ch) {
76 case 'a':
77 all = 1;
78 break;
79 case 'i':
80 ignore = strtol(optarg, &endptr, 10);
81 if (ignore < 0 || *endptr || errno) {
82 fprintf(stderr, "%s: bad ignore count: %s\n",
83 progname, optarg);
84 exit(2);
85 }
86 break;
87 default:
88 usage(progname);
89 }
90 }
91
92 argc -= optind;
93 argv += optind;
94
95 if (argc)
96 usage(progname);
97
98 if (all)
99 cpus = sysconf(_SC_NPROCESSORS_CONF);
100 else
101 cpus = sysconf(_SC_NPROCESSORS_ONLN);
102
103 if (cpus < 0) {
104 fprintf(stderr, "%s: sysconf: %s", progname, strerror(errno));
105 exit(1);
106 }
107
108 if (ignore >= cpus)
109 cpus = 1;
110 else
111 cpus -= ignore;
112
113 printf("%li\n", cpus);
114
115 return 0;
116 }