-
Notifications
You must be signed in to change notification settings - Fork 0
/
loopever.c
75 lines (68 loc) · 1.21 KB
/
loopever.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "loopever.h"
int main(int argc, char **argv)
{
if (argc == 1)
{
loop();
}
else
{
struct timespec sleeptime;
parseArg(argc, argv, &sleeptime);
loopWithSleep(&sleeptime);
}
return 0;
}
void loop()
{
while (1)
{
}
}
void loopWithSleep(struct timespec *sleepTime)
{
while (1)
{
nanosleep(sleepTime, NULL);
}
}
void parseArg(int argc, char **argv, struct timespec *result)
{
// check number of argument
if (argc < 2 || argc > 3)
{
fprintf(stderr, "Error: not enough arguments\n");
exit(1);
}
// convert first argument to integer
char *endPtr = NULL;
errno = 0;
const int sleepsec = strtol(*(argv + 1), &endPtr, 10);
if (*endPtr != '\0' || errno != 0)
{
fprintf(stderr, "Failed to strtol sleepsec.\n");
exit(1);
}
result->tv_sec = sleepsec;
// convert second argument to integer
if (argc > 2)
{
const long sleepnsec = strtol(*(argv + 2), &endPtr, 10);
if (*endPtr != '\0' ||
errno != 0 ||
sleepnsec < MIN_SLEEP_NANO_SEC ||
sleepnsec > MAX_SLEEP_NANO_SEC)
{
fprintf(stderr, "Failed to strtol sleepnsec in the range\n");
exit(1);
}
result->tv_nsec = sleepnsec;
}
else
{
result->tv_nsec = 0;
}
}