home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: DFÜ und Kommunikation / SOS-DFUE.ISO / programm / dos / utility / pccp076 / xmcrcs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-20  |  4.5 KB  |  256 lines

  1. /*    Copyright (C) 1992,1993 Peter Edward Cann, all rights reserved.
  2.  *    MicroSoft QuickC
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<bios.h>
  7. #include<dos.h>
  8. #include<fcntl.h>
  9. #include<sys\types.h>
  10. #include<sys\stat.h>
  11. #include<signal.h>
  12. #include"port.h"
  13. #include"tperday.h"
  14.  
  15. #define NAK 21
  16. #define ACK 6
  17. #define SOH 1
  18. #define EOT 4
  19. #define CAN 24
  20.  
  21. unsigned long tick;
  22.  
  23. void (interrupt far *oldtick)();
  24.  
  25. void interrupt far tickhndl()
  26.     {
  27.     tick++;
  28.     }
  29.  
  30. sendchar(c)
  31.     unsigned char c;
  32.     {
  33.     while(!((inp(basereg+STATREG)&TXMTMASK)&&(inp(basereg+MSTATREG)&CTSMASK)))
  34.         if(_bios_keybrd(_KEYBRD_READY))
  35.             if((_bios_keybrd(_KEYBRD_READ)&0xff)==0x03)
  36.                 quit();
  37.     outp(basereg, c);
  38.     }
  39.  
  40. int follow;
  41.  
  42. int rcharto(ticks)
  43.     int ticks;
  44.     {
  45.     int c;
  46.     tick=0L;
  47.     while(1)
  48.         {
  49.         if(_bios_keybrd(_KEYBRD_READY))
  50.             if((_bios_keybrd(_KEYBRD_READ)&0xff)==0x03)
  51.                 quit();
  52.         if(tick>ticks)
  53.             return(-1); /* NOTE: This is an INT!!! */
  54.         if(follow!=index)
  55.             {
  56.             c=buf[follow++];
  57.             if(follow>=TBUFSIZ)
  58.                 follow=0;
  59.             return(c);
  60.             }
  61.         }
  62.     }
  63.  
  64. int calccrc(ptr, count)
  65.     char *ptr;
  66.     int count;
  67.     {
  68.     int crc, i;
  69.     crc = 0;
  70.     while(--count >= 0)
  71.         {
  72.         crc = crc ^ (int)*ptr++ << 8;
  73.         for(i = 0; i < 8; ++i)
  74.             if(crc & 0x8000)
  75.                 crc = crc << 1 ^ 0x1021;
  76.             else
  77.                 crc = crc << 1;
  78.         }
  79.     return (crc & 0xFFFF);
  80.     }
  81.  
  82. unsigned char block[128];
  83.  
  84. sblock(blockn)
  85.     int blockn;
  86.     {
  87.     unsigned char c;
  88.     unsigned short crc, rcrc;
  89.     int i;
  90.     crc=calccrc(block, 128);
  91.     sendchar(SOH);
  92.     sendchar(blockn);
  93.     sendchar((blockn^0xff)&0xff);
  94.     for(i=0;i<128;++i)
  95.         sendchar(block[i]);
  96.     sendchar((crc>>8)&0xff);
  97.     sendchar(crc&0xff);
  98.     }
  99.  
  100. quit()
  101.     {
  102.     cleanup(0);
  103.     _dos_setvect(0x1c, oldtick);
  104.     exit(99);
  105.     }
  106.  
  107. main(argc, argv)
  108.     int argc;
  109.     char **argv;
  110.     {
  111.     int i, j, infd, ok, c;
  112.     unsigned char blocknum;
  113.     long nbytes;
  114.     index=follow=0;
  115.     printf("Copyright (C) 1992 Peter Edward Cann, all rights reserved.\n");
  116.     printf("xmodem crc send of %s.\n", argv[4]);
  117.     if(argc!=5)
  118.         {
  119.         printf("USAGE: xmodemr <comnum> <bps> <stopbits> <file pathname>\n");
  120.         exit(1);
  121.         }
  122.     if((infd=open(argv[4], O_RDONLY|O_BINARY))==-1)
  123.         {
  124.         printf("Error opening file %s.\n", argv[4]);
  125.         exit(2);
  126.         }
  127.     comnum=atoi(argv[1])-1;
  128.     speed=atoi(argv[2]);
  129.     databits='8';
  130.     parity='n';
  131.     stopbits=argv[3][0];
  132.     setport();
  133.     readset();
  134.     oldvect=_dos_getvect(0x1c);
  135.     signal(SIGINT, quit);
  136.     _dos_setvect(0x1c, tickhndl);
  137.     setup();
  138.     nbytes=0;
  139.     for(i=0;;++i)
  140.         {
  141.         if((c=rcharto(100))==CAN)
  142.             {
  143.             c=rcharto(100);
  144.             if(c==CAN)
  145.                 {
  146.                 printf("Received two consecutive CANcel codes (^X).\n");
  147.                 cleanup(0);
  148.                 _dos_setvect(0x1c, oldtick);
  149.                 exit(40);
  150.                 }
  151.             }
  152.         if(c=='C')
  153.             break;
  154.         if(i>=20)
  155.             {
  156.             printf("No C in 20 five-second tries.\n");
  157.             cleanup(0);
  158.             _dos_setvect(0x1c, oldtick);
  159.             exit(10);
  160.             }
  161.         }
  162.     blocknum=1;
  163.     while(1)
  164.         {
  165.         if((j=read(infd, block, 128))==0)
  166.             {
  167.             printf("\nEnd of file.\n");
  168.             sendchar(EOT);
  169.             do
  170.                 {
  171.                 c=rcharto(300);
  172.                 if(c==CAN)
  173.                     c=rcharto(300);
  174.                 }
  175.             while((c!=ACK)&&(c!=NAK)&&(c!=CAN)&&(c!=-1));
  176.             if(c==-1)
  177.                 {
  178.                 printf("\nTimeout.\n");
  179.                 cleanup(0);
  180.                 _dos_setvect(0x1c, oldtick);
  181.                 exit(30);
  182.                 }
  183.             else if(c==NAK)
  184.                 {
  185.                 printf("\nEOT NAKed.\n");
  186.                 cleanup(0);
  187.                 _dos_setvect(0x1c, oldtick);
  188.                 exit(13);
  189.                 }
  190.             else if(c==CAN)
  191.                 {
  192.                 printf("\nReceiver sent two CANcel codes (^X).\n");
  193.                 cleanup(0);
  194.                 _dos_setvect(0x1c, oldtick);
  195.                 exit(31);
  196.                 }
  197.             else
  198.                 {
  199.                 printf("\nSuccessful.\n");
  200.                 cleanup(0);
  201.                 _dos_setvect(0x1c, oldtick);
  202.                 exit(0);    
  203.                 }
  204.             }
  205.         for(c=j;c<128;c++)
  206.             block[c]=26;
  207.         i=0;
  208.         do
  209.             {
  210.             printf("\nSending block %d. ", blocknum);
  211.             sblock(blocknum);
  212.             do
  213.                 {
  214.                 c=rcharto(200);
  215.                 printf("%02x ", c);
  216.                 if(c==CAN)
  217.                     {
  218.                     c=rcharto(200);
  219.                     printf("%02x ", c);
  220.                     }
  221.                 }
  222.             while((c!=ACK)&&(c!=NAK)&&(c!=CAN)&&(c!=-1));
  223.             }
  224.         while((c==NAK)&&(i++<10));
  225.         if(c!=ACK)
  226.             if(c==NAK)
  227.                 {
  228.                 printf("\nRetry limit exceeded.\n");
  229.                 cleanup(0);
  230.                 _dos_setvect(0x1c, oldtick);
  231.                 exit(14);
  232.                 }
  233.             else if(c==-1)
  234.                 {
  235.                 printf("\nTimeout.\n");
  236.                 cleanup(0);
  237.                 _dos_setvect(0x1c, oldtick);
  238.                 exit(33);
  239.                 }
  240.             else
  241.                 {
  242.                 printf("\nReceiver sent two CANcel codes (^X).\n");
  243.                 cleanup(0);
  244.                 _dos_setvect(0x1c, oldtick);
  245.                 exit(11);
  246.                 }
  247.         nbytes+=128;
  248.         printf(" Successful. Bytes so far: %ld", nbytes);
  249.         blocknum++;
  250.         }
  251.     printf("Programming error; fell through end; see code.\n");
  252.     cleanup(0);
  253.     _dos_setvect(0x1c, oldtick);
  254.     exit(12);
  255.     }
  256.