/****************************************************************************/
/*	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 */
/** @{ */

/**
*	Concatenates a string with an other in a fixed size buffer.
*
*
*	@param	dst		Pointer to the destination buffer
*	@param	src		Pointer to the string to append to \p dst
*	@param	len		Size of the buffer (including dst!)
*	@return			The length of the final string or \p len if
*					the result would overflow the buffer.
*	@attention
*		This is not a standard C library function. This function was suggested
*		first by Todd C. Miller and Theo de Raadt and it is included in OpenBSD.
*/

size_t	strlcat( char *dst, const char *src, size_t len )
{
size_t	d;

	if ( ! len ) return( 0 );

	d = strnlen( dst, len );

	if ( d == len ) {

		// We can't fit anything in the buffer, just terminate the string.

		dst[ len - 1 ] = 0;
	}
	else {

		// The buffer is not yet full, append the new string.
		// Using strlcpy guarantees the termination and returns
		// the proper length.

		d += strlcpy( dst + d, src, len - d );
	}

	return( d );
}

/** @} */

