/* port - a few routines needed for portability
**
** The strl routines are derived from the FreeBSD versions.
*/

#include <sys/types.h>
#include <string.h>
#include "port.h"

size_t my_strlcpy( char* dst, const char* src, size_t size )
    {
    char* d = dst;
    const char* s = src;
    size_t n = size;

    /* Copy as many bytes as will fit. */
    if ( n != 0 )
	{
	while ( --n != 0 )
	    {
	    if ( ( *d++ = *s++ ) == '\0' )
		break;
	    }
	}

    /* Not enough room in dst, add \0 and traverse rest of src. */
    if ( n == 0 )
	{
	if ( size != 0 )
	    *d = '\0';		/* terminate dst */
	while ( *s++ )
	    ;
	}

    return s - src - 1;		/* count does not include NUL */
    }


size_t my_strlcat( char* dst, const char* src, size_t size )
    {
    char* d = dst;
    const char* s = src;
    size_t n = size;
    size_t dlen;

    /* Find the end of dst and adjust bytes left but don't go past end. */
    while ( n-- != 0 && *d != '\0' )
	d++;
    dlen = d - dst;
    n = size - dlen;

    if ( n == 0 )
	return dlen + strlen( s );
    while ( *s != '\0' )
	{
	if ( n != 1 )
	    {
	    *d++ = *s;
	    n--;
	    }
	s++;
	}
    *d = '\0';			/* terminate dst */

    return dlen + ( s - src );	/* count does not include NUL */
    }
