logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/
commit: d591630ffa1697e170790902df8bd17bd86e3d59
parent 25b9098a43a61c4d8cfb5c2a6d5998b948e20230
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Wed, 11 Dec 2024 20:58:33 +0100

lib/strtodur: Use argv0 for error prefix instead of "strtodur: "

Diffstat:

Mcmd/sleep.c2++
Mcmd/timeout.c2++
Mlib/strtodur.c48+++++++++++++++++++++++++++++-------------------
Mlib/strtodur.h3+++
Mtest-lib/t_strtodur.c1+
5 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/cmd/sleep.c b/cmd/sleep.c @@ -9,6 +9,8 @@ #include <stdio.h> // fprintf, perror #include <time.h> // nanosleep +const char *argv0 = "sleep"; + int main(int argc, char *argv[]) { diff --git a/cmd/timeout.c b/cmd/timeout.c @@ -27,7 +27,9 @@ #define CMD_EXIT_KILL 137 extern char **environ; + pid_t child = 0; +const char *argv0 = "timeout"; static void handle_sigchld(int sig) diff --git a/lib/strtodur.c b/lib/strtodur.c @@ -6,8 +6,11 @@ #include "strtodur.h" #include <assert.h> -#include <errno.h> // errno -#include <stdio.h> // fprintf, perror, sscanf +#include <errno.h> // errno +#include <stdio.h> // fprintf, perror, sscanf +#include <string.h> // strerror + +const char *errstr_nan = "Not a number"; int strtodur(char *s, struct timespec *dur) @@ -21,18 +24,19 @@ strtodur(char *s, struct timespec *dur) if(s[0] != '.' && s[0] != ',') { int parsed = 0; - assert(errno == 0); + + errno = 0; if(sscanf(s, "%10f%n", &in, &parsed) < 1) { - if(errno == 0) - { - fprintf(stderr, "strtodur: error: Not a number: %s\n", s); - } - else + const char *errstr = errstr_nan; + if((errno = !0)) { - perror("strtodur: error: sscanf"); + errstr = strerror(errno); errno = 0; } + + fprintf( + stderr, "%s: error: strtodur failed scanning '%s' as a number: %s\n", argv0, s, errstr); return -1; } @@ -45,18 +49,22 @@ strtodur(char *s, struct timespec *dur) if(s[0] == ',') s[0] = '.'; int parsed = 0; - assert(errno == 0); + + errno = 0; if(sscanf(s, "%10f%n", &fraction, &parsed) < 1) { - if(errno == 0) - { - fprintf(stderr, "strtodur: error: Decimal part is not a number: %s\n", s); - } - else + const char *errstr = errstr_nan; + if((errno = !0)) { - perror("strtodur: error: sscanf"); + errstr = strerror(errno); errno = 0; } + + fprintf(stderr, + "%s: error: strtodur failed scanning decimal part '%s' as a number: %s\n", + argv0, + s, + errstr); return -1; } @@ -68,8 +76,10 @@ strtodur(char *s, struct timespec *dur) { if(s[1] != '\0') { - fprintf( - stderr, "strtodur: error: suffix '%s' is too long, should be only one character\n", s); + fprintf(stderr, + "%s: error: duration suffix '%s' is too long, should be only one character\n", + argv0, + s); return -1; } @@ -87,7 +97,7 @@ strtodur(char *s, struct timespec *dur) in *= 24 * 60 * 60; break; default: - fprintf(stderr, "strtodur: error: Unknown suffix '%c'\n", s[0]); + fprintf(stderr, "%s: error: Unknown duration suffix '%c'\n", argv0, s[0]); return -1; } } diff --git a/lib/strtodur.h b/lib/strtodur.h @@ -3,4 +3,7 @@ // SPDX-License-Identifier: MPL-2.0 #include <time.h> // timespec + +extern const char *argv0; + int strtodur(char *s, struct timespec *dur); diff --git a/test-lib/t_strtodur.c b/test-lib/t_strtodur.c @@ -8,6 +8,7 @@ #include <assert.h> #include <stdio.h> // printf +const char *argv0 = "t_strtodur"; int counter = 0; int err = 0;