logo

utils-std

commit: 5656589f5a4538a8acc84fa225020d74bb84dd8e
parent d08803014350168a8299c6b39e4547b00f603d2f
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); }