home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume4 / clock / clock.c next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  5.6 KB  |  236 lines

  1. /*   Copyright (c) 1988 by George M. Sipe.  All rights reserved.
  2.  
  3. This software may only be redistributed without fee and without any
  4. other form of monetary gain (including sold, rented, leased, or
  5. traded), unless the express written permission of the copyright holder
  6. is obtained in advance.
  7.  
  8. This copyright notice must be reproduced in its entirety on all copies
  9. of this software.  Further, acknowledgment of the authorship of this
  10. software must not be removed from its current or derived
  11. documentation.
  12.  
  13. No expressed or implied warranty is made for this software.  No party
  14. connected with this software assumes any liability or responsibility
  15. for its use, the correctness of its operation, or its fitness for any
  16. purpose.
  17.  
  18. Any distributor of copies of this software shall grant the recipient
  19. permission for further redistribution as permitted by this notice.
  20.  
  21. Permission is hereby granted to copy, reproduce, redistribute and
  22. otherwise use this software as long as the conditions above are
  23. strictly adhered to.
  24.  
  25.     NOTE:  This software was originally written by Jim Earenluindil
  26.     Trethewey and modified by George M. Sipe (rebel!george) to the
  27.     point where Jim would no longer recognize it.  No copyright
  28.     notices were embodied in the original net distribution.
  29. */
  30.  
  31. #include <stdio.h>
  32. #include <curses.h>
  33. #include <signal.h>
  34. #include <sys/types.h>
  35.  
  36. #ifndef    MSDOS
  37.  
  38. #ifdef    BSD
  39.  
  40. #include <strings.h>
  41. #include <sys/ioctl.h>
  42. #include <sys/time.h>
  43. extern    time_t time();
  44.  
  45. #else    /* !BSD */
  46.  
  47. #include <string.h>
  48. #include <time.h>
  49. extern    long time();
  50. extern    unsigned int sleep();
  51. extern    void exit();
  52.  
  53. #endif    /* !BSD */
  54.  
  55. extern    struct tm *localtime();
  56. extern    unsigned int alarm();
  57. extern  void pr_big();
  58. extern  void pr_colon();
  59. extern  void pr_space();
  60. static  int on_intr();
  61. static  void big_nums();
  62. static  void time_update();
  63.  
  64. #else    /* MSDOS */
  65.  
  66. #include <stdlib.h>
  67. #undef    getch
  68. #include <conio.h>
  69. #include <string.h>
  70. #include <time.h>
  71. #include <local.h>
  72. extern  int main(int argc,char **argv);
  73. extern  void pr_big(int num,int col);
  74. extern  void pr_colon(int col);
  75. extern  void pr_space(void);
  76. static  int on_intr(void);
  77. static  void big_nums(int hrs,int mins);
  78. static  void time_update(void);
  79.  
  80. #endif    /* MSDOS */
  81.  
  82. #define    SYSLINE    0        /* line to put system name on */
  83. #define    DIGITS    3        /* line to put digits on */
  84. #define    DATETM    19        /* line to put full date and time on */
  85.  
  86. static int BASE;        /* base line for output */
  87. static struct tm *loc_time;    /* local time structure */
  88. static unsigned int refresh_rate;/* seconds between updates */
  89.  
  90. static char myname[40];        /* string to store system name in */
  91.  
  92. WINDOW *scr;            /* screen where large numbers displayed */
  93. int curr_col;            /* current center of colon column */
  94. int NBASE;            /* numeric base line for output */
  95.  
  96. static int on_intr()
  97. {
  98.     refresh_rate = 0;
  99.     alarm(0);
  100. #ifdef    BSD
  101.     kill(0, SIGALRM);
  102. #endif
  103.     return(0);
  104. }
  105.  
  106. static void big_nums(hrs, mins)
  107. int hrs, mins;
  108. {
  109.     static int x_hr_h = -1, x_hr_l = -1;
  110.     static int x_min_h = -1, x_min_l = -1, x_indent = -1;
  111.     int hr_h, hr_l, min_h, min_l, indent;
  112.  
  113.     hr_h = hrs / 10;
  114.     hr_l = hrs % 10;
  115.     min_h = mins / 10;
  116.     min_l = mins % 10;
  117.     indent = (hr_h == 0) ? 16 : 24;
  118.     if (indent != x_indent) {
  119.         pr_space();
  120.         x_hr_h = x_hr_l = x_min_h = x_min_l = -1;
  121.         pr_colon(indent + 15);
  122.         x_indent = indent;
  123.     }
  124.     if ((hr_h != 0) && (hr_h != x_hr_h)) {
  125.         pr_big(hr_h, 9);
  126.         x_hr_h = hr_h;
  127.     }
  128.     if (hr_l != x_hr_l) {
  129.         pr_big(hr_l, indent);
  130.         x_hr_l = hr_l;
  131.     }
  132.     if (min_h != x_min_h) {
  133.         pr_big(min_h, indent + 21);
  134.         x_min_h = min_h;
  135.     }
  136.     if (min_l != x_min_l) {
  137.         pr_big(min_l, indent + 36);
  138.         x_min_l = min_l;
  139.     }
  140. }
  141.  
  142. static void time_update()
  143. {
  144.     time_t clock;
  145.     char time_str[43];
  146.     int t_hour, t_min, t_sec;
  147.     int delta;
  148.     static int colon = 15;
  149.  
  150.     wmove(scr, BASE, 0);
  151.     wclrtoeol(scr);
  152.     wmove(scr, BASE, (COLS / 2 - strlen(myname) / 2));
  153.     wprintw(scr, myname);
  154.     wmove(scr, BASE + DATETM, 0);
  155.     wclrtoeol(scr);
  156.     (void) time(&clock);
  157.     (void) strcpy(time_str, ctime(&clock));
  158.     wmove(scr, BASE + DATETM, (COLS / 2 - strlen(time_str) / 2));
  159.     wprintw(scr, time_str);
  160.  
  161.     loc_time = localtime(&clock);
  162.     t_hour = loc_time->tm_hour;
  163.     if (t_hour > 12) t_hour = t_hour - 12;
  164.     if (t_hour == 0) t_hour = 12;
  165.     t_min = loc_time->tm_min;
  166.     big_nums(t_hour, t_min);
  167.     t_sec = loc_time->tm_sec;
  168.     colon = (colon == 9) ? 4 : 9;
  169.     wmove(scr, NBASE + colon, curr_col);
  170.     wrefresh(scr);
  171.     if (refresh_rate % 60 == 0)
  172.         delta = refresh_rate
  173.             - ((t_min % (refresh_rate / 60)) * 60)
  174.             - (t_sec % 60);
  175.     else
  176.         delta = refresh_rate - (t_sec % refresh_rate);
  177.     if (delta > 0) (void) sleep((unsigned) delta);
  178.     else (void) sleep(refresh_rate);
  179. #ifdef    MSDOS
  180.     while (kbhit()) (void) getch();
  181. #endif
  182. }
  183.  
  184. int main(argc, argv)
  185. int argc;
  186. char *argv[];
  187. {
  188. #ifdef BSD
  189.     struct sgttyb tty_old, tty_new;
  190. #endif
  191. #ifdef    MSDOS
  192.     int brk_old;
  193.  
  194.     brk_old = setbreak(TRUE);
  195.     if (argc == 1) refresh_rate = 1;
  196. #else
  197.     if (argc == 1) refresh_rate = 60;
  198. #endif
  199.     else if (argc == 2) refresh_rate = atoi(argv[1]);
  200.     else {
  201.         (void) printf("Usage: %s [refresh_rate]\n", argv[0]);
  202.         exit(1);
  203.     }
  204.     if (refresh_rate < 1) refresh_rate = 1;
  205. #ifdef BSD
  206.     if (ioctl(1, TIOCGETP, &tty_old) < 0) perror("ioctl TIOCGETP");
  207.     tty_new = tty_old;
  208.     tty_new.sg_flags |= XTABS;
  209.     if (ioctl(1, TIOCSETP, &tty_new) < 0) perror("ioctl TIOCSETP");
  210. #endif
  211.     initscr();
  212.     BASE = LINES - 21 + SYSLINE;
  213.     NBASE = BASE + DIGITS;
  214.     (void) gethostname(myname,sizeof(myname));
  215.     noecho();
  216.     nonl();
  217.     scr = newwin(LINES, COLS, 0, 0);
  218.     scrollok(scr, FALSE);
  219.     leaveok(scr, FALSE);
  220.     wclear(scr);
  221.     (void) signal(SIGINT, on_intr);
  222.     for ( ; refresh_rate; ) time_update();
  223.     wclear(scr);
  224.     wmove(scr, LINES - 1, 0);
  225.     wclrtoeol(scr);
  226.     wrefresh(scr);
  227.     endwin();
  228. #ifdef BSD
  229.     if (ioctl(1, TIOCSETP, &tty_old) < 0) perror("ioctl TIOCSETP");
  230. #endif
  231. #ifdef    MSDOS
  232.     (void) setbreak(brk_old);
  233. #endif
  234.     return(0);
  235. }
  236.