summaryrefslogtreecommitdiffstats
path: root/usr/klibc/strlcpy.c
blob: 51c72d877cb4094a1d05292a91c17070b1ca9db1 (plain)
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
/*
 * strlcpy.c
 */

#include <string.h>
#include <klibc/compiler.h>

size_t strlcpy(char *dst, const char *src, size_t size)
{
	size_t bytes = 0;
	char *q = dst;
	const char *p = src;
	char ch;

	while ((ch = *p++)) {
		if (bytes + 1 < size)
			*q++ = ch;

		bytes++;
	}

	/* If size == 0 there is no space for a final null... */
	if (size)
		*q = '\0';

	return bytes;
}