Unleashed v1.4
[unleashed.git] / share / man / man3c / ndbm.3c
blob5f50e78ece2d33d068fd1832b27b9beb811eb287
1 .\"
2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document.  The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
27 .\" The Berkeley software License Agreement specifies the terms and conditions
28 .\" for redistribution.
29 .\"
30 .\"
31 .\" Copyright (c) 1985 Regents of the University of California.
32 .\" All rights reserved.
33 .\" Portions Copyright (c) 1992, X/Open Company Limited  All Rights Reserved
34 .\" Copyright (c) 2001, Sun Microsystems, Inc.  All Rights Reserved.
35 .\"
36 .TH NDBM 3C "May 13, 2017"
37 .SH NAME
38 ndbm, dbm_clearerr, dbm_close, dbm_delete, dbm_error, dbm_fetch, dbm_firstkey,
39 dbm_nextkey, dbm_open, dbm_store \- database functions
40 .SH SYNOPSIS
41 .LP
42 .nf
43 #include <ndbm.h>
45 \fBint\fR \fBdbm_clearerr\fR(\fBDBM *\fR\fIdb\fR);
46 .fi
48 .LP
49 .nf
50 \fBvoid\fR \fBdbm_close\fR(\fBDBM *\fR\fIdb\fR);
51 .fi
53 .LP
54 .nf
55 \fBint\fR \fBdbm_delete\fR(\fBDBM *\fR\fIdb\fR, \fBdatum\fR \fIkey\fR);
56 .fi
58 .LP
59 .nf
60 \fBint\fR \fBdbm_error\fR(\fBDBM *\fR\fIdb\fR);
61 .fi
63 .LP
64 .nf
65 \fBdatum\fR \fBdbm_fetch\fR(\fBDBM *\fR\fIdb\fR, \fBdatum\fR \fIkey\fR);
66 .fi
68 .LP
69 .nf
70 \fBdatum\fR \fBdbm_firstkey\fR(\fBDBM *\fR\fIdb\fR);
71 .fi
73 .LP
74 .nf
75 \fBdatum\fR \fBdbm_nextkey\fR(\fBDBM *\fR\fIdb\fR);
76 .fi
78 .LP
79 .nf
80 \fBDBM *\fR\fBdbm_open\fR(\fBconst char *\fR\fIfile\fR, \fBint\fR \fIopen_flags\fR, \fBmode_t\fR \fIfile_mode\fR);
81 .fi
83 .LP
84 .nf
85 \fBint\fR \fBdbm_store\fR(\fBDBM *\fR\fIdb\fR, \fBdatum\fR \fIkey\fR, \fBdatum\fR \fIcontent\fR, \fBint\fR \fIstore_mode\fR);
86 .fi
88 .SH DESCRIPTION
89 .LP
90 These functions create, access and modify a database. They maintain
91 \fIkey\fR/\fIcontent\fR pairs in a database. The functions will handle large
92 databases (up to a billion blocks) and will access a keyed item in one or two
93 file system accesses. This package replaces the earlier \fBdbm\fR
94 library, which managed only a single database.
95 .sp
96 .LP
97 \fIkey\fRs and \fIcontent\fRs are described by the \fBdatum\fR typedef. A
98 \fBdatum\fR consists of at least two members, \fBdptr\fR and \fBdsize\fR. The
99 \fBdptr\fR member points to an object that is \fBdsize\fR bytes in length.
100 Arbitrary binary data, as well as ASCII character strings, may be stored in the
101 object pointed to by \fBdptr\fR.
104 The database is stored in two files. One file is a directory containing a bit
105 map of keys and has \fB\&.dir\fR as its suffix.  The second file contains all
106 data and has \fB\&.pag\fR as its suffix.
109 The \fBdbm_open()\fR function opens a database.  The \fBfile\fR argument to the
110 function is the pathname of the database.  The function opens two files named
111 \fBfile\fR\fB\&.dir\fR and \fBfile\fR\fB\&.pag.\fR The \fIopen_flags\fR
112 argument has the same meaning as the \fIflags\fR argument of \fBopen\fR(2)
113 except that a database opened for write-only access opens the files for read
114 and write access.  The \fIfile_mode\fR argument has the same meaning as the
115 third argument of \fBopen\fR(2).
118 The \fBdbm_close()\fR function closes a database.  The argument \fIdb\fR must
119 be a pointer to a \fBdbm\fR structure that has been returned from a call to
120 \fBdbm_open()\fR.
123 The \fBdbm_fetch()\fR function reads a record from a database.  The argument
124 \fIdb\fR is a pointer to a database structure that has been returned from a
125 call to \fBdbm_open()\fR. The argument \fIkey\fR is a \fBdatum\fR that has been
126 initialized by the application program to the value of the key that matches the
127 key of the record the program is fetching.
130 The \fBdbm_store()\fR function writes a record to a database.  The argument
131 \fIdb\fR is a pointer to a database structure that has been returned from a
132 call to \fBdbm_open()\fR. The argument \fIkey\fR is a \fBdatum\fR that has been
133 initialized by the application program to the value of the key that identifies
134 (for subsequent reading, writing or deleting) the record the program is
135 writing. The argument \fIcontent\fR is a \fBdatum\fR that has been initialized
136 by the application program to the value of the record the program is writing.
137 The argument \fIstore_mode\fR controls whether \fBdbm_store()\fR replaces any
138 pre-existing record that has the same key that is specified by the \fIkey\fR
139 argument.  The application program must set \fIstore_mode\fR to either
140 \fBDBM_INSERT\fR or \fBDBM_REPLACE\fR.  If the database contains a record that
141 matches the \fIkey\fR argument and \fIstore_mode\fR is \fBDBM_REPLACE\fR, the
142 existing record is replaced with the new record.  If the database contains a
143 record that matches the \fIkey\fR argument and \fIstore_mode\fR is
144 \fBDBM_INSERT\fR, the existing record is not replaced with the new record.  If
145 the database does not contain a record that matches the \fIkey\fR argument and
146 \fIstore_mode\fR is either \fBDBM_INSERT\fR or \fBDBM_REPLACE\fR, the new
147 record is inserted in the database.
150 The \fBdbm_delete()\fR function deletes a record and its key from the database.
151 The argument \fIdb\fR is a pointer to a database structure that has been
152 returned from a call to \fBdbm_open()\fR. The argument \fIkey\fR is a
153 \fBdatum\fR that has been initialized by the application program to the value
154 of the key that identifies the record the program is deleting.
157 The \fBdbm_firstkey()\fR function returns the first key in the database.  The
158 argument \fIdb\fR is a pointer to a database structure that has been returned
159 from a call to \fBdbm_open()\fR.
162 The \fBdbm_nextkey()\fR function returns the next key in the database.  The
163 argument \fIdb\fR is a pointer to a database structure that has been returned
164 from a call to \fBdbm_open()\fR. The \fBdbm_firstkey()\fR function must be
165 called before calling \fBdbm_nextkey()\fR. Subsequent calls to
166 \fBdbm_nextkey()\fR return the next key until all of the keys in the database
167 have been returned.
170 The \fBdbm_error()\fR function returns the error condition of the database.
171 The argument \fIdb\fR is a pointer to a database structure that has been
172 returned from a call to \fBdbm_open()\fR.
175 The \fBdbm_clearerr()\fR function clears the error condition of the database.
176 The argument \fIdb\fR is a pointer to a database structure that has been
177 returned from a call to \fBdbm_open()\fR.
180 These database functions support key/content pairs of at least 1024 bytes.
181 .SH RETURN VALUES
183 The \fBdbm_store()\fR and \fBdbm_delete()\fR functions return \fB0\fR when they
184 succeed and a negative value when they fail.
187 The \fBdbm_store()\fR function returns \fB1\fR if it is called with a
188 \fIflags\fR value of \fBDBM_INSERT\fR and the function finds an existing record
189 with the same key.
192 The \fBdbm_error()\fR function returns \fB0\fR if the error condition is not
193 set and returns a non-zero value if the error condition is set.
196 The return value of \fBdbm_clearerr()\fR is unspecified .
199 The \fBdbm_firstkey()\fR and \fBdbm_nextkey()\fR functions return a key
200 \fBdatum\fR. When the end of the database is reached, the \fBdptr\fR member of
201 the key is a null pointer.  If an error is detected, the \fBdptr\fR member of
202 the key is a null pointer and the error condition of the database is set.
205 The \fBdbm_fetch()\fR function returns a content \fBdatum\fR. If no record in
206 the database matches the key or if an error condition has been detected in the
207 database, the \fBdptr\fR member of the content is a null pointer.
210 The \fBdbm_open()\fR function returns a pointer to a database structure.  If an
211 error is detected during the operation, \fBdbm_open()\fR returns a (\fBDBM
212 *\fR)0.
213 .SH ERRORS
215 No errors are defined.
216 .SH USAGE
218 The following code can be used to traverse the database:
220 .in +2
222 for(key = dbm_firstkey(db); key.dptr != NULL; key = dbm_nextkey(db))
224 .in -2
228 The \fBdbm_\fR functions provided in this library should not be confused in any
229 way with those of a general-purpose database management system.  These
230 functions do not provide for multiple search keys per entry, they do not
231 protect against multi-user access (in other words they do not lock records or
232 files), and they do not provide the many other useful database functions that
233 are found in more robust database management systems.  Creating and updating
234 databases by use of these functions is relatively slow because of data copies
235 that occur upon hash collisions.  These functions are useful for applications
236 requiring fast lookup of relatively static information that is to be indexed by
237 a single key.
240 The \fBdptr\fR pointers returned by these functions may point into static
241 storage that may be changed by subsequent calls.
244 The \fBdbm_delete()\fR function does not physically reclaim file space,
245 although it does make it available for reuse.
248 After calling \fBdbm_store()\fR or \fBdbm_delete()\fR during a pass through the
249 keys by \fBdbm_firstkey()\fR and \fBdbm_nextkey()\fR, the application should
250 reset the database by calling \fBdbm_firstkey()\fR before again calling
251 \fBdbm_nextkey()\fR.
252 .SH EXAMPLES
254 \fBExample 1 \fRUsing the Database Functions
257 The following example stores and retrieves a phone number, using the name as
258 the key.  Note that this example does not include error checking.
261 .in +2
263 #include <ndbm.h>
264 #include <stdio.h>
265 #include <fcntl.h>
266 #define NAME      "Bill"
267 #define PHONE_NO          "123-4567"
268 #define DB_NAME   "phones"
269 main()
271      DBM *db;
272      datum name = {NAME, sizeof (NAME)};
273      datum put_phone_no = {PHONE_NO, sizeof (PHONE_NO)};
274      datum get_phone_no;
275      /* Open the database and store the record */
276      db = dbm_open(DB_NAME, O_RDWR | O_CREAT, 0660);
277      (void) dbm_store(db, name, put_phone_no, DBM_INSERT);
278      /* Retrieve the record */
279      get_phone_no = dbm_fetch(db, name);
280      (void) printf("Name: %s, Phone Number: %s\en", name.dptr,
281      get_phone_no.dptr);
282      /* Close the database */
283      dbm_close(db);
284      return (0);
287 .in -2
289 .SH ATTRIBUTES
291 See \fBattributes\fR(5) for descriptions of the following attributes:
296 box;
297 c | c
298 l | l .
299 ATTRIBUTE TYPE  ATTRIBUTE VALUE
301 Interface Stability     Standard
303 MT-Level        Unsafe
306 .SH SEE ALSO
308 \fBar\fR(1), \fBcat\fR(1), \fBcp\fR(1), \fBtar\fR(1), \fBopen\fR(2),
309 \fBnetconfig\fR(4), \fBattributes\fR(5), \fBstandards\fR(5)
310 .SH NOTES
312 The \fB\&.pag\fR file will contain holes so that its apparent size may be
313 larger than its actual content. Older versions of the UNIX operating system may
314 create real file blocks for these holes when touched. These files cannot be
315 copied by normal means ( \fBcp\fR(1), \fBcat\fR(1), \fBtar\fR(1), \fBar\fR(1))
316 without filling in the holes.
319 The sum of the sizes of a \fIkey\fR/\fIcontent\fR pair must not exceed the
320 internal block size (currently 1024 bytes). Moreover all
321 \fIkey\fR/\fIcontent\fR pairs that hash together must fit on a single block.
322 \fBdbm_store()\fR will return an error in the event that a disk block fills
323 with inseparable data.
326 The order of keys presented by \fBdbm_firstkey()\fR and \fBdbm_nextkey()\fR
327 depends on a hashing function.
330 There are no interlocks and no reliable cache flushing; thus concurrent
331 updating and reading is risky.
334 The database files (\fBfile\fR\fB\&.dir\fR and \fBfile\fR\fB\&.pag\fR) are
335 binary and are architecture-specific (for example, they depend on the
336 architecture's byte order.) These files are not guaranteed to be portable
337 across architectures.