micro-utils/coreutils/sleep.c
Your Name d3fc1c1f88 fix
2023-10-30 18:09:33 +03:00

48 lines
828 B
C

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