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

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