Category : Communication (modem) tools and utilities
Archive   : NAVYTIME.ZIP
Filename : NAVYTIME.C

 
Output of file : NAVYTIME.C contained in archive : NAVYTIME.ZIP
/* navytime -- get time from usnaval observatory */

#include
#include
#include
#include
#include
#include
#include "comm.h"
#include "timer.h"

#define RCFILE "c:\\etc\\navytime.rc"
#define MAXCMD 128 /* Max size command to modem */
#define LINESIZE 32 /* Max line from modem */
#define MAXRESPONSE 16 /* Max number modem response strings */
#define COMINIT 0x9a /* 1200 baud, even, 1, 8 */

#define SECONDS_PER_MINUTE 60l
#define SECONDS_PER_HOUR (60l * 60l)
#define SECONDS_PER_DAY (60l * 60l * 24l)
#define NAVY_TO_UNIX (-40587l)

int unit; /* COM1 = 0, COM2 = 1 */
int verbose = 0;

char dialcmd[MAXCMD + 1] = "",
hangcmd[MAXCMD + 1] = "";

char response_str[MAXRESPONSE][LINESIZE + 1];
int nresponse = 0;

FILE *rcfile;

long navytime(void);
void noconnect(char *s);
void sendcom(char *msg);
void getcomline(char *buf, int maxlen);
void putcom(int);
void chk_abort(void);
void delay(unsigned);
void timeout(void);

extern void settz(void);

main(argc, argv)
int argc;
char *argv[];
{
long timewk;
int c;
int i;
char *fname;
char unit_str[8];

/* input configuration file */
fname = getenv("NAVYTIME");
if ((rcfile = fopen((fname != NULL) ? fname : RCFILE, "r")) == NULL)
{
fputs("navytime: cannot open configuration file\n", stderr);
abort();
}

if (fgets(unit_str, 7, rcfile) == NULL
|| !isdigit(unit_str[0])
|| unit_str[0] == '0'
|| fgets(dialcmd, MAXCMD, rcfile) == NULL
|| fgets(hangcmd, MAXCMD, rcfile) == NULL)
{
fputs("navytime: invalid configuration file\n", stderr);
abort();
}
unit = unit_str[0] - '0' - 1;

for (nresponse = 0; nresponse < MAXRESPONSE; ++nresponse)
{
if (fgets(response_str[nresponse], LINESIZE, rcfile) == NULL)
break;
for (i = strlen(response_str[nresponse]) - 1;
i >= 0 && !isprint(response_str[nresponse][i]);
--i)
;
if (i < 0)
break;
response_str[nresponse][i + 1] = '\0';
++nresponse;
}

if (nresponse == 0)
{
fputs("navytime: invalid configuration file\n", stderr);
abort();
}

fclose(rcfile);

while ((c = getopt(argc, argv, "v")) != EOF)
switch (c)
{
case 'v': ++verbose;
}

comm_open(unit, 1200, 'e', 1, 7);

settz();
set_timer(60, timeout);
timewk = navytime();
set_timer(0, (void (*)()) NULL);
stime(&timewk);

if (verbose)
{
printf("GMT: %s", asctime(gmtime(&timewk)));
printf("LOC: %s", asctime(localtime(&timewk)));
}

sendcom(hangcmd);
comm_close(unit);
}

/* get the time */
long navytime(void)
{
char line[LINESIZE + 1];
long navy_julian, navy_hour, navy_min, navy_sec;
char navy_utc[LINESIZE + 1];
int i;
long ntime;

sendcom(dialcmd);

do
{
getcomline(line, LINESIZE);
if (verbose > 1)
printf("%s\n", line);
for (i = 0; i < nresponse; ++i)
if (strstr(line, response_str[i]) != NULL)
break;
} while (i == nresponse);

if (i > 0)
noconnect(line);

do
{
do
{
getcomline(line, LINESIZE);
} while (line[0] != '*');

getcomline(line, LINESIZE);
if (verbose > 1)
printf("%s\n", line);
} while (sscanf(line, "%ld %*s %2ld %2ld %2ld %s",
&navy_julian, &navy_hour, &navy_min, &navy_sec,
navy_utc) != 5
|| strcmp(navy_utc, "UTC") != 0);

ntime = (navy_julian + NAVY_TO_UNIX) * SECONDS_PER_DAY
+ navy_hour * SECONDS_PER_HOUR + navy_min * SECONDS_PER_MINUTE
+ navy_sec;

while (comm_getc(unit) != '*')
chk_abort();

return ntime;
}

/* abort after no connect message from modem */
void noconnect(char *s)
{
comm_close_all();
fprintf(stderr, "navytime: %s\n", s);
abort();
}

/* send a message to the modem */
void sendcom(char *msg)
{
while (*msg != '\0')
{
switch (*msg)
{
case '"': while (*++msg != '\0' && *msg != '"')
putcom(*msg);
if (*msg == '"')
++msg;
break;

case '^': if (*++msg != '\0')
putcom(*(msg++) & 0x1f);

break;

default: if (isdigit(*msg))
{
delay(atoi(msg));
while (isdigit(*msg))
++msg;
}
else
++msg;
}
}
}

/* put one character to modem */
void putcom(int c)
{
while (!comm_putc(unit, c))
chk_abort();
}

/* get a line from the modem */
void getcomline(char *buf, int maxlen)
{
int c;
int empty;

empty = TRUE;
for (;;)
{
do
{
chk_abort();
} while ((c = comm_getc(unit)) == -1);

c &= 0x7f;
if (c == '\r' || c == '\n')
if (empty)
continue;
else
{
*buf = '\0';
return;
}
empty = FALSE;
if (maxlen > 0)
{
*(buf++) = c;
--maxlen;
}
}
}

/* check for ^C or time out */
void chk_abort(void)
{
if ((bdos(0x06, 0x00ff, 0) & 0xff) == '\003')
abort();
chk_timer();
}

/* delay t/10 seconds */
#define DOSTIMER ((unsigned volatile far *) MK_FP(0x40, 0x6c))
void delay(unsigned t)
{
unsigned d;

t *= 182;
t /= 100;
if ((t += (d = *DOSTIMER)) < d)
while (*DOSTIMER > t)
;
while (*DOSTIMER < t)
;
}

/* handle time out */
void timeout(void)
{
fputs("navytime: timeout\n", stderr);
}


  3 Responses to “Category : Communication (modem) tools and utilities
Archive   : NAVYTIME.ZIP
Filename : NAVYTIME.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/