www.pudn.com > DTMS.rar > FIND.C
#include "mail.h"
/* Find the specified record.
* Called by mail_delete(), mail_fetch(), and mail_store(). */
int
_mail_find(MAIL *mail, const char *key, int writelock)
{
off_t offset, nextoffset;
/* Calculate hash value for this key, then calculate byte
offset of correspondin g chain ptr in hash table.
This is where our search starts. */
/* calc offset in hash table for this key */
mail->chainoff = (_mail_hash(mail, key) * PTR_SZ) + mail->hashoff;
mail->ptroff = mail->chainoff;
/* Here's where we lock this hash chain. It's the
caller's responsibility to unlock it when done.
Note we lock and unlock only the first byte. */
if (writelock) {
if (writew_lock(mail->idxfd, mail->chainoff, SEEK_SET, 1) < 0)
err_dump("writew_lock error");
} else {
if (readw_lock(mail->idxfd, mail->chainoff, SEEK_SET, 1) < 0)
err_dump("readw_lock error");
}
/* Get the offset in the index file of first record
on the hash chain (can be 0) */
offset = _mail_readptr(mail, mail->ptroff);
while (offset != 0) {
nextoffset = _mail_readidx(mail, offset,REALIDX);
if (strcmp(mail->keybuf, key) == 0)
break; /* found a match */
mail->ptroff = offset; /* offset of this (unequal) record */
offset = nextoffset; /* next one to compare */
}
if (offset == 0)
return(-1); /* error, record not found */
/* We have a match. We're guaranteed that mail->ptroff contains
the offset of the chain ptr that points to this matching
index record. _mail_dodelete() uses this fact. (The chain
ptr that points to this matching record could be in an
index record or in the hash table.) */
return(0);
}