home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lan / poor_lan / get.c next >
C/C++ Source or Header  |  1989-10-19  |  3KB  |  142 lines

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