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

  1. /*-
  2.  * Copyright (c) 1990, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Margo Seltzer.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)ndbm.c    8.4 (Berkeley) 7/21/94";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. #include "watcomfx.h"
  42.  
  43. /*
  44.  * This package provides a dbm compatible interface to the new hashing
  45.  * package described in db(3).
  46.  */
  47. #if !defined(_WIN32) && !defined(_WINDOWS)
  48. #include <sys/param.h>
  49. #endif
  50.  
  51. #if defined(__linux)
  52. #include <linux/limits.h>
  53. #endif
  54.  
  55. #include <stdio.h>
  56. #include <string.h>
  57.  
  58. #include "ndbm.h"
  59. #include "hash.h"
  60.  
  61. /*
  62.  * Returns:
  63.  *     *DBM on success
  64.  *     NULL on failure
  65.  */
  66. extern DBM *
  67. dbm_open(const char *file, int flags, int mode)
  68. {
  69.     HASHINFO info;
  70.     char path[MAXPATHLEN];
  71.  
  72.     info.bsize = 4096;
  73.     info.ffactor = 40;
  74.     info.nelem = 1;
  75.     info.cachesize = 0;
  76.     info.hash = NULL;
  77.     info.lorder = 0;
  78.     (void)strcpy(path, file);
  79.     (void)strcat(path, DBM_SUFFIX);
  80.     return ((DBM *)__hash_open(path, flags, mode, &info, 0));
  81. }
  82.  
  83. extern void
  84. dbm_close(DBM *db)
  85. {
  86.     (void)(db->close)(db);
  87. }
  88.  
  89. /*
  90.  * Returns:
  91.  *    DATUM on success
  92.  *    NULL on failure
  93.  */
  94. extern datum
  95. dbm_fetch(DBM *db, datum key)
  96. {
  97.     datum retval;
  98.     int status;
  99.  
  100.     status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
  101.     if (status) {
  102.         retval.dptr = NULL;
  103.         retval.dsize = 0;
  104.     }
  105.     return (retval);
  106. }
  107.  
  108. /*
  109.  * Returns:
  110.  *    DATUM on success
  111.  *    NULL on failure
  112.  */
  113. extern datum
  114. dbm_firstkey(DBM *db)
  115. {
  116.     int status;
  117.     datum retdata, retkey;
  118.  
  119.     status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
  120.     if (status)
  121.         retkey.dptr = NULL;
  122.     return (retkey);
  123. }
  124.  
  125. /*
  126.  * Returns:
  127.  *    DATUM on success
  128.  *    NULL on failure
  129.  */
  130. extern datum
  131. dbm_nextkey(DBM *db)
  132. {
  133.     int status;
  134.     datum retdata, retkey;
  135.  
  136.     status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
  137.     if (status)
  138.         retkey.dptr = NULL;
  139.     return (retkey);
  140. }
  141. /*
  142.  * Returns:
  143.  *     0 on success
  144.  *    <0 failure
  145.  */
  146. extern int
  147. dbm_delete(DBM *db, datum key)
  148. {
  149.     int status;
  150.  
  151.     status = (db->del)(db, (DBT *)&key, 0);
  152.     if (status)
  153.         return (-1);
  154.     else
  155.         return (0);
  156. }
  157.  
  158. /*
  159.  * Returns:
  160.  *     0 on success
  161.  *    <0 failure
  162.  *     1 if DBM_INSERT and entry exists
  163.  */
  164. extern int
  165. dbm_store(DBM *db, datum key, datum content, int flags)
  166. {
  167.     return ((db->put)(db, (DBT *)&key, (DBT *)&content,
  168.         (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
  169. }
  170.  
  171.  
  172. extern int
  173. dbm_error(DBM *db)
  174. {
  175.     HTAB *hp;
  176.  
  177.     hp = (HTAB *)db->internal;
  178.     return (hp->dbmerrno);
  179. }
  180.  
  181. extern int
  182. dbm_clearerr(DBM *db)
  183. {
  184.     HTAB *hp;
  185.  
  186.     hp = (HTAB *)db->internal;
  187.     hp->dbmerrno = 0;
  188.     return (0);
  189. }
  190.  
  191. extern int
  192. dbm_dirfno(DBM *db)
  193. {
  194.     return(((HTAB *)db->internal)->fp);
  195. }
  196.