home *** CD-ROM | disk | FTP | other *** search
- /*
- * CTCLIB.C module
- *
- * This sample is setup for the
- * UNIX V Operating System Release 2/3
- *
- * (UNIX is a trademark of AT&T)
- *
- * This program is the CONFIDENTIAL and PROPRIETARY property
- * of FairCom(R) Corporation. Any unauthorized use, reproduction or
- * transfer of this program is strictly prohibited.
- *
- * Copyright (c) 1985, 1986, 1987, 1988, 1989 FairCom Corporation
- * (Subject to limited distribution and
- * restricted disclosure only.)
- * *** ALL RIGHTS RESERVED ***
- *
- * 4006 West Broadway
- * Columbia, MO 65203
- *
- *
- * c-tree(R) Version 4.3
- * Release C
- * February 7, 1989 17:30
- *
- */
-
- #include "ctstdr.h" /* standard i/o header */
- #undef EXTERN
- #define EXTERN /* */
- #include "ctoptn.h" /* c-tree configuration options */
- #include "cterrc.h" /* c-tree error codes */
- #include "ctstrc.h" /* c-tree data structures */
- #include "ctgvar.h" /* c-tree global variables */
- #include <unistd.h>
-
- COUNT ctseek(),uerr();
-
- RNDFILE mbopen(ctnum,opmode)
- PFAST CTFILE *ctnum;
- COUNT opmode;
- {
- RNDFILE retval;
- int acflag;
- #ifndef CTSERVER
- int i;
- long lseek();
- #endif
-
-
- COUNT vtclose();
-
- ctnum->sekpos = DRNZERO;
- acflag = O_RDWR;
- if (opmode & READFIL)
- acflag = O_RDONLY;
-
- if (!(opmode & PERMANENT) && ct_numvfil >= MAXVFIL)
- vtclose();
-
- if ((retval = open(ctnum->flname,acflag)) < 0)
- if (vtclose() == YES)
- retval = open(ctnum->flname,acflag);
-
- /* if exclusive open, obtain lock on entire file */
- if (retval > -1 && !(opmode & NONEXCLUSIVE) &&
- lockf(retval,F_TLOCK,0L)) {
- close(retval);
- return(-1);
- }
- #ifdef CTSERVER
- if (retval > -1 && lockf(retval,F_TLOCK,0L)) {
- close(retval);
- return(-1);
- }
- #else
- /* obtain lock on entire file if exclusive open */
- if (retval > -1 && !(opmode & NONEXCLUSIVE)) {
- lseek(retval,ctnum->sekpos = HDRSIZ,0);
- if (lockf(retval,F_TLOCK,0L)) {
- close(retval);
- return(-1);
- }
- } else if (retval > -1 && (opmode & NONEXCLUSIVE)) {
- /* check file type to see if index: no lock on index */
- if (read(retval,ctnum,HDRSIZ) != HDRSIZ) {
- close(retval);
- return(-1);
- } else {
- #ifdef UNIFRMAT
- revhdr(ctnum);
- #endif
- ctnum->sekpos = HDRSIZ;
- }
- if (ctnum->clstyp != IDX_CLOSE) {
- for (i = HDRSIZ; i < 128; i++) {
- lseek(retval,ctnum->sekpos = i,0);
- if (lockf(retval,F_TLOCK,1L) == 0)
- break;
- }
- if (i >= 128) {
- close(retval);
- return(-1);
- }
- }
- }
- #endif
-
-
- if (!(opmode & PERMANENT) && retval >= 0)
- ct_numvfil++;
-
- return(retval);
- }
-
- /* ------------------------------------------------------------ */
-
- RNDFILE mbcrat(ctnum)
- PFAST CTFILE *ctnum;
- {
- RNDFILE retval;
-
- COUNT vtclose();
-
- ctnum->sekpos = DRNZERO;
- if (!(ctnum->flmode & PERMANENT) && ct_numvfil >= MAXVFIL)
- vtclose();
-
- if ((retval = creat(ctnum->flname,BCREATE)) < 0)
- if (vtclose() == YES)
- retval = creat(ctnum->flname,BCREATE);
-
- if (!(ctnum->flmode & PERMANENT) && retval >= 0)
- ct_numvfil++;
-
- return(retval);
- }
-
- COUNT mbsave( ctnum)
- PFAST CTFILE *ctnum;
- {
- sync();
- return(NO_ERROR);
- }
-
- VOID flushdos(datno)
- COUNT datno;
- {
- sync(); /* UNIX system call: schedules dirty buffers for write */
- }
-
- #ifndef CTSERVER
-
- /* --------------------------------------------------------------------
- LOCK index node
- */
-
- COUNT LOCK(node,knum) /* node == 0 => header */
- LONG node;
- PFAST KEYFILE *knum;
- {
-
- /*
- * c-tree node locking protocol is guaranteed to be deadlock free. Therefore,
- * the lock request allows the process to "sleep" if the lock is not
- * immediately available.
- */
-
- #ifdef FPUTFGET
- knum -= knum->kmem;
- if (!(knum->flmode & NONEXCLUSIVE))
- return(NO_ERROR);
-
- if (ctseek(knum,node))
- return(uerr(SEEK_ERR));
- if (lockf(knum->fd,F_LOCK,1L))
- return(uerr(LNOD_ERR));
- else {
- knum->lokcnt++;
- return(NO_ERROR);
- }
- #else
- return(NO_ERROR);
- #endif
-
- }
-
- /* --------------------------------------------------------------------
- UNLOCK index file node
- */
-
- COUNT UNLOCK(node,knum)
- LONG node;
- PFAST KEYFILE *knum;
- {
-
- #ifdef FPUTFGET
- knum -= knum->kmem;
- if (!(knum->flmode & NONEXCLUSIVE))
- return(NO_ERROR);
-
- if (knum->lokcnt)
- knum->lokcnt--;
- if (ctseek(knum,node))
- return(uerr(SEEK_ERR));
- else if (lockf(knum->fd,F_ULOCK,1L))
- return(uerr(UNOD_ERR));
- else
- return(NO_ERROR);
- #else
- return(NO_ERROR);
- #endif
-
- }
-
-
- /* --------------------------------------------------------------------
- LOCK data record
- */
-
- COUNT DLOCK(recbyt,dnum) /* recbyt == 0 => header record */
- POINTER recbyt;
- PFAST DATFILE *dnum;
- {
-
- /*
- * the data record locks are NOT guaranteed to be deadlock free. Therefore,
- * except for the header record which is only locked by internal c-tree
- * maintenance requests, the data record lock routine returns instead of
- * sleeping when a lock is denied because of a competing lock. The application
- * program must deal with the appropriate action if a lock is denied to a
- * data record.
- */
-
- #ifdef FPUTFGET
- COUNT lokreq;
-
- if (!(dnum->flmode & NONEXCLUSIVE))
- return(NO_ERROR);
-
- if (ctseek(dnum,recbyt))
- return(uerr(SEEK_ERR));
-
- if (recbyt == DRNZERO)
- lokreq = F_LOCK; /* allow wait for lock on header only */
- else
- lokreq = F_TLOCK;
-
- if (lockf(dnum->fd,lokreq,1L))
- return(uerr(DLOK_ERR));
- else {
- dnum->lokcnt++;
- return(NO_ERROR);
- }
- #else
- return(NO_ERROR);
- #endif
-
- }
-
- COUNT RLOCK(recbyt,dnum) /* recbyt == 0 => header record */
- POINTER recbyt;
- PFAST DATFILE *dnum;
- {
- return(NO);
- }
-
- /* --------------------------------------------------------------------
- UNLOCK data record
- */
-
- COUNT UDLOCK(recbyt,dnum)
-
- POINTER recbyt;
- PFAST DATFILE *dnum;
-
- {
-
- #ifdef FPUTFGET
- if (!(dnum->flmode & NONEXCLUSIVE))
- return(NO_ERROR);
-
- if (dnum->lokcnt)
- dnum->lokcnt--;
- if (ctseek(dnum,recbyt))
- return(uerr(SEEK_ERR));
- if (lockf(dnum->fd,F_ULOCK,1L))
- return(uerr(UDLK_ERR));
- else
- return(NO_ERROR);
-
- #else
- return(NO_ERROR);
- #endif
-
- }
-
- #endif
-
- /* end of ctclib.unx */
-