www.pudn.com > DTMS.rar > FINDFREE.C
#include "mail.h"
/* Try to find a free index record and accompanying data record
* of the correct sizes. We're only called by mail_store(). */
int
_mail_findfree(MAIL *mail, int keylen, int datlen)
{
int rc;
off_t offset, nextoffset, saveoffset;
/* Lock the free list */
if (writew_lock(mail->idxfd, FREE_OFF, SEEK_SET, 1) < 0)
err_dump("writew_lock error");
/* Read the free list pointer */
saveoffset = FREE_OFF;
offset = _mail_readptr(mail, saveoffset);
while (offset != 0) {
nextoffset = _mail_readidx(mail, offset,FREEIDX);
if (strlen(mail->idxbuf) == keylen && mail->datlen == datlen)
break; /* found a match */
saveoffset = offset;
offset = nextoffset;
}
if (offset == 0)
rc = -1; /* no match found */
else {
/* Found a free record with matching sizes.
The index record was read in by _mail_readidx() above,
which sets mail->ptrval. Also, saveoffset points to
the chain ptr that pointed to this empty record on
the free list. We set this chain ptr to mail->ptrval,
which removes the empty record from the free list. */
_mail_writeptr(mail, saveoffset, mail->ptrval);
rc = 0;
/* Notice also that _mail_readidx() set both mail->idxoff
and mail->datoff. This is used by the caller, mail_store(),
to write the new index record and data record. */
}
/* Unlock the free list */
if (un_lock(mail->idxfd, FREE_OFF, SEEK_SET, 1) < 0)
err_dump("un_lock error");
return(rc);
}