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