home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC15B44.ZIP / WATTSRC.ZIP / APPS / NTIME.C < prev    next >
Text File  |  1994-11-28  |  4KB  |  148 lines

  1.  
  2. /******************************************************************************
  3.     NTIME - set dos clock from internet
  4.             see RFC 868
  5.  
  6.     Copyright (C) 1991, University of Waterloo
  7.     portions Copyright (C) 1990, National Center for Supercomputer Applications
  8.  
  9.     This program is free software; you can redistribute it and/or modify
  10.     it, but you may not sell it.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but without any warranty; without even the implied warranty of
  14.     merchantability or fitness for a particular purpose.
  15.  
  16.         Erick Engelke                   or via E-Mail
  17.         Faculty of Engineering
  18.         University of Waterloo          Erick@development.watstar.uwaterloo.ca
  19.         200 University Ave.,
  20.         Waterloo, Ont., Canada
  21.         N2L 3G1
  22.  
  23. ******************************************************************************/
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <dos.h>
  28. #include <time.h>
  29. #include <tcp.h>
  30.  
  31. #define TCP_TIME 1
  32.  
  33.  
  34. /* Notes:
  35.  * The time is the number of seconds since 00:00 (midnight) 1 January 1900
  36.  * GMT, such that the time 1 is 12:00:01 am on 1 January 1900 GMT; this
  37.  * base will serve until the year 2036.
  38.  *
  39.  * For example:
  40.  *
  41.  *     2,208,988,800L corresponds to 00:00  1 Jan 1970 GMT, start of UNIX time
  42.  *
  43.  */
  44.  
  45. #define TIME_PORT 37
  46. #define BASE_TIME 2208988800L
  47.  
  48. /*
  49.  * ntime() given the host address, returns an Internet based time, not an
  50.  *         UNIX or DOS time.  The UNIX time may be derived by subtracting
  51.  *       BASE_TIME from the returned value.
  52.  */
  53.  
  54. long ntime( longword host )
  55. {
  56.     static tcp_Socket telsock;
  57.     tcp_Socket *s;
  58.     int status;
  59.     long temptime;
  60.  
  61.     s = &telsock;
  62.     status = 0;
  63.     temptime = 0L;
  64.  
  65. #ifdef TCP_TIME
  66.     if (!tcp_open( s, 0, host, TIME_PORT, NULL )) {
  67.     puts("Sorry, unable to connect to that machine right now!");
  68.     return( 1 );
  69.     }
  70.     printf("waiting...\r");
  71.     sock_wait_established(s, sock_delay , NULL, &status);
  72.     printf("connected \n");
  73. #else
  74.     if (!udp_open( s, 0, host, TIME_PORT, NULL )) {
  75.     puts("Sorry, unable to connect to that machine right now!");
  76.     return( 1 );
  77.     }
  78.     sock_write( s, "\n", 1 );
  79. #endif TCP_TIME
  80.  
  81.     while ( 1 ) {
  82.     sock_tick( s, &status );
  83.  
  84.     if (sock_dataready( s ) >= 4 ) {
  85.         sock_read( s, (byte *)&temptime, sizeof( long ));
  86.  
  87.         temptime = ntohl( temptime );    /* convert byte orderring */
  88.         sock_close( s );
  89. #if 1
  90. return( temptime );             /* TODO: ??? */
  91. #else
  92.         sock_wait_closed( s, sock_delay, NULL, &status );
  93.         break;
  94. #endif
  95.     }
  96.     }
  97.  
  98. sock_err:
  99.     switch (status) {
  100.     case 1 : /* foreign host closed */
  101.          return( temptime );
  102.     case -1: /* timeout */
  103.          printf("\nConnection timed out!");
  104.          return( 0 );
  105.     default: printf("Aborting");
  106.          return( 0 );
  107.     }
  108. }
  109.  
  110.  
  111. int main(int argc, char **argv )
  112. {
  113.     longword host;
  114.     longword newtime;
  115.     longword addminutes;
  116.     struct date dstruct;
  117.     struct time tstruct;
  118.  
  119.     if (argc < 2) {
  120.     puts("   DAYTIME  server  [addminutes]");
  121.     exit( 3 );
  122.     }
  123.  
  124.     if (argc == 3 )
  125.     addminutes = atol( argv[2] ) * 60L;
  126.     else
  127.     addminutes = 0L;
  128.  
  129.     sock_init();
  130.  
  131.     if ( (host = resolve( argv[1])) != 0uL ) {
  132.     if ( (newtime = ntime( host )) != 0uL ) {
  133.         newtime = newtime - BASE_TIME + addminutes;    /* now in UNIX format */
  134.         unixtodos( newtime, &dstruct, &tstruct );
  135.         settime( &tstruct );
  136.         setdate( &dstruct );
  137.         printf("Time set to %s", ctime( (time_t *)&newtime ));
  138.         exit( 0 );
  139.     }
  140.     printf("Unable to get the time from that host\n");
  141.     exit( 1 );
  142.     }
  143.  
  144.     printf("Could not resolve host '%s'\n", argv[1]);
  145.     exit( 3 );
  146.     return (0);  /* not reached */
  147. }
  148.