home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
lan
/
poor_lan
/
put.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-10-19
|
4KB
|
168 lines
/*
Store a file on the file server
*/
#define PORT 0
#include <dos.h>
#include <stdio.h>
void send_file(char *fname); unsigned int filesize(FILE *fp);
void send_file_name(char *f); void wait(int port);
void sport(int port, char c); char rport(int port); int check_stat(int port);
void port_init(int port);
main(int argc, char *argv[]) {
if(argc != 2) {
puts("Usage: put <filename>");
exit(1);
}
port_init(PORT); /* initialize the serial port */
send_file(argv[1]);
}
/* Send a file */
void send_file(char *fname) {
FILE *fp;
char ch;
union {
char c[2];
unsigned int count;
} cnt;
if(!(fp = fopen(fname, "rb"))) {
puts("cannot open input file");
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) != '.') {
puts("remote file failure");
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)) {
puts("error reading input file");
exit(1);
}
/* 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);
}
/* 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);
}
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 */
}