logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 4087f6a16718ffe7ca31ec4909ad7701e925b84b
parent 83e4725713546b8b662811d5051011bf0a73c105
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Wed, 28 Jun 2023 21:05:18 +0200

coreutils.txt: tee and touch are Done

Diffstat:

Acmd/nproc.c51+++++++++++++++++++++++++++++++++++++++++++++++++++
Mcoreutils.txt6+++---
2 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/cmd/nproc.c b/cmd/nproc.c @@ -0,0 +1,51 @@ +// Collection of Unix tools, comparable to coreutils +// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only + +#define _POSIX_C_SOURCE 200809L +#include <stdio.h> // printf +#include <unistd.h> // sysconf, getopt, opt* + +void +usage() +{ + fprintf(stderr, "Usage: nproc [-a]\n"); +} + +int +main(int argc, char *argv[]) +{ + // _SC_NPROCESSORS_CONF &_SC_NPROCESSORS_ONLN aren't in POSIX yet but have been accepted + // https://www.austingroupbugs.net/view.php?id=339 + int target = _SC_NPROCESSORS_ONLN; + + int c; + while((c = getopt(argc, argv, ":a")) != EOF) + { + switch(c) + { + case 'a': // all processors + target = _SC_NPROCESSORS_CONF; + break; + case ':': + fprintf(stderr, "nproc: Error: Missing operand for option: '-%c'\n", optopt); + usage(); + return 1; + case '?': + fprintf(stderr, "nproc: Error: Unrecognised option: '-%c'\n", optopt); + usage(); + return 1; + } + } + + long np = sysconf(target); + if(np < 0) + { + perror("sysconf"); + return 1; + } + + printf("%ld\n", np); + + return 0; +} diff --git a/coreutils.txt b/coreutils.txt @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +# SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> # SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only [: ?, see test arch: No @@ -85,10 +85,10 @@ sum: No sync: Done tac: Todo tail: ? -tee: Todo +tee: Done test: ? timeout: ? -touch: Todo (futimens(3p)) +touch: Done tr: Todo true: Done truncate: Todo