logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 7932f1f9f6bad98421775e7c35d5d6842fc908b8
parent a8e5ae5109b2b4805d7d965f480318b2afe85206
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 16 Sep 2023 06:09:12 +0200

cmd/sleep: handle sscanf not changing errno

Diffstat:

Mcmd/sleep.c23++++++++++++++++-------
1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/cmd/sleep.c b/cmd/sleep.c @@ -23,8 +23,9 @@ strtodur(char *s) int parsed = 0; if(s[0] != '.' && s[0] != ',') { - errno = 0; - if(sscanf(s, "%10ld%n", &dur.tv_sec, &parsed) < 1) + errno = 0; + int ret = sscanf(s, "%10ld%n", &dur.tv_sec, &parsed); + if(ret < 1) { if(errno == 0) { @@ -44,14 +45,22 @@ strtodur(char *s) if(s[0] == '.' || s[0] == ',') { - double fraction = 0.0; + float fraction = 0.0; + if(s[1] == 0) return dur; if(s[0] == ',') s[0] = '.'; parsed = 0; errno = 0; - if(sscanf(s, "%10lf%n", &fraction, &parsed) < 1) + if(sscanf(s, "%10f%n", &fraction, &parsed) < 1) { - perror("sleep: sscanf"); + if(errno == 0) + { + fprintf(stderr, "sleep: Decimal part is not a number: %s\n", s); + } + else + { + perror("sleep: sscanf"); + } exit(EXIT_FAILURE); } @@ -107,7 +116,7 @@ main(int argc, char *argv[]) return 1; } - //fprintf(stderr, "sleep: going to sleep for %ld.%ld seconds\n", dur.tv_sec, dur.tv_nsec); + //fprintf(stderr, "sleep: going to sleep for %ld.%09ld seconds\n", dur.tv_sec, dur.tv_nsec); errno = 0; if(nanosleep(&dur, &dur) < 0) @@ -115,7 +124,7 @@ main(int argc, char *argv[]) if(errno == EINTR) { fprintf(stderr, - "sleep: Interrupted during sleep, still had %ld.%ld seconds remaining\n", + "sleep: Interrupted during sleep, still had %ld.%09ld seconds remaining\n", dur.tv_sec, dur.tv_nsec); }