NDBM(3) NDBM(3) NAME dbm_open, dbm_close, dbm_fetch, dbm_store, dbm_delete, dbm_firstkey, dbm_nextkey, dbm_error, dbm_clearerr - data base subroutines SYNOPSIS #include typedef struct { char *dptr; int dsize; } datum; DBM *dbm_open(file, flags, mode) char *file; int flags, mode; void dbm_close(db) DBM *db; datum dbm_fetch(db, key) DBM *db; datum key; int dbm_store(db, key, content, flags) DBM *db; datum key, content; int flags; int dbm_delete(db, key) DBM *db; datum key; datum dbm_firstkey(db) DBM *db; datum dbm_nextkey(db) DBM *db; int dbm_error(db) DBM *db; int dbm_clearerr(db) DBM *db; DESCRIPTION These functions maintain key/content pairs in a data base. The func‐ tions will handle very large (a billion blocks) databases and will access a keyed item in one or two file system accesses. This package replaces the earlier _d_b_m(3x) library, which managed only a single database. _K_e_ys and _c_o_n_t_e_n_ts are described by the _d_a_t_u_m typedef. A _d_a_t_u_m speci‐ fies a string of _d_s_i_z_e bytes pointed to by _d_p_t_r_. Arbitrary binary data, as well as normal ASCII strings, are allowed. The data base is stored in two files. One file is a directory containing a bit map and has ‘.dir’ as its suffix. The second file contains all data and has ‘.pag’ as its suffix. Before a database can be accessed, it must be opened by _d_b_m__o_p_e_n. This will open and/or create the files _f_i_l_e.dir and _f_i_l_e.pag depending on the flags parameter (see _o_p_e_n(2)). Once open, the data stored under a key is accessed by _d_b_m__f_e_t_c_h and data is placed under a key by _d_b_m__s_t_o_r_e. The _f_l_a_g_s field can be either DBM_INSERT or DBM_REPLACE. DBM_INSERT will only insert new entries into the database and will not change an existing entry with the same key. DBM_REPLACE will replace an existing entry if it has the same key. A key (and its associated contents) is deleted by _d_b_m__d_e_l_e_t_e. A linear pass through all keys in a database may be made, in an (appar‐ ently) random order, by use of _d_b_m__f_i_r_s_t_k_e_y and _d_b_m__n_e_x_t_k_e_y. _D_b_m__f_i_r_s_t_k_e_y will return the first key in the database. _D_b_m__n_e_x_t_k_e_y will return the next key in the database. This code will traverse the data base: for (key = dbm_firstkey(db); key.dptr != NULL; key = dbm_nex‐ tkey(db)) _D_b_m__e_r_r_o_r returns non-zero when an error has occurred reading or writ‐ ing the database. _D_b_m__c_l_e_a_r_e_r_r resets the error condition on the named database. DIAGNOSTICS All functions that return an _i_n_t indicate errors with negative values. A zero return indicates ok. Routines that return a _d_a_t_u_m indicate errors with a null (0) _d_p_t_r_. If _d_b_m__s_t_o_r_e called with a _f_l_a_g_s value of DBM_INSERT finds an existing entry with the same key it returns 1. BUGS The ‘.pag’ file will contain holes so that its apparent size is about four times its actual content. Older UNIX systems may create real file blocks for these holes when touched. These files cannot be copied by normal means (cp, cat, tp, tar, ar) without filling in the holes. _D_p_t_r pointers returned by these subroutines point into static storage that is changed by subsequent calls. The sum of the sizes of a key/content pair must not exceed the internal block size (currently 4096 bytes). Moreover all key/content pairs that hash together must fit on a single block. _D_b_m__s_t_o_r_e will return an error in the event that a disk block fills with inseparable data. _D_b_m__d_e_l_e_t_e does not physically reclaim file space, although it does make it available for reuse. The order of keys presented by _d_b_m__f_i_r_s_t_k_e_y and _d_b_m__n_e_x_t_k_e_y depends on a hashing function, not on anything interesting. SEE ALSO dbm(3X) 4.3 Berkeley Distribution May 20, 1986 NDBM(3)