#include <stdio.h>
#include <dos.h>

#define PORT 0

/************************************************************************/
void port_init(port)                  /* Initialize the RS232 port */
int port;
{
 union REGS r;

 r.x.dx = port;                                  /* Gets the RS232 port */
 r.h.ah = 0;                                /* Initialize port function */
 r.h.al = 251;                                    /* 19.2kb Baud, 8,N,1 */
 int86 (0x14,&r,&r);
}
/***********************************************************************/
check_stat(port)                /* Checks the status of the RS232 port */
int port;
{
 union REGS r;

 r.x.dx = port;                                  /* Gets the RS232 port */
 r.h.ah = 3;                                  /* Send charcter function */
 int86 (0x14,&r,&r);
 return r.x.ax;
}
/************************************************************************/
void sport(port,c)            /* Sends a charcter out to the RS232 port */
int port;                                                   /* I/O port */
char c;                                             /* Charcter to send */
{
 union REGS r;

 r.x.dx = port;                                  /* Gets the RS232 port */
 r.h.al = c;                                        /* Charcter to send */
 r.h.ah = 1;                                  /* Send charcter function */
 int86 (0x14,&r,&r);
 if (r.h.ah & 128)                             /* Check the seventh bit */
 {
	printf ("send error detected in the RS232 port %d", r.h.ah);
	exit(1);
 }
}
/************************************************************************/
rport(port)                     /* Reads a charcter from the RS232 port */
int port;
{
 union REGS r;

 while (!(check_stat(PORT) &256))
	if (kbhit())                               /* Abort on keypress */
	{
	 getch();
	 exit(1);
	}
 r.x.dx = port;                                           /* RS232 port */
 r.h.ah = 2;                                 /* Reads charcter function */
 int86(0x14,&r,&r);
 if (r.h.ah & 128)
	printf ("read error detected in RS232 port");
 return r.h.al;
}
/************************************************************************/
void wait(port)                                  /* Wait for a responce */
int port;
{
 if (rport(port) != '.')
 {
  printf ("Communication Error\n");
  exit(1);
 }
}
/************************************************************************/
void send_file_name(f)                        /* Send the file name */
char *f;
{
 do
  sport(PORT,'?');
 while (!kbhit() && !(check_stat(PORT)&256));
  if (kbhit())
   {
    getch();
    exit(1);
   }
 wait(PORT);
 while (*f)
 {
  sport(PORT,*f++);
  wait(PORT);
 }
 sport(PORT,'\0');
 wait (PORT);
}
/************************************************************************/
void rec_file(fname)         /* Recive a file through the sprcified port */
char *fname;
{
 FILE *fp;
 char ch;
 union
 {
  char c[2];
  unsigned int count;
 }
 cnt;
 printf ("Receiving %s\n",fname);
 if (!(fp = fopen(fname,"wb")))
 {
  printf ("cannot open output file %s\n",fname);
  exit(1);
 }
 sport(PORT,'s');                                   /* Get file length */
 wait(PORT);

 send_file_name(fname);

 sport (PORT,'.');
 cnt.c[0] = rport(PORT);
 sport(PORT,'.');
 cnt.c[1] = rport(PORT);
 sport(PORT,'.');

 for (; cnt.count; cnt.count--)
 {
  ch = rport(PORT);
  putc (ch,fp);
  if (ferror(fp))
  {
   printf ("error writing file %s\n",fname);
   exit(1);
  }
  sport (PORT,'.');
 }
 fclose (fp);
}
/*************************************************************************/
main (argc,argv)
int argc;
char *argv[];
{
 if (argc != 2)
 {
  printf ("Usage:  GET <filename.ext>");
  exit(1);
 }
 port_init(PORT);
 rec_file(argv[1]);
}
