DBM(3)
πΎ DBM(3) β the Unix manual, but make it make sense β¨
okay so bestie, welcome to the Library Functions Manual moment of your life π
this whole page is about the dbm_* squad β a bunch of database access functions that live in <ndbm.h> and are literally built on top of dbopen(3) with a hash(3) database underneath. iykyk. let's get into it π«Ά
πΈ NAME πΈ
dbm_clearerr, dbm_close, dbm_delete, dbm_dirfno, dbm_error, dbm_fetch, dbm_firstkey, dbm_nextkey, dbm_open, dbm_store β the database access functions, aka the whole friend group π
π SYNOPSIS π
here's the whole squad's signatures, no cap:
#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);
β¨ DESCRIPTION β¨
so these are the database access functions, and the tea is they're implemented using dbopen(3) with a hash(3) database. iconic behavior honestly π₯
datum is declared in <ndbm.h> and it's giving very simple struct energy:
typedef struct {
void *dptr;
int dsize;
} datum;
π¦ dbm_open(base, flags, mode) π¦
this one opens or creates a database, main character energy fr. the base argument is the basename of the file containing the database β and the actual database has a .db suffix. so like, if base is "/home/me/mystuff" then the actual database is in the file /home/me/mystuff.db. yes bestie, the .db is automatic π
the flags and mode arguments get passed straight to open(2), no thoughts just vibes. (O_RDWR | O_CREAT) is a typical value for flags, and 0660 is a typical value for mode. the pointer that dbm_open() returns identifies the database and becomes the db argument for all the other functions. and if anything goes wrong? dbm_open() returns NULL and sets errno, tragic but at least you know π
π§ dbm_close(db) π§
closes the database. that's it. that's the tweet. short and sweet π
π dbm_store(db, key, data, flags) π
inserts or replaces an entry in the database. the flags argument is either DBM_INSERT or DBM_REPLACE. here's the nuance bestie: 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, depending on the flag.
dbm_store() 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) β so like a "no thanks, already got one" situation. or it returns -1 and sets errno if there were any errors π
π dbm_fetch(db, key) π
returns NULL or the data corresponding to key. simple as that, lowkey underrated β¨
π dbm_delete(db, key) π
deletes the entry for key. normally returns zero, or returns -1 and sets errno if there were any errors. bye bye entry π
β dbm_firstkey(db) and dbm_nextkey(db) β
dbm_firstkey(db) returns the first key in the database, and dbm_nextkey(db) returns subsequent keys. important detail bestie: the db_firstkey() function must be called before dbm_nextkey(). the order in which keys are returned is unspecified and may appear random (so don't come for it, it's just quirky like that π). dbm_nextkey() returns NULL after all keys have been returned β that's your cue that you're done π
π dbm_error(db) and dbm_clearerr(db) π
dbm_error(db) returns the errno value of the most recent error. dbm_clearerr(db) resets this value to 0 and returns 0. clean slate energy β¨
πΈ dbm_dirfno(db) πΈ
returns the file descriptor to the database. that's the whole function, she's a one-liner and she knows it π
π SEE ALSO π
open(2), dbopen(3), hash(3) β go read those too bestie, they're part of the lore π«Ά
π STANDARDS π
these functions (except dbm_dirfno()) are included in Version 2 of the Single UNIX Specification (aka βSUSv2β). so like, they're officially official π
π°οΈ HISTORY π°οΈ
the functions dbminit(), fetch(), store(), delete(), firstkey(), and nextkey() first appeared in Version 7 AT&T UNIX. old money, historic behavior, we love a legacy queen π
β οΈ quick vibe check bestie: this page is the real deal from the Library Functions Manual, brought to you by Debian, dated July 25, 2025. that's the whole tea. now go forth and use dbm_* responsibly, mwah π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- nixpkgs unstable: compat 15.0.0
- Copyright (c) 1999 Tim Singletary
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms