1502 Remove conversion cruft from manpages
[unleashed.git] / usr / src / man / man3c / ndbm.3c
blob786f92db24f97d75a9d27c125a49465c370a7cb7
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 "Sep 17, 2001"
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 .sp
62 .LP
63 These functions create, access and modify a database. They maintain
64 \fIkey\fR/\fIcontent\fR pairs in a database. The functions will handle large
65 databases (up to a billion blocks) and will access a keyed item in one or two
66 file system accesses. This package replaces the earlier \fBdbm\fR(3UCB)
67 library, which managed only a single database.
68 .sp
69 .LP
70 \fIkey\fRs and \fIcontent\fRs are described by the \fBdatum\fR typedef. A
71 \fBdatum\fR consists of at least two members, \fBdptr\fR and \fBdsize\fR. The
72 \fBdptr\fR member points to an object that is \fBdsize\fR bytes in length.
73 Arbitrary binary data, as well as ASCII character strings, may be stored in the
74 object pointed to by \fBdptr\fR.
75 .sp
76 .LP
77 The database is stored in two files. One file is a directory containing a bit
78 map of keys and has \fB\&.dir\fR as its suffix.  The second file contains all
79 data and has \fB\&.pag\fR as its suffix.
80 .sp
81 .LP
82 The \fBdbm_open()\fR function opens a database.  The \fBfile\fR argument to the
83 function is the pathname of the database.  The function opens two files named
84 \fBfile\fR\fB\&.dir\fR and \fBfile\fR\fB\&.pag.\fR The \fIopen_flags\fR
85 argument has the same meaning as the \fIflags\fR argument of \fBopen\fR(2)
86 except that a database opened for write-only access opens the files for read
87 and write access.  The \fIfile_mode\fR argument has the same meaning as the
88 third argument of \fBopen\fR(2).
89 .sp
90 .LP
91 The \fBdbm_close()\fR function closes a database.  The argument \fIdb\fR must
92 be a pointer to a \fBdbm\fR structure that has been returned from a call to
93 \fBdbm_open()\fR.
94 .sp
95 .LP
96 The \fBdbm_fetch()\fR function reads a record from a database.  The argument
97 \fIdb\fR is a pointer to a database structure that has been returned from a
98 call to \fBdbm_open()\fR. The argument \fIkey\fR is a \fBdatum\fR that has been
99 initialized by the application program to the value of the key that matches the
100 key of the record the program is fetching.
103 The \fBdbm_store()\fR function writes a record to a database.  The argument
104 \fIdb\fR is a pointer to a database structure that has been returned from a
105 call to \fBdbm_open()\fR. The argument \fIkey\fR is a \fBdatum\fR that has been
106 initialized by the application program to the value of the key that identifies
107 (for subsequent reading, writing or deleting) the record the program is
108 writing. The argument \fIcontent\fR is a \fBdatum\fR that has been initialized
109 by the application program to the value of the record the program is writing.
110 The argument \fIstore_mode\fR controls whether \fBdbm_store()\fR replaces any
111 pre-existing record that has the same key that is specified by the \fIkey\fR
112 argument.  The application program must set \fIstore_mode\fR to either
113 \fBDBM_INSERT\fR or \fBDBM_REPLACE\fR.  If the database contains a record that
114 matches the \fIkey\fR argument and \fIstore_mode\fR is \fBDBM_REPLACE\fR, the
115 existing record is replaced with the new record.  If the database contains a
116 record that matches the \fIkey\fR argument and \fIstore_mode\fR is
117 \fBDBM_INSERT\fR, the existing record is not replaced with the new record.  If
118 the database does not contain a record that matches the \fIkey\fR argument and
119 \fIstore_mode\fR is either \fBDBM_INSERT\fR or \fBDBM_REPLACE\fR, the new
120 record is inserted in the database.
123 The \fBdbm_delete()\fR function deletes a record and its key from the database.
124 The argument \fIdb\fR is a pointer to a database structure that has been
125 returned from a call to \fBdbm_open()\fR. The argument \fIkey\fR is a
126 \fBdatum\fR that has been initialized by the application program to the value
127 of the key that identifies the record the program is deleting.
130 The \fBdbm_firstkey()\fR function returns the first key in the database.  The
131 argument \fIdb\fR is a pointer to a database structure that has been returned
132 from a call to \fBdbm_open()\fR.
135 The \fBdbm_nextkey()\fR function returns the next key in the database.  The
136 argument \fIdb\fR is a pointer to a database structure that has been returned
137 from a call to \fBdbm_open()\fR. The \fBdbm_firstkey()\fR function must be
138 called before calling \fBdbm_nextkey()\fR. Subsequent calls to
139 \fBdbm_nextkey()\fR return the next key until all of the keys in the database
140 have been returned.
143 The \fBdbm_error()\fR function returns the error condition of the database.
144 The argument \fIdb\fR is a pointer to a database structure that has been
145 returned from a call to \fBdbm_open()\fR.
148 The \fBdbm_clearerr()\fR function clears the error condition of the database.
149 The argument \fIdb\fR is a pointer to a database structure that has been
150 returned from a call to \fBdbm_open()\fR.
153 These database functions support key/content pairs of at least 1024 bytes.
154 .SH RETURN VALUES
157 The \fBdbm_store()\fR and \fBdbm_delete()\fR functions return \fB0\fR when they
158 succeed and a negative value when they fail.
161 The \fBdbm_store()\fR function returns \fB1\fR if it is called with a
162 \fIflags\fR value of \fBDBM_INSERT\fR and the function finds an existing record
163 with the same key.
166 The \fBdbm_error()\fR function returns \fB0\fR if the error condition is not
167 set and returns a non-zero value if the error condition is set.
170 The return value of \fBdbm_clearerr()\fR is unspecified .
173 The \fBdbm_firstkey()\fR and \fBdbm_nextkey()\fR functions return a key
174 \fBdatum\fR. When the end of the database is reached, the \fBdptr\fR member of
175 the key is a null pointer.  If an error is detected, the \fBdptr\fR member of
176 the key is a null pointer and the error condition of the database is set.
179 The \fBdbm_fetch()\fR function returns a content \fBdatum\fR. If no record in
180 the database matches the key or if an error condition has been detected in the
181 database, the \fBdptr\fR member of the content is a null pointer.
184 The \fBdbm_open()\fR function returns a pointer to a database structure.  If an
185 error is detected during the operation, \fBdbm_open()\fR returns a (\fBDBM
186 *\fR)0.
187 .SH ERRORS
190 No errors are defined.
191 .SH USAGE
194 The following code can be used to traverse the database:
196 .in +2
198 for(key = dbm_firstkey(db); key.dptr != NULL; key = dbm_nextkey(db))
200 .in -2
204 The \fBdbm_\fR functions provided in this library should not be confused in any
205 way with those of a general-purpose database management system.  These
206 functions do not provide for multiple search keys per entry, they do not
207 protect against multi-user access (in other words they do not lock records or
208 files), and they do not provide the many other useful database functions that
209 are found in more robust database management systems.  Creating and updating
210 databases by use of these functions is relatively slow because of data copies
211 that occur upon hash collisions.  These functions are useful for applications
212 requiring fast lookup of relatively static information that is to be indexed by
213 a single key.
216 The \fBdptr\fR pointers returned by these functions may point into static
217 storage that may be changed by subsequent calls.
220 The \fBdbm_delete()\fR function does not physically reclaim file space,
221 although it does make it available for reuse.
224 After calling \fBdbm_store()\fR or \fBdbm_delete()\fR during a pass through the
225 keys by \fBdbm_firstkey()\fR and \fBdbm_nextkey()\fR, the application should
226 reset the database by calling \fBdbm_firstkey()\fR before again calling
227 \fBdbm_nextkey()\fR.
228 .SH EXAMPLES
230 \fBExample 1 \fRUsing the Database Functions
233 The following example stores and retrieves a phone number, using the name as
234 the key.  Note that this example does not include error checking.
237 .in +2
239 #include <ndbm.h>
240 #include <stdio.h>
241 #include <fcntl.h>
242 #define NAME      "Bill"
243 #define PHONE_NO          "123-4567"
244 #define DB_NAME   "phones"
245 main()
247      DBM *db;
248      datum name = {NAME, sizeof (NAME)};
249      datum put_phone_no = {PHONE_NO, sizeof (PHONE_NO)};
250      datum get_phone_no;
251      /* Open the database and store the record */
252      db = dbm_open(DB_NAME, O_RDWR | O_CREAT, 0660);
253      (void) dbm_store(db, name, put_phone_no, DBM_INSERT);
254      /* Retrieve the record */
255      get_phone_no = dbm_fetch(db, name);
256      (void) printf("Name: %s, Phone Number: %s\en", name.dptr,
257      get_phone_no.dptr);
258      /* Close the database */
259      dbm_close(db);
260      return (0);
263 .in -2
265 .SH ATTRIBUTES
268 See \fBattributes\fR(5) for descriptions of the following attributes:
273 box;
274 c | c
275 l | l .
276 ATTRIBUTE TYPE  ATTRIBUTE VALUE
278 Interface Stability     Standard
280 MT-Level        Unsafe
283 .SH SEE ALSO
286 \fBar\fR(1), \fBcat\fR(1), \fBcp\fR(1), \fBtar\fR(1), \fBopen\fR(2),
287 \fBdbm\fR(3UCB), \fBnetconfig\fR(4), \fBattributes\fR(5), \fBstandards\fR(5)
288 .SH NOTES
291 The \fB\&.pag\fR file will contain holes so that its apparent size may be
292 larger than its actual content. Older versions of the UNIX operating system may
293 create real file blocks for these holes when touched. These files cannot be
294 copied by normal means ( \fBcp\fR(1), \fBcat\fR(1), \fBtar\fR(1), \fBar\fR(1))
295 without filling in the holes.
298 The sum of the sizes of a \fIkey\fR/\fIcontent\fR pair must not exceed the
299 internal block size (currently 1024 bytes). Moreover all
300 \fIkey\fR/\fIcontent\fR pairs that hash together must fit on a single block.
301 \fBdbm_store()\fR will return an error in the event that a disk block fills
302 with inseparable data.
305 The order of keys presented by \fBdbm_firstkey()\fR and \fBdbm_nextkey()\fR
306 depends on a hashing function.
309 There are no interlocks and no reliable cache flushing; thus concurrent
310 updating and reading is risky.
313 The database files (\fBfile\fR\fB\&.dir\fR and \fBfile\fR\fB\&.pag\fR) are
314 binary and are architecture-specific (for example, they depend on the
315 architecture's byte order.)  These files are not guaranteed to be portable
316 across architectures.