/* * RDATE.NLM - an implementation of the RDATE command found on * many unix systems; Inspired by Brad Clements' RDATE.NLM, except: * - produces no ouput, except in debug mode * - does not unload itself if we don't get a reply * - Simpler interface */ #include #include #include #include /* timeval */ #include #include /* FIONBIO */ #include #include #include #include #include #include #include #define DEFAULT_SLEEP 60*5 #define TIME_PORT 37 #define TIME_OUT 1 #define MAXDIFF 2 void RdateEnd(void); char *myname,*pmy; int debug; int Socket; int iTimeout = DEFAULT_SLEEP; struct sockaddr_in server_sockaddr; void Usage(void) { ConsolePrintf("usage: %s -t \r\n", myname); ConsolePrintf("\tdefault interval is %d seconds\r\n",DEFAULT_SLEEP); exit(1); }; main(argc, argv) int argc; char **argv; { int rc, arg, i; struct timeval tv; fd_set rfds; time_t rtime, delta, now; struct tm *ptm; char tbuf[60]; debug=0; /* no debugging */ Socket=0; /* so Exit routine does not close a random socket */ /* no screen, use the system console */ i=CreateScreen("System Console",0); SetCurrentScreen(i); DisplayScreen(i); /* tidy up if we exit() or are unloaded */ ConsolePrintf("RDATE.NLM version %s Copyright (c) 1993 University of Salford\r\n",VERSION); atexit(RdateEnd); /* parse the command line */ myname = *argv; if (pmy=strrchr(myname,'\\') ) myname=pmy+1; else if (pmy=strrchr(myname,':')) myname=pmy+1; for (i=1;iMAXDIFF) { if (debug) { ptm=localtime(&now); strftime(tbuf,sizeof(tbuf),"%A %d %b, %Y %T %Z",ptm); ConsolePrintf("\r\nTime was %s\r\n",tbuf); }; ptm=localtime(&rtime); strftime(tbuf,sizeof(tbuf),"%A %d %b, %Y %T %Z",ptm); ConsolePrintf("Time set to %s (%d seconds change)\r\n",tbuf,delta); SetFileServerDateAndTime( ptm->tm_year, ptm->tm_mon+1, ptm->tm_mday, ptm->tm_hour ,ptm->tm_min, ptm->tm_sec); if (debug) { now=time(NULL); ptm=localtime(&now); strftime(tbuf,sizeof(tbuf),"%A %d %b, %Y %T %Z",ptm); ConsolePrintf("Time is now %s\r\n",tbuf); }; }; /* SLEEP for iTimeout Seconds */ tv.tv_sec = iTimeout; tv.tv_usec = 0; rc = select(FD_SETSIZE, (fd_set *) NULL, (fd_set *) NULL, (fd_set *) NULL, &tv); if (rc == -1) { perror("select"); exit(1); } /* collect garbage on the net... */ for (;;) { rc = recv(Socket,(char *) &rtime, sizeof (rtime), 0); if ((rc==-1) & (errno==EWOULDBLOCK)) break; if (rc == -1 ) { perror("recv"); exit(1); }; ConsolePrintf("%s spurious time response\r\n",myname); }; if (debug) ConsolePrintf("+"); } } void RdateEnd(void) { int rc; if (Socket) { rc = close(Socket); if (rc == -1) { perror("close"); exit(1); }; }; ConsolePrintf("%s: NLM Exiting\r\n", myname); }