blob: 43d0e58bcc0546351d7ed263b87b18503fceb6a2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include <stddef.h>
#include <string.h>
// Copy string src to buffer dst of size dsize. At most dsize-1
// chars will be copied. Always NUL terminates (unless dsize == 0).
// Returns strlen(src); if retval >= dsize, truncation occurred.
size_t strlcpy(char *dst, const char *src, size_t dsize) {
size_t n = dsize;
const char *osrc = src;
for (; n; n--) {
if ((*dst++ = *src++) == '\0')
break;
}
if (n == 0) {
if (dsize != 0)
*dst = '\0'; /* NUL-terminate dst */
while (*src++)
;
}
return src - osrc - 1;
}
|