xref: /csrg-svn/lib/libc/db/hash/ndbm.c (revision 46562)
146371Sbostic /*-
246371Sbostic  * Copyright (c) 1990 The Regents of the University of California.
346371Sbostic  * All rights reserved.
446371Sbostic  *
546371Sbostic  * This code is derived from software contributed to Berkeley by
646371Sbostic  * Margo Seltzer.
746371Sbostic  *
846371Sbostic  * %sccs.include.redist.c%
946371Sbostic  */
1046371Sbostic 
1146371Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*46562Sbostic static char sccsid[] = "@(#)ndbm.c	5.4 (Berkeley) 02/22/91";
1346371Sbostic #endif /* LIBC_SCCS and not lint */
1446371Sbostic 
1546371Sbostic /*
1646371Sbostic     This package provides a dbm compatible interface to the new hashing
1746371Sbostic     package described in db(3)
1846371Sbostic */
1946371Sbostic 
2046371Sbostic #include <sys/param.h>
2146371Sbostic #include <ndbm.h>
22*46562Sbostic #include <db.h>
23*46562Sbostic #include <stdio.h>
2446371Sbostic #include "hash.h"
2546371Sbostic 
2646371Sbostic /*
2746371Sbostic     return 	*DBM on success
2846371Sbostic 		NULL on failure
2946371Sbostic */
3046371Sbostic extern DBM *
3146371Sbostic dbm_open( file, flags, mode )
3246534Sbostic const char 	*file;
3346371Sbostic int	flags;
3446371Sbostic int	mode;
3546371Sbostic {
3646371Sbostic     HASHINFO	info;
3746405Sbostic     char path[MAXPATHLEN];
3846371Sbostic 
3946371Sbostic     info.bsize = 1024;
4046371Sbostic     info.ffactor = 5;
4146371Sbostic     info.nelem = 1;
4246371Sbostic     info.ncached = NULL;
4346371Sbostic     info.hash = NULL;
4446371Sbostic     info.lorder = 0;
4546405Sbostic     (void)sprintf(path, "%s%s", file, DBM_SUFFIX);
4646405Sbostic     return( hash_open ( path, flags, mode, &info ) );
4746371Sbostic }
4846371Sbostic 
4946371Sbostic extern void
5046371Sbostic dbm_close(db)
5146371Sbostic DBM	*db;
5246371Sbostic {
5346371Sbostic     (void)(db->close) (db);
5446371Sbostic }
5546371Sbostic 
5646371Sbostic /*
5746371Sbostic     Returns 	DATUM on success
5846371Sbostic 		NULL on failure
5946371Sbostic */
6046371Sbostic extern datum
6146371Sbostic dbm_fetch( db, key )
6246371Sbostic DBM 	*db;
6346371Sbostic datum	key;
6446371Sbostic {
6546371Sbostic     int	status;
6646371Sbostic     datum	retval;
6746371Sbostic 
6846371Sbostic     status = (db->get) ( db, (DBT *)&key, (DBT *)&retval );
6946371Sbostic     if ( status ) {
7046371Sbostic 	retval.dptr = NULL;
7146371Sbostic     }
7246371Sbostic     return(retval);
7346371Sbostic }
7446371Sbostic 
7546371Sbostic /*
7646371Sbostic     Returns 	DATUM on success
7746371Sbostic 		NULL on failure
7846371Sbostic */
7946371Sbostic extern datum
8046371Sbostic dbm_firstkey(db)
8146371Sbostic DBM 	*db;
8246371Sbostic {
8346371Sbostic     int	status;
8446371Sbostic     datum	retkey;
8546371Sbostic     datum	retdata;
8646371Sbostic 
8746371Sbostic     status = (db->seq) ( db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST );
8846371Sbostic     if ( status ) {
8946371Sbostic 	retkey.dptr = NULL;
9046371Sbostic     }
9146371Sbostic     return(retkey);
9246371Sbostic }
9346371Sbostic /*
9446371Sbostic     Returns 	DATUM on success
9546371Sbostic 		NULL on failure
9646371Sbostic */
9746371Sbostic extern datum
9846371Sbostic dbm_nextkey(db)
9946371Sbostic DBM 	*db;
10046371Sbostic {
10146371Sbostic     int	status;
10246371Sbostic     datum	retkey;
10346371Sbostic     datum	retdata;
10446371Sbostic 
10546371Sbostic     status = (db->seq) ( db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT );
10646371Sbostic     if ( status ) {
10746371Sbostic 	retkey.dptr = NULL;
10846371Sbostic     }
10946371Sbostic     return(retkey);
11046371Sbostic }
11146371Sbostic 
11246371Sbostic /*
11346371Sbostic     0 on success
11446371Sbostic     <0 failure
11546371Sbostic */
11646371Sbostic extern int
11746371Sbostic dbm_delete(db, key)
11846371Sbostic DBM 	*db;
11946371Sbostic datum	key;
12046371Sbostic {
12146371Sbostic     int	status;
12246371Sbostic 
12346534Sbostic     status = (db->del)( db, (DBT *)&key );
12446371Sbostic     if ( status ) {
12546371Sbostic 	return(-1);
12646371Sbostic     } else {
12746371Sbostic 	return(0);
12846371Sbostic     }
12946371Sbostic }
13046371Sbostic 
13146371Sbostic /*
13246371Sbostic     0 on success
13346371Sbostic     <0 failure
13446371Sbostic     1 if DBM_INSERT and entry exists
13546371Sbostic */
13646371Sbostic extern int
13746371Sbostic dbm_store(db, key, content, flags)
13846371Sbostic DBM 	*db;
13946371Sbostic datum	key;
14046371Sbostic datum	content;
14146371Sbostic int	flags;
14246371Sbostic {
14346371Sbostic     return ((db->put)( db, (DBT *)&key, (DBT *)&content,
14446371Sbostic 			(flags == DBM_INSERT) ? R_NOOVERWRITE : 0 ));
14546371Sbostic }
14646371Sbostic 
14746371Sbostic extern int
14846371Sbostic dbm_error(db)
14946371Sbostic DBM	*db;
15046371Sbostic {
15146371Sbostic     HTAB	*hp;
15246371Sbostic 
15346371Sbostic     hp = (HTAB *)db->internal;
15446371Sbostic     return ( hp->errno );
15546371Sbostic }
15646371Sbostic 
15746371Sbostic extern int
15846371Sbostic dbm_clearerr(db)
15946371Sbostic DBM	*db;
16046371Sbostic {
16146371Sbostic     HTAB	*hp;
16246371Sbostic 
16346371Sbostic     hp = (HTAB *)db->internal;
16446371Sbostic     hp->errno = 0;
16546371Sbostic     return ( 0 );
16646371Sbostic }
167