summaryrefslogtreecommitdiff
path: root/Userland/sleep.cpp
blob: dba5f1874a8f6bd45425a0b3775f6bad9bf077b1 (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
36
37
38
39
40
41
42
43
44
45
46
47
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <AK/AKString.h>

static unsigned parse_uint(const String& str, bool& ok)
{
    unsigned value = 0;
    for (int i = 0; i < str.length(); ++i) {
        if (str[i] < '0' || str[i] > '9') {
            ok = false;
            return 0;
        }
        value = value * 10;
        value += str[i] - '0';
    }
    ok = true;
    return value;
}

void handle_sigint(int)
{
}

int main(int argc, char** argv)
{
    if (argc != 2) {
        printf("usage: sleep <seconds>\n");
        return 1;
    }
    bool ok;
    unsigned secs = parse_uint(argv[1], ok);
    if (!ok) {
        fprintf(stderr, "Not a valid number of seconds: \"%s\"\n", argv[1]);
        return 1;
    }
    struct sigaction sa;
    memset(&sa, 0, sizeof(struct sigaction));
    sa.sa_handler = handle_sigint;
    sigaction(SIGINT, &sa, nullptr);
    unsigned remaining = sleep(secs);
    if (remaining) {
        printf("Sleep interrupted with %u seconds remaining.\n", remaining);
    }
    return 0;
}