2002-12-26 14:18:46 +01:00
|
|
|
/*
|
|
|
|
* ngIRCd -- The Next Generation IRC Daemon
|
2005-02-27 10:29:13 +01:00
|
|
|
* Copyright (c)2001-2005 Alexander Barton (alex@barton.de)
|
2002-12-26 14:18:46 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
* Please read the file COPYING, README and AUTHORS for more information.
|
2010-12-27 17:14:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portab.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2002-12-26 15:34:11 +01:00
|
|
|
* strlcpy() and strlcat() replacement functions.
|
2010-12-27 17:14:14 +01:00
|
|
|
*
|
2002-12-26 15:34:11 +01:00
|
|
|
* See <http://www.openbsd.org/papers/strlcpy-paper.ps> for details.
|
|
|
|
*
|
|
|
|
* Code partially borrowed from compat.c of rsync, written by Andrew
|
|
|
|
* Tridgell (1998) and Martin Pool (2002):
|
2005-02-27 10:29:13 +01:00
|
|
|
* <http://cvs.samba.org/cgi-bin/cvsweb/rsync/lib/compat.c>
|
2002-12-26 14:18:46 +01:00
|
|
|
*/
|
|
|
|
|
2002-12-26 15:34:11 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
2002-12-26 14:18:46 +01:00
|
|
|
|
2002-12-26 15:34:11 +01:00
|
|
|
#ifndef HAVE_STRLCAT
|
|
|
|
|
|
|
|
GLOBAL size_t
|
2005-03-19 19:43:48 +01:00
|
|
|
strlcat( char *dst, const char *src, size_t size )
|
2002-12-26 15:34:11 +01:00
|
|
|
{
|
|
|
|
/* Like strncat() but does not 0 fill the buffer and
|
|
|
|
* always null terminates. */
|
|
|
|
|
|
|
|
size_t len1 = strlen( dst );
|
|
|
|
size_t len2 = strlen( src );
|
|
|
|
size_t ret = len1 + len2;
|
2005-02-27 10:29:13 +01:00
|
|
|
|
|
|
|
if( size && ( len1 < size - 1 )) {
|
|
|
|
if( len2 >= size - len1 )
|
|
|
|
len2 = size - len1 - 1;
|
2002-12-26 15:34:11 +01:00
|
|
|
memcpy( dst + len1, src, len2 );
|
|
|
|
dst[len1 + len2] = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
} /* strlcat */
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_STRLCPY
|
|
|
|
|
|
|
|
GLOBAL size_t
|
2005-03-19 19:43:48 +01:00
|
|
|
strlcpy( char *dst, const char *src, size_t size )
|
2002-12-26 15:34:11 +01:00
|
|
|
{
|
|
|
|
/* Like strncpy but does not 0 fill the buffer and
|
|
|
|
* always null terminates. */
|
|
|
|
|
|
|
|
size_t len = strlen( src );
|
2005-01-18 10:05:37 +01:00
|
|
|
size_t ret = len;
|
2002-12-26 15:34:11 +01:00
|
|
|
|
2005-02-27 10:29:13 +01:00
|
|
|
if( size > 0 ) {
|
|
|
|
if( len >= size ) len = size - 1;
|
|
|
|
memcpy( dst, src, len );
|
|
|
|
dst[len] = 0;
|
|
|
|
}
|
2005-01-18 10:05:37 +01:00
|
|
|
return ret;
|
2002-12-26 15:34:11 +01:00
|
|
|
} /* strlcpy */
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2002-12-26 14:18:46 +01:00
|
|
|
/* -eof- */
|