2018-11-07 21:19:47 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <AK/String.h>
|
2018-10-25 13:53:49 +02:00
|
|
|
|
2018-11-07 21:19:47 +01:00
|
|
|
static unsigned parseUInt(const String& str, bool& ok)
|
2018-10-25 13:53:49 +02:00
|
|
|
{
|
2018-11-07 21:19:47 +01:00
|
|
|
unsigned value = 0;
|
|
|
|
|
for (size_t 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 = parseUInt(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);
|
|
|
|
|
}
|
2018-10-25 13:53:49 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|