home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.wwiv.com
/
ftp.wwiv.com.zip
/
ftp.wwiv.com
/
pub
/
PPPBCKP
/
SRC
/
SRC15B51.ZIP
/
NTIME.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1997-10-17
|
2KB
|
87 lines
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <time.h>
#include <string.h>
extern "C" {
#include "tcp.h"
}
#include "version.h"
#define TCP_TIME 1
#define TIME_PORT 37
#define BASE_TIME 2208988800L
long ntime(longword host)
{
static tcp_Socket telsock;
tcp_Socket *s;
int status;
long temptime;
s = &telsock;
status = 0;
temptime = 0L;
if (!tcp_open(s, 0, host, TIME_PORT, NULL)) {
fprintf(stderr, "\n ■ Unable to connect to %s right now!", host);
return 1;
}
sock_wait_established(s, sock_delay, NULL, &status);
while (1) {
sock_tick(s, &status);
if (sock_dataready(s) >= 4) {
sock_read(s, (byte *) & temptime, sizeof(long));
temptime = ntohl(temptime);
sock_close(s);
sock_wait_closed(s, 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;
char s[80];
struct date dstruct;
struct time tstruct;
if (argc < 2)
return 1;
sock_init();
if ((host = resolve(argv[1])) != 0uL) {
if ((newtime = ntime(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);
return 0;
}
fprintf(stderr, " Unable to get time.");
return 1;
}
fprintf(stderr, " Could not resolve.");
return 1;
}