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
| | #include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "word.h"
/**
* strlcpy - Word sized bsd strlcpy.
* @d: Destination
* @s: Source
* @n: Max @s
*/
size_t strlcpy(char *d, const char *s, size_t n)
{
char *z = d;
size_t *wd;
const size_t *ws;
/* A byte for nul */
if (!n--) goto terminate;
if ((uintptr_t)d % sizeof(size_t) != (uintptr_t)s % sizeof(size_t))
goto misaligned;
for (; (uintptr_t)s % sizeof(size_t); *d++ = *s++, n--)
if (!*s || !n) goto terminate;
for (wd = (size_t *)d, ws= (const size_t *)s
; !word_has_zero(*ws) && n >= sizeof(size_t)
; *wd = *ws, wd++, ws++, n -= sizeof(size_t))
d = (char *)wd;
s = (const char *)ws;
misaligned:
for (; (*d = *s) && n; d++, s++, n--);
terminate:
*d = '\0';
return d - z + strlen(s);
}
|