Fix the fix to really close 181
[sxemacs] / lib-src / wakeup.c
1 /* Program to produce output at regular intervals.  */
2
3 #include <config.h>
4
5 #if __STDC__ || defined(STDC_HEADERS)
6 #include <stdlib.h>
7 #ifdef HAVE_UNISTD_H
8 #include <unistd.h>
9 #endif
10 #endif
11
12 #include <stdio.h>
13 #include <sys/types.h>
14
15 #ifdef TIME_WITH_SYS_TIME
16 #include <sys/time.h>
17 #include <time.h>
18 #else
19 #ifdef HAVE_SYS_TIME_H
20 #include <sys/time.h>
21 #else
22 #include <time.h>
23 #endif
24 #endif
25
26 int main(int argc, char *argv[])
27 {
28         int period = 60;
29
30         if (argc > 1)
31                 period = atoi(argv[1]);
32
33         while (1) {
34                 /* Make sure wakeup stops when Emacs goes away.  */
35                 if (getppid() == 1)
36                         return 0;
37                 printf("Wake up!\n");
38                 /* If fflush fails, then our stdout pipe is broken. */
39                 if (fflush(stdout) != 0)
40                         return 0;
41                 /* If using a period of 60, produce the output when the minute
42                    changes. */
43                 if (period == 60) {
44                         time_t when;
45                         struct tm *tp;
46                         time(&when);
47                         tp = localtime(&when);
48                         sleep(60 - tp->tm_sec);
49                 } else
50                         sleep(period);
51         }
52 }