allow suppressing next suspend
[sleepy.git] / sleepy.c
1 #include <time.h>
2 #include <signal.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <unistd.h>
6
7 time_t last;
8
9 void keep_running(int _) {
10   time(&last);
11   puts("\nmarching on...");
12 }
13
14 int main(void) {
15   puts("stops roughly at 15-minute time boundaries");
16   puts("CTRL+C for no stop in the next 5 minutes");
17   puts("stop with ctrl+\\ (SIGQUIT)");
18   signal(SIGQUIT, exit);
19   signal(SIGINT, keep_running);
20   while(1) {
21     sleep(30);
22     time_t unixtime = time(NULL);
23     if (unixtime - last < 300) continue;
24     struct tm *t = localtime(&unixtime);
25     if (t->tm_hour >= 5 && t->tm_hour != 23) continue;
26     if (t->tm_min % 15 != 0) continue;
27     system("sstandby");
28     last = unixtime;
29   }
30 }