home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B53.ZIP / NTIME.CPP < prev    next >
C/C++ Source or Header  |  1997-10-17  |  2KB  |  87 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dos.h>
  4. #include <time.h>
  5. #include <string.h>
  6. extern "C" {
  7.   #include "tcp.h"
  8. }
  9. #include "version.h"
  10.  
  11. #define TCP_TIME 1
  12. #define TIME_PORT 37
  13. #define BASE_TIME 2208988800L
  14.  
  15. long ntime(longword host)
  16. {
  17.   static tcp_Socket telsock;
  18.   tcp_Socket *s;
  19.   int status;
  20.   long temptime;
  21.  
  22.   s = &telsock;
  23.   status = 0;
  24.   temptime = 0L;
  25.  
  26.   if (!tcp_open(s, 0, host, TIME_PORT, NULL)) {
  27.     fprintf(stderr, "\n ■ Unable to connect to %s right now!", host);
  28.     return 1;
  29.   }
  30.   sock_wait_established(s, sock_delay, NULL, &status);
  31.  
  32.   while (1) {
  33.     sock_tick(s, &status);
  34.     if (sock_dataready(s) >= 4) {
  35.       sock_read(s, (byte *) & temptime, sizeof(long));
  36.       temptime = ntohl(temptime);
  37.       sock_close(s);
  38.       sock_wait_closed(s, sock_delay, NULL, &status);
  39.       break;
  40.     }
  41.   }
  42.  
  43. sock_err:
  44.   switch (status) {
  45.     case 1:
  46.       return (temptime);
  47.     case -1:
  48.       printf("\n ■ Connection timed out!");
  49.       return 0;
  50.     default:
  51.       printf("\n ■ Aborting.");
  52.       return 0;
  53.   }
  54. }
  55.  
  56.  
  57. int main(int argc, char **argv)
  58. {
  59.   longword host;
  60.   longword newtime;
  61.   char s[80];
  62.   struct date dstruct;
  63.   struct time tstruct;
  64.  
  65.   if (argc < 2)
  66.     return 1;
  67.  
  68.   sock_init();
  69.  
  70.   if ((host = resolve(argv[1])) != 0uL) {
  71.     if ((newtime = ntime(host)) != 0uL) {
  72.       newtime = newtime - BASE_TIME;
  73.       unixtodos(newtime, &dstruct, &tstruct);
  74.       settime(&tstruct);
  75.       setdate(&dstruct);
  76.       sprintf(s, "%s", ctime((time_t *) & newtime));
  77.       s[strlen(s) - 1] = '\0';
  78.       fprintf(stderr, "clock set to %s.", s);
  79.       return 0;
  80.     }
  81.     fprintf(stderr, " Unable to get time.");
  82.     return 1;
  83.   }
  84.   fprintf(stderr, " Could not resolve.");
  85.   return 1;
  86. }
  87.