]> arthur.barton.de Git - netatalk.git/blob - libatalk/compat/strlcpy.c
Active Directory LDAP queries for ACL support
[netatalk.git] / libatalk / compat / strlcpy.c
1 /*
2    Copy from samba lib/replace.c
3
4    Unix SMB/CIFS implementation.
5    replacement routines for broken systems
6    Copyright (C) Andrew Tridgell 1992-1998
7  
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12  
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17  
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22    strlcpy strlcat functions.
23 */
24                           
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <atalk/util.h>
30 #include <string.h>
31
32 #ifndef HAVE_STRLCPY
33 /* like strncpy but does not 0 fill the buffer and always null
34    terminates. bufsize is the size of the destination buffer */
35  size_t strlcpy(char *d, const char *s, size_t bufsize)
36 {
37         size_t len = strlen(s);
38         size_t ret = len;
39
40         if (bufsize <= 0) 
41                 return 0;
42
43         if (len >= bufsize) 
44                 len = bufsize-1;
45
46         memcpy(d, s, len);
47         d[len] = 0;
48         return ret;
49 }
50 #endif
51  
52 #ifndef HAVE_STRLCAT
53 /* like strncat but does not 0 fill the buffer and always null
54    terminates. bufsize is the length of the buffer, which should
55    be one more than the maximum resulting string length */
56  size_t strlcat(char *d, const char *s, size_t bufsize)
57 {
58         size_t len1 = strlen(d);
59         size_t len2 = strlen(s);
60         size_t ret = len1 + len2;
61
62         if (len1 >= bufsize) {
63                 return 0;
64         } 
65         if (len1+len2 >= bufsize) {
66                 len2 = bufsize - (len1+1);
67         }
68         if (len2 > 0) {
69                 memcpy(d+len1, s, len2);
70                 d[len1+len2] = 0;
71         }
72         return ret;
73 }
74 #endif