ndbm.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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);
  76. (void)strcat(path, DBM_SUFFIX);
  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 retval;
  100. int status;
  101. status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
  102. if (status) {
  103. retval.dptr = NULL;
  104. retval.dsize = 0;
  105. }
  106. return (retval);
  107. }
  108. /*
  109. * Returns:
  110. * DATUM on success
  111. * NULL on failure
  112. */
  113. extern datum
  114. dbm_firstkey(db)
  115. DBM *db;
  116. {
  117. int status;
  118. datum retdata, retkey;
  119. status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
  120. if (status)
  121. retkey.dptr = NULL;
  122. return (retkey);
  123. }
  124. /*
  125. * Returns:
  126. * DATUM on success
  127. * NULL on failure
  128. */
  129. extern datum
  130. dbm_nextkey(db)
  131. DBM *db;
  132. {
  133. int status;
  134. datum retdata, retkey;
  135. status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
  136. if (status)
  137. retkey.dptr = NULL;
  138. return (retkey);
  139. }
  140. /*
  141. * Returns:
  142. * 0 on success
  143. * <0 failure
  144. */
  145. extern int
  146. dbm_delete(db, key)
  147. DBM *db;
  148. datum key;
  149. {
  150. int status;
  151. status = (db->del)(db, (DBT *)&key, 0);
  152. if (status)
  153. return (-1);
  154. else
  155. return (0);
  156. }
  157. /*
  158. * Returns:
  159. * 0 on success
  160. * <0 failure
  161. * 1 if DBM_INSERT and entry exists
  162. */
  163. extern int
  164. dbm_store(db, key, content, flags)
  165. DBM *db;
  166. datum key, content;
  167. int flags;
  168. {
  169. return ((db->put)(db, (DBT *)&key, (DBT *)&content,
  170. (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
  171. }
  172. extern int
  173. dbm_error(db)
  174. DBM *db;
  175. {
  176. HTAB *hp;
  177. hp = (HTAB *)db->internal;
  178. return (hp->errnum);
  179. }
  180. extern int
  181. dbm_clearerr(db)
  182. DBM *db;
  183. {
  184. HTAB *hp;
  185. hp = (HTAB *)db->internal;
  186. hp->errnum = 0;
  187. return (0);
  188. }
  189. extern int
  190. dbm_dirfno(db)
  191. DBM *db;
  192. {
  193. return(((HTAB *)db->internal)->fp);
  194. }