X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fportab%2Fstrlcpy.c;h=1f86a93304fcec3d2246374d3fe2d7815fca638e;hb=3f807e104572b38143a1015be57d875088ceaebb;hp=627b3218839319c588a7068c162643422538e939;hpb=6c5f4beb53d5e37fefe017cf28c513c44cfda1af;p=ngircd-alex.git diff --git a/src/portab/strlcpy.c b/src/portab/strlcpy.c index 627b3218..1f86a933 100644 --- a/src/portab/strlcpy.c +++ b/src/portab/strlcpy.c @@ -1,37 +1,34 @@ /* * ngIRCd -- The Next Generation IRC Daemon - * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de) + * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors. * * 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. - * + */ + +#include "portab.h" + +/** + * @file * strlcpy() and strlcat() replacement functions. + * * See for details. * * Code partially borrowed from compat.c of rsync, written by Andrew * Tridgell (1998) and Martin Pool (2002): - * + * */ - -#include "portab.h" - -static char UNUSED id[] = "$Id: strlcpy.c,v 1.3 2005/01/18 09:05:37 alex Exp $"; - -#include "imp.h" #include #include -#include "exp.h" - - #ifndef HAVE_STRLCAT GLOBAL size_t -strlcat( CHAR *dst, CONST CHAR *src, size_t size ) +strlcat( char *dst, const char *src, size_t size ) { /* Like strncat() but does not 0 fill the buffer and * always null terminates. */ @@ -39,10 +36,10 @@ strlcat( CHAR *dst, CONST CHAR *src, size_t size ) size_t len1 = strlen( dst ); size_t len2 = strlen( src ); size_t ret = len1 + len2; - - if( len1 + len2 >= size ) len2 = size - ( len1 + 1 ); - if( len2 > 0 ) - { + + if( size && ( len1 < size - 1 )) { + if( len2 >= size - len1 ) + len2 = size - len1 - 1; memcpy( dst + len1, src, len2 ); dst[len1 + len2] = 0; } @@ -51,11 +48,10 @@ strlcat( CHAR *dst, CONST CHAR *src, size_t size ) #endif - #ifndef HAVE_STRLCPY GLOBAL size_t -strlcpy( CHAR *dst, CONST CHAR *src, size_t size ) +strlcpy( char *dst, const char *src, size_t size ) { /* Like strncpy but does not 0 fill the buffer and * always null terminates. */ @@ -63,14 +59,14 @@ strlcpy( CHAR *dst, CONST CHAR *src, size_t size ) size_t len = strlen( src ); size_t ret = len; - if( size <= 0 ) return 0; - if( len >= size ) len = size - 1; - memcpy( dst, src, len ); - dst[len] = 0; + if( size > 0 ) { + if( len >= size ) len = size - 1; + memcpy( dst, src, len ); + dst[len] = 0; + } return ret; } /* strlcpy */ #endif - /* -eof- */