home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Commun⁄Network / Telnet 2.5.src.ThinkC / source / binsubs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-11  |  10.6 KB  |  473 lines  |  [TEXT/MPS ]

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