From 671e4430be0687f5aece44f30f82587aacee3bf0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 22 Oct 2023 09:03:58 +0300 Subject: [PATCH] sleep fix --- coreutils/sleep.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/coreutils/sleep.c b/coreutils/sleep.c index e3aab2b..3fe2b30 100644 --- a/coreutils/sleep.c +++ b/coreutils/sleep.c @@ -3,6 +3,15 @@ #include #include +double convert(const char *num) { + if (atof(num) <= 0) { + fprintf(stderr, "sleep: %s < 1\n", num); + exit(1); + } + + return atof(num); +} + int main(const int argc, const char **argv) { if (argc == 1) { printf("sleep [num[m - minute / h - hour / d - days].. ] / [inf (infinity)]\n"); @@ -13,26 +22,26 @@ int main(const int argc, const char **argv) { for (;;) sleep(1); - unsigned int sec = 0; + double sec = 0; for (int i = 1; i < argc; i++) { switch (argv[i][strlen(argv[i]) - 1]) { case 'd': - sec += atoi(argv[i]) * 86400; + sec += convert(argv[i]) * 86400; break; case 'h': - sec += atoi(argv[i]) * 3600; + sec += convert(argv[i]) * 3600; break; case 'm': - sec += atoi(argv[i]) * 60; + sec += convert(argv[i]) * 60; break; default: - sec += atoi(argv[i]); + sec += convert(argv[i]); } } - sleep(sec); + usleep(sec * 1000000); return 0; }