Back to index

lightning-sunbird  0.9+nobinonly
ndbm.c
Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1990, 1993
00003  *     The Regents of the University of California.  All rights reserved.
00004  *
00005  * This code is derived from software contributed to Berkeley by
00006  * Margo Seltzer.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. ***REMOVED*** - see 
00017  *    ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
00018  * 4. Neither the name of the University nor the names of its contributors
00019  *    may be used to endorse or promote products derived from this software
00020  *    without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  */
00034 
00035 #if defined(LIBC_SCCS) && !defined(lint)
00036 static char sccsid[] = "@(#)ndbm.c 8.4 (Berkeley) 7/21/94";
00037 #endif /* LIBC_SCCS and not lint */
00038 
00039 #include "watcomfx.h"
00040 
00041 /*
00042  * This package provides a dbm compatible interface to the new hashing
00043  * package described in db(3).
00044  */
00045 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(XP_OS2_VACPP)
00046 #include <sys/param.h>
00047 #endif
00048 
00049 #if defined(__linux)
00050 #include <linux/limits.h>
00051 #endif
00052 
00053 #include <stdio.h>
00054 #include <string.h>
00055 
00056 #include "ndbm.h"
00057 #include "hash.h"
00058 
00059 /*
00060  * Returns:
00061  *     *DBM on success
00062  *      NULL on failure
00063  */
00064 extern DBM *
00065 dbm_open(const char *file, int flags, int mode)
00066 {
00067        HASHINFO info;
00068        char path[MAXPATHLEN];
00069 
00070        info.bsize = 4096;
00071        info.ffactor = 40;
00072        info.nelem = 1;
00073        info.cachesize = 0;
00074        info.hash = NULL;
00075        info.lorder = 0;
00076        (void)strcpy(path, file);
00077        (void)strcat(path, DBM_SUFFIX);
00078        return ((DBM *)__hash_open(path, flags, mode, &info, 0));
00079 }
00080 
00081 extern void
00082 dbm_close(DBM *db)
00083 {
00084        (void)(db->close)(db);
00085 }
00086 
00087 /*
00088  * Returns:
00089  *     DATUM on success
00090  *     NULL on failure
00091  */
00092 extern datum
00093 dbm_fetch(DBM *db, datum key)
00094 {
00095        datum retval;
00096        int status;
00097 
00098        status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
00099        if (status) {
00100               retval.dptr = NULL;
00101               retval.dsize = 0;
00102        }
00103        return (retval);
00104 }
00105 
00106 /*
00107  * Returns:
00108  *     DATUM on success
00109  *     NULL on failure
00110  */
00111 extern datum
00112 dbm_firstkey(DBM *db)
00113 {
00114        int status;
00115        datum retdata, retkey;
00116 
00117        status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
00118        if (status)
00119               retkey.dptr = NULL;
00120        return (retkey);
00121 }
00122 
00123 /*
00124  * Returns:
00125  *     DATUM on success
00126  *     NULL on failure
00127  */
00128 extern datum
00129 dbm_nextkey(DBM *db)
00130 {
00131        int status;
00132        datum retdata, retkey;
00133 
00134        status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
00135        if (status)
00136               retkey.dptr = NULL;
00137        return (retkey);
00138 }
00139 /*
00140  * Returns:
00141  *      0 on success
00142  *     <0 failure
00143  */
00144 extern int
00145 dbm_delete(DBM *db, datum key)
00146 {
00147        int status;
00148 
00149        status = (db->del)(db, (DBT *)&key, 0);
00150        if (status)
00151               return (-1);
00152        else
00153               return (0);
00154 }
00155 
00156 /*
00157  * Returns:
00158  *      0 on success
00159  *     <0 failure
00160  *      1 if DBM_INSERT and entry exists
00161  */
00162 extern int
00163 dbm_store(DBM *db, datum key, datum content, int flags)
00164 {
00165        return ((db->put)(db, (DBT *)&key, (DBT *)&content,
00166            (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
00167 }
00168 
00169 
00170 extern int
00171 dbm_error(DBM *db)
00172 {
00173        HTAB *hp;
00174 
00175        hp = (HTAB *)db->internal;
00176        return (hp->dbmerrno);
00177 }
00178 
00179 extern int
00180 dbm_clearerr(DBM *db)
00181 {
00182        HTAB *hp;
00183 
00184        hp = (HTAB *)db->internal;
00185        hp->dbmerrno = 0;
00186        return (0);
00187 }
00188 
00189 extern int
00190 dbm_dirfno(DBM *db)
00191 {
00192        return(((HTAB *)db->internal)->fp);
00193 }