home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / dbm / src / db.c next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.4 KB  |  147 lines

  1. /*-
  2.  * Copyright (c) 1991, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)db.c    8.4 (Berkeley) 2/21/94";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #include "watcomfx.h"
  39.  
  40. #ifndef __DBINTERFACE_PRIVATE
  41. #define __DBINTERFACE_PRIVATE
  42. #endif
  43. #ifdef macintosh
  44. #include <unix.h>
  45. #else
  46. #include <sys/types.h>
  47. #endif
  48.  
  49. #include <errno.h>
  50. #ifndef macintosh
  51. #include <fcntl.h>
  52. #endif
  53. #include <stddef.h>
  54. #include <stdio.h>
  55.  
  56. #include "mcom_db.h"
  57.  
  58. /* a global flag that locks closed all databases */
  59. int all_databases_locked_closed = 0;
  60.  
  61. /* set or unset a global lock flag to disable the
  62.  * opening of any DBM file
  63.  */
  64. void 
  65. dbSetOrClearDBLock(DBLockFlagEnum type)
  66. {
  67.     if(type == LockOutDatabase)
  68.         all_databases_locked_closed = 1;
  69.     else
  70.         all_databases_locked_closed = 0;
  71. }
  72.  
  73. #if defined(__WATCOMC__) || defined(__WATCOM_CPLUSPLUS__)
  74. DB *
  75. #else
  76. PR_IMPLEMENT(DB *)
  77. #endif
  78. dbopen(const char *fname, int flags,int mode, DBTYPE type, const void *openinfo)
  79. {
  80.  
  81.     /* lock out all file databases.  Let in-memory databases through
  82.      */
  83.     if(all_databases_locked_closed && fname)
  84.       {
  85.         errno = EINVAL;
  86.         return(NULL);
  87.       }
  88.  
  89. #define    DB_FLAGS    (DB_LOCK | DB_SHMEM | DB_TXN)
  90.  
  91.  
  92. #if 0  /* most systems dont have EXLOCK and SHLOCK */
  93. #define    USE_OPEN_FLAGS                            \
  94.     (O_CREAT | O_EXCL | O_EXLOCK | O_NONBLOCK | O_RDONLY |        \
  95.      O_RDWR | O_SHLOCK | O_TRUNC)
  96. #else
  97. #define    USE_OPEN_FLAGS                            \
  98.     (O_CREAT | O_EXCL  | O_RDONLY |        \
  99.      O_RDWR | O_TRUNC)
  100. #endif
  101.  
  102.     if ((flags & ~(USE_OPEN_FLAGS | DB_FLAGS)) == 0)
  103.         switch (type) {
  104. /* we don't need btree and recno right now */    
  105. #if 0
  106.         case DB_BTREE:
  107.             return (__bt_open(fname, flags & USE_OPEN_FLAGS,
  108.                 mode, openinfo, flags & DB_FLAGS));
  109.         case DB_RECNO:
  110.             return (__rec_open(fname, flags & USE_OPEN_FLAGS,
  111.                 mode, openinfo, flags & DB_FLAGS));
  112. #endif
  113.  
  114.         case DB_HASH:
  115.             return (__hash_open(fname, flags & USE_OPEN_FLAGS,
  116.                 mode, (const HASHINFO *)openinfo, flags & DB_FLAGS));
  117.         default:
  118.             break;
  119.         }
  120.     errno = EINVAL;
  121.     return (NULL);
  122. }
  123.  
  124. static int
  125. __dberr()
  126. {
  127.     return (RET_ERROR);
  128. }
  129.  
  130. /*
  131.  * __DBPANIC -- Stop.
  132.  *
  133.  * Parameters:
  134.  *    dbp:    pointer to the DB structure.
  135.  */
  136. void
  137. __dbpanic(DB *dbp)
  138. {
  139.     /* The only thing that can succeed is a close. */
  140.     dbp->del = (int (*)(const struct __db *, const DBT *, uint))__dberr;
  141.     dbp->fd = (int (*)(const struct __db *))__dberr;
  142.     dbp->get = (int (*)(const struct __db *, const DBT *, DBT *, uint))__dberr;
  143.     dbp->put = (int (*)(const struct __db *, DBT *, const DBT *, uint))__dberr;
  144.     dbp->seq = (int (*)(const struct __db *, DBT *, DBT *, uint))__dberr;
  145.     dbp->sync = (int (*)(const struct __db *, uint))__dberr;
  146. }
  147.