Robust Reading and Writing

In some situations, system-level read and write functions transfer few bytes than the application requests. Such short counts do not indicate an error. They occur for a number of reasons: encountering EOF on reads, reading text lines from a terminal, reading and writing network sockets.

      An I/O package called RIO (Robust I/O) package that handles these short counts automatically. Applications can transfer data directly between memory and a file by calling the rio_readn and rio_writen functions.

      The rio_readn function transfers up to n bytes from the current file position of descriptor fd to memory location usrbuf. The rio_readn function can only return a short count if it encounters EOF.

ssize_t rio_readn(int fd, void *usrbuf, size_t n)
{
		size_t nleft = n;
		ssize_t nread;
		char *bufp = usrbuf;
		
		while (nleft > 0) {
		    if ((nread = read(fd, bufp, nleft)) < 0) {
		    		if (errno == EINTR)		// Interrupted by signal handle return
		    		    nread = 0;			// and call read() again
		    		else
		    		    return -1;			// errno set by read()
		    }
		    else if (nread == 0)			
		        break;						// EOF
		    nleft -= nread;
		    bufp += nread;
		}
		return (n - nleft);					// Return >= 0
}

      The rio_writen function transfers up to n bytes from memory location usrbuf to descriptor fd. The rio_writen function never returns a short count.

ssize_t rio_writen(int fd, void *usrbuf, size_t n)
{
		size_t nleft = n;
		ssize_t nwritten;
		char *bufp = usrbuf;
		
		while (nleft > 0) {
		    if ((nwritten = write(fd, bufp, nleft)) < 0) {
		    		if (errno == EINTR)		// Interrupted by signal handle return
		    		    nwritten = 0;		// and call write() again
		    		else
		    		    return -1;			// errno set by write()
		    }
		    nleft -= nwritten;
		    bufp += nwritten;
		}
		return n;						
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值