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