]> pd.if.org Git - pdclib/blob - functions/string/strlcpy.c
6739f85854135ede3a4d91cc911d45a9ee85294e
[pdclib] / functions / string / strlcpy.c
1 /* strlcpy( char *, const char *, size_t )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <string.h>
8
9 #ifndef REGTEST
10
11 #pragma weak strlcpy = _PDCLIB_strlcpy
12 size_t _PDCLIB_strlcpy(
13     char *restrict dst,
14     const char *restrict src,
15     size_t dstsize);
16
17 size_t _PDCLIB_strlcpy(
18     char *restrict dst,
19     const char *restrict src,
20     size_t dstsize)
21 {
22     size_t needed = 0;
23     while(needed < dstsize && (dst[needed] = src[needed]))
24         needed++;
25
26     while(src[needed++]);
27
28     if (needed > dstsize && dstsize)
29       dst[dstsize - 1] = 0;
30
31     return needed;
32 }
33
34 #endif
35
36 #ifdef TEST
37 #include <_PDCLIB_test.h>
38
39 int main( void )
40 {
41 #ifndef REGTEST
42     char destbuf[10];
43 #endif
44     TESTCASE_NOREG( strlcpy(NULL, "a", 0) == 2 );
45     TESTCASE_NOREG( strlcpy(destbuf, "a", 10) == 2 );
46     TESTCASE_NOREG( strcmp(destbuf, "a") == 0 );
47     TESTCASE_NOREG( strlcpy(destbuf, "helloworld", 10) == 11 );
48     TESTCASE_NOREG( strcmp(destbuf, "helloworl") == 0 );
49     return TEST_RESULTS;
50 }
51
52 #endif