xref: /openbsd/lib/libc/db/man/ndbm.3 (revision 78b63d65)
1.\" David Leonard, 1998. Placed in the public domain.
2.\" $OpenBSD: ndbm.3,v 1.12 2000/10/25 16:44:08 aaron Exp $
3.Dd May 13, 1998
4.Dt NDBM 3
5.Os
6.Sh NAME
7.Nm dbm_clearerr ,
8.Nm dbm_close ,
9.Nm dbm_delete ,
10.Nm dbm_dirfno ,
11.Nm dbm_error ,
12.Nm dbm_fetch ,
13.Nm dbm_firstkey ,
14.Nm dbm_nextkey ,
15.Nm dbm_open ,
16.Nm dbm_pagfno ,
17.Nm dbm_store
18.Nd database access methods
19.Sh SYNOPSIS
20.Fd #include <ndbm.h>
21.Ft int
22.Fn dbm_clearerr "DBM *db"
23.Ft void
24.Fn dbm_close "DBM *db"
25.Ft int
26.Fn dbm_delete "DBM *db" "datum key"
27.Ft int
28.Fn dbm_dirfno "DBM *db"
29.Ft int
30.Fn dbm_error "DBM *db"
31.Ft datum
32.Fn dbm_fetch "DBM *db" "datum key"
33.Ft datum
34.Fn dbm_firstkey "DBM *db"
35.Ft datum
36.Fn dbm_nextkey "DBM *db"
37.Ft "DBM *"
38.Fn dbm_open "const char *file" "int flags" "int mode"
39.Ft int
40.Fn dbm_pagfno "DBM *db"
41.Ft int
42.Fn dbm_store "DBM *db" "datum key" "datum content" "int store_mode"
43.Sh DESCRIPTION
44These functions provide a ndbm-compatible interface to the
45database access methods described in
46.Xr db 3 .
47Each unique record in the database is a key/content pair,
48the components of which may be any arbitrary binary data.
49The key and the content data are described by the
50.Ft datum
51data structure:
52.Bd -literal -offset indent
53typedef struct {
54	char *dptr;
55	int dsize;
56} datum
57.Ed
58.Pp
59The
60.Fn dbm_open
61function is used to open a database in the file named by
62.Fa file ,
63suffixed with
64.Dv DBM_SUFFIX
65.Pq Sq Pa .db .
66If necessary, the file is created with mode
67.Ar mode .
68Access to this file depends on the
69.Fa flags
70parameter (see
71.Xr open 2 ) .
72Read-only access may be indicated by specifying
73.Dv DBM_RDONLY .
74.Pp
75Once the database is open,
76.Fn dbm_fetch
77is used to retrieve the data content associated with the key
78.Fa key .
79Similarly,
80.Fn dbm_store
81is used to store the
82.Fa content
83data with the key
84.Fa key .
85When storing, the
86.Fa store_mode
87parameter must be one of:
88.Bl -tag -width DBM_REPLACE -offset indent
89.It Dv DBM_INSERT
90Only insert new keys into the database.
91Existing key/content pairs are untouched.
92.It Dv DBM_REPLACE
93Replace any existing entry with the same key.
94Any previously stored records with the same key are lost.
95.El
96.Pp
97The
98.Fn dbm_delete
99function removes the key
100.Fa key
101and its associated content from the database.
102.Pp
103The functions
104.Fn dbm_firstkey
105and
106.Fn dbm_nextkey
107are used to iterate over all of the records in the database.
108Each record will be reached exactly once, but in no particular order.
109The
110.Fn dbm_firstkey
111function returns the first record of the database, and thereafter
112.Fn dbm_nextkey
113returns the following records.
114The following code traverses the entire database:
115.Bd -literal
116  for (key = dbm_firstkey(db); key.dptr != NULL; key = dbm_nextkey(db))
117.Ed
118.Pp
119The behaviour of
120.Fn dbm_nextkey
121is undefined if the database is modified after a call to
122.Fn dbm_firstkey .
123.Pp
124The
125.Fn dbm_error
126function returns the last error condition of the database,
127or 0 if no error had occurred or had been cleared.
128The
129.Fn dbm_clearerr
130function clears the error condition of the database.
131.Pp
132The
133.Fn dbm_dirfno
134function is used to find the file descriptor associated with the
135directory file of an open database.
136Since a directory bitmap file is not used in this implementation,
137this function returns the file descriptor of the datbase file opened with
138.Fn dbm_open .
139.Pp
140The
141.Fn dbm_pagfno
142function is used to find the file descriptor associated with the
143page file of an open database.
144Since a page file is not used in this implementation, this function
145is implemented as a macro that always returns the (undefined) value
146.Dv DBM_PAGFNO_NOT_AVAILABLE .
147.Pp
148The database is closed with the
149.Fn dbm_close
150function.
151Thereafter, the
152.Fa db
153handle is invalid.
154.Ss Implementation notes
155The underlying database is a
156.Xr hash 3
157database with a
158bucket size of 4096,
159a filling factor of 40,
160default hashing function and cache size,
161and uses the host's native byte order.
162.Sh RETURN VALUES
163Upon successful completion, all functions that return
164.Ft int
165return a value of 0, otherwise a negative value is returned.
166.Pp
167Routines that return a
168.Ft datum
169indicate errors by setting the
170.Va dptr
171field to
172.Dv NULL .
173.Pp
174The
175.Fn dbm_open
176function returns
177.Dv NULL
178on error, and sets
179.Va errno
180appropriately.
181On success, it returns a handle to the database that should be
182used as the
183.Fa db
184argument in the other functions.
185.Pp
186The
187.Fn dbm_store
188function returns 1 when it is called with a
189.Fa flags
190value of
191.Dv DBM_INSERT
192and a record with the specified key already exists.
193.Sh ERRORS
194If an error occurs, the error can be retrieved with
195.Fn dbm_error
196and corresponds to those errors described in
197.Xr db 3 .
198.Sh SEE ALSO
199.Xr open 2 ,
200.Xr db 3 ,
201.Xr hash 3
202