FreeBSD/sys/kern/subr_msgbuf.c
/*
 * Read and mark as read a number of characters from a message buffer.
 * Returns the number of characters that were placed in `buf'.
 */
Read and mark as read a number of characters from a message buffer.
Returns the number of characters that were placed in `buf'.
Read and mark as read a number of characters from a message buffer . Returns the number of characters that were placed in ` buf ' .
int
msgbuf_getbytes(struct msgbuf *mbp, char *buf, int buflen)
{
	u_int len, pos, wseq;
	mtx_lock_spin(&mbp->msg_lock);
	wseq = mbp->msg_wseq;
	len = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_rseq);
	if (len == 0) {
		mtx_unlock_spin(&mbp->msg_lock);
		return (0);
	}
	if (len > mbp->msg_size) {
		mbp->msg_rseq = MSGBUF_SEQNORM(mbp, wseq - mbp->msg_size);
		len = mbp->msg_size;
	}
	pos = MSGBUF_SEQ_TO_POS(mbp, mbp->msg_rseq);
	len = min(len, mbp->msg_size - pos);
	len = min(len, (u_int)buflen);
	bcopy(&mbp->msg_ptr[pos], buf, len);
	mbp->msg_rseq = MSGBUF_SEQNORM(mbp, mbp->msg_rseq + len);
	mtx_unlock_spin(&mbp->msg_lock);
	return (len);
}
int msgbuf_getbytes ( struct msgbuf * mbp , char * buf , int buflen ) { u_int len , pos , wseq ; mtx_lock_spin ( & mbp -> msg_lock ) ; wseq = mbp -> msg_wseq ; len = MSGBUF_SEQSUB ( mbp , wseq , mbp -> msg_rseq ) ; if ( len == 0 ) { mtx_unlock_spin ( & mbp -> msg_lock ) ; return ( 0 ) ; } if ( len > mbp -> msg_size ) { mbp -> msg_rseq = MSGBUF_SEQNORM ( mbp , wseq - mbp -> msg_size ) ; len = mbp -> msg_size ; } pos = MSGBUF_SEQ_TO_POS ( mbp , mbp -> msg_rseq ) ; len = min ( len , mbp -> msg_size - pos ) ; len = min ( len , ( u_int ) buflen ) ; bcopy ( & mbp -> msg_ptr [ pos ] , buf , len ) ; mbp -> msg_rseq = MSGBUF_SEQNORM ( mbp , mbp -> msg_rseq + len ) ; mtx_unlock_spin ( & mbp -> msg_lock ) ; return ( len ) ; }
Read and mark as read a number of characters from a message buffer.
Read and mark as read a number of characters from a message buffer .