-
Notifications
You must be signed in to change notification settings - Fork 48
/
strlcpy.c
45 lines (36 loc) · 1.41 KB
/
strlcpy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Copyright (c) 1996-2024, OPC Foundation. All rights reserved.
The source code in this file is covered under a dual-license scenario:
- RCL: for OPC Foundation members in good-standing
- GPL V2: everybody else
RCL license terms accompanied with this source code. See http://opcfoundation.org/License/RCL/1.00/
GNU General Public License as published by the Free Software Foundation;
version 2 of the License are accompanied with this source code. See http://opcfoundation.org/License/GPLv2
This source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <sys/types.h>
#include <string.h>
/** Size limited copy string function.
* This function guaranties that \c dst will be zero terminated,
* as long as \c dst is at least one byte long.
* Note that strlcpy works on true 'C' strings, which means
* both \c src and \c dst must be zero terminated.
* @param dst destination buffer
* @param src source buffer
* @param len length of destination
* @return total length of the created string in \c dst
*/
size_t strlcpy(char *dst, const char *src, size_t len)
{
size_t pos = 0;
if (len < 1) return 0; /* sanity check */
len--; /* reserve space for null terminator */
while (pos < len && *src)
{
dst[pos++] = *src;
src++;
}
dst[pos] = 0;
return pos;
}