www.pudn.com > DTMS.rar > WRITEDAT.C


#include	"mail.h"
#include			/* struct iovec */

/* Write a data record.  Called by _mail_dodelete() (to write
   the record with blanks) and mail_store(). */

void
_mail_writedat(MAIL *mail, const char *data, off_t offset, int whence)
{
	struct iovec	iov[2];
	static char		newline = '\n';

		/* If we're appending, we have to lock before doing the lseek()
		   and write() to make the two an atomic operation.  If we're
		   overwriting an existing record, we don't have to lock. */
	if (whence == SEEK_END)		/* we're appending, lock entire file */
		if (writew_lock(mail->datfd, 0, SEEK_SET, 0) < 0)
			err_dump("writew_lock error");

	if ( (mail->datoff = lseek(mail->datfd, offset, whence)) == -1)
		err_dump("lseek error");
        if(whence==SEEK_END)
	   mail->datlen = strlen(data) + 1;	/* datlen includes newline */
         /* else datalength=mail->datlen*/ 

	iov[0].iov_base = (char *) data;
	iov[0].iov_len  = mail->datlen - 1;
	iov[1].iov_base = &newline;
	iov[1].iov_len  = 1;
	if (writev(mail->datfd, &iov[0], 2) != mail->datlen)
		err_dump("writev error of data record");

	if (whence == SEEK_END)
		if (un_lock(mail->datfd, 0, SEEK_SET, 0) < 0)
			err_dump("un_lock error");
}