1
\input texinfo @c -*- Texinfo -*-
2
@comment %**start of header (This is for running Texinfo on a region.)
5
@c @setchapternewpage odd
6
@comment %**end of header (This is for running Texinfo on a region.)
13
This file documents the GNU dbm utility.
15
Copyright (C) 1989-1993 Free Software Foundation, Inc.
17
Permission is granted to make and distribute verbatim copies of
18
this manual provided the copyright notice and this permission notice
19
are preserved on all copies.
22
Permission is granted to process this file through Tex and print the
23
results, provided the printed document carries copying permission
24
notice identical to this one except for the removal of this paragraph
25
(this paragraph not being relevant to the printed manual).
28
Permission is granted to copy and distribute modified versions of this
29
manual under the conditions for verbatim copying, provided also that
30
the entire resulting derived work is distributed under the terms of a
31
permission notice identical to this one.
33
Permission is granted to copy and distribute translations of this manual
34
into another language, under the above conditions for modified versions.
40
@center @titlefont{GNU dbm}
42
@center A Database Manager
44
@center by Philip A. Nelson
46
@center Manual by Pierre Gaumond and Philip A. Nelson
47
@center Updated by Jason Downs
51
@center for GNU @code{dbm}, Version 1.7.3.
54
@vskip 0pt plus 1filll
55
Copyright @copyright{} 1993-94 Free Software Foundation, Inc.
58
This is Edition 1.4.1 of the @cite{GNU @code{dbm} Manual}, for @code{gdbm}
60
Last updated May 19, 1994
62
Published by the Free Software Foundation @*
63
675 Massachusetts Avenue, @*
64
Cambridge, MA 02139 USA @*
66
Permission is granted to make and distribute verbatim copies of
67
this manual provided the copyright notice and this permission notice
68
are preserved on all copies.
70
Permission is granted to copy and distribute modified versions of this
71
manual under the conditions for verbatim copying, provided also that
72
the entire resulting derived work is distributed under the terms of a
73
permission notice identical to this one.
75
Permission is granted to copy and distribute translations of this manual
76
into another language, under the above conditions for modified versions,
77
except that this permission notice may be stated in a translation approved
78
by the Free Software Foundation.
82
@node Top, Copying, (dir), (dir)
84
GNU @code{dbm} is a library of functions implementing a hashed database
85
on a disk file. This manual documents GNU @code{dbm} Version 1.7.1
86
(@code{gdbm}). The software was written by Philip A. Nelson. This
87
document was originally written by Pierre Gaumond from texts written by
95
* Copying:: Your rights.
96
* Intro:: Introduction to GNU dbm.
97
* List:: List of functions.
101
* Open:: Opening the database.
102
* Close:: Closing the database.
103
* Store:: Inserting and replacing records in the database.
104
* Fetch:: Searching records in the database.
105
* Delete:: Removing records from the database.
106
* Sequential:: Sequential access to records.
107
* Reorganization:: Database reorganization.
108
* Sync:: Insure all writes to disk have competed.
109
* Errors:: Convert internal error codes into English.
110
* Options:: Setting internal options.
114
* Variables:: Two useful variables.
115
* Compatibility:: Compatibility with UNIX dbm and ndbm.
116
* Conversion:: Converting dbm files to gdbm format.
117
* Bugs:: Problems and bugs.
120
@node Copying, Intro, Top, Top
121
@chapter Copying Conditions.
122
This library is @dfn{free}; this means that everyone is free to use
123
it and free to redistribute it on a free basis. GNU @code{dbm} (@code{gdbm})
124
is not in the public domain; it is copyrighted and there
125
are restrictions on its distribution, but these restrictions are
126
designed to permit everything that a good cooperating citizen would want
127
to do. What is not allowed is to try to prevent others from further
128
sharing any version of @code{gdbm} that they might get from
131
Specifically, we want to make sure that you have the right to give
132
away copies @code{gdbm}, that you receive
133
source code or else can get it if you want it, that you can change these
134
functions or use pieces of them in new free programs, and that you know
135
you can do these things.@refill
137
To make sure that everyone has such rights, we have to forbid you to
138
deprive anyone else of these rights. For example, if you distribute
139
copies @code{gdbm}, you must give the recipients all
140
the rights that you have. You must make sure that they, too, receive or
141
can get the source code. And you must tell them their rights.@refill
143
Also, for our own protection, we must make certain that everyone finds
144
out that there is no warranty for anything in the @code{gdbm} distribution.
145
If these functions are modified by someone else and passed on, we want
146
their recipients to know that what they have is not what we distributed,
147
so that any problems introduced by others will not reflect on our
150
@code{gdbm} is currently distributed under the terms of the GNU General
151
Public License, Version 2. (@emph{NOT} under the GNU General Library
152
Public License.) A copy the GNU General Public License is included with
153
the distribution of @code{gdbm}.
155
@node Intro, List, Copying, Top
156
@chapter Introduction to GNU @code{dbm}.
158
GNU @code{dbm} (@code{gdbm})is a library of database functions that use
159
extendible hashing and works similar to the standard UNIX @code{dbm}
160
functions. These routines are provided to a programmer needing to
161
create and manipulate a hashed database. (@code{gdbm} is @emph{NOT} a
162
complete database package for an end user.)
164
The basic use of @code{gdbm} is to store key/data pairs in a data file.
165
Each key must be unique and each key is paired with only one data item.
166
The keys can not be directly accessed in sorted order. The basic unit
167
of data in @code{gdbm} is the structure:
176
This structure allows for arbitrary sized keys and data items.
178
The key/data pairs are stored in a @code{gdbm} disk file, called a
179
@code{gdbm} database. An application must open a @code{gdbm} database
180
to be able manipulate the keys and data contained in the database.
181
@code{gdbm} allows an application to have multiple databases open at the
182
same time. When an application opens a @code{gdbm} database, it is
183
designated as a @code{reader} or a @code{writer}. A @code{gdbm}
184
database opened by at most one writer at a time. However, many readers
185
may open the database open simultaneously. Readers and writers can not
186
open the @code{gdbm} database at the same time.
188
@node List, Open, Intro, Top
189
@chapter List of functions.
191
The following is a quick list of the functions contained in the @code{gdbm}
192
library. The include file @code{gdbm.h}, that can be included by the user,
193
contains a definition of these functions.
198
GDBM_FILE gdbm_open(name, block_size, flags, mode, fatal_func);
199
void gdbm_close(dbf);
200
int gdbm_store(dbf, key, content, flag);
201
datum gdbm_fetch(dbf, key);
202
int gdbm_delete(dbf, key);
203
datum gdbm_firstkey(dbf);
204
datum gdbm_nextkey(dbf, key);
205
int gdbm_reorganize(dbf);
207
int gdbm_exists(dbf, key);
208
char *gdbm_strerror(errno);
209
int gdbm_setopt(dbf, option, value, size)
212
The @code{gdbm.h} include file is often in the @file{/usr/local/gnu/include}
213
directory. (The actual location of @code{gdbm.h} depends on your local
214
installation of @code{gdbm}.)
216
@node Open, Close, List, Top
217
@chapter Opening the database.
219
Initialize @code{gdbm} system. If the file has a size of zero bytes, a file
220
initialization procedure is performed, setting up the initial structure in the
223
The procedure for opening a @code{gdbm} file is:
228
dbf = gdbm_open(name, block_size, flags, mode, fatal_func);
235
The name of the file (the complete name, @code{gdbm} does not append any
236
characters to this name).
238
It is used during initialization to determine the size of various constructs. It
239
is the size of a single transfer from disk to memory. This parameter is ignored
240
if the file has been previously initialized. The minimum size is 512.
241
If the value is less than 512, the file system blocksize is used, otherwise the
242
value of @code{block_size} is used.
244
If @code{flags} is set to GDBM_READER, the user wants to just read the
245
database and any call to @code{gdbm_store} or @code{gdbm_delete} will fail.
246
Many readers can access the database at the same time. If @code{flags} is
247
set to GDBM_WRITER, the user wants both read and write access to the database
248
and requires exclusive access. If @code{flags} is set to GDBM_WRCREAT, the
249
user wants both read and write access to the database and if the database does
250
not exist, create a new one. If @code{flags} is set to GDBM_NEWDB, the
251
user want a new database created, regardless of whether one existed, and wants
252
read and write access to the new database. For all writers (GDBM_WRITER,
253
GDBM_WRCREAT and GDBM_NEWDB) the value GDBM_FAST can be added to the
254
@code{flags} field using logical or. This option causes @code{gdbm}
255
to write the database without any disk file synchronization. This allows
256
faster writes, but may produce an inconsistent database in the event of
257
abnormal termination of the writer.
258
Any error detected will cause a
259
return value of NULL and an appropriate value will be in @code{gdbm_errno} (see
260
Variables). If no errors occur, a pointer to the @code{gdbm} file descriptor
263
File mode (see chmod(2) and open(2) if the file is created).
264
@item void (*fatal_func) ()
265
A function for @code{gdbm} to call if it detects a fatal error. The only
266
parameter of this function is a string. If the value of NULL is provided,
267
@code{gdbm} will use a default function.
270
The return value, @code{dbf}, is the pointer needed by all other functions to
271
access that @code{gdbm} file. If the return is the NULL pointer,
272
@code{gdbm_open} was not successful. The errors can be found in
273
@code{gdbm_errno} for @code{gdbm} errors and in @code{errno} for file system
274
errors (for error codes, see @code{gdbm.h}).
276
In all of the following calls, the parameter @code{dbf} refers to the pointer
277
returned from @code{gdbm_open}.
279
@node Close, Store, Open, Top
280
@chapter Closing the database.
282
It is important that every file opened is also closed. This is needed to
283
update the reader/writer count on the file. This is done by:
293
The pointer returned by @code{gdbm_open}.
296
Closes the @code{gdbm} file and frees all memory associated with the file
299
@node Store, Fetch, Close, Top
300
@chapter Inserting and replacing records in the database.
302
The function @code{gdbm_store} inserts or replaces records in the database.
305
ret = gdbm_store(dbf, key, content, flag);
312
The pointer returned by @code{gdbm_open}.
316
The data to be associated with the key.
318
Defines the action to take when the key is already in the database. The value
319
GDBM_REPLACE (defined in @code{gdbm.h}) asks that the old data be replaced by
320
the new @code{content}. The value GDBM_INSERT asks that an error be returned
321
and no action taken if the @code{key} already exists.
324
The values returned in @code{ret} are:
328
The item was not stored in the database because the caller was not an
329
official writer or either @code{key} or @code{content} have a NULL dptr field.
330
Both @code{key} and @code{content} must have the dptr field be a non-NULL value.
331
Since a NULL dptr field is used by other functions to indicate an error, a NULL
332
field cannot be valid data.
334
The item was not stored because the argument @code{flag} was GDBM_INSERT and
335
the @code{key} was already in the database.
337
No error. @code{content} is keyed by @code{key}. The file on disk is updated
338
to reflect the structure of the new database before returning from this
342
If you store data for a @code{key} that is already in the data base,
343
@code{gdbm} replaces the old data with the new data if called with
344
GDBM_REPLACE. You do not get two data items for the same @code{key} and you do
345
not get an error from @code{gdbm_store}.
347
The size in @code{gdbm} is not restricted like @code{dbm} or @code{ndbm}. Your
348
data can be as large as you want.
350
@node Fetch, Delete, Store,Top
351
@chapter Searching for records in the database.
353
Looks up a given @code{key} and returns the information associated with that
354
key. The pointer in the structure that is returned is a pointer to dynamically
355
allocated memory block. To search for some data:
358
content = gdbm_fetch(dbf, key);
365
The pointer returned by @code{gdbm_open}.
370
The datum returned in @code{content} is a pointer to the data found. If the
371
dptr is NULL, no data was found. If dptr is not NULL, then it points
372
to data allocated by malloc. @code{gdbm} does not automatically free this data.
373
The user must free this storage when done using it. This eliminates the
374
need to copy the result to save it for later use (you just save the pointer).
376
You may also search for a particular key without retrieving it, using:
379
ret = gdbm_exists(dbf, key);
386
The pointer returned by @code{gdbm_open}.
391
Unlike @code{gdbm_fetch}, this routine does not allocate any memory, and
392
simply returns true or false, depending on whether the @code{key} exists,
395
@node Delete, Sequential, Fetch, Top
396
@chapter Removing records from the database.
398
To remove some data from the database:
401
ret = gdbm_delete(dbf, key);
408
The pointer returned by @code{gdbm_open}.
413
The ret value is -1 if the item is not present or the requester is a reader.
414
The ret value is 0 if there was a successful delete.
416
@code{gdbm_delete} removes the keyed item and the @code{key} from the database
417
@code{dbf}. The file on disk is updated to reflect the structure of the new
418
database before returning from this function.
420
@node Sequential, Reorganization, Delete, Top
421
@chapter Sequential access to records.
423
The next two functions allow for accessing all items in the database. This
424
access is not @code{key} sequential, but it is guaranteed to visit every
425
@code{key} in the database once. The order has to do with the hash values.
426
@code{gdbm_firstkey} starts the visit of all keys in the database.
427
@code{gdbm_nextkey} finds and reads the next entry in the hash structure for
431
key = gdbm_firstkey(dbf);
433
nextkey = gdbm_nextkey(dbf, key);
440
The pointer returned by @code{gdbm_open}.
441
@item datum @code{key}
446
The return values are both datum. If @code{key}.dptr or nextkey.dptr is NULL,
447
there is no first @code{key} or next @code{key}. Again notice that dptr points to
448
data allocated by malloc and @code{gdbm} will not free it for you.
450
These functions were intended to visit the database in read-only algorithms,
451
for instance, to validate the database or similar operations.
453
File @code{visiting} is based on a @code{hash table}. @code{gdbm_delete}
454
re-arranges the hash table to make sure that any collisions in the table do not
455
leave some item @code{un-findable}. The original key order is NOT guaranteed to
456
remain unchanged in ALL instances. It is possible that some key will not be
457
visited if a loop like the following is executed:
461
key = gdbm_firstkey ( dbf );
462
while ( key.dptr ) @{
463
nextkey = gdbm_nextkey ( dbf, key );
464
if ( some condition ) @{
465
gdbm_delete ( dbf, key );
473
@node Reorganization, Sync, Sequential, Top
474
@chapter Database reorganization.
476
The following function should be used very seldom.
479
ret = gdbm_reorganize(dbf);
486
The pointer returned by @code{gdbm_open}.
489
If you have had a lot of deletions and would like to shrink the space
490
used by the @code{gdbm} file, this function will reorganize the database.
491
@code{gdbm} will not shorten the length of a @code{gdbm} file (deleted file space will be
492
reused) except by using this reorganization.
494
This reorganization requires creating a new file and inserting all the elements
495
in the old file @code{dbf} into the new file. The new file is then renamed to
496
the same name as the old file and @code{dbf} is updated to contain all the
497
correct information about the new file. If an error is detected, the return
498
value is negative. The value zero is returned after a successful
501
@node Sync, Errors, Reorganization, Top
502
@chapter Database Synchronization
504
If your database was opened with the GDBM_FAST flag, @code{gdbm} does not
505
wait for writes to the disk to complete before continuing. This allows
506
faster writing of databases at the risk of having a corrupted database if
507
the application terminates in an abnormal fashion. The following function
508
allows the programmer to make sure the disk version of the
509
database has been completely updated with all changes to the current time.
519
The pointer returned by @code{gdbm_open}.
522
This would usually be called after a complete set of changes have been
523
made to the database and before some long waiting time.
524
@code{gdbm_close} automatically calls the equivalent of @code{gdbm_sync}
525
so no call is needed if the database is to be closed immediately after
526
the set of changes have been made.
528
@node Errors, Options, Sync, Top
529
@chapter Error strings.
531
To convert a @code{gdbm} error code into English text, use this routine:
534
ret = gdbm_strerror(errno)
540
@item gdbm_error errno
541
The @code{gdbm} error code, usually @code{gdbm_errno}.
544
The appropiate phrase for reading by humans is returned.
546
@node Options, Variables, Errors, top
547
@chapter Seting options.
549
@code{Gdbm} now supports the ability to set certain options on an already
553
ret = gdbm_setopt(dbf, option, value, size)
560
The pointer returned by @code{gdbm_open}.
562
The option to be set.
564
A pointer to the value to which @code{option} will be set.
566
The length of the data pointed to by @code{value}.
569
The valid options are currently:
571
GDBM_CACHESIZE - Set the size of the internal bucket cache. This
572
option may only be set once on each GDBM_FILE descriptor, and
573
is set automatically to 100 upon the first access to the database.
575
GDBM_FASTMODE - Set fast mode to either on or off. This allows
576
fast mode to be toggled on an already open and active database.
577
value (see below) should be set to either TRUE or FALSE.
579
The return value will be -1 upon failure, or 0 upon success. The global
580
variable @code{gdbm_errno} will be set upon failure.
582
For instance, to set a database to use a cache of 10, after opening it
583
with @code{gdbm_open}, but prior to accessing it in any way, the following
588
ret = gdbm_setopt(dbf, GDBM_CACHESIZE, &value, sizeof(int));
591
@node Variables, Compatibility, Options, Top
592
@chapter Two useful variables.
594
The following two variables are variables that may need to be used:
597
@item gdbm_error gdbm_errno
598
The variable that contains more information about @code{gdbm} errors
599
(@code{gdbm.h} has the definitions of the error values).
600
@item const char * gdbm_version
601
The string containing the version information.
604
@node Compatibility, Conversion, Variables, Top
605
@chapter Compatibility with standard @code{dbm} and @code{ndbm}.
607
GNU @code{dbm} files are not @code{sparse}. You can copy them with the UNIX
608
@code{cp} command and they will not expand in the copying process.
610
There is a compatibility mode for use with programs that already use UNIX
611
@code{dbm} and UNIX @code{ndbm}.
613
GNU @code{dbm} has compatibility functions for @code{dbm}. For @code{dbm}
614
compatibility functions, you need the include file @code{dbm.h}.
616
In this compatibility mode, no @code{gdbm} file pointer is required
617
by the user, and Only one file may be opened at a time. All users in
618
compatibility mode are assumed to be writers. If the @code{gdbm} file is a
619
read only, it will fail as a writer, but will also try to open it as a reader.
620
All returned pointers in datum structures point to data that @code{gdbm} WILL
621
free. They should be treated as static pointers (as standard UNIX @code{dbm}
622
does). The compatibility function names are the same as the UNIX @code{dbm}
623
function names. Their definitions follow:
627
int store(key, content);
635
Standard UNIX @code{dbm} and GNU @code{dbm} do not have the same data
636
format in the file. You cannot access a standard UNIX @code{dbm} file with GNU
637
@code{dbm}! If you want to use an old database with GNU @code{dbm}, you must
638
use the @code{conv2gdbm} program.
640
Also, GNU @code{dbm} has compatibility functions for @code{ndbm}. For
641
@code{ndbm} compatibility functions, you need the include file @code{ndbm.h}.
643
Again, just like @code{ndbm}, any returned datum can be assumed to be static
644
storage. You do not have to free that memory, the @code{ndbm} compatibility
645
functions will do it for you.
650
DBM *dbm_open(name, flags, mode);
651
void dbm_close(file);
652
datum dbm_fetch(file, key);
653
int dbm_store(file, key, @code{content}, flags);
654
int dbm_delete(file, key);
655
datum dbm_firstkey(file);
656
datum dbm_nextkey(file);
658
int dbm_clearerr(file);
659
int dbm_dirfno(file);
660
int dbm_pagfno(file);
661
int dbm_rdonly(file);
664
If you want to compile an old C program that used UNIX @code{dbm} or @code{ndbm}
665
and want to use @code{gdbm} files, execute the following @code{cc} command:
668
cc ... -L /usr/local/lib -lgdbm
671
@node Conversion, Bugs, Compatibility, Top
672
@chapter Converting @code{dbm} files to @code{gdbm} format.
674
The program @code{conv2gdbm} has been provided to help you convert from @code{dbm}
675
databases to @code{gdbm}. The usage is:
678
conv2gdbm [-q] [-b block_size] dbm_file [gdbm_file]
685
Causes @code{conv2gdbm} to work quietly.
687
Is the same as in @code{gdbm_open}.
689
Is the name of the @code{dbm} file without the @code{.pag} or @code{.dir}
692
Is the complete file name. If not included, the @code{gdbm} file name is the
693
same as the @code{dbm} file name without any extensions. That is
694
@code{conv2gdbm} @code{dbmfile} converts the files @code{dbmfile.pag} and
695
@code{dbmfile.dir} into a @code{gdbm} file called @code{dbmfile}.
699
@node Bugs, , Conversion, Top
700
@chapter Problems and bugs.
702
If you have problems with GNU @code{dbm} or think you've found a bug,
703
please report it. Before reporting a bug, make sure you've actually
704
found a real bug. Carefully reread the documentation and see if it
705
really says you can do what you're trying to do. If it's not clear
706
whether you should be able to do something or not, report that too; it's
707
a bug in the documentation!
709
Before reporting a bug or trying to fix it yourself, try to isolate it
710
to the smallest possible input file that reproduces the problem. Then
711
send us the input file and the exact results @code{gdbm} gave you. Also
712
say what you expected to occur; this will help us decide whether the
713
problem was really in the documentation.
715
Once you've got a precise problem, send e-mail to:
718
Internet: @file{bug-gnu-utils@@prep.ai.mit.edu}.
719
UUCP: @file{mit-eddie!prep.ai.mit.edu!bug-gnu-utils}.
722
Please include the version number of GNU @code{dbm} you are using. You can get
723
this information by printing the variable @code{gdbm_version} (see Variables).
725
Non-bug suggestions are always welcome as well. If you have questions
726
about things that are unclear in the documentation or are just obscure
727
features, please report them too.
729
You may contact the author by:
731
e-mail: phil@@cs.wwu.edu
732
us-mail: Philip A. Nelson
733
Computer Science Department
734
Western Washington University
738
You may contact the current maintainer by:
740
e-mail: downsj@@CSOS.ORST.EDU