home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c004 / 2.ddi / UNIX / CTCLIB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-18  |  6.0 KB  |  301 lines

  1. /*
  2.  *    CTCLIB.C module
  3.  *
  4.  *    This sample is setup for the 
  5.  *    UNIX V Operating System Release 2/3
  6.  *
  7.  *    (UNIX is a trademark of AT&T)
  8.  *
  9.  *    This program is the CONFIDENTIAL and PROPRIETARY property 
  10.  *    of FairCom(R) Corporation. Any unauthorized use, reproduction or
  11.  *    transfer of this program is strictly prohibited.
  12.  *
  13.  *      Copyright (c) 1985, 1986, 1987, 1988, 1989 FairCom Corporation
  14.  *    (Subject to limited distribution and
  15.  *     restricted disclosure only.)
  16.  *    *** ALL RIGHTS RESERVED ***
  17.  *
  18.  *    4006 West Broadway
  19.  *    Columbia, MO 65203
  20.  *
  21.  *
  22.  *    c-tree(R)    Version 4.3
  23.  *            Release C
  24.  *            February 7, 1989 17:30
  25.  *
  26.  */
  27.  
  28. #include "ctstdr.h"        /* standard i/o header         */
  29. #undef   EXTERN
  30. #define  EXTERN /* */
  31. #include "ctoptn.h"        /* c-tree configuration options */
  32. #include "cterrc.h"        /* c-tree error codes        */
  33. #include "ctstrc.h"        /* c-tree data structures    */
  34. #include "ctgvar.h"        /* c-tree global variables    */
  35. #include <unistd.h>
  36.  
  37. COUNT ctseek(),uerr();
  38.  
  39. RNDFILE mbopen(ctnum,opmode)
  40. PFAST CTFILE  *ctnum;
  41. COUNT             opmode;
  42. {
  43.     RNDFILE retval;
  44.     int    acflag;
  45. #ifndef CTSERVER
  46.     int    i;
  47.     long lseek();
  48. #endif
  49.  
  50.  
  51.     COUNT vtclose();
  52.  
  53.     ctnum->sekpos = DRNZERO;
  54.     acflag          = O_RDWR;
  55.     if (opmode & READFIL)
  56.         acflag = O_RDONLY;
  57.  
  58.     if (!(opmode & PERMANENT) && ct_numvfil >= MAXVFIL)
  59.         vtclose();
  60.  
  61.     if ((retval = open(ctnum->flname,acflag)) < 0)
  62.         if (vtclose() == YES)
  63.             retval = open(ctnum->flname,acflag);
  64.  
  65.     /* if exclusive open, obtain lock on entire file */
  66.     if (retval > -1 && !(opmode & NONEXCLUSIVE) &&
  67.         lockf(retval,F_TLOCK,0L)) {
  68.         close(retval);
  69.         return(-1);
  70.     }
  71. #ifdef CTSERVER
  72.     if (retval > -1 && lockf(retval,F_TLOCK,0L)) {
  73.         close(retval);
  74.         return(-1);
  75.     }
  76. #else
  77.     /* obtain lock on entire file if exclusive open */
  78.     if (retval > -1 && !(opmode & NONEXCLUSIVE)) {
  79.         lseek(retval,ctnum->sekpos = HDRSIZ,0);
  80.         if (lockf(retval,F_TLOCK,0L)) {
  81.             close(retval);
  82.             return(-1);
  83.         }
  84.     } else if (retval > -1 && (opmode & NONEXCLUSIVE)) {
  85.         /* check file type to see if index: no lock on index */
  86.         if (read(retval,ctnum,HDRSIZ) != HDRSIZ) {
  87.             close(retval);
  88.             return(-1);
  89.         } else {
  90. #ifdef UNIFRMAT
  91.             revhdr(ctnum);
  92. #endif
  93.             ctnum->sekpos = HDRSIZ;
  94.         }
  95.         if (ctnum->clstyp != IDX_CLOSE) {
  96.             for (i = HDRSIZ; i < 128; i++) {
  97.                 lseek(retval,ctnum->sekpos = i,0);
  98.                 if (lockf(retval,F_TLOCK,1L) == 0)
  99.                     break;
  100.             }
  101.             if (i >= 128) {
  102.                 close(retval);
  103.                 return(-1);
  104.             }
  105.         }
  106.     }
  107. #endif
  108.  
  109.  
  110.     if (!(opmode & PERMANENT) && retval >= 0)
  111.         ct_numvfil++;
  112.  
  113.     return(retval);
  114. }
  115.  
  116. /* ------------------------------------------------------------ */
  117.  
  118. RNDFILE mbcrat(ctnum)
  119. PFAST CTFILE  *ctnum;
  120. {
  121.     RNDFILE retval;
  122.  
  123.     COUNT vtclose();
  124.  
  125.     ctnum->sekpos = DRNZERO;
  126.     if (!(ctnum->flmode & PERMANENT) && ct_numvfil >= MAXVFIL)
  127.         vtclose();
  128.  
  129.     if ((retval = creat(ctnum->flname,BCREATE)) < 0)
  130.         if (vtclose() == YES)
  131.             retval = creat(ctnum->flname,BCREATE);
  132.  
  133.     if (!(ctnum->flmode & PERMANENT) && retval >= 0)
  134.         ct_numvfil++;
  135.  
  136.     return(retval);
  137. }
  138.  
  139. COUNT mbsave( ctnum)
  140. PFAST CTFILE *ctnum;
  141. {
  142.     sync();
  143.     return(NO_ERROR);
  144. }
  145.  
  146. VOID flushdos(datno)
  147. COUNT          datno;
  148. {
  149.     sync();    /* UNIX system call: schedules dirty buffers for write */
  150. }
  151.  
  152. #ifndef CTSERVER
  153.  
  154. /* --------------------------------------------------------------------
  155.    LOCK index node
  156.  */
  157.  
  158. COUNT LOCK(node,knum)    /* node == 0  => header */
  159. LONG    node;
  160. PFAST KEYFILE *knum;
  161. {
  162.  
  163. /*
  164.  * c-tree node locking protocol is guaranteed to be deadlock free. Therefore,
  165.  * the lock request allows the process to "sleep" if the lock is not
  166.  * immediately available.
  167.  */
  168.  
  169. #ifdef FPUTFGET
  170.     knum -= knum->kmem;
  171.     if (!(knum->flmode & NONEXCLUSIVE))
  172.         return(NO_ERROR);
  173.  
  174.     if (ctseek(knum,node))
  175.         return(uerr(SEEK_ERR));
  176.     if (lockf(knum->fd,F_LOCK,1L))
  177.         return(uerr(LNOD_ERR));
  178.     else {
  179.         knum->lokcnt++;
  180.         return(NO_ERROR);
  181.     }
  182. #else
  183.     return(NO_ERROR);
  184. #endif
  185.  
  186. }
  187.  
  188. /* --------------------------------------------------------------------
  189.    UNLOCK index file node
  190. */
  191.  
  192. COUNT UNLOCK(node,knum)
  193. LONG    node;
  194. PFAST KEYFILE *knum;
  195. {
  196.  
  197. #ifdef FPUTFGET
  198.     knum -= knum->kmem;
  199.     if (!(knum->flmode & NONEXCLUSIVE))
  200.         return(NO_ERROR);
  201.  
  202.     if (knum->lokcnt)
  203.         knum->lokcnt--;
  204.     if (ctseek(knum,node))
  205.         return(uerr(SEEK_ERR));
  206.     else if (lockf(knum->fd,F_ULOCK,1L))
  207.         return(uerr(UNOD_ERR));
  208.     else
  209.         return(NO_ERROR);
  210. #else
  211.     return(NO_ERROR);
  212. #endif
  213.  
  214. }
  215.  
  216.  
  217. /* --------------------------------------------------------------------
  218.    LOCK data record
  219.  */
  220.  
  221. COUNT DLOCK(recbyt,dnum)        /* recbyt == 0  => header record */
  222. POINTER        recbyt;
  223. PFAST DATFILE  *dnum;
  224. {
  225.  
  226. /*
  227.  * the data record locks are NOT guaranteed to be deadlock free.  Therefore,
  228.  * except for the header record which is only locked by internal c-tree 
  229.  * maintenance requests, the data record lock routine returns instead of
  230.  * sleeping when a lock is denied because of a competing lock.  The application
  231.  * program must deal with the appropriate action if a lock is denied to a
  232.  * data record.
  233.  */
  234.  
  235. #ifdef FPUTFGET
  236.     COUNT lokreq;
  237.  
  238.     if (!(dnum->flmode & NONEXCLUSIVE))
  239.         return(NO_ERROR);
  240.  
  241.     if (ctseek(dnum,recbyt))
  242.         return(uerr(SEEK_ERR));
  243.  
  244.     if (recbyt == DRNZERO)
  245.         lokreq = F_LOCK; /* allow wait for lock on header only */
  246.     else
  247.         lokreq = F_TLOCK;
  248.  
  249.     if (lockf(dnum->fd,lokreq,1L))
  250.         return(uerr(DLOK_ERR));
  251.     else {
  252.         dnum->lokcnt++;
  253.         return(NO_ERROR);
  254.     }
  255. #else
  256.     return(NO_ERROR);
  257. #endif
  258.  
  259. }
  260.  
  261. COUNT RLOCK(recbyt,dnum)        /* recbyt == 0  => header record */
  262. POINTER        recbyt;
  263. PFAST DATFILE  *dnum;
  264. {
  265.     return(NO);
  266. }
  267.  
  268. /* --------------------------------------------------------------------
  269.    UNLOCK data record
  270. */
  271.  
  272. COUNT UDLOCK(recbyt,dnum)
  273.  
  274. POINTER        recbyt;
  275. PFAST DATFILE  *dnum;
  276.  
  277. {
  278.  
  279. #ifdef FPUTFGET
  280.     if (!(dnum->flmode & NONEXCLUSIVE))
  281.         return(NO_ERROR);
  282.  
  283.     if (dnum->lokcnt)
  284.         dnum->lokcnt--;
  285.     if (ctseek(dnum,recbyt))
  286.         return(uerr(SEEK_ERR));
  287.     if (lockf(dnum->fd,F_ULOCK,1L))
  288.         return(uerr(UDLK_ERR));
  289.     else
  290.         return(NO_ERROR);
  291.  
  292. #else
  293.     return(NO_ERROR);
  294. #endif
  295.  
  296. }
  297.  
  298. #endif
  299.  
  300. /* end of ctclib.unx */
  301.