home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / timer.shar / timer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-14  |  1.6 KB  |  71 lines

  1. /*
  2.  * Timer program for GNU Emacs timer implementation.
  3.  * Copyright (C) 1988, 1991 Kyle E. Jones
  4.  *
  5.  * Verbatim copies of this file may be freely redistributed.
  6.  *
  7.  * Modified versions of this file may be redistributed provided that this
  8.  * notice remains unchanged, the file contains prominent notice of
  9.  * author and time of modifications, and redistribution of the file
  10.  * is not further restricted in any way.
  11.  *
  12.  * This file is distributed `as is', without warranties of any kind.
  13.  */
  14.  
  15. /*
  16.  * #define USG if this is a System V system.
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <signal.h>
  21.  
  22. #define boolean char
  23. #define TRUE 1
  24. #define FALSE 0
  25.  
  26. boolean signaled = FALSE;
  27.  
  28. wakeup()
  29. {
  30. #ifdef USG
  31.     (void) signal(SIGINT, wakeup);
  32.     (void) signal(SIGALRM, wakeup);
  33. #endif
  34.     signaled = TRUE;
  35. }
  36.  
  37. main()
  38. {
  39.     unsigned sleeptime;
  40.     long time(), lastwakeup, now;
  41.     char timebuf[20];
  42.  
  43.     (void) signal(SIGINT, wakeup);
  44.     (void) signal(SIGALRM, wakeup);
  45.  
  46.     (void) time(&lastwakeup);
  47.  
  48.     /*
  49.      * 1. read the number of seconds to sleep frmo stdin.
  50.      * 2. sleep until a SIGALRM or SIGINT arrives.
  51.      * 3. report the number of seconds actually slept to stdout.
  52.      * 4. repeat...
  53.      */
  54.     while (1) {
  55.     /* read the number of seconds we should sleep */
  56.     (void) gets(timebuf);
  57.     sleeptime = atoi(timebuf);
  58.     (void) alarm(sleeptime);
  59.     /* sleep if no signal received since last wakeup */
  60.     if (! signaled)
  61.       (void) pause();
  62.     signaled = FALSE;
  63.     /* report the number of seconds we actually slept */
  64.     (void) time(&now);
  65.     (void) sprintf(timebuf, "%d", now - lastwakeup);
  66.     (void) fputs(timebuf, stdout);
  67.     (void) fflush(stdout);
  68.     lastwakeup = now;
  69.     }
  70. }
  71.