Home | History | Annotate | Line # | Download | only in hash
ndbm.c revision 1.22
      1  1.22     joerg /*	$NetBSD: ndbm.c,v 1.22 2008/09/10 17:52:35 joerg 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.22     joerg __RCSID("$NetBSD: ndbm.c,v 1.22 2008/09/10 17:52:35 joerg Exp $");
     38   1.1       cgd 
     39   1.1       cgd /*
     40  1.20  christos  * This package provides a dbm compatible interface to the new hashing
     41  1.20  christos  * package described in db(3).
     42   1.2       cgd  */
     43  1.20  christos #include "namespace.h"
     44  1.20  christos #include <sys/param.h>
     45  1.20  christos 
     46  1.20  christos #include <fcntl.h>
     47  1.20  christos #include <stdio.h>
     48  1.20  christos #include <string.h>
     49  1.20  christos 
     50  1.20  christos #include <ndbm.h>
     51  1.20  christos #include "hash.h"
     52  1.20  christos 
     53  1.20  christos /*
     54  1.20  christos  * Returns:
     55  1.20  christos  * 	*DBM on success
     56  1.20  christos  *	 NULL on failure
     57  1.20  christos  */
     58  1.21  christos DBM *
     59  1.21  christos dbm_open(const char *file, int flags, mode_t mode)
     60  1.20  christos {
     61  1.20  christos 	HASHINFO info;
     62  1.20  christos 	char path[MAXPATHLEN];
     63  1.20  christos 
     64  1.20  christos 	info.bsize = 4096;
     65  1.20  christos 	info.ffactor = 40;
     66  1.20  christos 	info.nelem = 1;
     67  1.20  christos 	info.cachesize = 0;
     68  1.20  christos 	info.hash = NULL;
     69  1.20  christos 	info.lorder = 0;
     70  1.20  christos 	(void)strncpy(path, file, sizeof(path) - 1);
     71  1.20  christos 	(void)strncat(path, DBM_SUFFIX, sizeof(path) - strlen(path) - 1);
     72  1.20  christos 	if ((flags & O_ACCMODE) == O_WRONLY) {
     73  1.20  christos 		flags &= ~O_WRONLY;
     74  1.20  christos 		flags |= O_RDWR;
     75  1.20  christos 	}
     76  1.20  christos 	return ((DBM *)__hash_open(path, flags, mode, &info, 0));
     77  1.20  christos }
     78  1.20  christos 
     79  1.21  christos void
     80  1.21  christos dbm_close(DBM *db)
     81  1.20  christos {
     82  1.20  christos 	(void)(db->close)(db);
     83  1.20  christos }
     84  1.20  christos 
     85  1.21  christos int
     86  1.21  christos dbm_error(DBM *db)
     87  1.20  christos {
     88  1.20  christos 	HTAB *hp;
     89  1.20  christos 
     90  1.21  christos 	hp = db->internal;
     91  1.20  christos 	return (hp->err);
     92  1.20  christos }
     93  1.20  christos 
     94  1.21  christos int
     95  1.21  christos dbm_clearerr(DBM *db)
     96  1.20  christos {
     97  1.20  christos 	HTAB *hp;
     98   1.2       cgd 
     99  1.21  christos 	hp = db->internal;
    100  1.20  christos 	hp->err = 0;
    101  1.20  christos 	return (0);
    102  1.20  christos }
    103   1.1       cgd 
    104  1.21  christos int
    105  1.21  christos dbm_dirfno(DBM *db)
    106  1.20  christos {
    107  1.21  christos 	HTAB *hp;
    108  1.21  christos 
    109  1.21  christos 	hp = db->internal;
    110  1.21  christos 	return hp->fp;
    111  1.20  christos }
    112