home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2164 / rdate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  3.3 KB  |  190 lines

  1. /* rdate --- get the date from the date server on a specified host */
  2. /*           and reset the local system date/time */
  3.  
  4. /* Copyright (c) 1990, W. Keith Pyle, Austin, Texas */
  5.  
  6. #include "rdate.h"
  7.  
  8. extern int optind;
  9. extern int opterr;
  10.  
  11. void change_time();
  12.  
  13. /* ------------------------------------------------------------------------- */
  14.  
  15. main(argc, argv)
  16.  
  17. int argc;
  18. char *argv[];
  19.  
  20. {
  21.     int adjust;
  22.     int argno;
  23.     int display_all;
  24.     int display_only;
  25.     int flag;
  26.     int socket_fd;
  27.     int success;
  28.  
  29.     unsigned long tod;
  30.  
  31.     adjust = FALSE;
  32.     display_all = FALSE;
  33.     display_only = FALSE;
  34.     opterr = 0;
  35.     success = FALSE;
  36.  
  37.     while ((flag = getopt(argc, argv, "adD")) != EOF) {
  38.  
  39.         switch (flag) {
  40.  
  41.             case 'a':
  42.  
  43. #ifdef HAS_ADJTIME
  44.                 adjust = TRUE;
  45. #else
  46.                 (void)fprintf(stderr, "-a not supported on this system\n");
  47.                 exit(1);
  48. #endif
  49.                 break;
  50.             
  51.             case 'd':
  52.  
  53.                 display_only = TRUE;
  54.                 break;
  55.             
  56.             case 'D':
  57.  
  58.                 display_only = TRUE;
  59.                 display_all = TRUE;
  60.                 break;
  61.             
  62.             default:
  63.  
  64.                 (void)fprintf(stderr, "%s: invalid argument: %c\n", argv[0],
  65.                     flag);
  66.                 exit(1);
  67.         }
  68.     }
  69.  
  70.     /* Was a host specified? */
  71.  
  72.     if ((argc - optind) < 1) {
  73.  
  74.         (void)fprintf(stderr, "usage: %s [-ad] host [host] ...\n", argv[0]);
  75.         exit(2);
  76.     }
  77.  
  78.     /* Try the hosts in order until one of them responds */
  79.  
  80.     for (argno = optind ; argno < argc ; argno++) {
  81.  
  82.         /* Open the timed port on the host specified by the argument */
  83.  
  84.         if ((socket_fd = open_port(argv[argno], TIME_PORT)) < 0)
  85.             continue;
  86.  
  87.         /* Get the time value */
  88.  
  89.         if (read_socket(socket_fd, (char *)&tod, sizeof(int)) == sizeof(int)) {
  90.  
  91.             success = TRUE;
  92.  
  93.             /* Convert tod to host byte order and correct it to Unix time */
  94.             /* (timed returns seconds since 00:00:00 1 January 1900 GMT) */
  95.  
  96.             tod = ntohl(tod);
  97.             tod -= 2208988800;
  98.  
  99.             if (!display_only)
  100.                 change_time(tod, adjust);
  101.             
  102.             /* Display the value and where we got it */
  103.  
  104.             (void)printf("%s%s: %s",
  105.                 display_only ? "" : (adjust ? "adjusted to " : "set to "),
  106.                 argv[argno], ctime((time_t *)&tod));
  107.  
  108.             if (!display_all)
  109.                 break;
  110.         }
  111.         
  112.         (void)close(socket_fd);
  113.     }
  114.  
  115.     /* Was an attempt successful? */
  116.  
  117.     if (!success) {
  118.  
  119.         (void)fprintf(stderr, "couldn't get time from any listed host\n");
  120.         exit(1);
  121.     }
  122.  
  123.     /* We're done */
  124.  
  125.     (void)close(socket_fd);
  126.     exit(0);
  127.     /* NOTREACHED */
  128. }
  129.  
  130. /* ------------------------------------------------------------------------- */
  131.  
  132. void
  133. change_time(tod, adjust)
  134.  
  135. unsigned long tod;
  136. int adjust;
  137.  
  138. {
  139. #ifndef SYSV
  140.     struct timeval timeval;
  141.  
  142.     /* Put it in the timeval structure for settimeofday */
  143.  
  144.     timeval.tv_sec = tod;
  145.     timeval.tv_usec = 0;
  146. #endif
  147.  
  148.     /* Are we to adjust the time or just set it? */
  149.  
  150.     if (adjust) {
  151.  
  152. #ifdef HAS_ADJTIME
  153.         struct timeval currtime;
  154.  
  155.         /* Get the local time and determine the adjustment */
  156.  
  157.         if (gettimeofday(&currtime, (struct timezone *)NULL) < 0) {
  158.  
  159.             perror("couldn't get local time of day");
  160.             exit(1);
  161.         }
  162.  
  163.         timeval.tv_sec -= currtime.tv_sec;
  164.         timeval.tv_usec -= currtime.tv_usec;
  165.  
  166.         /* Adjust it */
  167.  
  168.         if (adjtime(&timeval, (struct timeval *)NULL) < 0) {
  169.  
  170.             perror("couldn't adjust time");
  171.             exit(1);
  172.         }
  173. #endif
  174.     }
  175.  
  176.     /* Set the time of day, but leave the timezone alone */
  177.  
  178. #ifndef SYSV
  179.     else if (settimeofday(&timeval, (struct timezone *)NULL) < 0) {
  180. #else
  181.     else if (stime(&tod) < 0) {
  182. #endif
  183.  
  184.         perror("couldn't set time of day");
  185.         exit(1);
  186.     }
  187.  
  188.     return;
  189. }
  190.