FreeBSD/sys/kern/subr_msgbuf.c
/*
 * Peek at the full contents of a message buffer without marking any
 * data as read. `seqp' should point to an unsigned integer that
 * msgbuf_peekbytes() can use to retain state between calls so that
 * the whole message buffer can be read in multiple short reads.
 * To initialise this variable to the start of the message buffer,
 * call msgbuf_peekbytes() with a NULL `buf' parameter.
 *
 * Returns the number of characters that were placed in `buf'.
 */
Peek at the full contents of a message buffer without marking any
data as read. `seqp' should point to an unsigned integer that
msgbuf_peekbytes() can use to retain state between calls so that
the whole message buffer can be read in multiple short reads.
To initialise this variable to the start of the message buffer,
call msgbuf_peekbytes() with a NULL `buf' parameter.

Returns the number of characters that were placed in `buf'.
Peek at the full contents of a message buffer without marking any data as read . ` seqp ' should point to an unsigned integer that msgbuf_peekbytes () can use to retain state between calls so that the whole message buffer can be read in multiple short reads . To initialise this variable to the start of the message buffer call msgbuf_peekbytes () with a NULL ` buf ' parameter . Returns the number of characters that were placed in ` buf ' .
int
msgbuf_peekbytes(struct msgbuf *mbp, char *buf, int buflen, u_int *seqp)
{
	u_int len, pos, wseq;
	mtx_lock_spin(&mbp->msg_lock);
	if (buf == NULL) {
		*seqp = MSGBUF_SEQNORM(mbp, mbp->msg_wseq - mbp->msg_size);
		mtx_unlock_spin(&mbp->msg_lock);
		return (0);
	}
	wseq = mbp->msg_wseq;
	len = MSGBUF_SEQSUB(mbp, wseq, *seqp);
	if (len == 0) {
		mtx_unlock_spin(&mbp->msg_lock);
		return (0);
	}
	if (len > mbp->msg_size) {
		*seqp = MSGBUF_SEQNORM(mbp, wseq - mbp->msg_size);
		len = mbp->msg_size;
	}
	pos = MSGBUF_SEQ_TO_POS(mbp, *seqp);
	len = min(len, mbp->msg_size - pos);
	len = min(len, (u_int)buflen);
	bcopy(&mbp->msg_ptr[MSGBUF_SEQ_TO_POS(mbp, *seqp)], buf, len);
	*seqp = MSGBUF_SEQNORM(mbp, *seqp + len);
	mtx_unlock_spin(&mbp->msg_lock);
	return (len);
}
int msgbuf_peekbytes ( struct msgbuf * mbp , char * buf , int buflen , u_int * seqp ) { u_int len , pos , wseq ; mtx_lock_spin ( & mbp -> msg_lock ) ; if ( buf == NULL ) { * seqp = MSGBUF_SEQNORM ( mbp , mbp -> msg_wseq - mbp -> msg_size ) ; mtx_unlock_spin ( & mbp -> msg_lock ) ; return ( 0 ) ; } wseq = mbp -> msg_wseq ; len = MSGBUF_SEQSUB ( mbp , wseq , * seqp ) ; if ( len == 0 ) { mtx_unlock_spin ( & mbp -> msg_lock ) ; return ( 0 ) ; } if ( len > mbp -> msg_size ) { * seqp = MSGBUF_SEQNORM ( mbp , wseq - mbp -> msg_size ) ; len = mbp -> msg_size ; } pos = MSGBUF_SEQ_TO_POS ( mbp , * seqp ) ; len = min ( len , mbp -> msg_size - pos ) ; len = min ( len , ( u_int ) buflen ) ; bcopy ( & mbp -> msg_ptr [ MSGBUF_SEQ_TO_POS ( mbp , * seqp ) ] , buf , len ) ; * seqp = MSGBUF_SEQNORM ( mbp , * seqp + len ) ; mtx_unlock_spin ( & mbp -> msg_lock ) ; return ( len ) ; }
Peek at the full contents of a message buffer without marking any
 data as read.
Peek at the full contents of a message buffer without marking any data as read .
/* Just initialise *seqp. */