Standard C Library Functions ndbm(3C)
NAME
ndbm, dbmclearerr, dbmclose, dbmdelete, dbmerror,
dbmfetch, dbmfirstkey, dbmnextkey, dbmopen, dbmstore -
database functions
SYNOPSIS
#include
int dbmclearerr(DBM *db);
void dbmclose(DBM *db);
int dbmdelete(DBM *db, datum key);
int dbmerror(DBM *db);
datum dbmfetch(DBM *db, datum key);
datum dbmfirstkey(DBM *db);
datum dbmnextkey(DBM *db);
DBM *dbmopen(const char *file, int openflags, modet filemode);
int dbmstore(DBM *db, datum key, datum content, int storemode);
DESCRIPTION
These functions create, access and modify a database. They
maintain key/content pairs in a database. The functions will
handle large databases (up to a billion blocks) and will
access a keyed item in one or two file system accesses. This
package replaces the earlier dbm(3UCB) library, which
managed only a single database.
keys and contents are described by the datum typedef. A
datum consists of at least two members, dptr and dsize. The
dptr member points to an object that is dsize bytes in
length. Arbitrary binary data, as well as ASCI character
strings, may be stored in the object pointed to by dptr.
SunOS 5.11 Last change: 17 Sep 2001 1
Standard C Library Functions ndbm(3C)
The database is stored in two files. One file is a directory
containing a bit map of keys and has .dir as its suffix.
The second file contains all data and has .pag as its suf-
fix.
The dbmopen() function opens a database. The file argument
to the function is the pathname of the database. The func-
tion opens two files named file.dir and file.pag. The
openflags argument has the same meaning as the flags argu-
ment of open(2) except that a database opened for write-only
access opens the files for read and write access. The
filemode argument has the same meaning as the third argu-
ment of open(2).
The dbmclose() function closes a database. The argument db
must be a pointer to a dbm structure that has been returned
from a call to dbmopen().
The dbmfetch() function reads a record from a database.
The argument db is a pointer to a database structure that
has been returned from a call to dbmopen(). The argument
key is a datum that has been initialized by the application
program to the value of the key that matches the key of the
record the program is fetching.
The dbmstore() function writes a record to a database. The
argument db is a pointer to a database structure that has
been returned from a call to dbmopen(). The argument key is
a datum that has been initialized by the application program
to the value of the key that identifies (for subsequent
reading, writing or deleting) the record the program is
writing. The argument content is a datum that has been ini-
tialized by the application program to the value of the
record the program is writing. The argument storemode con-
trols whether dbmstore() replaces any pre-existing record
that has the same key that is specified by the key argument.
The application program must set storemode to either
DBMINSERT or DBMREPLACE. If the database contains a
record that matches the key argument and storemode is
DBMREPLACE, the existing record is replaced with the new
record. If the database contains a record that matches the
key argument and storemode is DBMINSERT, the existing
record is not replaced with the new record. If the database
does not contain a record that matches the key argument and
storemode is either DBMINSERT or DBMREPLACE, the new
record is inserted in the database.
SunOS 5.11 Last change: 17 Sep 2001 2
Standard C Library Functions ndbm(3C)
The dbmdelete() function deletes a record and its key from
the database. The argument db is a pointer to a database
structure that has been returned from a call to dbmopen().
The argument key is a datum that has been initialized by the
application program to the value of the key that identifies
the record the program is deleting.
The dbmfirstkey() function returns the first key in the
database. The argument db is a pointer to a database struc-
ture that has been returned from a call to dbmopen().
The dbmnextkey() function returns the next key in the data-
base. The argument db is a pointer to a database structure
that has been returned from a call to dbmopen(). The
dbmfirstkey() function must be called before calling
dbmnextkey(). Subsequent calls to dbmnextkey() return the
next key until all of the keys in the database have been
returned.
The dbmerror() function returns the error condition of the
database. The argument db is a pointer to a database struc-
ture that has been returned from a call to dbmopen().
The dbmclearerr() function clears the error condition of
the database. The argument db is a pointer to a database
structure that has been returned from a call to dbmopen().
These database functions support key/content pairs of at
least 1024 bytes.
RETURN VALUES
The dbmstore() and dbmdelete() functions return 0 when
they succeed and a negative value when they fail.
The dbmstore() function returns 1 if it is called with a
flags value of DBMINSERT and the function finds an existing
record with the same key.
The dbmerror() function returns 0 if the error condition is
not set and returns a non-zero value if the error condition
is set.
The return value of dbmclearerr() is unspecified .
SunOS 5.11 Last change: 17 Sep 2001 3
Standard C Library Functions ndbm(3C)
The dbmfirstkey() and dbmnextkey() functions return a key
datum. When the end of the database is reached, the dptr
member of the key is a null pointer. If an error is
detected, the dptr member of the key is a null pointer and
the error condition of the database is set.
The dbmfetch() function returns a content datum. If no
record in the database matches the key or if an error condi-
tion has been detected in the database, the dptr member of
the content is a null pointer.
The dbmopen() function returns a pointer to a database
structure. If an error is detected during the operation,
dbmopen() returns a (DBM *)0.
ERORS
No errors are defined.
USAGE
The following code can be used to traverse the database:
for(key = dbmfirstkey(db); key.dptr != NUL; key = dbmnextkey(db))
The dbm functions provided in this library should not be
confused in any way with those of a general-purpose database
management system. These functions do not provide for mul-
tiple search keys per entry, they do not protect against
multi-user access (in other words they do not lock records
or files), and they do not provide the many other useful
database functions that are found in more robust database
management systems. Creating and updating databases by use
of these functions is relatively slow because of data copies
that occur upon hash collisions. These functions are useful
for applications requiring fast lookup of relatively static
information that is to be indexed by a single key.
The dptr pointers returned by these functions may point into
static storage that may be changed by subsequent calls.
The dbmdelete() function does not physically reclaim file
space, although it does make it available for reuse.
After calling dbmstore() or dbmdelete() during a pass
through the keys by dbmfirstkey() and dbmnextkey(), the
application should reset the database by calling
SunOS 5.11 Last change: 17 Sep 2001 4
Standard C Library Functions ndbm(3C)
dbmfirstkey() before again calling dbmnextkey().
EXAMPLES
Example 1 Using the Database Functions
The following example stores and retrieves a phone number,
using the name as the key. Note that this example does not
include error checking.
#include
#include
#include
#define NAME "Bill"
#define PHONENO "123-4567"
#define DBNAME "phones"
main()
{
DBM *db;
datum name = {NAME, sizeof (NAME)};
datum putphoneno = {PHONENO, sizeof (PHONENO)};
datum getphoneno;
/* Open the database and store the record */
db = dbmopen(DBNAME, ORDWR OCREAT, 0660);
(void) dbmstore(db, name, putphoneno, DBMINSERT);
/* Retrieve the record */
getphoneno = dbmfetch(db, name);
(void) printf("Name: %s, Phone Number: %s\n", name.dptr,
getphoneno.dptr);
/* Close the database */
dbmclose(db);
return (0);
}
ATRIBUTES
See attributes(5) for descriptions of the following attri-
butes:
SunOS 5.11 Last change: 17 Sep 2001 5
Standard C Library Functions ndbm(3C)
ATRIBUTE TYPE ATRIBUTE VALUE
Interface Stability Standard
MT-Level Unsafe
SEE ALSO
ar(1), cat(1), cp(1), tar(1), open(2), dbm(3UCB), netcon-
fig(4), attributes(5), standards(5)
NOTES
The .pag file will contain holes so that its apparent size
may be larger than its actual content. Older versions of the
UNIX operating system may create real file blocks for these
holes when touched. These files cannot be copied by normal
means ( cp(1), cat(1), tar(1), ar(1)) without filling in the
holes.
The sum of the sizes of a key/content pair must not exceed
the internal block size (currently 1024 bytes). Moreover all
key/content pairs that hash together must fit on a single
block. dbmstore() will return an error in the event that a
disk block fills with inseparable data.
The order of keys presented by dbmfirstkey() and
dbmnextkey() depends on a hashing function.
There are no interlocks and no reliable cache flushing; thus
concurrent updating and reading is risky.
The database files (file.dir and file.pag) are binary and
are architecture-specific (for example, they depend on the
architecture's byte order.) These files are not guaranteed
to be portable across architectures.
SunOS 5.11 Last change: 17 Sep 2001 6
|