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 >
Wrap
Text File
|
1990-04-10
|
9KB
|
436 lines
#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 "stdio.h"
#include "fcntl.h"
#include "::main:mpw.h"
#include "Windows.h"
#include "Files.h"
#include "Packages.h"
#include "MacBinary.h"
extern MBFile /* BYU mod */
*ftp_mbfp, /* BYU mod */
*mbfp; /* BYU mod */
char *strncpy(), *malloc();
#define BLOCKS(x) ((x+127)/128)
#define lmove(f,t) movmem(f,t,4)
MBHead
*mbh;
char
buffer[128];
char *strsave( p)
char *p;
{
char *t;
t=malloc(strlen(p));
strncpy(t,p,strlen(p));
}
init_mb_files() { /* BYU mod */
ftp_mbfp->fd = 0; /* BYU mod */
mbfp->fd = 0; /* BYU mod */
} /* BYU mod */
/* BYU mod */
close_mb_files() { /* BYU mod */
if (ftp_mbfp->fd != 0) MBclose( ftp_mbfp ); /* BYU mod - close input file */
if (mbfp->fd != 0) MBclose( mbfp ); /* BYU mod - close input file */
} /* BYU mod */
GetFileInfo(vol,name,iop)
short vol;
char *name;
FileParam *iop;
{
iop->ioNamePtr = name;
iop->ioVRefNum=vol;
iop->ioFVersNum=iop->ioFDirIndex=0;
PBGetFInfo(iop, FALSE);
}
SetFileInfo(vol,name,iop)
short vol;
char *name;
FileParam *iop;
{
iop->ioNamePtr = name;
iop->ioVRefNum=vol;
iop->ioFVersNum=iop->ioFDirIndex=0;
PBSetFInfo(iop, FALSE);
}
MakeTextFile(vol,name,iop)
short vol;
char *name;
FileParam *iop;
{ GetFileInfo(vol,name,iop);
iop->ioFlFndrInfo.fdType='TEXT';
iop->ioFlFndrInfo.fdCreator='EDIT';
SetFileInfo(vol,name,iop);
}
isMacBinary(p)
MBHead *p;
{
return( (p->nlen > 0) &&
(p->nlen < 65) &&
(p->zero1 == 0) &&
(p->zero2 == 0) &&
(p->zero3 == 0));
}
MBsize( mbfp )
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 mod */
return(0L); /* BYU mod */
if (mode & MB_WRITE)
putln("MBOpen for write");
else
putln("MBOpen for read");
if (mode & MB_DISABLE)
putln("MacBinary Protocol Disabled");
mbfp=malloc( sizeof(MBFile));
if (mbfp==0L)
return(0L);
movmem( file, mbfp->name,64);
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, '????','TEXT');
if (FSOPEN( mbfp->name, vrefnum, &mbfp->fd))
return( 0L);
}
else
return(0L);
}
mbfp->binary=0;
mbfp->sector1=1;
mbfp->fork=0;
return( mbfp);
}
MBwrite( out, buffer, size)
MBFile *out;
int size;
char *buffer;
{
int rsize;
if (size < 1)
return(0);
rsize=size;
if (out->sector1 && (size >= sizeof(struct MBHead)) &&
isMacBinary(buffer) && (!(out->mode & MB_DISABLE))) {
putln("First sector of MacBinary file");
ProcessMBHead( out, buffer);
buffer+=128;
if ((size-=128) <1)
return(rsize);
}
if (bwrite( out,buffer,size))
return(-1);
else
return( rsize);
}
bwrite( out, buffer, size)
MBFile *out;
int size;
char *buffer;
{
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);
}
MBclose( out)
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);
movmem( &out->header.type[0], &finfo.ioFlFndrInfo, sizeof(FInfo));
lmove( &out->header.cdate[0], &finfo.ioFlCrDat);
lmove( &out->header.mdate[0], &finfo.ioFlMdDat);
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);
}
ProcessMBHead( out, header)
MBFile *out;
MBHead *header;
{
int err;
movmem( header, &out->header, sizeof(MBHead));
out->binary=1;
lmove( &header->dflen[0], &out->dlen);
lmove( &header->rflen[0], &out->rlen);
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 ");
movmem( &out->header.nlen, out->name,63);
MBstat( &out->header.nlen, 1, (int)(BLOCKS(out->dlen)+BLOCKS(out->rlen)) );
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;
*/
lmove(out->header.type, &typ);
lmove(out->header.creator, &cre);
CREATE( out->name, out->vrefnum, cre,typ);
if (FSOPEN( out->name, out->vrefnum, &out->fd))
return( 0L);
}
else {
return(0L);
}
}
}
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;
*/
lmove(out->header.type, &typ);
lmove(out->header.creator, &cre);
CREATE( out->name, out->vrefnum, cre,typ);
if (OPENRF( out->name, out->vrefnum, &out->fd))
return( 0L);
}
else {
return(0L);
}
}
out->fork = 1;
out->bytes=BLOCKS(out->rlen)*128;
}
}
MBread( in, buffer, size)
MBFile *in;
int size;
char *buffer;
{
char *p;
int rsize=size;
if (in->fork<0) {
return(-1);
}
p=buffer;
if (in->sector1) {
FileParam finfo;
setmem( &in->header, sizeof(MBHead), 0);
movmem( in->name, &in->header.nlen, 64);
GetFileInfo( in->vrefnum, in->name, &finfo);
movmem( &finfo.ioFlFndrInfo, &in->header.type[0], sizeof(FInfo) );
in->header.protected = (in->header.zero2 & 0x40)?1:0;
in->header.zero2 = 0;
lmove( &finfo.ioFlLgLen, &in->header.dflen[0]);
lmove( &finfo.ioFlRLgLen,&in->header.rflen[0]);
lmove( &finfo.ioFlCrDat, &in->header.cdate[0]);
lmove( &finfo.ioFlMdDat, &in->header.mdate[0]);
in->dlen=finfo.ioFlLgLen;
in->rlen=finfo.ioFlRLgLen;
if (! (in->mode & MB_DISABLE) ) {
if (size<128) return(-1);
movmem( &in->header, p, 128);
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;
MBstat( &in->header.nlen, 1, (int) (BLOCKS(in->dlen)+BLOCKS(in->rlen)) );
}
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);
}