scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 7aa86bd1b38fb9ddd568ed8c13f534a274f50fbb
parent 7dcb0d9d9eee6306f01cf4788b8eef1fa9baa9c7
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date:   Tue, 25 Mar 2025 20:34:38 +0100

libc/wchar: Add wcsncpy()

Diffstat:
Msrc/libc/objs/common-objs.mk | 1+
Asrc/libc/wchar/wcsncpy.c | 18++++++++++++++++++
Mtests/libc/execute/.gitignore | 1+
Atests/libc/execute/0054-wcsncpy.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
Mtests/libc/execute/libc-tests.lst | 1+
5 files changed, 69 insertions(+), 0 deletions(-)

diff --git a/src/libc/objs/common-objs.mk b/src/libc/objs/common-objs.mk @@ -135,6 +135,7 @@ COMMON_OBJS =\ wchar/wcwidth.$O\ wchar/wmemchr.$O\ wchar/wmemcpy.$O\ + wchar/wcsncpy.$O\ wchar/wmemmove.$O\ wchar/wmemset.$O\ wchar/wmemcmp.$O\ diff --git a/src/libc/wchar/wcsncpy.c b/src/libc/wchar/wcsncpy.c @@ -0,0 +1,18 @@ +#include <wchar.h> + +#undef wcsncpy + +wchar_t * +wcsncpy(wchar_t *restrict s1, const wchar_t *restrict s2, size_t n) +{ + wchar_t *ret = s1; + + for (; n > 0 && *s2; --n) + *s1++ = *s2++; + + while (n-- > 0) + *s1++ = '\0'; + + return ret; + +} diff --git a/tests/libc/execute/.gitignore b/tests/libc/execute/.gitignore @@ -50,4 +50,5 @@ 0052-wmemset 0051-wmemmove 0053-wmemcmp +0054-wcsncpy test.log diff --git a/tests/libc/execute/0054-wcsncpy.c b/tests/libc/execute/0054-wcsncpy.c @@ -0,0 +1,48 @@ +#include <assert.h> +#include <stdio.h> +#include <wchar.h> + +#define SIZ 6 + +/* +output: +testing +test1 +test2 +test3 +done +end: +*/ + +int +main() +{ + wchar_t *s, buf[SIZ]; + wchar_t abc[] = {'a', 'b', 'c'}; + + puts("testing"); + + puts("test1"); + wmemset(buf, '0', SIZ); + s = wcsncpy(buf, abc, SIZ); + assert(s == buf); + assert(!wmemcmp(s, abc, SIZ)); + + puts("test2"); + wmemset(buf, '0', SIZ); + s = wcsncpy(buf, (wchar_t[]) {0}, SIZ); + assert(s == buf); + assert(!wmemcmp(s, (wchar_t[SIZ]) {0}, SIZ)); + + puts("test3"); + wmemset(buf, '0', SIZ); + s = wcsncpy(buf, (wchar_t[]) {0}, 1); + assert(s == buf); + assert(!wmemcmp(s, + (wchar_t[SIZ]) {'\0', '0', '0', '0', '0', '0'}, + SIZ)); + + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/libc-tests.lst b/tests/libc/execute/libc-tests.lst @@ -49,3 +49,4 @@ 0051-wmemmove 0052-wmemset 0053-wmemcmp +0054-wcsncpy