Home | History | Annotate | Line # | Download | only in hash
ndbm.c revision 1.21
      1  1.21  christos /*	$NetBSD: ndbm.c,v 1.21 2007/02/03 23:46:09 christos Exp $	*/
      2  1.20  christos /*	from: NetBSD: ndbm.c,v 1.18 2004/04/27 20:03:45 kleink Exp 	*/
      3  1.20  christos 
      4  1.20  christos /*-
      5  1.20  christos  * Copyright (c) 1990, 1993
      6  1.20  christos  *	The Regents of the University of California.  All rights reserved.
      7  1.20  christos  *
      8  1.20  christos  * This code is derived from software contributed to Berkeley by
      9  1.20  christos  * Margo Seltzer.
     10  1.20  christos  *
     11  1.20  christos  * Redistribution and use in source and binary forms, with or without
     12  1.20  christos  * modification, are permitted provided that the following conditions
     13  1.20  christos  * are met:
     14  1.20  christos  * 1. Redistributions of source code must retain the above copyright
     15  1.20  christos  *    notice, this list of conditions and the following disclaimer.
     16  1.20  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.20  christos  *    notice, this list of conditions and the following disclaimer in the
     18  1.20  christos  *    documentation and/or other materials provided with the distribution.
     19  1.20  christos  * 3. Neither the name of the University nor the names of its contributors
     20  1.20  christos  *    may be used to endorse or promote products derived from this software
     21  1.20  christos  *    without specific prior written permission.
     22  1.20  christos  *
     23  1.20  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.20  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.20  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.20  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.20  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.20  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.20  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.20  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.20  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.20  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.20  christos  * SUCH DAMAGE.
     34  1.20  christos  */
     35  1.20  christos 
     36  1.20  christos #include <sys/cdefs.h>
     37  1.20  christos #if defined(LIBC_SCCS) && !defined(lint)
     38  1.20  christos #if 0
     39  1.20  christos static char sccsid[] = "@(#)ndbm.c	8.4 (Berkeley) 7/21/94";
     40  1.20  christos #else
     41  1.21  christos __RCSID("$NetBSD: ndbm.c,v 1.21 2007/02/03 23:46:09 christos Exp $");
     42  1.20  christos #endif
     43  1.20  christos #endif /* LIBC_SCCS and not lint */
     44   1.1       cgd 
     45   1.1       cgd /*
     46  1.20  christos  * This package provides a dbm compatible interface to the new hashing
     47  1.20  christos  * package described in db(3).
     48   1.2       cgd  */
     49  1.20  christos #include "namespace.h"
     50  1.20  christos #include <sys/param.h>
     51  1.20  christos 
     52  1.20  christos #include <fcntl.h>
     53  1.20  christos #include <stdio.h>
     54  1.20  christos #include <string.h>
     55  1.20  christos 
     56  1.20  christos #include <ndbm.h>
     57  1.20  christos #include "hash.h"
     58  1.20  christos 
     59  1.20  christos /*
     60  1.20  christos  * Returns:
     61  1.20  christos  * 	*DBM on success
     62  1.20  christos  *	 NULL on failure
     63  1.20  christos  */
     64  1.21  christos DBM *
     65  1.21  christos dbm_open(const char *file, int flags, mode_t mode)
     66  1.20  christos {
     67  1.20  christos 	HASHINFO info;
     68  1.20  christos 	char path[MAXPATHLEN];
     69  1.20  christos 
     70  1.20  christos 	info.bsize = 4096;
     71  1.20  christos 	info.ffactor = 40;
     72  1.20  christos 	info.nelem = 1;
     73  1.20  christos 	info.cachesize = 0;
     74  1.20  christos 	info.hash = NULL;
     75  1.20  christos 	info.lorder = 0;
     76  1.20  christos 	(void)strncpy(path, file, sizeof(path) - 1);
     77  1.20  christos 	(void)strncat(path, DBM_SUFFIX, sizeof(path) - strlen(path) - 1);
     78  1.20  christos 	if ((flags & O_ACCMODE) == O_WRONLY) {
     79  1.20  christos 		flags &= ~O_WRONLY;
     80  1.20  christos 		flags |= O_RDWR;
     81  1.20  christos 	}
     82  1.20  christos 	return ((DBM *)__hash_open(path, flags, mode, &info, 0));
     83  1.20  christos }
     84  1.20  christos 
     85  1.21  christos void
     86  1.21  christos dbm_close(DBM *db)
     87  1.20  christos {
     88  1.20  christos 	(void)(db->close)(db);
     89  1.20  christos }
     90  1.20  christos 
     91  1.21  christos int
     92  1.21  christos dbm_error(DBM *db)
     93  1.20  christos {
     94  1.20  christos 	HTAB *hp;
     95  1.20  christos 
     96  1.21  christos 	hp = db->internal;
     97  1.20  christos 	return (hp->err);
     98  1.20  christos }
     99  1.20  christos 
    100  1.21  christos int
    101  1.21  christos dbm_clearerr(DBM *db)
    102  1.20  christos {
    103  1.20  christos 	HTAB *hp;
    104   1.2       cgd 
    105  1.21  christos 	hp = db->internal;
    106  1.20  christos 	hp->err = 0;
    107  1.20  christos 	return (0);
    108  1.20  christos }
    109   1.1       cgd 
    110  1.21  christos int
    111  1.21  christos dbm_dirfno(DBM *db)
    112  1.20  christos {
    113  1.21  christos 	HTAB *hp;
    114  1.21  christos 
    115  1.21  christos 	hp = db->internal;
    116  1.21  christos 	return hp->fp;
    117  1.20  christos }
    118