xref: /csrg-svn/lib/libc/db/hash/ndbm.c (revision 55313)
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*55313Sbostic static char sccsid[] = "@(#)ndbm.c	5.12 (Berkeley) 07/17/92";
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>
2151048Sbostic #define __DBINTERFACE_PRIVATE
2246371Sbostic #include <ndbm.h>
2346562Sbostic #include <stdio.h>
2450997Sbostic #include <string.h>
2546371Sbostic #include "hash.h"
2646371Sbostic 
2746371Sbostic /*
2850997Sbostic  * Returns:
2950997Sbostic  * 	*DBM on success
3050997Sbostic  *	 NULL on failure
3150997Sbostic  */
3246371Sbostic extern DBM *
3350997Sbostic dbm_open(file, flags, mode)
3450997Sbostic 	const char *file;
3550997Sbostic 	int flags, mode;
3646371Sbostic {
3750997Sbostic 	HASHINFO info;
3850997Sbostic 	char path[MAXPATHLEN];
3946371Sbostic 
4050997Sbostic 	info.bsize = 1024;
4150997Sbostic 	info.ffactor = 5;
4250997Sbostic 	info.nelem = 1;
4350997Sbostic 	info.cachesize = NULL;
4450997Sbostic 	info.hash = NULL;
4550997Sbostic 	info.lorder = 0;
4650997Sbostic 	(void)strcpy(path, file);
4750997Sbostic 	(void)strcat(path, DBM_SUFFIX);
4850997Sbostic 	return ((DBM *)__hash_open(path, flags, mode, &info));
4946371Sbostic }
5046371Sbostic 
5150997Sbostic extern void
5246371Sbostic dbm_close(db)
5350997Sbostic 	DBM *db;
5446371Sbostic {
5550997Sbostic 	(void)(db->close)(db);
5646371Sbostic }
5746371Sbostic 
5846371Sbostic /*
5950997Sbostic  * Returns:
6050997Sbostic  *	DATUM on success
6150997Sbostic  *	NULL on failure
6250997Sbostic  */
6350997Sbostic extern datum
6450997Sbostic dbm_fetch(db, key)
6550997Sbostic 	DBM *db;
6650997Sbostic 	datum key;
6746371Sbostic {
6850997Sbostic 	datum retval;
6950997Sbostic 	int status;
7046371Sbostic 
7150997Sbostic 	status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
7250997Sbostic 	if (status) {
7350997Sbostic 		retval.dptr = NULL;
7450997Sbostic 		retval.dsize = 0;
7550997Sbostic 	}
7650997Sbostic 	return (retval);
7746371Sbostic }
7846371Sbostic 
7946371Sbostic /*
8050997Sbostic  * Returns:
8150997Sbostic  *	DATUM on success
8250997Sbostic  *	NULL on failure
8350997Sbostic  */
8450997Sbostic extern datum
8546371Sbostic dbm_firstkey(db)
8650997Sbostic 	DBM *db;
8746371Sbostic {
8850997Sbostic 	int status;
8950997Sbostic 	datum retdata, retkey;
9046371Sbostic 
9150997Sbostic 	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
9250997Sbostic 	if (status)
9350997Sbostic 		retkey.dptr = NULL;
9450997Sbostic 	return (retkey);
9546371Sbostic }
9650997Sbostic 
9746371Sbostic /*
9850997Sbostic  * Returns:
9950997Sbostic  *	DATUM on success
10050997Sbostic  *	NULL on failure
10150997Sbostic  */
10250997Sbostic extern datum
10346371Sbostic dbm_nextkey(db)
10450997Sbostic 	DBM *db;
10546371Sbostic {
10650997Sbostic 	int status;
10750997Sbostic 	datum retdata, retkey;
10846371Sbostic 
10950997Sbostic 	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
11050997Sbostic 	if (status)
11150997Sbostic 		retkey.dptr = NULL;
11250997Sbostic 	return (retkey);
11346371Sbostic }
11446371Sbostic /*
11550997Sbostic  * Returns:
11650997Sbostic  *	 0 on success
11750997Sbostic  *	<0 failure
11850997Sbostic  */
11950997Sbostic extern int
12046371Sbostic dbm_delete(db, key)
12150997Sbostic 	DBM *db;
12250997Sbostic 	datum key;
12346371Sbostic {
12450997Sbostic 	int status;
12546371Sbostic 
12650997Sbostic 	status = (db->del)(db, (DBT *)&key, 0);
12750997Sbostic 	if (status)
12850997Sbostic 		return (-1);
12950997Sbostic 	else
13050997Sbostic 		return (0);
13146371Sbostic }
13246371Sbostic 
13346371Sbostic /*
13450997Sbostic  * Returns:
13550997Sbostic  *	 0 on success
13650997Sbostic  *	<0 failure
13750997Sbostic  *	 1 if DBM_INSERT and entry exists
13850997Sbostic  */
13950997Sbostic extern int
14046371Sbostic dbm_store(db, key, content, flags)
14150997Sbostic 	DBM *db;
14250997Sbostic 	datum key, content;
14350997Sbostic 	int flags;
14446371Sbostic {
14550997Sbostic 	return ((db->put)(db, (DBT *)&key, (DBT *)&content,
14650997Sbostic 	    (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
14746371Sbostic }
14846371Sbostic 
14946371Sbostic extern int
15046371Sbostic dbm_error(db)
15150997Sbostic 	DBM *db;
15246371Sbostic {
15350997Sbostic 	HTAB *hp;
15446371Sbostic 
15550997Sbostic 	hp = (HTAB *)db->internal;
15650997Sbostic 	return (hp->errno);
15746371Sbostic }
15846371Sbostic 
15946371Sbostic extern int
16046371Sbostic dbm_clearerr(db)
16150997Sbostic 	DBM *db;
16246371Sbostic {
16350997Sbostic 	HTAB *hp;
16446371Sbostic 
16550997Sbostic 	hp = (HTAB *)db->internal;
16650997Sbostic 	hp->errno = 0;
16750997Sbostic 	return (0);
16846371Sbostic }
16951054Sbostic 
170*55313Sbostic extern int
17151054Sbostic dbm_dirfno(db)
17251054Sbostic 	DBM *db;
17351054Sbostic {
17451054Sbostic 	return(((HTAB *)db->internal)->fp);
17551054Sbostic }
176