home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / UTIL / WWIVE / SRRCV.C < prev    next >
Text File  |  1991-12-24  |  8KB  |  365 lines

  1. /*****************************************************************************
  2.  
  3.                 WWIV Version 4
  4.                     Copyright (C) 1988-1991 by Wayne Bell
  5.  
  6. Distribution of the source code for WWIV, in any form, modified or unmodified,
  7. without PRIOR, WRITTEN APPROVAL by the author, is expressly prohibited.
  8. Distribution of compiled versions of WWIV is limited to copies compiled BY
  9. THE AUTHOR.  Distribution of any copies of WWIV not compiled by the author
  10. is expressly prohibited.
  11.  
  12.  
  13. *****************************************************************************/
  14.  
  15.  
  16.  
  17. #include "vars.h"
  18.  
  19. #pragma hdrstop
  20.  
  21. #include <math.h>
  22.  
  23.  
  24. char modemkey(int *tout)
  25. {
  26.   double d1;
  27.   char ch;
  28.  
  29.   if (comhit()) {
  30.     ch=get1c();
  31.     calc_CRC(ch);
  32.     return(ch);
  33.   }
  34.   if (*tout)
  35.     return(0);
  36.   d1=timer();
  37.   while ((fabs(timer()-d1)<0.5) && (!comhit()) && (!hangup)) {
  38.     checkhangup();
  39.   }
  40.   if (comhit()) {
  41.     ch=get1c();
  42.     calc_CRC(ch);
  43.     return(ch);
  44.   }
  45.   *tout=1;
  46.   return(0);
  47. }
  48.  
  49.  
  50.  
  51. int receive_block(char *b, unsigned char *bln, int ucrc)
  52. {
  53.   int i,i1,bs,cerr,abort,err,tout,cc1;
  54.   unsigned char ch,bn,bn1,cs1;
  55.  
  56.   abort=0;
  57.   ch=gettimeout(5.0,&abort);
  58.   err=0;
  59.   if (abort)
  60.     return(6);
  61.   tout=0;
  62.   if (ch==0x81) {
  63.     bn=modemkey(&tout);
  64.     bn1=modemkey(&tout);
  65.     if ((bn ^ bn1)==0xff) {
  66.       b[0]=bn;
  67.       *bln=bn;
  68.       return(8);
  69.     } else
  70.       return(3);
  71.   } else
  72.     if (ch==1) {
  73.       bn=modemkey(&tout);
  74.       bn1=modemkey(&tout);
  75.       if ((bn ^ bn1)!=0xff)
  76.         err=3;
  77.       *bln=bn;
  78.       crc=0;
  79.       checksum=0;
  80.       for (i=0; (i<128) && (!hangup); i++)
  81.         b[i]=modemkey(&tout);
  82.       if ((!ucrc) && (!hangup)) {
  83.         cs1=checksum;
  84.         bn1=modemkey(&tout);
  85.         if (bn1!=cs1) {
  86.           err=2;
  87.         }
  88.       } else if (!hangup) {
  89.         cc1=crc;
  90.         bn=modemkey(&tout);
  91.         bn1=modemkey(&tout);
  92.         if ((bn!=(unsigned char)(cc1 >> 8)) || (bn1!=(unsigned char)(cc1 & 0x00ff)))
  93.           err=2;
  94.       }
  95.       if (tout)
  96.         return(7);
  97.       return(err);
  98.     } else
  99.       if (ch==2) {
  100.         bn=modemkey(&tout);
  101.         bn1=modemkey(&tout);
  102.         crc=0;
  103.         checksum=0;
  104.         if ((bn ^ bn1)!=0xff)
  105.           err=3;
  106.         *bln=bn;
  107.         for (i=0; (i<1024) && (!hangup); i++)
  108.           b[i]=modemkey(&tout);
  109.         if ((!ucrc) && (!hangup)){
  110.           cs1=checksum;
  111.           bn1=modemkey(&tout);
  112.           if (bn1!=cs1)
  113.             err=2;
  114.         } else if (!hangup) {
  115.           cc1=crc;
  116.           bn=modemkey(&tout);
  117.           bn1=modemkey(&tout);
  118.           if ((bn!=(unsigned char)(cc1 >> 8)) || (bn1!=(unsigned char)(cc1 & 0x00ff)))
  119.             err=2;
  120.         }
  121.         if (tout)
  122.           return(7);
  123.         if (err==0)
  124.           return(1);
  125.         else
  126.           return(err);
  127.       } else
  128.         if (ch==24) {
  129.           return(4);
  130.         } else
  131.           if (ch==4) {
  132.             return(5);
  133.           } else
  134.             if (ch==0)
  135.               return(7);
  136.              else
  137.               return(9);
  138. }
  139.  
  140.  
  141. void xymodem_receive(char *fn, char *ft, int *received, int ucrc)
  142. {
  143.   char b[1025],x[81],x1[20],ch;
  144.   unsigned char bln;
  145.   unsigned int bn;
  146.   int done,ok,lastcan,lasteot,terr,cerr;
  147.   long pos,reallen,filedatetime,lx;
  148.   int f,i,i1,i2,i3,ox,oy;
  149.   double tpb;
  150.   struct ftime ff;
  151.   struct date d;
  152.   struct time t;
  153.   double d1;
  154.  
  155.   unlink(fn);
  156.   f=open(fn,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  157.   ok=1;
  158.   lastcan=0;
  159.   lasteot=0;
  160.   terr=0;
  161.   cerr=0;
  162.   if (f<0) {
  163.     nl();
  164.     nl();
  165.     pl("DOS error - Can't create file.");
  166.     nl();
  167.     *received=0;
  168.     return;
  169.   }
  170.   pos=0L;
  171.   reallen=0L;
  172.   filedatetime=0L;
  173.   bn=1;
  174.   done=0;
  175.   tpb=(12.656) / ((double) (modem_speed));
  176.   nl();
  177.   pl("1>7 Ready to receive, ^X to abort1 <");
  178.   ox=wherex();
  179.   oy=wherey();
  180.   movecsr(52,0);
  181.   outs("│ Filename :               ");
  182.   movecsr(52,1);
  183.   outs("│ Xfer Time:               ");
  184.   movecsr(52,2);
  185.   outs("│ File Size:               ");
  186.   movecsr(52,3);
  187.   outs("│ Cur Block: 1 - 1k        ");
  188.   movecsr(52,4);
  189.   outs("│ Consec Errors: 0         ");
  190.   movecsr(52,5);
  191.   outs("│ Total Errors : 0         ");
  192.   movecsr(52,6);
  193.   outs("└──────────────────────────");
  194.   movecsr(65,0);
  195.   outs(stripfn(fn));
  196.   i=0;
  197.   do {
  198.     if (i++>9) {
  199.       *received=0;
  200.       return;
  201.     }
  202.     if (ucrc)
  203.       outcomch('C');
  204.     else
  205.       outcomch(21);
  206.  
  207.     d1=timer();
  208.     while ((fabs(timer()-d1)<10.0) && (!comhit()) && (!hangup)) {
  209.       checkhangup();
  210.       if (kbhitb()) {
  211.         ch=getchd();
  212.         if (ch==0)
  213.           getchd();
  214.         else
  215.           if (ch==27) {
  216.             done=1;
  217.             ok=0;
  218.           }
  219.       }
  220.     }
  221.   } while(!comhit() && !hangup);
  222.  
  223.   do {
  224.     bln=255;
  225.     itoa(cerr,x,10);
  226.     strcat(x,"  ");
  227.     movecsr(69,4);
  228.     outs(x);
  229.     itoa(terr,x,10);
  230.     movecsr(69,5);
  231.     outs(x);
  232.     sprintf(x,"%ld - %ldk",
  233.       pos/128+1,
  234.       pos/1024+1);
  235.     movecsr(65,3);
  236.     outs(x);
  237.     if (reallen) {
  238.       movecsr(65,1);
  239.       outs(ctim(((double)(reallen-pos))*tpb));
  240.     }
  241.  
  242.     i=receive_block(b,&bln,ucrc);
  243.     if ((i==0) || (i==1)) {
  244.       if ((bln==0) && (pos==0L)) {
  245.         i1=strlen(b)+1;
  246.     i3=i1;
  247.     while ((b[i3]>='0') && (b[i3]<='9') && ((i3-i1)<15))
  248.       x[i3-i1]=b[i3++];
  249.     x[i3-i1]=0;
  250.         reallen=atol(x);
  251.         sprintf(x,"%ld - %ldk",
  252.           (reallen+127)/128,
  253.           (reallen+1023)/1024);
  254.         movecsr(65,2);
  255.         outs(x);
  256.         while ((b[i1]!=32) && (i1<64))
  257.           ++i1;
  258.         if (b[i1]==32) {
  259.           ++i1;
  260.           while ((b[i1]>='0') && (b[i1]<='8')) {
  261.             filedatetime=(filedatetime*8) + ((long) (b[i1]-'0'));
  262.             ++i1;
  263.           }
  264.           i1+=timezone+5*60*60;
  265.         }
  266.         outcomch(6);
  267.       } else
  268.         if ((bn & 0x00ff)==(unsigned int)bln) {
  269.           lseek(f,pos, SEEK_SET);
  270.           lx=reallen-pos;
  271.           if (i==0)
  272.             i2=128;
  273.           else
  274.             i2=1024;
  275.           if ((((long) i2)>lx) && (reallen))
  276.             i2=(int) lx;
  277.           write(f,(void *)b,i2);
  278.           pos += (long)i2;
  279.           ++bn;
  280.           outcomch(6);
  281.         } else
  282.           if (((bn-1) & 0x00ff)==(unsigned int)bln) {
  283.             outcomch(6);
  284.           } else {
  285.             outcomch(24);
  286.             ok=0;
  287.             done=1;
  288.           }
  289.       cerr=0;
  290.     } else
  291.       if ((i==2) || (i==7) || (i==3)) {
  292.         if ((pos==0L) && (reallen==0L) && (ucrc))
  293.           outcomch('C');
  294.         else
  295.           outcomch(21);
  296.         ++cerr;
  297.         ++terr;
  298.         if (cerr>9) {
  299.           outcomch(24);
  300.           ok=0;
  301.           done=1;
  302.         }
  303.       } else
  304.         if (i==6) {
  305.           ok=0;
  306.           done=1;
  307.           outcomch(24);
  308.         } else
  309.           if (i==4) {
  310.             if (lastcan) {
  311.               ok=0;
  312.               done=1;
  313.               outcomch(6);
  314.             } else {
  315.               lastcan=1;
  316.               outcomch(21);
  317.             }
  318.           } else
  319.             if (i==5) {
  320.           lasteot=1;
  321.               if (lasteot) {
  322.                 done=1;
  323.                 outcomch(6);
  324.               } else {
  325.                 lasteot=1;
  326.                 outcomch(21);
  327.               }
  328.             } else
  329.               if (i==8) {
  330.                 *ft=bln;
  331.                 outcomch(6);
  332.                 cerr=0;
  333.               } else
  334.                 if (i==9)
  335.                   dump();
  336.     if (i!=4)
  337.       lastcan=0;
  338.     if (i!=5)
  339.       lasteot=0;
  340.   } while ((!hangup) && (!done));
  341.   movecsr(ox,oy);
  342.   if (ok) {
  343.     if (filedatetime) {
  344.       unixtodos(filedatetime,&d,&t);
  345.       if (d.da_year>=1980) {
  346.         ff.ft_min=t.ti_min;
  347.         ff.ft_hour=t.ti_hour;
  348.         ff.ft_tsec=t.ti_sec/2;
  349.         ff.ft_year=d.da_year-1980;
  350.         ff.ft_day=d.da_day;
  351.         ff.ft_month=d.da_mon;
  352.         setftime(f,&ff);
  353.       }
  354.     }
  355.     close(f);
  356.     *received=1;
  357.   } else {
  358.     close(f);
  359.     unlink(fn);
  360.     *received=0;
  361.   }
  362. }
  363.  
  364.  
  365.