xref: /csrg-svn/lib/libc/db/hash/ndbm.c (revision 64481)
146371Sbostic /*-
261202Sbostic  * Copyright (c) 1990, 1993
361202Sbostic  *	The Regents of the University of California.  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*64481Sbostic static char sccsid[] = "@(#)ndbm.c	8.2 (Berkeley) 09/11/93";
1346371Sbostic #endif /* LIBC_SCCS and not lint */
1446371Sbostic 
1546371Sbostic /*
1650997Sbostic  * This package provides a dbm compatible interface to the new hashing
1750997Sbostic  * package described in db(3).
1850997Sbostic  */
1946371Sbostic 
2046371Sbostic #include <sys/param.h>
2157932Sbostic 
2246371Sbostic #include <ndbm.h>
2346562Sbostic #include <stdio.h>
2450997Sbostic #include <string.h>
2557932Sbostic 
2646371Sbostic #include "hash.h"
2746371Sbostic 
2846371Sbostic /*
2950997Sbostic  * Returns:
3050997Sbostic  * 	*DBM on success
3150997Sbostic  *	 NULL on failure
3250997Sbostic  */
3346371Sbostic extern DBM *
3450997Sbostic dbm_open(file, flags, mode)
3550997Sbostic 	const char *file;
3650997Sbostic 	int flags, mode;
3746371Sbostic {
3850997Sbostic 	HASHINFO info;
3950997Sbostic 	char path[MAXPATHLEN];
4046371Sbostic 
4160245Sbostic 	info.bsize = 4096;
4260245Sbostic 	info.ffactor = 40;
4350997Sbostic 	info.nelem = 1;
4450997Sbostic 	info.cachesize = NULL;
4550997Sbostic 	info.hash = NULL;
4650997Sbostic 	info.lorder = 0;
4750997Sbostic 	(void)strcpy(path, file);
4850997Sbostic 	(void)strcat(path, DBM_SUFFIX);
49*64481Sbostic 	return ((DBM *)__hash_open(path, flags, mode, &info, 0));
5046371Sbostic }
5146371Sbostic 
5250997Sbostic extern void
5346371Sbostic dbm_close(db)
5450997Sbostic 	DBM *db;
5546371Sbostic {
5650997Sbostic 	(void)(db->close)(db);
5746371Sbostic }
5846371Sbostic 
5946371Sbostic /*
6050997Sbostic  * Returns:
6150997Sbostic  *	DATUM on success
6250997Sbostic  *	NULL on failure
6350997Sbostic  */
6450997Sbostic extern datum
6550997Sbostic dbm_fetch(db, key)
6650997Sbostic 	DBM *db;
6750997Sbostic 	datum key;
6846371Sbostic {
6950997Sbostic 	datum retval;
7050997Sbostic 	int status;
7146371Sbostic 
7250997Sbostic 	status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
7350997Sbostic 	if (status) {
7450997Sbostic 		retval.dptr = NULL;
7550997Sbostic 		retval.dsize = 0;
7650997Sbostic 	}
7750997Sbostic 	return (retval);
7846371Sbostic }
7946371Sbostic 
8046371Sbostic /*
8150997Sbostic  * Returns:
8250997Sbostic  *	DATUM on success
8350997Sbostic  *	NULL on failure
8450997Sbostic  */
8550997Sbostic extern datum
8646371Sbostic dbm_firstkey(db)
8750997Sbostic 	DBM *db;
8846371Sbostic {
8950997Sbostic 	int status;
9050997Sbostic 	datum retdata, retkey;
9146371Sbostic 
9250997Sbostic 	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
9350997Sbostic 	if (status)
9450997Sbostic 		retkey.dptr = NULL;
9550997Sbostic 	return (retkey);
9646371Sbostic }
9750997Sbostic 
9846371Sbostic /*
9950997Sbostic  * Returns:
10050997Sbostic  *	DATUM on success
10150997Sbostic  *	NULL on failure
10250997Sbostic  */
10350997Sbostic extern datum
10446371Sbostic dbm_nextkey(db)
10550997Sbostic 	DBM *db;
10646371Sbostic {
10750997Sbostic 	int status;
10850997Sbostic 	datum retdata, retkey;
10946371Sbostic 
11050997Sbostic 	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
11150997Sbostic 	if (status)
11250997Sbostic 		retkey.dptr = NULL;
11350997Sbostic 	return (retkey);
11446371Sbostic }
11546371Sbostic /*
11650997Sbostic  * Returns:
11750997Sbostic  *	 0 on success
11850997Sbostic  *	<0 failure
11950997Sbostic  */
12050997Sbostic extern int
12146371Sbostic dbm_delete(db, key)
12250997Sbostic 	DBM *db;
12350997Sbostic 	datum key;
12446371Sbostic {
12550997Sbostic 	int status;
12646371Sbostic 
12750997Sbostic 	status = (db->del)(db, (DBT *)&key, 0);
12850997Sbostic 	if (status)
12950997Sbostic 		return (-1);
13050997Sbostic 	else
13150997Sbostic 		return (0);
13246371Sbostic }
13346371Sbostic 
13446371Sbostic /*
13550997Sbostic  * Returns:
13650997Sbostic  *	 0 on success
13750997Sbostic  *	<0 failure
13850997Sbostic  *	 1 if DBM_INSERT and entry exists
13950997Sbostic  */
14050997Sbostic extern int
14146371Sbostic dbm_store(db, key, content, flags)
14250997Sbostic 	DBM *db;
14350997Sbostic 	datum key, content;
14450997Sbostic 	int flags;
14546371Sbostic {
14650997Sbostic 	return ((db->put)(db, (DBT *)&key, (DBT *)&content,
14750997Sbostic 	    (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
14846371Sbostic }
14946371Sbostic 
15046371Sbostic extern int
15146371Sbostic dbm_error(db)
15250997Sbostic 	DBM *db;
15346371Sbostic {
15450997Sbostic 	HTAB *hp;
15546371Sbostic 
15650997Sbostic 	hp = (HTAB *)db->internal;
15750997Sbostic 	return (hp->errno);
15846371Sbostic }
15946371Sbostic 
16046371Sbostic extern int
16146371Sbostic dbm_clearerr(db)
16250997Sbostic 	DBM *db;
16346371Sbostic {
16450997Sbostic 	HTAB *hp;
16546371Sbostic 
16650997Sbostic 	hp = (HTAB *)db->internal;
16750997Sbostic 	hp->errno = 0;
16850997Sbostic 	return (0);
16946371Sbostic }
17051054Sbostic 
17155313Sbostic extern int
17251054Sbostic dbm_dirfno(db)
17351054Sbostic 	DBM *db;
17451054Sbostic {
17551054Sbostic 	return(((HTAB *)db->internal)->fp);
17651054Sbostic }
177