writedat.c

来自「用C语言编的邮件系统(毕业论文)」· C语言 代码 · 共 37 行

C
37
字号
#include	"mail.h"#include	<sys/uio.h>		/* 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");}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?