home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
lan
/
poor_lan
/
get.c
next >
Wrap
C/C++ Source or Header
|
1989-10-19
|
3KB
|
142 lines
/*
Load a file from the file server
*/
#define PORT 0
#include <dos.h>
#include <stdio.h>
void rec_file(char *fname); void send_file_name(char *f);
void sport(int port, char c); void wait(int port); char rport(int port);
int check_stat(int port); void port_init(int port);
main(int argc, char *argv[]) {
if(argc != 2) {
puts("Usage: get <filename>");
exit(1);
}
port_init(PORT); /* initialize the serial port */
rec_file(argv[1]);
}
/* Receive a file */
void rec_file(char *fname) {
FILE *fp;
char ch;
union {
char c[2];
unsigned int count;
} cnt;
printf("Loading file %s\n", fname);
remove(fname);
if(!(fp = fopen(fname, "wb"))) {
puts("cannot open output file");
exit(1);
}
sport(PORT, 's'); /* tell server to send a file */
wait(PORT); /* wait until server is ready */
/* get file length */
send_file_name(fname);
sport(PORT, '.'); /* acknowledge */
cnt.c[0] = rport(PORT);
sport(PORT, '.'); /* acknowledge */
cnt.c[1] = rport(PORT);
sport(PORT, '.'); /* acknowledge */
for( ; cnt.count; cnt.count--) {
ch = rport(PORT);
putc(ch, fp);
if(ferror(fp)) {
puts("error writing file");
exit(1);
}
sport(PORT, '.'); /* acknowledge */
}
fclose(fp);
}
/* Send the file name */
void send_file_name(char *f) {
do {
sport(PORT, '?'); /* wait until server is ready */
} while(!kbhit() && !(check_stat(PORT) & 256));
if(kbhit()) {
getch();
exit(1);
}
wait(PORT);
while(*f) {
sport(PORT, *f++);
exit(PORT);
}
sport(PORT, '\0'); /* null terminator */
}
/* Wait for a response */
void wait(int port) {
if(rport(port) != '.') {
puts("communication error");
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 in serial port %d", r.h.ah);
exit(1);
}
}
/* Read a character from a port */
char 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("read error detected in serial port");
return r.h.al;
}
/* Check the status of the serial port */
int check_stat(int port) {
union REGS r;
r.x.dx = port; /* serial port */
r.h.ah = 3; /* read status */
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; /* initialization code */
int86(0x14, &r, &r);
}