From owner-freebsd-database Tue Aug 18 04:49:37 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id EAA09187 for freebsd-database-outgoing; Tue, 18 Aug 1998 04:49:37 -0700 (PDT) (envelope-from owner-freebsd-database@FreeBSD.ORG) Received: from teel.info-noire.com (XP11-1-4-01.interlinx.qc.ca [207.253.79.81]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id EAA09176 for ; Tue, 18 Aug 1998 04:49:34 -0700 (PDT) (envelope-from alex@gel.usherb.ca) Received: from localhost (alex@localhost) by teel.info-noire.com (8.8.8/8.8.8) with SMTP id HAA29798; Tue, 18 Aug 1998 07:51:43 -0400 (EDT) (envelope-from alex@teel.info-noire.com) Date: Tue, 18 Aug 1998 07:51:43 -0400 (EDT) From: Alex Boisvert Reply-To: boia01@gel.usherb.ca To: animal cc: freebsd-database@FreeBSD.ORG Subject: Re: .dir .pag .db In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-database@FreeBSD.ORG Precedence: bulk On Fri, 14 Aug 1998, animal wrote: > > can you tell me what i am doing wrong.... > > I use this to create the database...... > > #include > #include > #include > > 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 > #include > #include > > > 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