Category : C Source Code
Archive   : PML.ZIP
Filename : PUT.C

 
Output of file : PUT.C contained in archive : PML.ZIP

/* Store a file on the file server.
*/

#define PORT 0

#include "dos.h"
#include "stdio.h"

unsigned int filesize();

void sport(),
send_file(),
send_file_name(),
port_init(),
wait();


main (int argc, char *argv[])
{
if (argc != 2) {
printf ("Usage: put ");
exit (1);
}

port_init (PORT); /* initialize the serial port */

send_file (argv[1]);
}


/* Send the specified file through specified port */
void send_file (char *fname)
{
FILE *fp;
char ch;
union {
char c[2];
unsigned int count;
} cnt;

if ((fp = fopen (fname, "rb")) == NULL) {
printf ("cannot open input file\n");
exit (1);
}
printf ("sending file %s\n", fname);

/* request service */
sport (PORT, 'r'); /* request server to receive a file */
wait (PORT); /* wait until server is ready */

send_file_name (fname); /* send the file name */

if (rport(PORT) != '.') {
printf ("remote file failure\n");
exit (1);
}

/* found out the size of the file */
cnt.count = filesize (fp);

/* send size */
sport (PORT, cnt.c[0]);
wait (PORT);
sport (PORT, cnt.c[1]);

do {
ch = getc(fp);
if (ferror(fp)) {
printf("error reading input file");
break;
}

/* wait until receiver is ready */
if (!feof(fp)) {
wait (PORT);
sport (PORT, ch);
}
} while (!feof(fp));

wait (PORT); /* read the last period from port */
fclose (fp);
}


/* Return the length, in bytes, of a file */
unsigned int filesize (FILE *fp)
{
unsigned long int i;

i = 0;
do {
getc(fp);
i++;
} while (!feof(fp));
rewind(fp);
return i-1; /* don't count the EOF char */
}


/* Send the file name */
void send_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);
}


/* Wait for a response. */
void wait (int port)
{
if (rport (port) != '.') {
printf ("communication error\n");
exit (1);
}
}


/* Send a character out the serial port */
void sport (int port, char c)
{
union REGS r;

r.x.dx = port; /* serial port */
r.h.al = c; /* byte to send */
r.h.ah = 1; /* send character function */
int86 (0x14, &r, &r);
if (r.h.ah & 128) {
printf ("send error detected int serial port %d", r.h.ah);
exit (1);
}
}


/* Read a character from a port */
rport (int port)
{
union REGS r;

/* wait for a character */
while (!(check_stat (port) & 256))
if (kbhit()) {
getch();
exit (1);
}

r.x.dx = port; /* serial port */
r.h.ah = 2; /* read character function */
int86 (0x14, &r, &r);

if (r.h.ah & 128)
printf ("error detected in the serial port");
return r.h.al;
}


/* Check the status of the serial port */
check_stat (int port)
{
union REGS r;

r.x.dx = port; /* serial port */
r.h.ah = 3; /* read status function */
int86 (0x14, &r, &r);
return r.x.ax;
}


/* Initialize port to 9600 baud, two stop-bits,
no parity, 8 data bits.
*/
void port_init (int port)
{
union REGS r;

r.x.dx = port; /* serial port */
r.h.ah = 0; /* initialize port function */
r.h.al = 231; /* initialize code -- see text for details */
int86 (0x14, &r, &r);
}