2 60cf07c8 2002-12-26 alex * ngIRCd -- The Next Generation IRC Daemon
3 60cf07c8 2002-12-26 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 60cf07c8 2002-12-26 alex * This program is free software; you can redistribute it and/or modify
6 60cf07c8 2002-12-26 alex * it under the terms of the GNU General Public License as published by
7 60cf07c8 2002-12-26 alex * the Free Software Foundation; either version 2 of the License, or
8 60cf07c8 2002-12-26 alex * (at your option) any later version.
9 60cf07c8 2002-12-26 alex * Please read the file COPYING, README and AUTHORS for more information.
11 fbdf85b5 2002-12-26 alex * strlcpy() and strlcat() replacement functions.
12 fbdf85b5 2002-12-26 alex * See <http://www.openbsd.org/papers/strlcpy-paper.ps> for details.
14 fbdf85b5 2002-12-26 alex * Code partially borrowed from compat.c of rsync, written by Andrew
15 fbdf85b5 2002-12-26 alex * Tridgell (1998) and Martin Pool (2002):
16 fbdf85b5 2002-12-26 alex * <http://samba.anu.edu.au/rsync/doxygen/head/lib_2compat_8c.html>
20 60cf07c8 2002-12-26 alex #include "portab.h"
22 6c5f4beb 2005-01-18 alex static char UNUSED id[] = "$Id: strlcpy.c,v 1.3 2005/01/18 09:05:37 alex Exp $";
24 60cf07c8 2002-12-26 alex #include "imp.h"
25 fbdf85b5 2002-12-26 alex #include <string.h>
26 fbdf85b5 2002-12-26 alex #include <sys/types.h>
28 60cf07c8 2002-12-26 alex #include "exp.h"
31 fbdf85b5 2002-12-26 alex #ifndef HAVE_STRLCAT
33 fbdf85b5 2002-12-26 alex GLOBAL size_t
34 fbdf85b5 2002-12-26 alex strlcat( CHAR *dst, CONST CHAR *src, size_t size )
36 fbdf85b5 2002-12-26 alex /* Like strncat() but does not 0 fill the buffer and
37 fbdf85b5 2002-12-26 alex * always null terminates. */
39 fbdf85b5 2002-12-26 alex size_t len1 = strlen( dst );
40 fbdf85b5 2002-12-26 alex size_t len2 = strlen( src );
41 fbdf85b5 2002-12-26 alex size_t ret = len1 + len2;
43 fbdf85b5 2002-12-26 alex if( len1 + len2 >= size ) len2 = size - ( len1 + 1 );
44 fbdf85b5 2002-12-26 alex if( len2 > 0 )
46 fbdf85b5 2002-12-26 alex memcpy( dst + len1, src, len2 );
47 fbdf85b5 2002-12-26 alex dst[len1 + len2] = 0;
50 fbdf85b5 2002-12-26 alex } /* strlcat */
55 fbdf85b5 2002-12-26 alex #ifndef HAVE_STRLCPY
57 fbdf85b5 2002-12-26 alex GLOBAL size_t
58 fbdf85b5 2002-12-26 alex strlcpy( CHAR *dst, CONST CHAR *src, size_t size )
60 fbdf85b5 2002-12-26 alex /* Like strncpy but does not 0 fill the buffer and
61 fbdf85b5 2002-12-26 alex * always null terminates. */
63 fbdf85b5 2002-12-26 alex size_t len = strlen( src );
64 6c5f4beb 2005-01-18 alex size_t ret = len;
66 6c5f4beb 2005-01-18 alex if( size <= 0 ) return 0;
67 fbdf85b5 2002-12-26 alex if( len >= size ) len = size - 1;
68 fbdf85b5 2002-12-26 alex memcpy( dst, src, len );
69 fbdf85b5 2002-12-26 alex dst[len] = 0;
71 fbdf85b5 2002-12-26 alex } /* strlcpy */