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