logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: cb63c7b5437116d5f79c83c400f2e79aa324b75d
parent d3e79ea42c4d17fde77f719c79d4763ddc74dad8
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 20 Sep 2024 03:33:48 +0200

cmd/nproc: unify error message formatting

Diffstat:

Mcmd/nproc.c16++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/cmd/nproc.c b/cmd/nproc.c @@ -2,12 +2,17 @@ // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> // SPDX-License-Identifier: MPL-2.0 -// _SC_NPROCESSORS_CONF &_SC_NPROCESSORS_ONLN aren't in POSIX yet but have been accepted +// _SC_NPROCESSORS_CONF &_SC_NPROCESSORS_ONLN got added in POSIX.1-2024 // https://www.austingroupbugs.net/view.php?id=339 #define _DEFAULT_SOURCE +#define _POSIX_C_SOURCE 202405L +#include <errno.h> #include <stdio.h> // printf +#include <string.h> // strerror #include <unistd.h> // sysconf, getopt, opt* +const char *argv0 = "nproc"; + static void usage(void) { @@ -19,6 +24,7 @@ main(int argc, char *argv[]) { // currently available int target = _SC_NPROCESSORS_ONLN; + const char *target_str = "_SC_NPROCESSORS_ONLN"; int c = -1; while((c = getopt(argc, argv, ":a")) != -1) @@ -27,13 +33,14 @@ main(int argc, char *argv[]) { case 'a': // can be made available target = _SC_NPROCESSORS_CONF; + target_str = "_SC_NPROCESSORS_CONF"; break; case ':': - fprintf(stderr, "nproc: Error: Missing operand for option: '-%c'\n", optopt); + fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); return 1; case '?': - fprintf(stderr, "nproc: Error: Unrecognised option: '-%c'\n", optopt); + fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; } @@ -42,7 +49,8 @@ main(int argc, char *argv[]) long np = sysconf(target); if(np < 0) { - perror("nproc: sysconf"); + fprintf( + stderr, "%s: error: Failed getting sysconf '%s': %s\n", argv0, target_str, strerror(errno)); return 1; }