manual page first distributed with 4.3BSD
[unix-history] / usr / src / old / libndbm / ndbm.3
CommitLineData
53e9b2ee
KM
1.\" Copyright (c) 1985 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
5.\" @(#)ndbm.3 6.1 (Berkeley) %G%
6.\"
7.TH NDBM 3X ""
8.UC 6
9.SH NAME
10dbm_open, dbm_close, dbm_fetch, dbm_store, dbm_delete, dbm_firstkey, dbm_nextkey, dbm_error, dbm_clearerr \- data base subroutines
11.SH SYNOPSIS
12.nf
13.PP
14.ft B
15#include <ndbm.h>
16.PP
17.ft B
18typedef struct {
19 char *dptr;
20 int dsize;
21} datum;
22.PP
23.ft B
24DBM *dbm_open(file, flags, mode)
25 char *file;
26 int flags, mode;
27.PP
28.ft B
29dbm_close(db)
30 DBM *db;
31.PP
32.ft B
33datum dbm_fetch(db, key)
34 DBM *db;
35 struct key;
36 datum key;
37.PP
38.ft B
39dbm_store(db, key, content, flags)
40 DBM *db;
41 datum key, content;
42 int flags;
43.PP
44.ft B
45dbm_delete(db, key)
46 DBM *db;
47 datum key;
48.PP
49.ft B
50datum dbm_firstkey(db)
51 DBM *db;
52.PP
53.ft B
54datum dbm_nextkey(db)
55 DBM *db;
56.PP
57.ft B
58datum dbm_error(db)
59 DBM *db;
60.PP
61.ft B
62datum dbm_clearerr(db)
63 DBM *db;
64.SH DESCRIPTION
65These functions maintain key/content pairs in a data base.
66The functions will handle very large (a billion blocks)
67databases and will access a keyed item in one or two file system accesses.
68.PP
69.IR Key s
70and
71.IR content s
72are described by the
73.I datum
74typedef. A
75.I datum
76specifies a string of
77.I dsize
78bytes pointed to by
79.I dptr.
80Arbitrary binary data, as well as normal ASCII strings, are allowed.
81The data base is stored in two files.
82One file is a directory containing a bit map and has `.dir' as its suffix.
83The second file contains all data and has `.pag' as its suffix.
84.PP
85Before a database can be accessed, it must be opened by
86.IR dbm_open .
87This will open and/or create the files
88.IB file .dir
89and
90.IB file .pag
91depending on the flags parameter (see
92.IR open (2)).
93.PP
94Once open, the data stored under a key is accessed by
95.I dbm_fetch
96and data is placed under a key by
97.IR dbm_store .
98The
99.I flags
100field can be either
101.B DBM_INSERT
102or
103.B DBM_REPLACE.
104.B DBM_INSERT
105will only insert new entries into the database and will not
106change an existing entry with the same key.
107.B DBM_REPLACE
108will replace an existing entry if it has the same key.
109A key (and its associated contents) is deleted by
110.IR dbm_delete .
111A linear pass through all keys in a database may be made,
112in an (apparently) random order, by use of
113.I dbm_firstkey
114and
115.IR dbm_nextkey .
116.I Dbm_firstkey
117will return the first key in the database.
118.I Dbm_nextkey
119will return the next key in the database.
120This code will traverse the data base:
121.IP
122.B for
123(key = dbm_firstkey(db); key.dptr != NULL; key = dbm_nextkey(db))
124.PP
125.I Dbm_error
126returns non-zero when an error has occured reading or writing the database.
127.I Dbm_clearerr
128Resets the error condition on the named database.
129.SH DIAGNOSTICS
130All functions that return an
131.I int
132indicate errors with negative values. A zero return indicates ok.
133Routines that return a
134.I datum
135indicate errors with a null (0)
136.I dptr.
137.SH BUGS
138The `.pag' file will contain holes so that its apparent size is about
139four times its actual content. Older UNIX systems may create real
140file blocks for these holes when touched. These files cannot be copied
141by normal means (cp, cat, tp, tar, ar) without filling in the holes.
142.PP
143.I Dptr
144pointers returned by these subroutines point into static storage
145that is changed by subsequent calls.
146.PP
147The sum of the sizes of a key/content pair must not exceed
148the internal block size (currently 4096 bytes).
149Moreover all key/content pairs that hash together must fit on a single block.
150.I Dbm_store
151will return an error in the event that a disk block fills with inseparable data.
152.PP
153.I Dbm_delete
154does not physically reclaim file space,
155although it does make it available for reuse.
156.PP
157The order of keys presented by
158.I dbm_firstkey
159and
160.I dbm_nextkey
161depends on a hashing function, not on anything interesting.