]> arthur.barton.de Git - ngircd-alex.git/blob - src/portab/strlcpy.c
Update copyright notices of recently changed files
[ngircd-alex.git] / src / portab / strlcpy.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
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
12 #include "portab.h"
13
14 /**
15  * @file
16  * strlcpy() and strlcat() replacement functions.
17  *
18  * See <http://www.openbsd.org/papers/strlcpy-paper.ps> for details.
19  *
20  * Code partially borrowed from compat.c of rsync, written by Andrew
21  * Tridgell (1998) and Martin Pool (2002):
22  * <http://cvs.samba.org/cgi-bin/cvsweb/rsync/lib/compat.c>
23  */
24
25 #include <string.h>
26 #include <sys/types.h>
27
28 #ifndef HAVE_STRLCAT
29
30 GLOBAL size_t
31 strlcat( char *dst, const char *src, size_t size )
32 {
33         /* Like strncat() but does not 0 fill the buffer and
34          * always null terminates. */
35
36         size_t len1 = strlen( dst );
37         size_t len2 = strlen( src );
38         size_t ret = len1 + len2;
39
40         if( size && ( len1 < size - 1 )) {
41                 if( len2 >= size - len1 )
42                         len2 = size - len1 - 1;
43                 memcpy( dst + len1, src, len2 );
44                 dst[len1 + len2] = 0;
45         }
46         return ret;
47 } /* strlcat */
48
49 #endif
50
51 #ifndef HAVE_STRLCPY
52
53 GLOBAL size_t
54 strlcpy( char *dst, const char *src, size_t size )
55 {
56         /* Like strncpy but does not 0 fill the buffer and
57          * always null terminates. */
58
59         size_t len = strlen( src );
60         size_t ret = len;
61
62         if( size > 0 ) {
63                 if( len >= size ) len = size - 1;
64                 memcpy( dst, src, len );
65                 dst[len] = 0;
66         }
67         return ret;
68 } /* strlcpy */
69
70 #endif
71
72 /* -eof- */