home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char *SCCSid = "%W% (NCSA) %G%";
- #endif
- /*
- * binsubs.c
- * by Gaige B. Paulsen
- ****************************************************************************
- * *
- * Uses : *
- * TCP/IP kernel for NCSA Telnet *
- * by Tim Krauskopf *
- * with Macintosh code by Gaige B. Paulsen *
- * *
- * National Center for Supercomputing Applications *
- * 152 Computing Applications Building *
- * 605 E. Springfield Ave. *
- * Champaign, IL 61820 *
- * *
- * *
- ****************************************************************************
- *
- * MacBinary Subroutines.
- *
- * Called by:
- * bkgr.c
- */
-
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Files.h>
- #include <Packages.h>
- #include <Memory.h>
-
- #include <stdio.h>
- #include <fcntl.h>
- #include "mpw.h"
- #include <string.h>
- /*#include <strings.h> /* BYU LSC */
- #include "configrec.h"
- #include "maclook.h"
- #include "bkgr.h"
- #include "MacBinary.h"
-
- extern MBFile /* BYU */
- *ftp_mbfp, /* BYU */
- *mbfp; /* BYU */
-
- #if 0 /* BYU LSC */
- char *strncpy();
- #endif /* BYU LSC */
-
- #define BLOCKS(x) ((x+127)/128)
- /*#define lmove(f,t) memmove(f,t,(size_t) 4) /* BYU LSC */
-
- #define MyFavouriteTextEditor '????'
-
- static ResType creator = 'EDIT'; /* BYU LSC */
- static ResType filetype = 'TEXT'; /* BYU LSC */
-
- MBHead
- *mbh;
- char
- buffer[128];
-
- char *strsave( p)
- char *p;
- {
- char *t;
-
- t=NewPtr(strlen(p));
- return strncpy(t,p,strlen(p));
- }
-
- GetFileInfo(vol,name,iop)
- short vol;
- char *name;
- FileParam *iop;
- {
- iop->ioNamePtr = (unsigned char *) name; /* BYU LSC */
- iop->ioVRefNum=vol;
- iop->ioFVersNum=iop->ioFDirIndex=0;
- PBGetFInfo((ParmBlkPtr) iop, FALSE);
- }
-
- SetFileInfo(vol,name,iop)
- short vol;
- char *name;
- FileParam *iop;
- {
- iop->ioNamePtr = (unsigned char *) name; /* BYU LSC */
- iop->ioVRefNum=vol;
- iop->ioFVersNum=iop->ioFDirIndex=0;
- PBSetFInfo((ParmBlkPtr) iop, FALSE);
- }
-
- MakeTextFile(vol,name,iop)
- short vol;
- char *name;
- FileParam *iop;
- { GetFileInfo(vol,name,iop);
- iop->ioFlFndrInfo.fdType = 'TEXT';
- iop->ioFlFndrInfo.fdCreator = MyFavouriteTextEditor;
- SetFileInfo(vol,name,iop);
- }
-
- isMacBinary(p)
- MBHead *p;
- {
- return( (p->nlen > 0) &&
- (p->nlen < 65) &&
- (p->zero1 == 0) &&
- (p->zero2 == 0) &&
- (p->zero3 == 0));
- }
-
- int MBsize
- (
- MBFile *mbfp
- )
- {
- long int size;
- int ret;
-
- size = 0;
-
- ret = GetEOF( mbfp->fd, &size ); /* length of file data fork */
- if (ret != noErr)
- size = 0;
-
- return(size);
- }
-
- MBFile *MBopen( file, vrefnum, mode)
- char *file;
- short vrefnum,mode;
- {
- MBFile *mbfp;
- int err;
-
- if (strlen(file) > 63) /* BYU */
- return(0L); /* BYU */
-
- #if 0 /* BYU 2.4.12 */
- if (mode & MB_WRITE)
- putln("MBOpen/write"); /* BYU LSC */
- else
- putln("MBOpen/read"); /* BYU LSC */
-
- if (mode & MB_DISABLE)
- putln("MacBinary Disabled"); /* BYU LSC */
- #endif /* BYU 2.4.12 */
-
- mbfp = (MBFile *) NewPtr( (long) sizeof(MBFile)); /* BYU LSC */
- memset(mbfp, 0, (long) sizeof(MBFile)); /* BYU 2.4.15 */
- if (mbfp==0L)
- return(0L);
-
- if (strlen(file) > 64) /* BYU 2.4.16 */
- memmove(mbfp->name, file, (size_t) 64); /* BYU 2.4.16 */
- else /* BYU 2.4.16 */
- strcpy(mbfp->name, file); /* BYU 2.4.16 */
-
- putln(mbfp->name);
- c2pstr(mbfp->name);
- mbfp->vrefnum=vrefnum;
- mbfp->mode = mode;
-
- if ((err=FSOpen( mbfp->name, vrefnum, &mbfp->fd))) {
- if ((err==-43) && (mode & MB_WRITE)) {
- Create( mbfp->name, vrefnum, creator, filetype); /* BYU LSC */
- if (FSOpen( mbfp->name, vrefnum, &mbfp->fd))
- return( 0L);
- }
- else
- return(0L);
- }
-
- mbfp->binary=0;
- mbfp->sector1=1;
- mbfp->fork=0;
- return( mbfp);
- }
-
- int bwrite
- (
- MBFile *out,
- char *buffer,
- int size
- )
- {
- long len=size;
- int error;
-
- error = 0;
-
- if (out->binary) {
- if (out->bytes>0) {
- if (out->bytes < len) len = out->bytes;
- error= FSWrite( out->fd, &len, buffer);
- out->bytes -= len;
- buffer +=len;
- size -= (int)len;
- }
- if (out->bytes<= 0) {
- if (!out->fork) {
- out->fork=1;
- out->bytes=BLOCKS(out->rlen)*128;
- SetEOF( out->fd, (long) out->dlen);
- FSClose( out->fd);
- if (out->bytes) {
- OpenRF( out->name, out->vrefnum,&out->fd);
- if (size) {
- len = (long) size;
- error= FSWrite( out->fd, &len, buffer);
- }
- }
- else
- out->fd=0;
- }
- else SetEOF( out->fd, (long) out->rlen);
- }
- }
- else {
- error = FSWrite( out->fd, &len, buffer);
- }
- return (error);
- }
-
- void ProcessMBHead
- (
- MBFile *out,
- MBHead *header
- )
- {
- int err;
-
- memmove((char *) &out->header,(char *) header,(size_t) sizeof(MBHead)); /* BYU LSC */
- out->binary=1;
- memmove((char *) &out->dlen, &header->dflen[0], (size_t) 4); /* BYU LSC */
- memmove((char *) &out->rlen, &header->rflen[0], (size_t) 4); /* BYU LSC */
- out->bytes= BLOCKS(out->dlen)*128;
- out->fork=0;
- out->sector1=0;
-
- FSClose(out->fd);
- if (FSDelete( out->name, out->vrefnum))
- putln("Error Deleting Old File ");
-
- memmove(out->name, &out->header.nlen, (size_t) 63); /* BYU LSC */
-
- #if 0 /* BYU 2.4.17 */
- MBstat( &out->header.nlen, 1, (int)(BLOCKS(out->dlen)+BLOCKS(out->rlen)) );
- #endif /* BYU 2.4.17 */
-
- if (out->bytes) {
- if ((err=FSOpen( out->name, out->vrefnum, &out->fd))) {
- if (err=-43) {
- long cre,typ;
-
- /* this crashes Mac Pluses
- typ = *(long *)out->header.type;
- cre = *(long *)out->header.creator;
- */
- memmove((char *) &typ, out->header.type, (size_t) 4); /* BYU LSC */
- memmove((char *) &cre, out->header.creator, (size_t) 4); /* BYU LSC */
-
- Create( out->name, out->vrefnum, cre,typ);
- if (FSOpen( out->name, out->vrefnum, &out->fd))
- return;
- }
- else {
- return;
- }
- }
- }
- else {
- if ((err=OpenRF( out->name, out->vrefnum, &out->fd))) {
- if (err=-43) {
- long cre,typ;
-
- /* this crashes Mac Pluses
- typ = *(long *)out->header.type;
- cre = *(long *)out->header.creator;
- */
- memmove((char *) &typ, out->header.type, (size_t) 4); /* BYU LSC */
- memmove((char *) &cre, out->header.creator, (size_t) 4); /* BYU LSC */
-
- Create( out->name, out->vrefnum, cre,typ);
- if (OpenRF( out->name, out->vrefnum, &out->fd))
- return;
- }
- else {
- return;
- }
- }
- out->fork = 1;
- out->bytes=BLOCKS(out->rlen)*128;
- }
- }
-
- int MBwrite
- (
- MBFile *out,
- char *buffer, /* BYU LSC */
- int size
- )
- {
- int rsize;
-
- if (size < 1)
- return(0);
-
- rsize=size;
-
- if (out->sector1 && (size >= sizeof(struct MBHead)) &&
- isMacBinary((MBHead *) buffer) && (!(out->mode & MB_DISABLE))) { /* BYU LSC */
- putln("First sector MacBinary file"); /* BYU LSC */
- ProcessMBHead( out, (MBHead *) buffer);
- buffer+=128; /* BYU LSC */
- if ((size-=128) <1)
- return(rsize);
- }
-
- if (bwrite( out,buffer,size))
- return(-1);
- else
- return( rsize);
- }
-
- void MBclose
- (
- MBFile *out
- )
- {
- FileParam finfo;
- long fpos;
-
- putln("MBclose");
-
- if (!out->fd)
- return;
-
- if (!(out->mode & MB_DISABLE) && (out->mode & MB_WRITE)) {
- if (out->fork)
- SetEOF( out->fd, (long) out->rlen);
- else
- SetEOF( out->fd, (long) out->dlen);
-
- FSClose( out->fd);
- GetFileInfo( 0, out->name, &finfo);
-
- memmove((char *) &finfo.ioFlFndrInfo, &out->header.type[0], (size_t) sizeof(FInfo)); /* BYU LSC */
- memmove((char *) &finfo.ioFlCrDat, &out->header.cdate[0], (size_t) 4); /* BYU LSC */
- memmove((char *) &finfo.ioFlMdDat, &out->header.mdate[0], (size_t) 4); /* BYU LSC */
- finfo.ioFlFndrInfo.fdFlags &= 0xfeff;
- finfo.ioFlRLgLen=out->rlen;
- finfo.ioFlLgLen =out->dlen;
-
- SetFileInfo( 0, out->name, &finfo);
- }
- else if (out->mode & MB_WRITE) {
- GetFPos( out->fd, &fpos);
- SetEOF( out->fd, fpos);
- FSClose( out->fd);
- }
- else
- FSClose( out->fd);
- }
-
- int MBread
- (
- MBFile *in,
- void *buffer,
- int size
- )
- {
- char *p;
- int rsize=size;
-
-
- if (in->fork<0) {
- return(-1);
- }
-
- p=buffer;
- if (in->sector1) {
- FileParam finfo;
-
- setmem( &in->header, sizeof(MBHead), 0);
- memmove(&in->header.nlen, in->name, (size_t) 64); /* BYU LSC */
- GetFileInfo( in->vrefnum, in->name, &finfo);
- memmove((char *) &in->header.type[0],(char *) &finfo.ioFlFndrInfo,(size_t) sizeof(FInfo) ); /* BYU LSC */
- in->header.protected = (in->header.zero2 & 0x40)?1:0;
- in->header.zero2 = 0;
- memmove((char *) &in->header.dflen[0], (char *) &finfo.ioFlLgLen, (size_t) 4); /* BYU LSC */
- memmove((char *) &in->header.rflen[0], (char *) &finfo.ioFlRLgLen, (size_t) 4); /* BYU LSC */
- memmove((char *) &in->header.cdate[0], (char *) &finfo.ioFlCrDat, (size_t) 4); /* BYU LSC */
- memmove((char *) &in->header.mdate[0], (char *) &finfo.ioFlMdDat, (size_t) 4); /* BYU LSC */
- in->dlen=finfo.ioFlLgLen;
- in->rlen=finfo.ioFlRLgLen;
- if (! (in->mode & MB_DISABLE) ) {
- if (size<128) return(-1);
-
- memmove(p, (char *) &in->header, (size_t) 128); /* BYU LSC */
- p +=128;
- size -= 128;
- in->bytes= BLOCKS(in->dlen)*128;
- in->binary=1;
- }
- else {
- in->bytes = in->dlen;
- in->rlen=0;
- in->binary=0;
- }
- in->sector1=0;
- #if 0 /* BYU 2.4.17 */
- MBstat( &in->header.nlen, 1, (int) (BLOCKS(in->dlen)+BLOCKS(in->rlen)) );
- #endif /* BYU 2.4.17 */
- }
-
- if ( size >0) {
- long length = (long)size;
- int err;
-
- err = FSRead( in->fd, &length, p);
-
- size -=(int)length;
- in->bytes -=length;
- p += length;
-
- if (err == -39 || (in->bytes<=0) ) {
- FSClose( in->fd );
- if (in->bytes<0L) in->bytes=0L;
- size -= (int)in->bytes;
- p += in->bytes; /* Make adjustments for necessary 128 byte term */
- if (!in->fork ) {
- in->fork=1;
- in->bytes= BLOCKS(in->rlen)*128;
- if (in->bytes) {
- OpenRF( in->name, in->vrefnum, &in->fd);
- #ifdef READ
- length=(long)size;
- if (length >0L) {
- err = FSRead( in->fd, &length, p);
- size -= (int)length;
- in->bytes -=length;
- }
- #endif READ
- }
- else {
- in->fd=0;
- in->fork=-1; /* Time to close up shop */
- }
- }
- else {
- in->fd=0;
- in->fork=-1; /* Time to close up shop */
- }
- }
- }
- return( rsize-size);
- }
-
- init_mb_files() { /* BYU */
- ftp_mbfp->fd = 0; /* BYU */
- mbfp->fd = 0; /* BYU */
- } /* BYU */
- /* BYU */
- close_mb_files() { /* BYU */
- if (ftp_mbfp->fd != 0) MBclose( ftp_mbfp ); /* BYU - close input file */
- if (mbfp->fd != 0) MBclose( mbfp ); /* BYU - close input file */
- } /* BYU */
-