/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss of reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#include <string.h>

/** @addtogroup string */
/** @{ */

/**
*	Copy a memory area with a sentinel.
*
*	The function copies the source memory area to the destination memory area.
*	The copying stops if either the given number of bytes were copied, or if
*	the last copied byte is identical to the character given. If the copy was
*	stopped because the given character was found (and copied), then a pointer
*	to the byte in the destination area just after the last copied byte is
*	returned. If the copy stopped because the given number of bytes were copied
*	and the given character was not found, then NULL is returned.
*
*	@param	d	Pointer to the destination area
*	@param	s	Pointer to the source area
*	@param	c	The character to look for
*	@param	n	The maximum number of bytes to copy
*
*	@return		NULL if \p c was not found while copying \p n bytes or a
*				pointer in \p d to the location just after the copied \p c
*				byte.
*/

void	*memccpy( void *d, const void *s, int c, size_t n )
{
unsigned char *dp = (unsigned char *) d;
unsigned char *sp = (unsigned char *) s;

	c &= 255;

	while ( n-- )

		if ( ( *dp++ = *sp++ ) == c )

			return( dp );

	return( NULL );
}

/** @} */

