2 60cf07c8 2002-12-26 alex * ngIRCd -- The Next Generation IRC Daemon
3 3da942e2 2005-02-27 alex * Copyright (c)2001-2005 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 3da942e2 2005-02-27 alex * <http://cvs.samba.org/cgi-bin/cvsweb/rsync/lib/compat.c>
20 60cf07c8 2002-12-26 alex #include "portab.h"
22 8adff592 2005-03-19 fw static char UNUSED id[] = "$Id: strlcpy.c,v 1.5 2005/03/19 18:43:50 fw 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 8adff592 2005-03-19 fw 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 3da942e2 2005-02-27 alex if( size && ( len1 < size - 1 )) {
44 3da942e2 2005-02-27 alex if( len2 >= size - len1 )
45 3da942e2 2005-02-27 alex len2 = size - len1 - 1;
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 8adff592 2005-03-19 fw 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 3da942e2 2005-02-27 alex if( size > 0 ) {
67 3da942e2 2005-02-27 alex if( len >= size ) len = size - 1;
68 3da942e2 2005-02-27 alex memcpy( dst, src, len );
69 3da942e2 2005-02-27 alex dst[len] = 0;
72 fbdf85b5 2002-12-26 alex } /* strlcpy */