]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/portab/strlcpy.c
Add Doxygen @file documentation to each source and header file
[ngircd-alex.git] / src / portab / strlcpy.c
index f77527a09f068788d34a97c0ec53ad3c63c39f28..ae2018bcf940a3c47a1cc265740c58fbff70dce8 100644 (file)
@@ -1,26 +1,27 @@
 /*
  * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
+ * Copyright (c)2001-2005 Alexander Barton (alex@barton.de)
  *
  * 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 <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):
- * <http://samba.anu.edu.au/rsync/doxygen/head/lib_2compat_8c.html>
+ * <http://cvs.samba.org/cgi-bin/cvsweb/rsync/lib/compat.c>
  */
 
-
-#include "portab.h"
-
-static char UNUSED id[] = "$Id: strlcpy.c,v 1.2 2002/12/26 14:34:11 alex Exp $";
-
 #include "imp.h"
 #include <string.h>
 #include <sys/types.h>
@@ -31,7 +32,7 @@ static char UNUSED id[] = "$Id: strlcpy.c,v 1.2 2002/12/26 14:34:11 alex Exp $";
 #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 +40,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;
        }
@@ -55,18 +56,20 @@ strlcat( CHAR *dst, CONST CHAR *src, size_t size )
 #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. */
 
        size_t len = strlen( src );
+       size_t ret = len;
 
-       if( size <= 0 ) return len;
-       if( len >= size ) len = size - 1;
-       memcpy( dst, src, len );
-       dst[len] = 0;
-       return len;
+       if( size > 0 ) {
+               if( len >= size ) len = size - 1;
+               memcpy( dst, src, len );
+               dst[len] = 0;
+       }
+       return ret;
 } /* strlcpy */
 
 #endif