add dbm_dirfno, dbm_pagfno functions
[unix-history] / usr / src / lib / libc / db / hash / ndbm.c
/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Margo Seltzer.
*
* %sccs.include.redist.c%
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)ndbm.c 5.10 (Berkeley) %G%";
#endif /* LIBC_SCCS and not lint */
/*
* This package provides a dbm compatible interface to the new hashing
* package described in db(3).
*/
#include <sys/param.h>
#define __DBINTERFACE_PRIVATE
#include <ndbm.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include "hash.h"
/*
* Returns:
* *DBM on success
* NULL on failure
*/
extern DBM *
dbm_open(file, flags, mode)
const char *file;
int flags, mode;
{
HASHINFO info;
char path[MAXPATHLEN];
info.bsize = 1024;
info.ffactor = 5;
info.nelem = 1;
info.cachesize = NULL;
info.hash = NULL;
info.lorder = 0;
(void)strcpy(path, file);
(void)strcat(path, DBM_SUFFIX);
return ((DBM *)__hash_open(path, flags, mode, &info));
}
extern void
dbm_close(db)
DBM *db;
{
(void)(db->close)(db);
}
/*
* Returns:
* DATUM on success
* NULL on failure
*/
extern datum
dbm_fetch(db, key)
DBM *db;
datum key;
{
datum retval;
int status;
status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
if (status) {
retval.dptr = NULL;
retval.dsize = 0;
}
return (retval);
}
/*
* Returns:
* DATUM on success
* NULL on failure
*/
extern datum
dbm_firstkey(db)
DBM *db;
{
int status;
datum retdata, retkey;
status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
if (status)
retkey.dptr = NULL;
return (retkey);
}
/*
* Returns:
* DATUM on success
* NULL on failure
*/
extern datum
dbm_nextkey(db)
DBM *db;
{
int status;
datum retdata, retkey;
status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
if (status)
retkey.dptr = NULL;
return (retkey);
}
/*
* Returns:
* 0 on success
* <0 failure
*/
extern int
dbm_delete(db, key)
DBM *db;
datum key;
{
int status;
status = (db->del)(db, (DBT *)&key, 0);
if (status)
return (-1);
else
return (0);
}
/*
* Returns:
* 0 on success
* <0 failure
* 1 if DBM_INSERT and entry exists
*/
extern int
dbm_store(db, key, content, flags)
DBM *db;
datum key, content;
int flags;
{
return ((db->put)(db, (DBT *)&key, (DBT *)&content,
(flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
}
extern int
dbm_error(db)
DBM *db;
{
HTAB *hp;
hp = (HTAB *)db->internal;
return (hp->errno);
}
extern int
dbm_clearerr(db)
DBM *db;
{
HTAB *hp;
hp = (HTAB *)db->internal;
hp->errno = 0;
return (0);
}
dbm_dirfno(db)
DBM *db;
{
return(((HTAB *)db->internal)->fp);
}
dbm_pagfno(db)
DBM *db;
{
#define PAGFNOERR "ndbm: hash: dbm_pagfno not available\n"
(void)write(STDERR_FILENO, PAGFNOERR, sizeof(PAGFNOERR) - 1);
abort();
}