home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
TELECOM
/
stg_v4.lzh
/
db9.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-11-11
|
11KB
|
682 lines
#define ERR (-1)
#include "db9.h"
extern int errno;
#define DB9_Version 0x01
#define DB9_CopyRight "DB9 File Format (c) 1992 by Scott Griepentrog"
#define MAX_Paths 32
#define MAX_BlockSize 512
#define DEF_BlockSize 512
struct sPath_Rec
{
C acFile[16]; /* name of file (dataset) */
C acKey[16]; /* name of key */
H hFile; /* file handle */
O oFile; /* current file (datset) descriptor */
O oPos; /* current record position */
B* pbData; /* data ptr argument */
I iLen; /* length argument
W wBlockSize; /* blocksize of db9 file */
B bFile; /* current file code number */
B bLock; /* header locked */
B* pbMatch; /* seek match map (-1=case insensitive) */
struct sHdr_Block sHead;
};
struct sPath_Rec *apsPath[MAX_Paths];
#define pbBlk _db9_blk
#define psHdr ((struct sHdr_Block*)_db9_blk)
#define psFil ((struct sFil_Block*)_db9_blk)
B* _db9_blk;
B* malloc();
/* identify header or create one if file empty */
_db9_head(p)
struct sPath_Rec *p;
{
int n;
if (!_gs_size(p->hFile))
{
memset(&(p->sHead),0,sizeof(struct sHdr_Block));
p->sHead.bBlk_Type=BT_HEAD;
p->sHead.wHdr_BlockSize=DEF_BlockSize;
p->sHead.acDB9_Label[0]='D';
p->sHead.acDB9_Label[1]='B';
p->sHead.acDB9_Label[2]='9';
p->sHead.bHdr_Version=DB9_Version;
p->sHead.oHdr_FileSize=DEF_BlockSize;
strcpy(p->sHead.acBlk_FileName,".");
strcpy(p->sHead.acHdr_CopyRight,DB9_CopyRight);
memset(pbBlk,0,DEF_BlockSize);
memcpy(pbBlk,&(p->sHead),sizeof(struct sHdr_Block));
if (write(p->hFile,pbBlk,DEF_BlockSize)==ERR)
return(ERR);
return(0);
}
lseek(p->hFile,0L,0);
n=read(p->hFile,&(p->sHead),sizeof(struct sHdr_Block));
if (n==ERR)
return(ERR);
if
(
n!=sizeof(struct sHdr_Block) ||
p->sHead.bBlk_Type!=BT_HEAD ||
(p->sHead.wHdr_BlockSize&0x00FF) ||
p->sHead.wHdr_BlockSize>MAX_BlockSize ||
p->sHead.acDB9_Label[0]!='D' ||
p->sHead.acDB9_Label[1]!='B' ||
p->sHead.acDB9_Label[2]!='9' ||
p->sHead.bHdr_Version>DB9_Version
)
{
errno=DE_BADHDR;
return(ERR);
}
_ss_lock(p->hFile,0);
return(0);
}
_db9_lock(p)
struct sPath_Rec *p;
{
if (p->bLock)
return(0);
_ss_lock(p->hFile,-1);
lseek(p->hFile,0L,0);
if (read(p->hFile,&(p->sHead),sizeof(struct sHdr_Block))==ERR)
return(ERR);
p->bLock++;
return(0);
}
_db9_ulock(p)
struct sPath_Rec *p;
{
if (!p->bLock)
return(0);
lseek(p->hFile,0L,0);
if (write(p->hFile,&(p->sHead),sizeof(struct sHdr_Block))==ERR)
return(ERR);
p->bLock=0;
return(0);
}
_db9_gblk(p,pos)
struct sPath_Rec *p;
O pos;
{
int n;
pos&=~(p->sHead.wHdr_BlockSize-1);
if (pos>p->sHead.oHdr_FileSize)
{
errno=DE_BADBLK;
return(ERR);
}
if (lseek(p->hFile,pos,0)==ERR)
return(ERR);
n=read(p->hFile,pbBlk,p->sHead.wHdr_BlockSize);
if (n==ERR)
return(ERR);
if (n!=p->sHead.wHdr_BlockSize)
{
errno=DE_BADSIZ;
return(ERR);
}
return(0);
}
_db9_pblk(p,pos)
struct sPath_Rec *p;
O pos;
{
int n;
pos&=~(p->sHead.wHdr_BlockSize-1);
if (pos>p->sHead.oHdr_FileSize)
{
errno=DE_BADBLK;
return(ERR);
}
if (lseek(p->hFile,pos,0)==ERR)
return(ERR);
n=write(p->hFile,pbBlk,p->sHead.wHdr_BlockSize);
if (n==ERR)
return(ERR);
if (n!=p->sHead.wHdr_BlockSize)
{
errno=DE_BADSIZ;
return(ERR);
}
return(0);
}
long
_db9_gfre(p)
struct sPath_Rec *p;
{
long pos;
long size;
_db9_lock(p);
pos=p->sHead.oHdr_FreeList;
if (!pos)
{
size=p->sHead.oHdr_FileSize;
if (size<16384)
size+=8192;
else
size+=(size/2);
size&=~8191;
memset(pbBlk,0,p->sHead.wHdr_BlockSize);
psHdr->bBlk_Type=BT_FREE;
/* strcpy(psHdr->acBlk_FileName,"*FREE*"); */
pos=p->sHead.oHdr_FileSize;
p->sHead.oHdr_FreeList=pos;
while (pos<size)
{
if (lseek(p->hFile,pos,0)==ERR)
return(ERR);
pos+=p->sHead.wHdr_BlockSize;
if (pos<size)
psHdr->oHdr_FreeList=pos;
else
psHdr->oHdr_FreeList=0;
if (write(p->hFile,pbBlk,p->sHead.wHdr_BlockSize)==ERR)
return(ERR);
}
pos=p->sHead.oHdr_FreeList;
p->sHead.oHdr_FileSize=size;
}
if (_db9_gblk(p,pos)==ERR)
return(ERR);
p->sHead.oHdr_FreeList=psHdr->oHdr_FreeList;
memset(pbBlk,0,p->sHead.wHdr_BlockSize);
return(pos);
}
long
_db9_gfil(p,s)
char *s;
struct sPath_Rec *p;
{
O pos;
pos=p->sHead.oFil_FileList;
while (pos)
{
if (_db9_gblk(p,pos)==ERR)
return(ERR);
if (psFil->bBlk_Type!=BT_FILE)
{
errno=DE_BADTYP;
return(ERR);
}
if (!stricmp(s,psFil->acBlk_FileName))
break;
pos=psFil->oFil_FileList;
}
return(pos);
}
_db9_open(p)
struct sPath_Rec *p;
{
O pos;
pos=strlen((C*)p->pbData);
if (pos>15)
{
errno=DE_BADPRM;
return(ERR);
}
if (pos>p->iLen)
{
errno=DE_BIGDAT;
return(ERR);
}
pos=_db9_gfil(p,(C*)p->pbData);
if (!pos)
{
errno=DE_DSETNF;
return(ERR);
}
strcpy(p->acFile,psFil->acBlk_FileName);
*p->acKey=0;
p->oFile=pos;
p->oPos=0;
p->bFile=psFil->bBlk_File;
_ss_lock(p->hFile,0);
return(0);
}
_db9_make(p)
struct sPath_Rec *p;
{
O pos;
pos=strlen((C*)p->pbData);
if (pos>15)
{
errno=DE_BADPRM;
return(ERR);
}
if (pos<p->iLen)
{
errno=DE_BIGDAT;
return(ERR);
}
if (_db9_lock(p)==ERR)
return(ERR);
pos=_db9_gfil(p,(C*)p->pbData);
if ((int)pos==ERR)
return(ERR);
if (pos)
{
errno=DE_DSETAE;
return(ERR);
}
/* need to check p->sHead.oFil_FreeList */
pos=_db9_gfre(p);
if (pos==ERR)
return(ERR);
p->sHead.bBlk_File++;
memset(pbBlk,0,p->sHead.wHdr_BlockSize);
psFil->bBlk_File=p->sHead.bBlk_File;
psFil->bBlk_Type=BT_FILE;
strcpy(psFil->acBlk_FileName,(C*)p->pbData);
psFil->oFil_FileList=p->sHead.oFil_FileList;
if (_db9_pblk(p,pos)==ERR)
return(ERR);
p->sHead.oFil_FileList=pos;
return(_db9_open(p));
}
_db9_add(p)
struct sPath_Rec *p;
{
O pos;
if (p->iLen > p->sHead.wHdr_BlockSize-4)
{
errno=DE_BADPRM;
return(ERR);
}
pos=_db9_gfre(p);
if (pos==ERR)
return(ERR);
psHdr->bBlk_File=p->bFile;
psHdr->bBlk_Type=BT_DFS;
*((W*)(pbBlk+2))=p->iLen;
memcpy(pbBlk+4,p->pbData,p->iLen);
if (_db9_pblk(p,pos)==ERR)
return(ERR);
return(p->iLen);
}
_db9_comp(pb1,pb2,iLen,pbMatch)
register B* pb1;
register B* pb2;
register I iLen;
B* pbMatch;
{
if (!pbMatch)
{
while (iLen && *pb1==*pb2)
{
iLen--;
pb1++;
pb2++;
}
return(iLen);
}
if ((int)pbMatch==-1)
{
while (iLen && toupper(*pb1)==toupper(*pb2))
{
iLen--;
pb1++;
pb2++;
}
return(iLen);
}
while (iLen && pbMatch[*pb1]==pbMatch[*pb2])
{
iLen--;
pb1++;
pb2++;
}
return(iLen);
}
_db9_seek(p)
struct sPath_Rec *p;
{
O pos;
if (!p->iLen)
{
p->oPos=0;
return(0);
}
pos=p->sHead.wHdr_BlockSize;
while (pos<p->sHead.oHdr_FileSize)
{
if (_db9_gblk(p,pos)==ERR)
return(ERR);
_ss_lock(p->hFile,0);
if (psHdr->bBlk_File==p->bFile && psHdr->bBlk_Type==BT_DFS)
{
if (*((W*)(pbBlk+2)) >= p->iLen)
{
if (!_db9_comp(p->pbData,pbBlk+4,p->iLen,p->pbMatch))
{
p->oPos=pos;
return(1);
}
}
}
pos+=p->sHead.wHdr_BlockSize;
}
p->oPos=0;
_ss_lock(p->hFile,0);
return(0);
}
_db9_read(p)
struct sPath_Rec *p;
{
if (!p->iLen)
{
errno=DE_BADPRM;
return(ERR);
}
if (!p->oPos)
return(0);
if (_db9_gblk(p,p->oPos)==ERR)
return(ERR);
if (psHdr->bBlk_File!=p->bFile)
{
errno=DE_BADFIL;
return(ERR);
}
if (psHdr->bBlk_Type!=BT_DFS)
{
errno=DE_BADTYP;
return(ERR);
}
if (*((W*)(pbBlk+2)) > p->iLen)
{
errno=DE_BIGDAT;
return(ERR);
}
p->iLen=*((W*)(pbBlk+2));
memcpy(p->pbData,pbBlk+4,p->iLen);
return(p->iLen);
}
_db9_write(p)
struct sPath_Rec *p;
{
if (!p->iLen)
{
errno=DE_BADPRM;
return(ERR);
}
if (!p->oPos)
return(0);
if (p->iLen > p->sHead.wHdr_BlockSize-4)
{
errno=DE_BADPRM;
return(ERR);
}
psHdr->bBlk_File=p->bFile;
psHdr->bBlk_Type=BT_DFS;
*((W*)(pbBlk+2))=p->iLen;
memcpy(pbBlk+4,p->pbData,p->iLen);
if (_db9_pblk(p,p->oPos)==ERR)
return(ERR);
return(p->iLen);
}
_db9_next(p)
struct sPath_Rec *p;
{
O pos;
if (!p->iLen)
{
errno=DE_BADPRM;
return(ERR);
}
pos=p->oPos;
pos+=p->sHead.wHdr_BlockSize;
while (pos<p->sHead.oHdr_FileSize)
{
if (_db9_gblk(p,pos)==ERR)
return(ERR);
if (psHdr->bBlk_File==p->bFile && psHdr->bBlk_Type==BT_DFS)
{
p->oPos=pos;
return(_db9_read(p));
}
pos+=p->sHead.wHdr_BlockSize;
}
p->oPos=0;
_ss_lock(p->hFile,0);
return(0);
}
_db9_match(p)
struct sPath_Rec *p;
{
if ((int)(p->pbData)==-1)
{
(int)(p->pbMatch)=-1;
return(0);
}
if (!p->iLen)
{
if (p->pbMatch && (int)(p->pbMatch)!=-1)
free(p->pbMatch);
p->pbMatch=0;
return(0);
}
if (p->iLen>256)
{
errno=DE_BADPRM;
return(ERR);
}
p->pbMatch=malloc(256);
if (!p->pbMatch)
return(ERR);
memset(p->pbMatch,0,256);
memcpy(p->pbMatch,p->pbData,p->iLen);
return(0);
}
_db9(iCode,hFile,pbData,iLen)
I iCode;
H hFile;
B* pbData;
I iLen;
{
if (hFile<0 || hFile>=MAX_Paths)
{
errno=DE_BADPTH;
return(ERR);
}
if (!apsPath[hFile])
{
if (iCode!=DB_MAKE && iCode!=DB_OPEN)
{
errno=DE_BADPTH;
return(ERR);
}
if (!pbBlk)
{
pbBlk=malloc(MAX_BlockSize);
if (!pbBlk)
return(ERR);
}
apsPath[hFile]=(struct sPath_Rec *)malloc(sizeof(struct sPath_Rec));
if (!apsPath[hFile])
return(ERR);
memset(apsPath[hFile],0,sizeof(struct sPath_Rec));
apsPath[hFile]->hFile=hFile;
if (_db9_head(apsPath[hFile])==ERR)
return(ERR);
}
apsPath[hFile]->pbData=pbData;
apsPath[hFile]->iLen=iLen;
switch (iCode)
{
case DB_MAKE:
iLen=_db9_make(apsPath[hFile]);
break;
case DB_OPEN:
iLen=_db9_open(apsPath[hFile]);
break;
case DB_ADD:
iLen=_db9_add(apsPath[hFile]);
break;
case DB_SEEK:
iLen=_db9_seek(apsPath[hFile]);
break;
case DB_READ:
iLen=_db9_read(apsPath[hFile]);
break;
case DB_NEXT:
iLen=_db9_next(apsPath[hFile]);
break;
case DB_WRITE:
iLen=_db9_write(apsPath[hFile]);
break;
case DB_MATCH:
iLen=_db9_match(apsPath[hFile]);
break;
case DB_ULOCK:
iLen=_ss_lock(hFile,0);
break;
default:
errno=DE_BADPRM;
return(ERR);
}
_db9_ulock(apsPath[hFile]);
return(iLen);
}