home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.wwiv.com
/
ftp.wwiv.com.zip
/
ftp.wwiv.com
/
pub
/
PPPBCKP
/
SRC
/
SRC15B80.ZIP
/
NTIME.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1997-12-31
|
2KB
|
103 lines
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <time.h>
#include <string.h>
#include <malloc.h>
extern "C" {
#include "tcp.h"
}
#include "version.h"
extern unsigned _stklen = (8*1024);
#define TCP_TIME 1
#define TIME_PORT 37
#define BASE_TIME 2208988800L
typedef struct {
tcp_Socket *sock;
} Mail_Socket;
long ntime(Mail_Socket *TIME_sock, longword host)
{
int status;
long temptime;
status = 0;
temptime = 0L;
if (!tcp_open(TIME_sock->sock, 0, host, TIME_PORT, NULL)) {
fprintf(stderr, "\n ■ Unable to connect to time server!");
return 1;
}
sock_wait_established(TIME_sock->sock, sock_delay, NULL, &status);
while (1) {
sock_tick(TIME_sock->sock, sock_delay, NULL, &status);
if (sock_dataready(TIME_sock->sock) >= 4) {
sock_read(TIME_sock->sock, (byte *) & temptime, sizeof(long));
temptime = ntohl(temptime);
sock_close(TIME_sock->sock);
sock_wait_closed(TIME_sock->sock, sock_delay, NULL, &status);
break;
}
}
sock_err:
switch (status) {
case 1:
return (temptime);
case -1:
printf("\n ■ Connection timed out!");
return 0;
default:
printf("\n ■ Aborting.");
return 0;
}
}
int main(int argc, char **argv)
{
longword host;
longword newtime;
int ok;
char s[81];
struct date dstruct;
struct time tstruct;
Mail_Socket *TIME_sock = NULL;
if (argc < 2)
return 1;
sock_init();
ok = 1;
if ((host = resolve(argv[1])) != 0uL) {
if ((TIME_sock = (Mail_Socket *) farmalloc(sizeof(Mail_Socket))) == NULL) {
fprintf(stderr, "\n ■ Insufficient memory to create socket... aborting");
return ok;
}
if ((TIME_sock->sock = (tcp_Socket *) farmalloc(sizeof(tcp_Socket))) == NULL) {
fprintf(stderr, "\n ■ Insufficient memory to create socket... aborting");
farfree(TIME_sock);
return ok;
}
if ((newtime = ntime(TIME_sock, host)) != 0uL) {
newtime = newtime - BASE_TIME;
unixtodos(newtime, &dstruct, &tstruct);
settime(&tstruct);
setdate(&dstruct);
sprintf(s, "%s", ctime((time_t *) & newtime));
s[strlen(s) - 1] = '\0';
fprintf(stderr, "clock set to %s.", s);
ok = 0;
} else
fprintf(stderr, " Unable to get time.");
} else
fprintf(stderr, " Could not resolve.");
return ok;
}