home *** CD-ROM | disk | FTP | other *** search
/ GRIPS 2: Government Rast…rocessing Software & Data / GRIPS_2.cdr / dos / ncsa_tel / contribu / byu_tel2.hqx / tcpip / binsubs.c < prev    next >
Text File  |  1990-04-10  |  9KB  |  436 lines

  1. #ifndef lint
  2. static char *SCCSid = "%W%    (NCSA)    %G%";
  3. #endif
  4. /*
  5. *    binsubs.c
  6. *     by Gaige B. Paulsen
  7. ****************************************************************************
  8. *                                                                          *
  9. *      Uses    :                                                               *
  10. *      TCP/IP kernel for NCSA Telnet                                       *
  11. *      by Tim Krauskopf                                                    *
  12. *       with Macintosh code by Gaige B. Paulsen                                 *
  13. *                                                                          *
  14. *      National Center for Supercomputing Applications                     *
  15. *      152 Computing Applications Building                                 *
  16. *      605 E. Springfield Ave.                                             *
  17. *      Champaign, IL  61820                                                *
  18. *                                                                          *
  19. *                                                                          *
  20. ****************************************************************************
  21. *
  22. *    MacBinary Subroutines.    
  23. *
  24. *    Called by:
  25. *        bkgr.c
  26. */
  27.  
  28. #include "stdio.h"
  29. #include "fcntl.h"
  30. #include "::main:mpw.h"
  31.  
  32. #include "Windows.h"
  33. #include "Files.h"
  34. #include "Packages.h"
  35.  
  36. #include "MacBinary.h"
  37.  
  38. extern MBFile        /* BYU mod */
  39.     *ftp_mbfp,        /* BYU mod */
  40.     *mbfp;            /* BYU mod */
  41.  
  42. char *strncpy(), *malloc();
  43.  
  44. #define BLOCKS(x)    ((x+127)/128)
  45. #define lmove(f,t)    movmem(f,t,4)
  46.  
  47. MBHead
  48.     *mbh;
  49. char 
  50.     buffer[128];
  51.  
  52. char *strsave( p)
  53. char *p;
  54. {
  55.     char *t;
  56.  
  57.     t=malloc(strlen(p));
  58.     strncpy(t,p,strlen(p));
  59. }
  60.  
  61. init_mb_files() {            /* BYU mod */
  62.   ftp_mbfp->fd = 0;            /* BYU mod */
  63.   mbfp->fd = 0;                /* BYU mod */
  64. }                            /* BYU mod */
  65.                             /* BYU mod */
  66. close_mb_files() {            /* BYU mod */
  67.     if (ftp_mbfp->fd != 0) MBclose( ftp_mbfp );        /* BYU mod - close input file */
  68.     if (mbfp->fd != 0) MBclose( mbfp );                /* BYU mod - close input file */
  69. }                            /* BYU mod */
  70.  
  71. GetFileInfo(vol,name,iop)
  72. short vol;
  73. char *name;
  74. FileParam *iop;
  75. {
  76.     iop->ioNamePtr = name;
  77.     iop->ioVRefNum=vol;
  78.     iop->ioFVersNum=iop->ioFDirIndex=0;
  79.     PBGetFInfo(iop, FALSE);
  80. }
  81.  
  82. SetFileInfo(vol,name,iop)
  83. short vol;
  84. char *name;
  85. FileParam *iop;
  86. {
  87.     iop->ioNamePtr = name;
  88.     iop->ioVRefNum=vol;
  89.     iop->ioFVersNum=iop->ioFDirIndex=0;
  90.     PBSetFInfo(iop, FALSE);
  91. }
  92.  
  93. MakeTextFile(vol,name,iop)
  94. short vol;
  95. char *name;
  96. FileParam *iop;
  97. {    GetFileInfo(vol,name,iop);
  98.     iop->ioFlFndrInfo.fdType='TEXT';
  99.     iop->ioFlFndrInfo.fdCreator='EDIT';
  100.     SetFileInfo(vol,name,iop);
  101. }
  102.  
  103. isMacBinary(p)
  104. MBHead *p;
  105. {
  106.     return( (p->nlen > 0)   &&
  107.             (p->nlen < 65)  &&
  108.             (p->zero1 == 0) &&
  109.             (p->zero2 == 0) &&
  110.             (p->zero3 == 0));
  111. }
  112.  
  113. MBsize( mbfp )
  114. MBFile *mbfp;
  115. {
  116.     long int size;
  117.     int ret;
  118.     
  119.     size = 0;
  120.     
  121.     ret = GetEOF( mbfp->fd, &size );            /* length of file data fork */
  122.     if (ret != noErr) 
  123.         size = 0;
  124.  
  125.     return(size);
  126. }
  127.  
  128. MBFile *MBopen( file, vrefnum, mode)
  129. char *file;
  130. short vrefnum,mode;
  131. {
  132.     MBFile *mbfp;
  133.     int err;
  134.  
  135.     if (strlen(file) > 63)             /* BYU mod */
  136.         return(0L);                    /* BYU mod */
  137.  
  138.     if (mode & MB_WRITE)
  139.         putln("MBOpen for write");
  140.     else 
  141.         putln("MBOpen for read");
  142.  
  143.     if (mode & MB_DISABLE)
  144.         putln("MacBinary Protocol Disabled");
  145.  
  146.     mbfp=malloc( sizeof(MBFile));
  147.     if (mbfp==0L)
  148.         return(0L);
  149.     movmem( file, mbfp->name,64);
  150.     putln(mbfp->name);
  151.     c2pstr(mbfp->name);
  152.     mbfp->vrefnum=vrefnum;
  153.     mbfp->mode = mode;
  154.  
  155.     if ((err=FSOPEN( mbfp->name, vrefnum, &mbfp->fd))) {
  156.         if ((err==-43) && (mode & MB_WRITE)) {
  157.             CREATE( mbfp->name, vrefnum, '????','TEXT');
  158.             if (FSOPEN( mbfp->name, vrefnum, &mbfp->fd)) 
  159.                 return( 0L);
  160.             }
  161.         else 
  162.             return(0L);
  163.         }
  164.  
  165.     mbfp->binary=0;
  166.     mbfp->sector1=1;
  167.     mbfp->fork=0;
  168.     return( mbfp);
  169. }
  170.  
  171. MBwrite( out, buffer, size)
  172. MBFile *out;
  173. int size;
  174. char *buffer;
  175. {
  176.     int rsize;
  177.     
  178.     if (size < 1)
  179.         return(0);
  180.  
  181.     rsize=size;
  182.  
  183.     if (out->sector1 && (size >= sizeof(struct MBHead)) &&
  184.         isMacBinary(buffer) && (!(out->mode & MB_DISABLE))) {
  185.         putln("First sector of MacBinary file");
  186.         ProcessMBHead( out, buffer);
  187.         buffer+=128;
  188.         if ((size-=128) <1)
  189.             return(rsize);
  190.         }
  191.  
  192.     if (bwrite( out,buffer,size))
  193.         return(-1);
  194.     else
  195.         return( rsize);
  196. }
  197.  
  198. bwrite( out, buffer, size)
  199. MBFile *out;
  200. int size;
  201. char *buffer;
  202. {
  203.     long len=size;
  204.     int error;
  205.  
  206.     error = 0;
  207.     
  208.     if (out->binary) {
  209.         if (out->bytes>0) {
  210.             if (out->bytes < len) len = out->bytes;
  211.             error= FSWrite( out->fd, &len, buffer);
  212.             out->bytes -= len;
  213.             buffer +=len;
  214.             size -= (int)len;
  215.             }
  216.         if (out->bytes<= 0) {
  217.             if (!out->fork) {
  218.                 out->fork=1;
  219.                 out->bytes=BLOCKS(out->rlen)*128;
  220.                 SetEOF( out->fd, (long) out->dlen);
  221.                 FSClose( out->fd);
  222.                 if (out->bytes) {
  223.                     OPENRF( out->name, out->vrefnum,&out->fd);
  224.                     if (size) {
  225.                         len = (long) size;
  226.                         error= FSWrite( out->fd, &len, buffer);
  227.                         }
  228.                     }
  229.                 else
  230.                     out->fd=0;
  231.                 }
  232.             else SetEOF( out->fd, (long) out->rlen);
  233.             }
  234.         }
  235.     else {
  236.         error = FSWrite( out->fd, &len, buffer);
  237.         }
  238.     return (error);
  239. }
  240.  
  241. MBclose( out)
  242. MBFile *out;
  243. {
  244.     FileParam finfo;
  245.     long fpos;
  246.  
  247.     putln("MBclose");
  248.     
  249.     if (!out->fd)
  250.         return;
  251.  
  252.     if (!(out->mode & MB_DISABLE) && (out->mode & MB_WRITE)) {
  253.         if (out->fork)
  254.             SetEOF( out->fd, (long) out->rlen);
  255.         else
  256.             SetEOF( out->fd, (long) out->dlen);
  257.  
  258.         FSClose( out->fd);
  259.         GetFileInfo( 0, out->name, &finfo);
  260.     
  261.         movmem( &out->header.type[0], &finfo.ioFlFndrInfo, sizeof(FInfo));
  262.         lmove( &out->header.cdate[0], &finfo.ioFlCrDat);
  263.         lmove( &out->header.mdate[0], &finfo.ioFlMdDat);
  264.         finfo.ioFlFndrInfo.fdFlags &= 0xfeff;
  265.         finfo.ioFlRLgLen=out->rlen;
  266.         finfo.ioFlLgLen =out->dlen;
  267.     
  268.         SetFileInfo( 0, out->name, &finfo);
  269.         }
  270.     else if (out->mode & MB_WRITE) {
  271.         GetFPos( out->fd, &fpos);
  272.         SetEOF(  out->fd,  fpos);
  273.         FSClose( out->fd);
  274.         }
  275.     else
  276.         FSClose( out->fd);
  277. }
  278.  
  279. ProcessMBHead( out, header)
  280. MBFile *out;
  281. MBHead *header;
  282. {
  283.     int err;
  284.  
  285.     movmem( header, &out->header, sizeof(MBHead));
  286.     out->binary=1;
  287.     lmove( &header->dflen[0], &out->dlen);
  288.     lmove( &header->rflen[0], &out->rlen);
  289.     out->bytes= BLOCKS(out->dlen)*128;
  290.     out->fork=0;
  291.     out->sector1=0;
  292.  
  293.     FSClose(out->fd);
  294.     if (FSDELETE( out->name, out->vrefnum))
  295.         putln("Error Deleting Old File ");
  296.         
  297.     movmem( &out->header.nlen, out->name,63);
  298.     
  299.     MBstat( &out->header.nlen, 1, (int)(BLOCKS(out->dlen)+BLOCKS(out->rlen)) );
  300.  
  301.     if (out->bytes) {
  302.         if ((err=FSOPEN( out->name, out->vrefnum, &out->fd))) {
  303.             if (err=-43) {
  304.                 long cre,typ;
  305.  
  306. /*  this crashes Mac Pluses                
  307.                 typ = *(long *)out->header.type;
  308.                 cre = *(long *)out->header.creator;
  309. */
  310.                 lmove(out->header.type, &typ);
  311.                 lmove(out->header.creator, &cre);
  312.  
  313.                 CREATE( out->name, out->vrefnum, cre,typ);
  314.                 if (FSOPEN( out->name, out->vrefnum, &out->fd)) 
  315.                     return( 0L);
  316.                 }
  317.             else {
  318.                 return(0L);
  319.                 }
  320.             }
  321.         }
  322.     else {
  323.         if ((err=OPENRF( out->name, out->vrefnum, &out->fd))) {
  324.             if (err=-43) {
  325.                 long cre,typ;
  326.                 
  327. /*  this crashes Mac Pluses                
  328.                 typ = *(long *)out->header.type;
  329.                 cre = *(long *)out->header.creator;
  330. */
  331.                 lmove(out->header.type, &typ);
  332.                 lmove(out->header.creator, &cre);
  333.  
  334.                 CREATE( out->name, out->vrefnum, cre,typ);
  335.                 if (OPENRF( out->name, out->vrefnum, &out->fd)) 
  336.                     return( 0L);
  337.                 }
  338.             else {
  339.                 return(0L);
  340.                 }
  341.             }
  342.         out->fork = 1;
  343.         out->bytes=BLOCKS(out->rlen)*128;
  344.         }
  345. }
  346.  
  347. MBread( in, buffer, size)
  348. MBFile *in;
  349. int size;
  350. char *buffer;
  351. {
  352.     char *p;
  353.     int rsize=size;
  354.  
  355.  
  356.     if (in->fork<0) {
  357.         return(-1);
  358.         }
  359.  
  360.     p=buffer;
  361.     if (in->sector1) {
  362.         FileParam finfo;
  363.  
  364.         setmem( &in->header, sizeof(MBHead), 0);
  365.         movmem(  in->name, &in->header.nlen, 64);
  366.         GetFileInfo( in->vrefnum, in->name, &finfo);
  367.         movmem( &finfo.ioFlFndrInfo, &in->header.type[0], sizeof(FInfo) );
  368.         in->header.protected = (in->header.zero2 & 0x40)?1:0;
  369.         in->header.zero2 = 0;
  370.         lmove( &finfo.ioFlLgLen, &in->header.dflen[0]);
  371.         lmove( &finfo.ioFlRLgLen,&in->header.rflen[0]);
  372.         lmove( &finfo.ioFlCrDat, &in->header.cdate[0]);
  373.         lmove( &finfo.ioFlMdDat, &in->header.mdate[0]);
  374.         in->dlen=finfo.ioFlLgLen;
  375.         in->rlen=finfo.ioFlRLgLen;
  376.         if (! (in->mode & MB_DISABLE) ) {
  377.             if (size<128) return(-1);
  378.  
  379.             movmem( &in->header, p, 128);
  380.             p +=128;
  381.             size -= 128;
  382.             in->bytes= BLOCKS(in->dlen)*128;
  383.             in->binary=1;
  384.             }
  385.         else {
  386.             in->bytes = in->dlen;
  387.             in->rlen=0;
  388.             in->binary=0;
  389.             }
  390.         in->sector1=0;
  391.         MBstat( &in->header.nlen, 1, (int) (BLOCKS(in->dlen)+BLOCKS(in->rlen)) );
  392.         }
  393.  
  394.     if ( size >0) {
  395.         long length = (long)size;
  396.         int err;
  397.  
  398.         err = FSRead( in->fd, &length, p);
  399.  
  400.         size -=(int)length;
  401.         in->bytes -=length;
  402.         p += length;
  403.  
  404.         if (err == -39 || (in->bytes<=0) ) {
  405.             FSClose( in->fd );
  406.             if (in->bytes<0L) in->bytes=0L;
  407.             size -= (int)in->bytes;
  408.             p    +=      in->bytes;                /* Make adjustments for necessary 128 byte term */
  409.             if (!in->fork ) {
  410.                 in->fork=1;
  411.                 in->bytes= BLOCKS(in->rlen)*128;
  412.                 if (in->bytes) {
  413.                     OPENRF( in->name, in->vrefnum, &in->fd);
  414. #ifdef READ
  415.                     length=(long)size;
  416.                     if (length >0L) {
  417.                         err = FSRead( in->fd, &length, p);
  418.                         size -= (int)length;
  419.                         in->bytes -=length;
  420.                         }
  421. #endif READ
  422.                     }
  423.                 else {
  424.                     in->fd=0;
  425.                     in->fork=-1;                    /* Time to close up shop */
  426.                     }
  427.                 }
  428.             else {
  429.                 in->fd=0;
  430.                 in->fork=-1;                    /* Time to close up shop */
  431.                 }
  432.             }
  433.         }
  434.     return( rsize-size); 
  435. }
  436.