From: git Date: Tue, 28 Apr 2026 15:24:32 +0000 (-0400) Subject: add nproc X-Git-Url: https://git.datadissipation.net/?a=commitdiff_plain;h=2da8ebf1ab081ea9956ecf858a967c75006ef348;p=baseutils.git add nproc --- diff --git a/Makefile b/Makefile index 8f17082..c2c1e70 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ include config.mk -TARGETS = realpath +TARGETS = realpath nproc SRC = $(TARGETS:=.c) BIN = $(TARGETS:=-install) UNI = $(TARGETS:=-uninstall) diff --git a/nproc.1 b/nproc.1 new file mode 100644 index 0000000..2d25c8f --- /dev/null +++ b/nproc.1 @@ -0,0 +1,71 @@ +.\"- +.\" Copyright (c) 2023 Piotr Paweł Stefaniak +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" Redistribution and use in source and binary forms, with +.\" or without modification, are permitted provided that the +.\" following conditions are met: +.\" +.\" 1. Redistributions of source code must retain the above +.\" copyright notice, this list of conditions and the +.\" following disclaimer. +.\" 2. Redistributions in binary form must reproduce the +.\" above copyright notice, this list of conditions and +.\" the following disclaimer in the documentation and/or +.\" other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +.\" CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +.\" WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +.\" PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +.\" COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY +.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +.\" USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +.\" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE +.\" USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY +.\" OF SUCH DAMAGE. +.\" +.Dd April 2026 +.Dt NPROC 1 +.Os +.Sh NAME +.Nm nproc +.Nd print the number of processors +.Sh SYNOPSIS +.Nm +.Op Fl a +.Op Fl i Ar # +.Sh DESCRIPTION +The +.Nm +utility is used to print the number of processors available to the +current process, unless the +.Fl a +flag is specified. +.Pp +The available flags are: +.Bl -tag -width ".Fl i Ar #" +.It Fl a +Count all processors available on the system. +.It Fl i Ar # +The result is decreased by +.Ar # , +but never below 1. +.El +.Sh COMPATIBILITY +This program is +.Sy NOT +compatible with nproc as found in GNU coreutils. +.Sh SEE ALSO +.Xr cpuset 1 +.Sh AUTHORS +.An -nosplit +.An Mateusz Guzik Aq Mt mjg@FreeBSD.org +wrote the original program for FreeBSD and +.An Piotr Paweł Stefaniak Aq Mt pstef@FreeBSD.org +wrote the original of this page. diff --git a/nproc.c b/nproc.c new file mode 100644 index 0000000..921ce7c --- /dev/null +++ b/nproc.c @@ -0,0 +1,109 @@ +/*- + * Copyright (c) 2023 Mateusz Guzik + * + * Redistribution and use in source and binary forms, with + * or without modification, are permitted provided that the + * following conditions are met: + * + * 1. Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * 2. Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + */ +#include +#include +#include +#include +#include +#include +#include + +#ifndef _SC_NPROCESSORS_CONF +#error "_SC_NPROCESSORS_CONF not defined, system incompatible" +#endif + +#ifndef _SC_NPROCESSORS_ONLN +#error "_SC_NPROCESSORS_ONLN not defined, system incompatible" +#endif + +static void +usage(const char* progname) +{ + fprintf(stderr, "usage: %s [-a] [-i #]\n", progname); + exit(2); +} + +int +main(int argc, char *argv[]) +{ + char *progname; + char *endptr; + int ch, all; + long ignore, cpus; + + ignore = 0; + all = 0; + + progname = argv[0]; + while ((ch = getopt(argc, argv, "ai:")) >= 0) { + switch (ch) { + case 'a': + all = 1; + break; + case 'i': + ignore = strtol(optarg, &endptr, 10); + if (ignore < 0 || *endptr || errno) { + fprintf(stderr, "%s: bad ignore count: %s\n", + progname, optarg); + exit(2); + } + break; + default: + usage(progname); + } + } + + argc -= optind; + argv += optind; + + if (argc) + usage(progname); + + if (all) + cpus = sysconf(_SC_NPROCESSORS_CONF); + else + cpus = sysconf(_SC_NPROCESSORS_ONLN); + + if (cpus < 0) { + fprintf(stderr, "%s: sysconf: %s", progname, strerror(errno)); + exit(1); + } + + if (ignore >= cpus) + cpus = 1; + else + cpus -= ignore; + + printf("%li\n", cpus); + + return 0; +}