xref: /csrg-svn/lib/libc/db/hash/ndbm.c (revision 46534)
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*46534Sbostic static char sccsid[] = "@(#)ndbm.c	5.3 (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>
2246371Sbostic #include <stddef.h>
2346371Sbostic #include "hash.h"
2446371Sbostic 
2546371Sbostic /*
2646371Sbostic     return 	*DBM on success
2746371Sbostic 		NULL on failure
2846371Sbostic */
2946371Sbostic extern DBM *
3046371Sbostic dbm_open( file, flags, mode )
31*46534Sbostic const char 	*file;
3246371Sbostic int	flags;
3346371Sbostic int	mode;
3446371Sbostic {
3546371Sbostic     HASHINFO	info;
3646405Sbostic     char path[MAXPATHLEN];
3746371Sbostic 
3846371Sbostic     info.bsize = 1024;
3946371Sbostic     info.ffactor = 5;
4046371Sbostic     info.nelem = 1;
4146371Sbostic     info.ncached = NULL;
4246371Sbostic     info.hash = NULL;
4346371Sbostic     info.lorder = 0;
4446405Sbostic     (void)sprintf(path, "%s%s", file, DBM_SUFFIX);
4546405Sbostic     return( hash_open ( path, flags, mode, &info ) );
4646371Sbostic }
4746371Sbostic 
4846371Sbostic extern void
4946371Sbostic dbm_close(db)
5046371Sbostic DBM	*db;
5146371Sbostic {
5246371Sbostic     (void)(db->close) (db);
5346371Sbostic }
5446371Sbostic 
5546371Sbostic /*
5646371Sbostic     Returns 	DATUM on success
5746371Sbostic 		NULL on failure
5846371Sbostic */
5946371Sbostic extern datum
6046371Sbostic dbm_fetch( db, key )
6146371Sbostic DBM 	*db;
6246371Sbostic datum	key;
6346371Sbostic {
6446371Sbostic     int	status;
6546371Sbostic     datum	retval;
6646371Sbostic 
6746371Sbostic     status = (db->get) ( db, (DBT *)&key, (DBT *)&retval );
6846371Sbostic     if ( status ) {
6946371Sbostic 	retval.dptr = NULL;
7046371Sbostic     }
7146371Sbostic     return(retval);
7246371Sbostic }
7346371Sbostic 
7446371Sbostic /*
7546371Sbostic     Returns 	DATUM on success
7646371Sbostic 		NULL on failure
7746371Sbostic */
7846371Sbostic extern datum
7946371Sbostic dbm_firstkey(db)
8046371Sbostic DBM 	*db;
8146371Sbostic {
8246371Sbostic     int	status;
8346371Sbostic     datum	retkey;
8446371Sbostic     datum	retdata;
8546371Sbostic 
8646371Sbostic     status = (db->seq) ( db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST );
8746371Sbostic     if ( status ) {
8846371Sbostic 	retkey.dptr = NULL;
8946371Sbostic     }
9046371Sbostic     return(retkey);
9146371Sbostic }
9246371Sbostic /*
9346371Sbostic     Returns 	DATUM on success
9446371Sbostic 		NULL on failure
9546371Sbostic */
9646371Sbostic extern datum
9746371Sbostic dbm_nextkey(db)
9846371Sbostic DBM 	*db;
9946371Sbostic {
10046371Sbostic     int	status;
10146371Sbostic     datum	retkey;
10246371Sbostic     datum	retdata;
10346371Sbostic 
10446371Sbostic     status = (db->seq) ( db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT );
10546371Sbostic     if ( status ) {
10646371Sbostic 	retkey.dptr = NULL;
10746371Sbostic     }
10846371Sbostic     return(retkey);
10946371Sbostic }
11046371Sbostic 
11146371Sbostic /*
11246371Sbostic     0 on success
11346371Sbostic     <0 failure
11446371Sbostic */
11546371Sbostic extern int
11646371Sbostic dbm_delete(db, key)
11746371Sbostic DBM 	*db;
11846371Sbostic datum	key;
11946371Sbostic {
12046371Sbostic     int	status;
12146371Sbostic 
122*46534Sbostic     status = (db->del)( db, (DBT *)&key );
12346371Sbostic     if ( status ) {
12446371Sbostic 	return(-1);
12546371Sbostic     } else {
12646371Sbostic 	return(0);
12746371Sbostic     }
12846371Sbostic }
12946371Sbostic 
13046371Sbostic /*
13146371Sbostic     0 on success
13246371Sbostic     <0 failure
13346371Sbostic     1 if DBM_INSERT and entry exists
13446371Sbostic */
13546371Sbostic extern int
13646371Sbostic dbm_store(db, key, content, flags)
13746371Sbostic DBM 	*db;
13846371Sbostic datum	key;
13946371Sbostic datum	content;
14046371Sbostic int	flags;
14146371Sbostic {
14246371Sbostic     return ((db->put)( db, (DBT *)&key, (DBT *)&content,
14346371Sbostic 			(flags == DBM_INSERT) ? R_NOOVERWRITE : 0 ));
14446371Sbostic }
14546371Sbostic 
14646371Sbostic extern int
14746371Sbostic dbm_error(db)
14846371Sbostic DBM	*db;
14946371Sbostic {
15046371Sbostic     HTAB	*hp;
15146371Sbostic 
15246371Sbostic     hp = (HTAB *)db->internal;
15346371Sbostic     return ( hp->errno );
15446371Sbostic }
15546371Sbostic 
15646371Sbostic extern int
15746371Sbostic dbm_clearerr(db)
15846371Sbostic DBM	*db;
15946371Sbostic {
16046371Sbostic     HTAB	*hp;
16146371Sbostic 
16246371Sbostic     hp = (HTAB *)db->internal;
16346371Sbostic     hp->errno = 0;
16446371Sbostic     return ( 0 );
16546371Sbostic }
166