diff options
-rw-r--r-- | libc/string/Makefile | 6 | ||||
-rw-r--r-- | libc/string/wstring.c | 100 |
2 files changed, 97 insertions, 9 deletions
diff --git a/libc/string/Makefile b/libc/string/Makefile index f19782c81..e7136a1c3 100644 --- a/libc/string/Makefile +++ b/libc/string/Makefile @@ -31,14 +31,14 @@ MOBJW= basename.o bcopy.o bzero.o dirname.o ffs.o memccpy.o memchr.o memcmp.o \ strcmp.o strcpy.o strcspn.o strdup.o strlen.o strncasecmp.o strncat.o \ strncmp.o strncpy.o strndup.o strnlen.o strpbrk.o strrchr.o strsep.o \ strspn.o strstr.o strtok.o strtok_r.o \ - __xpg_basename.o # strcoll.o strerror.o + __xpg_basename.o strlcat.o strlcpy.o # strcoll.o strerror.o MOBJW2= wcscasecmp.o wcscat.o wcschrnul.o wcschr.o wcscmp.o wcscpy.o wcscspn.o \ wcsdup.o wcslen.o wcsncasecmp.o wcsncat.o wcsncmp.o wcsncpy.o \ wcsnlen.o wcspbrk.o wcsrchr.o wcsspn.o wcsstr.o wcstok.o wmemchr.o \ - wmemcmp.o wmemcpy.o wmemmove.o wmempcpy.o wmemset.o + wmemcmp.o wmemcpy.o wmemmove.o wmempcpy.o wmemset.o wcpcpy.o wcpncpy.o -# wcscoll wcsxfrm wcpcpy wcpncpy +# wcscoll wcsxfrm MSRC1=strsignal.c MOBJ1=strsignal.o psignal.o diff --git a/libc/string/wstring.c b/libc/string/wstring.c index 59300f164..c78dbce0f 100644 --- a/libc/string/wstring.c +++ b/libc/string/wstring.c @@ -954,9 +954,16 @@ void *memrchr(const void *s, int c, size_t n) #endif /**********************************************************************/ +#ifdef L_wcpcpy +#define L_stpcpy +#define Wstpcpy wcpcpy +#else +#define Wstpcpy stpcpy +#endif + #ifdef L_stpcpy -char *stpcpy(register char * __restrict s1, const char * __restrict s2) +Wchar *Wstpcpy(register Wchar * __restrict s1, const Wchar * __restrict s2) { #ifdef __BCC__ do { @@ -971,14 +978,21 @@ char *stpcpy(register char * __restrict s1, const char * __restrict s2) #endif /**********************************************************************/ +#ifdef L_wcpncpy +#define L_stpncpy +#define Wstpncpy wcpncpy +#else +#define Wstpncpy stpncpy +#endif + #ifdef L_stpncpy -char *stpncpy(register char * __restrict s1, - register const char * __restrict s2, - size_t n) +Wchar *Wstpncpy(register Wchar * __restrict s1, + register const Wchar * __restrict s2, + size_t n) { - char *s = s1; - const char *p = s2; + Wchar *s = s1; + const Wchar *p = s2; #ifdef __BCC__ while (n--) { @@ -1274,3 +1288,77 @@ char *dirname(char *path) #endif /**********************************************************************/ +#ifdef L_strlcat + +/* OpenBSD function: + * Append at most n-1-strlen(dst) chars from src to dst and nul-terminate dst. + * Returns strlen(src) + strlen({original} dst), so truncation occurred if the + * return val is >= n. + * Note: If dst doesn't contain a nul in the first n chars, strlen(dst) is + * taken as n. */ + +size_t strlcat(register char *__restrict dst, + register const char *__restrict src, + size_t n) +{ + size_t len; + char dummy[1]; + + len = 0; + + while (1) { + if (len >= n) { + dst = dummy; + break; + } + if (!*dst) { + break; + } + ++dst; + ++len; + } + + while ((*dst = *src) != 0) { + if (++len < n) { + ++dst; + } + ++src; + } + + return len; +} + +#endif +/**********************************************************************/ +#ifdef L_strlcpy + +/* OpenBSD function: + * Copy at most n-1 chars from src to dst and nul-terminate dst. + * Returns strlen(src), so truncation occurred if the return value is >= n. */ + +size_t strlcpy(register char *__restrict dst, + register const char *__restrict src, + size_t n) +{ + const char *src0 = src; + char dummy[1]; + + if (!n) { + dst = dummy; + } else { + --n; + } + + while ((*dst = *src) != 0) { + if (n) { + --n; + ++dst; + } + ++src; + } + + return src - src0; +} + +#endif +/**********************************************************************/ |