1 1.5 christos /* $NetBSD: ndbmdatum.c,v 1.5 2012/03/13 21:13:33 christos Exp $ */ 2 1.1 christos /* from: NetBSD: ndbm.c,v 1.18 2004/04/27 20:03:45 kleink Exp */ 3 1.1 christos 4 1.1 christos /*- 5 1.1 christos * Copyright (c) 1990, 1993 6 1.1 christos * The Regents of the University of California. All rights reserved. 7 1.1 christos * 8 1.1 christos * This code is derived from software contributed to Berkeley by 9 1.1 christos * Margo Seltzer. 10 1.1 christos * 11 1.1 christos * Redistribution and use in source and binary forms, with or without 12 1.1 christos * modification, are permitted provided that the following conditions 13 1.1 christos * are met: 14 1.1 christos * 1. Redistributions of source code must retain the above copyright 15 1.1 christos * notice, this list of conditions and the following disclaimer. 16 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 17 1.1 christos * notice, this list of conditions and the following disclaimer in the 18 1.1 christos * documentation and/or other materials provided with the distribution. 19 1.1 christos * 3. Neither the name of the University nor the names of its contributors 20 1.1 christos * may be used to endorse or promote products derived from this software 21 1.1 christos * without specific prior written permission. 22 1.1 christos * 23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 1.1 christos * SUCH DAMAGE. 34 1.1 christos */ 35 1.1 christos 36 1.4 joerg #if HAVE_NBTOOL_CONFIG_H 37 1.4 joerg #include "nbtool_config.h" 38 1.4 joerg #endif 39 1.4 joerg 40 1.1 christos #include <sys/cdefs.h> 41 1.5 christos __RCSID("$NetBSD: ndbmdatum.c,v 1.5 2012/03/13 21:13:33 christos Exp $"); 42 1.1 christos 43 1.1 christos /* 44 1.1 christos * This package provides a dbm compatible interface to the new hashing 45 1.1 christos * package described in db(3). 46 1.1 christos */ 47 1.1 christos #include "namespace.h" 48 1.1 christos #include <sys/param.h> 49 1.1 christos 50 1.1 christos #include <fcntl.h> 51 1.1 christos #include <stdio.h> 52 1.1 christos #include <string.h> 53 1.1 christos 54 1.1 christos #include <ndbm.h> 55 1.1 christos #include "hash.h" 56 1.1 christos 57 1.5 christos #ifndef datum_truncate 58 1.5 christos #define datum_truncate(a) (a) 59 1.5 christos #endif 60 1.1 christos /* 61 1.1 christos * Returns: 62 1.1 christos * DATUM on success 63 1.1 christos * NULL on failure 64 1.1 christos */ 65 1.2 christos datum 66 1.2 christos dbm_fetch(DBM *db, datum key) 67 1.1 christos { 68 1.1 christos datum retdata; 69 1.1 christos int status; 70 1.1 christos DBT dbtkey, dbtretdata; 71 1.1 christos 72 1.1 christos dbtkey.data = key.dptr; 73 1.1 christos dbtkey.size = key.dsize; 74 1.1 christos status = (db->get)(db, &dbtkey, &dbtretdata, 0); 75 1.1 christos if (status) { 76 1.1 christos dbtretdata.data = NULL; 77 1.1 christos dbtretdata.size = 0; 78 1.1 christos } 79 1.1 christos retdata.dptr = dbtretdata.data; 80 1.5 christos retdata.dsize = datum_truncate(dbtretdata.size); 81 1.1 christos return (retdata); 82 1.1 christos } 83 1.1 christos 84 1.1 christos /* 85 1.1 christos * Returns: 86 1.1 christos * DATUM on success 87 1.1 christos * NULL on failure 88 1.1 christos */ 89 1.2 christos datum 90 1.2 christos dbm_firstkey(DBM *db) 91 1.1 christos { 92 1.1 christos int status; 93 1.1 christos datum retkey; 94 1.1 christos DBT dbtretkey, dbtretdata; 95 1.1 christos 96 1.1 christos status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST); 97 1.1 christos if (status) 98 1.1 christos dbtretkey.data = NULL; 99 1.1 christos retkey.dptr = dbtretkey.data; 100 1.5 christos retkey.dsize = datum_truncate(dbtretkey.size); 101 1.1 christos return (retkey); 102 1.1 christos } 103 1.1 christos 104 1.1 christos /* 105 1.1 christos * Returns: 106 1.1 christos * DATUM on success 107 1.1 christos * NULL on failure 108 1.1 christos */ 109 1.2 christos datum 110 1.2 christos dbm_nextkey(DBM *db) 111 1.1 christos { 112 1.1 christos int status; 113 1.1 christos datum retkey; 114 1.1 christos DBT dbtretkey, dbtretdata; 115 1.1 christos 116 1.1 christos status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT); 117 1.1 christos if (status) 118 1.1 christos dbtretkey.data = NULL; 119 1.1 christos retkey.dptr = dbtretkey.data; 120 1.5 christos retkey.dsize = datum_truncate(dbtretkey.size); 121 1.1 christos return (retkey); 122 1.1 christos } 123 1.1 christos 124 1.1 christos /* 125 1.1 christos * Returns: 126 1.1 christos * 0 on success 127 1.1 christos * <0 failure 128 1.1 christos */ 129 1.2 christos int 130 1.2 christos dbm_delete(DBM *db, datum key) 131 1.1 christos { 132 1.1 christos int status; 133 1.1 christos DBT dbtkey; 134 1.1 christos 135 1.1 christos dbtkey.data = key.dptr; 136 1.1 christos dbtkey.size = key.dsize; 137 1.1 christos status = (db->del)(db, &dbtkey, 0); 138 1.1 christos if (status) 139 1.1 christos return (-1); 140 1.1 christos else 141 1.1 christos return (0); 142 1.1 christos } 143 1.1 christos 144 1.1 christos /* 145 1.1 christos * Returns: 146 1.1 christos * 0 on success 147 1.1 christos * <0 failure 148 1.1 christos * 1 if DBM_INSERT and entry exists 149 1.1 christos */ 150 1.2 christos int 151 1.2 christos dbm_store(DBM *db, datum key, datum data, int flags) 152 1.1 christos { 153 1.1 christos DBT dbtkey, dbtdata; 154 1.1 christos 155 1.1 christos dbtkey.data = key.dptr; 156 1.1 christos dbtkey.size = key.dsize; 157 1.1 christos dbtdata.data = data.dptr; 158 1.1 christos dbtdata.size = data.dsize; 159 1.1 christos return ((db->put)(db, &dbtkey, &dbtdata, 160 1.1 christos (u_int)((flags == DBM_INSERT) ? R_NOOVERWRITE : 0))); 161 1.1 christos } 162