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

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