]> arthur.barton.de Git - ngircd-alex.git/blob - src/portab/strlcpy.c
ee93d1d6fcfba52a31b550d4e633edfd63ca3e42
[ngircd-alex.git] / src / portab / strlcpy.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2005 Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  *
11  * strlcpy() and strlcat() replacement functions.
12  * See <http://www.openbsd.org/papers/strlcpy-paper.ps> for details.
13  *
14  * Code partially borrowed from compat.c of rsync, written by Andrew
15  * Tridgell (1998) and Martin Pool (2002):
16  * <http://cvs.samba.org/cgi-bin/cvsweb/rsync/lib/compat.c>
17  */
18
19
20 #include "portab.h"
21
22 static char UNUSED id[] = "$Id: strlcpy.c,v 1.5 2005/03/19 18:43:50 fw Exp $";
23
24 #include "imp.h"
25 #include <string.h>
26 #include <sys/types.h>
27
28 #include "exp.h"
29
30
31 #ifndef HAVE_STRLCAT
32
33 GLOBAL size_t
34 strlcat( char *dst, const char *src, size_t size )
35 {
36         /* Like strncat() but does not 0 fill the buffer and
37          * always null terminates. */
38
39         size_t len1 = strlen( dst );
40         size_t len2 = strlen( src );
41         size_t ret = len1 + len2;
42
43         if( size && ( len1 < size - 1 )) {
44                 if( len2 >= size - len1 )
45                         len2 = size - len1 - 1;
46                 memcpy( dst + len1, src, len2 );
47                 dst[len1 + len2] = 0;
48         }
49         return ret;
50 } /* strlcat */
51
52 #endif
53
54
55 #ifndef HAVE_STRLCPY
56
57 GLOBAL size_t
58 strlcpy( char *dst, const char *src, size_t size )
59 {
60         /* Like strncpy but does not 0 fill the buffer and
61          * always null terminates. */
62
63         size_t len = strlen( src );
64         size_t ret = len;
65
66         if( size > 0 ) {
67                 if( len >= size ) len = size - 1;
68                 memcpy( dst, src, len );
69                 dst[len] = 0;
70         }
71         return ret;
72 } /* strlcpy */
73
74 #endif
75
76
77 /* -eof- */