-
Notifications
You must be signed in to change notification settings - Fork 1
/
sleep.c
48 lines (38 loc) · 1.02 KB
/
sleep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "sleep.h"
int custom_sleep(int argc, char *argv[]) {
struct timespec mytime;
if (argc != 1){
fprintf(stderr, "Error: not enough arguments specified.");
return -1;
}
if (parse(&mytime, argv[1]) == -1){
fprintf(stderr, "Error: couldn't parse supplied time \'%s\'.", argv[1]);
return -1;
}
if (nanosleep(&mytime, NULL)){
fprintf(stderr, "Error: could not sleep: %s", strerror(errno));
return -2;
}
return 0;
}
static int parse(struct timespec *mytime, const char *input) {
char *end;
time_t interval;
int scalar;
double val;
val = strtod(input, &end);
if (!*input || errno || val < 0)
return -1;
if (!*end || strcmp(end, "s"))
scalar = 1;
else if (strcmp(end, "m"))
scalar = 60;
else if (strcmp(end, "h"))
scalar = 60 * 60;
else
return -1;
interval = (time_t)val;
mytime->tv_sec = interval * scalar;
mytime->tv_nsec = (val - interval) * 1e9;
return 0;
}