dbm_close(3)

dbm_close3

Database access functions

Part of dbm(3).

Usage

#include <fcntl.h>

#include <ndbm.h>

DBM *

dbm_open(const char *base, int flags, mode_t mode);

void

dbm_close(DBM *db);

int

dbm_store(DBM *db, datum key, datum data, int flags);

datum

dbm_fetch(DBM *db, datum key);

int

dbm_delete(DBM *db, datum key);

datum

dbm_firstkey(DBM *db);

datum

dbm_nextkey(DBM *db);

int

dbm_error(DBM *db);

int

dbm_clearerr(DBM *db);

int

dbm_dirfno(DBM *db);

Overview

Database access functions. These functions are implemented using dbopen(3) with a hash(3) database.

datum is declared in ⟨ndbm.h⟩:

typedef struct {
	void *dptr;
	int dsize;
} datum;

The dbm_open(base, flags, mode) function opens or creates a database. The base argument is the basename of the file containing the database; the actual database has a .db suffix. I.e., if base is "/home/me/mystuff" then the actual database is in the file /home/me/mystuff.db. The flags and mode arguments are passed to open(2). (O_RDWR | O_CREAT) is a typical value for flags; 0660 is a typical value for mode. The pointer returned by dbm_open() identifies the database and is the db argument to the other functions. The dbm_open() function returns NULL and sets errno if there were any errors.

The dbm_close(db) function closes the database.

The dbm_store(db, key, data, flags) function inserts or replaces an entry in the database. The flags argument is either DBM_INSERT or DBM_REPLACE. If flags is DBM_INSERT and the database already contains an entry for key, that entry is not replaced. Otherwise the entry is replaced or inserted. The dbm_store() function normally returns zero but returns 1 if the entry could not be inserted (because flags is DBM_INSERT, and an entry with key already exists) or returns -1 and sets errno if there were any errors.

The dbm_fetch(db, key) function returns NULL or the data corresponding to key.

The dbm_delete(db, key) function deletes the entry for key. The dbm_delete() function normally returns zero or returns -1 and sets errno if there were any errors.

The dbm_firstkey(db) function returns the first key in the database. The dbm_nextkey(db) function returns subsequent keys. The db_firstkey() function must be called before dbm_nextkey(). The order in which keys are returned is unspecified and may appear random. The dbm_nextkey() function returns NULL after all keys have been returned.

The dbm_error(db) function returns the errno value of the most recent error. The dbm_clearerr(db) function resets this value to 0 and returns 0.

The dbm_dirfno(db) function returns the file descriptor to the database.

Releases

66 releases since FreeBSD 4.3-RELEASE; the text changed 44 times.

  1. 4.3
  2. 4.4
  3. 4.5–4.7
  4. 4.8–4.11
  5. 5.0
  6. 5.1
  7. 5.2–5.4
  8. 5.5
  9. 6.0
  10. 6.1
  11. 6.2–6.3
  12. 6.4
  13. 7.0
  14. 7.1
  15. 7.2
  16. 7.3
  17. 7.4
  18. 8.0
  19. 8.1
  20. 8.2
  21. 8.3
  22. 8.4
  23. 9.0
  24. 9.1
  25. 9.2
  26. 9.3
  27. 10.0
  28. 10.1
  29. 10.2
  30. 10.3
  31. 10.4
  32. 11.0
  33. 11.1
  34. 11.2
  35. 11.3
  36. 11.4
  37. 12.0
  38. 12.1
  39. 12.2
  40. 12.3
  41. 12.4
  42. 13.0–13.1
  43. 13.2
  44. 13.3–14.5
  45. 15.0–16.0

Standards

These functions (except dbm_dirfno()) are included in the Version 2 of the Single UNIX Specification (“SUSv2”).

History

The functions dbminit(), fetch(), store(), delete(), firstkey(), and nextkey() first appeared in Version 7 AT&T UNIX.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.