home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lan / poor_lan / put.c < prev    next >
C/C++ Source or Header  |  1989-10-19  |  4KB  |  168 lines

  1. /*
  2.     Store a file on the file server
  3. */
  4.  
  5.  
  6. #define PORT 0
  7.  
  8. #include <dos.h>
  9. #include <stdio.h>
  10.  
  11. void send_file(char *fname); unsigned int filesize(FILE *fp);
  12. void send_file_name(char *f); void wait(int port);
  13. void sport(int port, char c); char rport(int port); int check_stat(int port);
  14. void port_init(int port);
  15.  
  16. main(int argc, char *argv[]) {
  17.     if(argc != 2) {
  18.         puts("Usage: put <filename>");
  19.         exit(1);
  20.     }
  21.  
  22.     port_init(PORT);        /* initialize the serial port */
  23.     send_file(argv[1]);
  24. }
  25.  
  26. /* Send a file */
  27. void send_file(char *fname) {
  28.     FILE *fp;
  29.     char ch;
  30.     union {
  31.         char c[2];
  32.         unsigned int count;
  33.     } cnt;
  34.  
  35.     if(!(fp = fopen(fname, "rb"))) {
  36.         puts("cannot open input file");
  37.         exit(1);
  38.     }
  39.     printf("Sending file %s\n", fname);
  40.  
  41.     /* request service */
  42.     sport(PORT, 'r');   /* request server to receive a file */
  43.     wait(PORT);         /* wait until server is ready */
  44.  
  45.     send_file_name(fname);  /* send the file name */
  46.  
  47.     if(rport(PORT) != '.') {
  48.         puts("remote file failure");
  49.         exit(1);
  50.     }
  51.  
  52.     /* found out the size of the file */
  53.     cnt.count = filesize(fp);
  54.  
  55.     /* send size */
  56.     sport(PORT, cnt.c[0]);
  57.     wait(PORT);
  58.     sport(PORT, cnt.c[1]);
  59.     do {
  60.         ch = getc(fp);
  61.  
  62.         if(ferror(fp)) {
  63.             puts("error reading input file");
  64.             exit(1);
  65.         }
  66.  
  67.         /* wait until receiver is ready */
  68.         if(!feof(fp)) {
  69.             wait(PORT);
  70.             sport(PORT, ch);
  71.         }
  72.     } while(!feof(fp));
  73.     wait(PORT); /* read the last period from port */
  74.     fclose(fp);
  75. }
  76.  
  77. /* Send the file name */
  78. void send_file_name(char *f) {
  79.     do {
  80.         sport(PORT, '?');   /* wait until server is ready */
  81.     } while(!kbhit() && !(check_stat(PORT) & 256));
  82.     if(kbhit()) {
  83.         getch();
  84.         exit(1);
  85.     }
  86.     wait(PORT);
  87.  
  88.     while(*f) {
  89.         sport(PORT, *f++);
  90.         exit(PORT);
  91.     }
  92.     sport(PORT, '\0');  /* null terminator */
  93. }
  94.  
  95. /* Wait for a response */
  96. void wait(int port) {
  97.     if(rport(port) != '.') {
  98.         puts("communication error");
  99.         exit(1);
  100.     }
  101. }
  102.  
  103. /* Send a character out the serial port */
  104. void sport(int port, char c) {
  105.     union REGS r;
  106.  
  107.     r.x.dx = port;      /* serial port */
  108.     r.h.al = c;         /* byte to send */
  109.     r.h.ah = 1;         /* send character function */
  110.     int86(0x14, &r, &r);
  111.     if(r.h.ah & 128) {
  112.         printf("send error detected in serial port %d", r.h.ah);
  113.         exit(1);
  114.     }
  115. }
  116.  
  117. /* Read a character from a port */
  118. char rport(int port) {
  119.     union REGS r;
  120.  
  121.     /* wait for a character */
  122.     while(!(check_stat(port) & 256))
  123.         if(kbhit()) {
  124.             getch();
  125.             exit(1);
  126.         }
  127.     r.x.dx = port;      /* serial port */
  128.     r.h.ah = 2;         /* read character function */
  129.     int86(0x14, &r, &r);
  130.     if(r.h.ah & 128)
  131.         printf("read error detected in serial port");
  132.     return r.h.al;
  133. }
  134.  
  135. /* Check the status of the serial port */
  136. int check_stat(int port) {
  137.     union REGS r;
  138.  
  139.     r.x.dx = port;      /* serial port */
  140.     r.h.ah = 3;         /* read status */
  141.     int86(0x14, &r, &r);
  142.     return r.x.ax;
  143. }
  144.  
  145. /* Initialize port to 9600 baud, two stop bits,
  146.    no parity, 8 data bits.
  147. */
  148. void port_init(int port) {
  149.     union REGS r;
  150.  
  151.     r.x.dx = port;      /* serial port */
  152.     r.h.ah = 0;         /* initialize port function */
  153.     r.h.al = 231;       /* initialization code */
  154.     int86(0x14, &r, &r);
  155. }
  156.  
  157. unsigned int filesize(FILE *fp) {
  158.     unsigned long int i;
  159.  
  160.     i = 0;
  161.     do {
  162.         getc(fp);
  163.         i++;
  164.     } while (!feof(fp));
  165.     rewind(fp);
  166.     return i - 1; /* don't count the EOF char */
  167. }
  168.