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

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