summaryrefslogtreecommitdiffhomepage
path: root/tests/tools/zos-sleep.c
blob: f6272ab49f4a208d2ef96a80fc559ac85dcfb2d7 (plain)
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
/**
 * @file zos-sleep.c
 * @brief A `sleep` that takes fractions, for the z/OS test runs.
 *
 * /bin/sleep on z/OS rejects "0.3" ("Improper sleep interval
 * specification"), and tests/zash/jobs.sh and tests/scripts/parallel.sh
 * order background jobs with sub-second sleeps.  `make zos-check` builds
 * this and puts its directory in front of PATH; Linux never builds it,
 * because its own sleep is already fine.
 */
#include <errno.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char **argv) {
    struct timespec ts = {0, 0};
    if (argc < 2)
        return 1;
    for (int i = 1; i < argc; i++) {
        char *end = NULL;
        double seconds = strtod(argv[i], &end);
        if (end == argv[i] || *end != '\0' || seconds < 0)
            return 1;
        long whole = (long)seconds;
        ts.tv_sec += (time_t)whole;
        ts.tv_nsec += (long)((seconds - (double)whole) * 1e9);
    }
    ts.tv_sec += (time_t)(ts.tv_nsec / 1000000000L);
    ts.tv_nsec %= 1000000000L;
    while (nanosleep(&ts, &ts) < 0) {
        if (errno != EINTR)
            return 1;
    }
    return 0;
}