home *** CD-ROM | disk | FTP | other *** search
/ Shareware 1 2 the Maxx / sw_1.zip / sw_1 / PROGRAM / PCL4C30.ZIP / XYMODEM.C < prev    next >
Text File  |  1992-01-04  |  7KB  |  215 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <sys\types.h>
  4. #include <sys\stat.h>
  5.  
  6. #include "pcl4c.h"
  7. #include "ascii.h"
  8.  
  9. #define FALSE 0
  10. #define TRUE !FALSE
  11. #define ONESECOND 18
  12.  
  13. extern int GetChar();
  14. extern int PutChar();
  15.  
  16. int EOTflag;
  17.  
  18. int TxyModem(Port,Filename,Buffer,OneKflag,BatchFlag)
  19. int Port;            /* COM port [0..3] */
  20. char Filename[];     /* filename buffer */
  21. char Buffer[];       /* 1024 byte data buffer */
  22. int OneKflag;        /* if TRUE, use 1K blocks when possible */
  23. int BatchFlag;       /* if TRUE, send filename in packet 0 */
  24. {int i, k;
  25.  int Code;
  26.  int Handle;         /* file Handle */
  27.  char c;
  28.  int p;
  29.  char PacketType;
  30.  char PacketNbr;
  31.  int  PacketSize;
  32.  int  FirstPacket;
  33.  int  EOTflag = FALSE;
  34.  unsigned short CheckSum;
  35.  long filelength();
  36.  int Number1K = 0;       /* total # 1K packets */
  37.  int Number128 = 0;      /* total # 128 byte packets */
  38.  int CRCflag = FALSE;
  39.  long FileSize;
  40.  char temp[81];
  41.  int EmptyFlag = FALSE;
  42.  /* begin */
  43.  if(BatchFlag) if(Filename[0]=='\0') EmptyFlag = TRUE;
  44.  if(!EmptyFlag)
  45.      {/* Filename is not empty */
  46.       EmptyFlag = FALSE;
  47.       Handle = open(Filename,O_RDONLY|O_BINARY,S_IREAD);
  48.       if(Handle<0)
  49.           {strcpy(temp,"Cannot open ");
  50.            strcat(temp,Filename);
  51.            DisplayLine(temp,NULL,0);
  52.            return(FALSE);
  53.           }
  54.      }
  55.  DisplayLine("XYMODEM send: waiting for Receiver ",NULL,0);
  56.  while(SioKeyPress()) SioKeyRead();
  57.  /* compute # blocks */
  58.  if(!EmptyFlag)
  59.      {FileSize = filelength(Handle);
  60.       if(OneKflag) Number1K = (int) (FileSize / 1024L);
  61.       Number128 = 1 + (int) ((FileSize -1024L*(long)Number1K -1L) / 128L);
  62.       sprintf(temp,"%d 1024 & %d 128 byte packets",Number1K,Number128);
  63.       DisplayLine(temp,NULL,0);
  64.      }
  65.  else
  66.      {/* empty file */
  67.       Number128 = 0;
  68.       Number1K = 0;
  69.       /*DisplayLine("Empty File",NULL,0);*/
  70.      }
  71.  /* clear comm port ( there may be several NAKs queued up ) */
  72.  SioRxFlush(Port);
  73.  /* get receivers start up NAK or 'C' */
  74.  if(!TxStartup(Port,&CRCflag)) return(FALSE);
  75.  /* loop over all packets */
  76.  if(BatchFlag) FirstPacket = 0;
  77.  else FirstPacket = 1;
  78.  for(p=FirstPacket;p<=Number1K+Number128;p++)
  79.        {/* issue message */
  80.         sprintf(temp,"Packet %d",p);
  81.         DisplayLine(temp,NULL,0);
  82.         /* load up Buffer */
  83.         if(p==0)
  84.               {
  85.                /* Filename packet ! */
  86.                PacketSize = 128;
  87.                k = 0;
  88.                for(i=0;i<strlen(Filename);i++) Buffer[k++] = Filename[i];
  89.                Buffer[k++] = '\0';
  90.                sprintf(temp,"%ld",FileSize);
  91.                for(i=0;i<strlen(temp);i++) Buffer[k++] = temp[i];
  92.                while(k<128) Buffer[k++] = '\0';
  93.               }
  94.         else /* p > 0 */
  95.               {/* DATA Packet: use 1K or 128 byte block ? */
  96.                if(p<=Number1K) PacketSize = 1024;
  97.                else PacketSize = 128;
  98.                /* read next block from disk */
  99.                Code = read(Handle,Buffer,PacketSize);
  100.                if(Code<=0)
  101.                      {SayError(Port,"Error on disk read");
  102.                       return(FALSE);
  103.                      }
  104.                for(i=Code;i<PacketSize;i++) Buffer[i] = 0x1a;
  105.               }
  106.         /* send this packet */
  107.         if(!TxPacket(Port,p,PacketSize,Buffer,CRCflag)) return(FALSE);
  108.         SioDelay(5);
  109.         /* must 'restart' after non null packet 0 */
  110.         if(!EmptyFlag&&(p==0)) TxStartup(Port,&CRCflag);
  111.        } /* end -- for(p) */
  112.  /* done if empty packet 0 */
  113.  if(EmptyFlag)
  114.         {DisplayLine("Batch transfer complete",NULL,0);
  115.          return(TRUE);
  116.         }
  117.  /* all done. send EOT up to 10 times */
  118.  close(Handle);
  119.  if(!TxEOT(Port))
  120.      {SayError(Port,"EOT not acknowledged");
  121.       return(FALSE);
  122.      }
  123.  DisplayLine("Transfer Complete",NULL,0);
  124.  return(TRUE);
  125. } /* end -- TxyModem */
  126.  
  127. int RxyModem(Port,Filename,Buffer,CRCflag,BatchFlag)
  128. int Port;            /* COM port [0..3] */
  129. char Filename[];     /* filename buffer */
  130. char Buffer[];       /* 1024 byte data buffer */
  131. int CRCflag;         /* if TRUE, use CRC instead of checksum */
  132. int BatchFlag;       /* if TRUE, get filename from packet 0 */
  133. {int i;
  134.  int Handle;         /* file Handle */
  135.  int p;              /* packet index */
  136.  int Code;           /* return code */
  137.  int FirstPacket;
  138.  char PacketNbr;
  139.  int PacketSize;           /* 128 or 1024 */
  140.  long atol();
  141.  long FileSize;
  142.  long BytesWritten = 0L;
  143.  char temp[81];
  144.  int EmptyFlag = FALSE;
  145.  /* begin */
  146.  EOTflag = FALSE;
  147.  DisplayLine("XYMODEM Receive: Waiting for Sender ",NULL,0);
  148.  while(SioKeyPress()) SioKeyRead();
  149.  /* clear comm port */
  150.  SioRxFlush(Port);
  151.  /* Send NAKs or 'C's */
  152.  if(!RxStartup(Port,&CRCflag)) return(FALSE);
  153.  /* open file unless BatchFlag is on */
  154.  if(BatchFlag) FirstPacket = 0;
  155.  else
  156.      {/* start with packet 1 */
  157.       FirstPacket = 1;
  158.       /* open file passed in Filename[] for write */
  159.       Handle = open(Filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IWRITE);
  160.       if(Handle<0)
  161.           {strcpy(temp,"Cannot open ");
  162.            strcat(temp,Filename);
  163.            DisplayLine(temp,NULL,0);
  164.            return(FALSE);
  165.           }
  166.      }
  167.  /* get each packet in turn */
  168.  for(p=FirstPacket;;p++)
  169.      {/* issue message */
  170.       sprintf(temp,"Packet %d",p);
  171.       DisplayLine(temp,NULL,0);
  172.       PacketNbr = p & 0x00ff;
  173.       /* get next packet */
  174.       if(!RxPacket(Port,p,&PacketSize,Buffer,CRCflag,&EOTflag)) return(FALSE);
  175.       if(p==0)
  176.         {/* copy Filename */
  177.          strcpy(Filename,Buffer);
  178.          /* done if null packet 0 */
  179.          if(Filename[0]=='\0')
  180.                 {DisplayLine("Batch Transfer Complete",NULL,0);
  181.                  return(TRUE);
  182.                 }
  183.         }
  184.       /* all done if EOT was received */
  185.       if(EOTflag)
  186.           {close(Handle);
  187.            DisplayLine("Transfer Complete",NULL,0);
  188.            return(TRUE);
  189.           }
  190.       /* process packet */
  191.       if(p==0)
  192.           {/* open file using filename in packet 0 */
  193.            Handle = open(Filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IWRITE);
  194.            if(Handle<0)
  195.                 {strcat(Buffer," -- open failed");
  196.                  DisplayLine(Buffer,NULL,0);
  197.                  return(FALSE);
  198.                 }
  199.            /* get file length */
  200.            FileSize = atol(&Buffer[1+strlen(Buffer)]);
  201.            /* must 'restart' after packet 0 */
  202.            RxStartup(Port,&CRCflag);
  203.           }
  204.       else /* DATA packet */
  205.           {/* write Buffer */
  206.            if(BatchFlag)
  207.                {if(FileSize<(long)PacketSize) i = (int) FileSize;
  208.                 else i = PacketSize;
  209.                 Code = write(Handle,Buffer,i);
  210.                 FileSize -= (long)i;
  211.                }
  212.            else Code = write(Handle,Buffer,PacketSize);
  213.           } /* end -- else */
  214.      } /* end -- for(p) */
  215. } /* end - RxyModem */