home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / everex-led / part01 / ledtime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-20  |  1.5 KB  |  64 lines

  1. /*
  2.  * ledtime()
  3.  *
  4.  * written by:    Stephen J. Friedl
  5.  *        V-Systems, Inc.
  6.  *        +1 714 545 6442
  7.  *        friedl@mtndew.Tustin.CA.US
  8.  *
  9.  *  Modified to use ioctl() by D'Arcy J.M. Cain (darcy@druid.UUCP)
  10.  *
  11.  *    This program sits in the background and writes the current time to the
  12.  *  led device via ioctl function 1 so the Everex front-panel display always
  13.  *  shows the time.  This is run from inittab.  If you prefer not to run this
  14.  *  change the relevant line in the Makefile.
  15.  *
  16.  *    This is in the public domain (not that you couldn't figure
  17.  *    this out in about two minutes anyway).
  18.  */
  19.  
  20. #include    <stdio.h>
  21. #include    <stdlib.h>
  22. #include    <string.h>
  23. #include    <errno.h>
  24. #include    <unistd.h>
  25. #include    <fcntl.h>
  26. #include    <time.h>
  27.  
  28. int        main(int argc, char **argv)
  29. {
  30.     const char    *led_device = "/dev/ledclock";
  31.     struct tm    *tm;
  32.     time_t        now;        /* current UNIX time            */
  33.     int            led;        /* LED device                    */
  34.     char        buf[40];    /* buffer for the time string    */
  35.  
  36.     if (argc > 1)        /* we can name led device if we want */
  37.         led_device = argv[1];
  38.  
  39.     if ((led = open(led_device, O_WRONLY)) == -1)
  40.     {
  41.         fprintf(stderr, "Can't open %s - %s", led_device, strerror(errno));
  42.         return(1);
  43.     }
  44.  
  45.     for (;;)
  46.     {
  47.         time(&now);
  48.         tm = localtime(&now);
  49.  
  50. #ifdef    CLOCK12
  51.         if (tm->tm_hour > 12)
  52.             tm->tm_hour -= 12;
  53.         else if (!tm->tm_hour)
  54.             tm->tm_hour = 12;
  55.  
  56.         sprintf(buf, "%2d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
  57. #else
  58.         sprintf(buf, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
  59. #endif
  60.         ioctl(led, 1, buf);
  61.         sleep(1);
  62.     }
  63. }
  64.