sleep fix

This commit is contained in:
Your Name 2023-10-22 09:03:58 +03:00
parent a60d0f6420
commit 671e4430be

View File

@ -3,6 +3,15 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
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) { int main(const int argc, const char **argv) {
if (argc == 1) { if (argc == 1) {
printf("sleep [num[m - minute / h - hour / d - days].. ] / [inf (infinity)]\n"); 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 (;;) for (;;)
sleep(1); sleep(1);
unsigned int sec = 0; double sec = 0;
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
switch (argv[i][strlen(argv[i]) - 1]) { switch (argv[i][strlen(argv[i]) - 1]) {
case 'd': case 'd':
sec += atoi(argv[i]) * 86400; sec += convert(argv[i]) * 86400;
break; break;
case 'h': case 'h':
sec += atoi(argv[i]) * 3600; sec += convert(argv[i]) * 3600;
break; break;
case 'm': case 'm':
sec += atoi(argv[i]) * 60; sec += convert(argv[i]) * 60;
break; break;
default: default:
sec += atoi(argv[i]); sec += convert(argv[i]);
} }
} }
sleep(sec); usleep(sec * 1000000);
return 0; return 0;
} }