home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / WWIV2.ZIP / SRSEND.C < prev    next >
C/C++ Source or Header  |  1992-12-03  |  6KB  |  316 lines

  1. /*****************************************************************************
  2.  
  3.                 WWIV Version 4
  4.                     Copyright (C) 1988-1993 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.  
  25. void send_block(char *b, int type, int ucrc, char bn)
  26. {
  27.   int maxb,i;
  28.   char ch;
  29.  
  30.   checkhangup();
  31.   switch(type) {
  32.     case 5:
  33.       maxb=128;
  34.       outcomch(1);
  35.       break;
  36.     case 4:
  37.       outcomch(0x81);
  38.       outcomch(bn);
  39.       outcomch(bn ^ 0xff);
  40.       break;
  41.     case 3:
  42.       outcomch(24);
  43.       break;
  44.     case 2:
  45.       outcomch(4);
  46.       break;
  47.     case 1:
  48.       maxb=1024;
  49.       outcomch(2);
  50.       break;
  51.     case 0:
  52.       maxb=128;
  53.       outcomch(1);
  54.   }
  55.   if ((type>1) && (type<5))
  56.     return;
  57.   outcomch(bn);
  58.   outcomch(bn ^ 0xff);
  59.   crc=0;
  60.   checksum=0;
  61.   for (i=0; i<maxb; i++) {
  62.     ch=b[i];
  63.     outcomch(ch);
  64.     calc_CRC(ch);
  65.   }
  66.   if (ucrc) {
  67.      outcomch(crc >> 8);
  68.      outcomch(crc & 0x00ff);
  69.   } else {
  70.     outcomch(checksum);
  71.   }
  72.   dump();
  73. }
  74.  
  75.  
  76. char send_b(int f, long pos, int type, char bn, int *ucrc, char *fn, int *terr, int *abort)
  77. {
  78.   char b[1025],ch,x[20],x1[20];
  79.   int i,i1,nb,done,nerr;
  80.   struct ftime ff;
  81.   struct date d;
  82.   struct time t;
  83.  
  84.   nb=0;
  85.   if (type==0)
  86.     nb=128;
  87.   if (type==1)
  88.     nb=1024;
  89.   if (nb) {
  90.     lseek(f,pos,SEEK_SET);
  91.     i=read(f,(void *)b,nb);
  92.     for (i1=i; i1<nb; i1++)
  93.       b[i1]=0;
  94.   } else
  95.     if (type==5) {
  96.       for (i1=0; i1<128; i1++)
  97.         b[i1]=0;
  98.       nb=128;
  99.       strcpy(b,stripfn(fn));
  100.       ltoa(pos,x,10);
  101.       strcat(x," ");
  102.       getftime(f,&ff);
  103.       t.ti_min=ff.ft_min;
  104.       t.ti_hour=ff.ft_hour;
  105.       t.ti_hund=0;
  106.       t.ti_sec=ff.ft_tsec*2;
  107.       d.da_year=1980+ff.ft_year;
  108.       d.da_day=ff.ft_day;
  109.       d.da_mon=ff.ft_month;
  110.       ltoa(dostounix(&d,&t)-timezone,x1,8);
  111.       strcat(x,x1);
  112.       strcpy(&(b[strlen(b)+1]),x);
  113.       b[127]=(unsigned char) (((int) (pos+127)/128) >> 8);
  114.       b[126]=(unsigned char) (((int) (pos+127)/128) & 0x00ff);
  115.     }
  116.   done=0;
  117.   nerr=0;
  118.   do {
  119.     send_block(b,type,*ucrc,bn);
  120.     ch=gettimeout(5.0,abort);
  121.     if ((ch=='C') && (pos==0))
  122.       *ucrc=1;
  123.     if ((ch==6) || (ch==24))
  124.       done=1;
  125.     else {
  126.       ++nerr;
  127.       ++(*terr);
  128.       if (nerr>=9)
  129.         done=1;
  130.       itoa(nerr,x,10);
  131.       movecsr(69,4);
  132.       outs(x);
  133.       itoa(*terr,x,10);
  134.       movecsr(69,5);
  135.       outs(x);
  136.     }
  137.   } while ((!done) && (!hangup) && (!*abort));
  138.   if (ch==6)
  139.     return(6);
  140.   if (ch==24)
  141.     return(24);
  142.   return(21);
  143. }
  144.  
  145.  
  146. int okstart(int *ucrc, int *abort)
  147. {
  148.   char ch;
  149.   double d;
  150.   int ok,done;
  151.  
  152.   d=timer();
  153.   ok=0;
  154.   done=0;
  155.   while ((fabs(timer()-d)<90.0) && (!done) && (!hangup) && (!*abort)) {
  156.     ch=gettimeout(91.0-d,abort);
  157.     if (ch=='C') {
  158.       *ucrc=1;
  159.       ok=1;
  160.       done=1;
  161.     }
  162.     if (ch==21) {
  163.       *ucrc=0;
  164.       ok=1;
  165.       done=1;
  166.     }
  167.     if (ch==24) {
  168.       ok=0;
  169.       done=1;
  170.     }
  171.   }
  172.   return(ok);
  173. }
  174.  
  175.  
  176.  
  177. void xymodem_send(char *fn, int *sent, double *percent, char ft, int ucrc, int ym, int ymb)
  178. {
  179.   char bn,ch,s[81],s1[10];
  180.   int f,i,i1,abort,terr,xx1,yy1;
  181.   long cp,len;
  182.   double tpb;
  183.  
  184.   cp=0L;
  185.   bn=1;
  186.   abort=0;
  187.   terr=0;
  188.   f=open(fn,O_RDONLY | O_BINARY);
  189.   if (f<0) {
  190.     if (!ymb) {
  191.       nl();
  192.       pl(get_string(89));
  193.       nl();
  194.     }
  195.     *sent=0;
  196.     *percent=0.0;
  197.     return;
  198.   }
  199.   len=filelength(f);
  200.   if (!len)
  201.     len=1;
  202.   tpb=(12.656) / ((double) (modem_speed));
  203.  
  204.   if (!ymb) {
  205.     nl();
  206.     pl(get_string(886));
  207.   }
  208.   xx1=wherex();
  209.   yy1=wherey();
  210.   movecsr(52,0);
  211.   outs(get_stringx(1,67));
  212.   movecsr(52,1);
  213.   outs(get_stringx(1,68));
  214.   movecsr(52,2);
  215.   outs(get_stringx(1,69));
  216.   movecsr(52,3);
  217.   outs(get_stringx(1,70));
  218.   movecsr(52,4);
  219.   outs(get_stringx(1,71));
  220.   movecsr(52,5);
  221.   outs(get_stringx(1,72));
  222.   movecsr(52,6);
  223.   outs(get_stringx(1,73));
  224.   movecsr(65,0);
  225.   outs(stripfn(fn));
  226.   sprintf(s,"%ld - %ldk",
  227.     (len+127)/128,
  228.     (len+1023)/1024);
  229.   movecsr(65,2);
  230.   outs(s);
  231.  
  232.   if (!okstart(&ucrc,&abort))
  233.     abort=1;
  234.   if ((ft) && (!abort) && (!hangup)) {
  235.     ch=send_b(f, len, 4, ft, &ucrc,fn,&terr,&abort);
  236.     if (ch==24)
  237.       abort=1;
  238.     if (ch==21) {
  239.       send_b(f,0L,3,0,&ucrc,fn,&terr,&abort);
  240.       abort=1;
  241.     }
  242.   }
  243.   if ((ym) && (!abort) && (!hangup)) {
  244.     ch=send_b(f, len, 5, 0, &ucrc,fn,&terr,&abort);
  245.     if (ch==24)
  246.       abort=1;
  247.     if (ch==21) {
  248.       send_b(f,0L,3,0,&ucrc,fn,&terr,&abort);
  249.       abort=1;
  250.     }
  251.   }
  252.   while ((!hangup) && (!abort) && (cp<len)) {
  253.     if (ym)
  254.       i=1;
  255.     else
  256.       i=0;
  257.     if ((len-cp)<768L)
  258.       i=0;
  259.     sprintf(s,"%ld - %ldk",
  260.       cp/128+1,
  261.       cp/1024+1);
  262.     movecsr(65,3);
  263.     outs(s);
  264.     movecsr(65,1);
  265.     outs(ctim(((double)(len-cp))*tpb));
  266.     movecsr(69,4);
  267.     outs("0");
  268.  
  269.     ch=send_b(f,cp,i,bn,&ucrc,fn,&terr,&abort);
  270.     if (ch==24)
  271.       abort=1;
  272.     else
  273.       if (ch==21) {
  274.         wait1(18);
  275.         dump();
  276.         send_b(f,0L,3,0,&ucrc,fn,&terr,&abort);
  277.         abort=1;
  278.       } else {
  279.         ++bn;
  280.         if (i)
  281.           cp+=1024;
  282.         else
  283.           cp+=128;
  284.       }
  285.   }
  286.   if ((!hangup) && (!abort))
  287.     send_b(f,0L,2,0,&ucrc,fn,&terr,&abort);
  288.   if (!abort) {
  289.     *sent=1;
  290.     *percent=1.0;
  291.   } else {
  292.     *sent=0;
  293.     if (i)
  294.       cp+=1024;
  295.     else
  296.       cp+=128;
  297.     if (cp>=len)
  298.       *percent=1.0;
  299.     else {
  300.       if (i)
  301.         cp-=1024;
  302.       else
  303.         cp-=128;
  304.       *percent=((double)(cp))/((double)(len));
  305.     }
  306.   }
  307.   close(f);
  308.   movecsr(xx1,yy1);
  309.   if ((*sent) && (!ymb)) {
  310.     pl(get_string(909));
  311.     nl();
  312.   }
  313. }
  314.  
  315.  
  316.