Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 18 Aug 1998 07:51:43 -0400 (EDT)
From:      Alex Boisvert <alex@gel.usherb.ca>
To:        animal <animal@skylink.net>
Cc:        freebsd-database@FreeBSD.ORG
Subject:   Re: .dir .pag .db
Message-ID:  <Pine.BSF.3.95q.980818074159.29144B-100000@teel.info-noire.com>
In-Reply-To: <Pine.BSF.3.95q.980814140621.18381E-100000@shell.skylink.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, 14 Aug 1998, animal wrote:
> 
> can you tell me what i am doing wrong....
> 
> I use this to create the database......
> 
> #include <stdio.h>
> #include <fcntl.h>
> #include <ndbm.h>
> 
> main() {
> 
> 	DBM *db;
> 
> 	db = dbm_open("/usr/local/radius/users",O_CREAT,0000664);

You should also supply the O_RDWR flag here since you intend to create the
initial file format.

Something like:
 	db = dbm_open("/usr/local/radius/users",O_CREAT|O_RDWR,0000664);

> 	
> 	dbm_close(db);
> }
> 
> 
> 
> and then i am trying to add data to it by..........
> 
> #include <stdio.h>
> #include <fcntl.h>
> #include <ndbm.h>
> 
> 
> main() {
> 
> 	DBM *db
> 	datum kename,whatto;
> 
> 	db = dbm_open("/usr/local/radius/users",O_WRONLY,0000664);

You can't pass the O_WRONLY flag here since you're not only "appending" to
the file (in the traditional sense of append).  The dbm file format
necessitate that header information be read and modified each time that
you add information to the file. 

The line should read:
 	db = dbm_open("/usr/local/radius/users",O_RDWR,0000664);

> 
> 	kename.dptr = "animal";
> 	kename.dsize = strlen(kename.dptr)+1;
> 
> 	whatto.dptr = "Data to be stored in database";
> 	whatto.dsize = strlen(whatto.dptr)+1;
> 
> 	if (dbm_store(db,kename,whatto,DBM_INSERT) < 0) {
> 		printf("Insert messed up");
> 	}
> 	
> 	dbm_close(db);
> }
> 
> 
> both progs compile correctly and the create one works fine gives me a file
> of
> 
> -rw-r--r-- 1 root wheel       0 Aug 14 14:24 users.db
> 

This should have alerted you since "db" files can't be zero in length
because they always contain a header (16K on my system). 

> but when i run the second one to insert a key and some data.....
> 
> i get
> 
> Segmentation fault (core dumped)

By the way, you should always check for "errno" and/or "dbm_error" after
any operations made on the database.  In this case, errno was being set to
"2" meaning "can't open file" because you didn't specify O_RDWR and after
you would have got "79" meaning "invalid file format" because there was
not header in the file. 

Good luck,
Alex Boisvert


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-database" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.3.95q.980818074159.29144B-100000>