Open a dbm database with the specified name, which can include a directory
path. Any file extensions needed will be supplied automatically by the dbm
library. For example, Berkeley DB appends ‘.db’, and GNU gdbm uses two
physical files with extensions ‘.dir’ and ‘.pag’.
The mode should be an integer, as for Unix chmod.
Flags should be one of READER, WRITER, WRCREAT or NEWDB.
static VALUE
fdbm_initialize(int argc, VALUE *argv, VALUE obj)
{
volatile VALUE file;
VALUE vmode, vflags;
DBM *dbm;
struct dbmdata *dbmp;
int mode, flags = 0;
if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
mode = 0666; /* default value */
}
else if (NIL_P(vmode)) {
mode = -1; /* return nil if DB not exist */
}
else {
mode = NUM2INT(vmode);
}
if (!NIL_P(vflags))
flags = NUM2INT(vflags);
FilePathValue(file);
if (flags & RUBY_DBM_RW_BIT) {
flags &= ~RUBY_DBM_RW_BIT;
dbm = dbm_open(RSTRING_PTR(file), flags, mode);
}
else {
dbm = 0;
if (mode >= 0) {
dbm = dbm_open(RSTRING_PTR(file), O_RDWR|O_CREAT, mode);
}
if (!dbm) {
dbm = dbm_open(RSTRING_PTR(file), O_RDWR, 0);
}
if (!dbm) {
dbm = dbm_open(RSTRING_PTR(file), O_RDONLY, 0);
}
}
if (!dbm) {
if (mode == -1) return Qnil;
rb_sys_fail(RSTRING_PTR(file));
}
dbmp = ALLOC(struct dbmdata);
DATA_PTR(obj) = dbmp;
dbmp->di_dbm = dbm;
dbmp->di_size = -1;
return obj;
}