ndbm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*-
  2. * Copyright (c) 1990, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Margo Seltzer.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. * must display the following acknowledgement:
  18. * This product includes software developed by the University of
  19. * California, Berkeley and its contributors.
  20. * 4. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. */
  36. #if defined(LIBC_SCCS) && !defined(lint)
  37. static char sccsid[] = "@(#)ndbm.c 8.4 (Berkeley) 7/21/94";
  38. #endif /* LIBC_SCCS and not lint */
  39. /*
  40. * This package provides a dbm compatible interface to the new hashing
  41. * package described in db(3).
  42. */
  43. #include <sys/param.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include <stdlib.h>
  47. #include <ndbm.h>
  48. #include "hash.h"
  49. /*
  50. * Returns:
  51. * *DBM on success
  52. * NULL on failure
  53. */
  54. extern DBM *
  55. dbm_open(file, flags, mode)
  56. const char *file;
  57. int flags, mode;
  58. {
  59. DBM *db;
  60. HASHINFO info;
  61. const size_t len = strlen(file) + sizeof (DBM_SUFFIX);
  62. #ifdef __GNUC__
  63. char path[len];
  64. #else
  65. char *path = malloc(len);
  66. if (path == NULL)
  67. return NULL;
  68. #endif
  69. info.bsize = 4096;
  70. info.ffactor = 40;
  71. info.nelem = 1;
  72. info.cachesize = 0;
  73. info.hash = NULL;
  74. info.lorder = 0;
  75. (void)strcpy(path, file); /* SAFE */
  76. (void)strncat(path, DBM_SUFFIX, len - strlen(path) - 1);
  77. db = (DBM *)__hash_open(path, flags, mode, &info, 0);
  78. #ifndef __GNUC__
  79. free(path);
  80. #endif
  81. return db;
  82. }
  83. extern void
  84. dbm_close(db)
  85. DBM *db;
  86. {
  87. (void)(db->close)(db);
  88. }
  89. /*
  90. * Returns:
  91. * DATUM on success
  92. * NULL on failure
  93. */
  94. extern datum
  95. dbm_fetch(db, key)
  96. DBM *db;
  97. datum key;
  98. {
  99. datum retdata;
  100. int status;
  101. DBT dbtkey, dbtretdata;
  102. dbtkey.data = key.dptr;
  103. dbtkey.size = key.dsize;
  104. status = (db->get)(db, &dbtkey, &dbtretdata, 0);
  105. if (status) {
  106. dbtretdata.data = NULL;
  107. dbtretdata.size = 0;
  108. }
  109. retdata.dptr = dbtretdata.data;
  110. retdata.dsize = dbtretdata.size;
  111. return (retdata);
  112. }
  113. /*
  114. * Returns:
  115. * DATUM on success
  116. * NULL on failure
  117. */
  118. extern datum
  119. dbm_firstkey(db)
  120. DBM *db;
  121. {
  122. int status;
  123. datum retkey;
  124. DBT dbtretkey, dbtretdata;
  125. status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
  126. if (status)
  127. dbtretkey.data = NULL;
  128. retkey.dptr = dbtretkey.data;
  129. retkey.dsize = dbtretkey.size;
  130. return (retkey);
  131. }
  132. /*
  133. * Returns:
  134. * DATUM on success
  135. * NULL on failure
  136. */
  137. extern datum
  138. dbm_nextkey(db)
  139. DBM *db;
  140. {
  141. int status;
  142. datum retkey;
  143. DBT dbtretkey, dbtretdata;
  144. status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
  145. if (status)
  146. dbtretkey.data = NULL;
  147. retkey.dptr = dbtretkey.data;
  148. retkey.dsize = dbtretkey.size;
  149. return (retkey);
  150. }
  151. /*
  152. * Returns:
  153. * 0 on success
  154. * <0 failure
  155. */
  156. extern int
  157. dbm_delete(db, key)
  158. DBM *db;
  159. datum key;
  160. {
  161. int status;
  162. DBT dbtkey;
  163. dbtkey.data = key.dptr;
  164. dbtkey.size = key.dsize;
  165. status = (db->del)(db, &dbtkey, 0);
  166. if (status)
  167. return (-1);
  168. else
  169. return (0);
  170. }
  171. /*
  172. * Returns:
  173. * 0 on success
  174. * <0 failure
  175. * 1 if DBM_INSERT and entry exists
  176. */
  177. extern int
  178. dbm_store(db, key, data, flags)
  179. DBM *db;
  180. datum key, data;
  181. int flags;
  182. {
  183. DBT dbtkey, dbtdata;
  184. dbtkey.data = key.dptr;
  185. dbtkey.size = key.dsize;
  186. dbtdata.data = data.dptr;
  187. dbtdata.size = data.dsize;
  188. return ((db->put)(db, &dbtkey, &dbtdata,
  189. (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
  190. }
  191. extern int
  192. dbm_error(db)
  193. DBM *db;
  194. {
  195. HTAB *hp;
  196. hp = (HTAB *)db->internal;
  197. return (hp->errnum);
  198. }
  199. extern int
  200. dbm_clearerr(db)
  201. DBM *db;
  202. {
  203. HTAB *hp;
  204. hp = (HTAB *)db->internal;
  205. hp->errnum = 0;
  206. return (0);
  207. }
  208. extern int
  209. dbm_dirfno(db)
  210. DBM *db;
  211. {
  212. return(((HTAB *)db->internal)->fp);
  213. }