libroot: Add stpncpy.

Imported from musl.

It is in POSIX.1-2017.
This commit is contained in:
Augustin Cavalier
2023-11-28 20:37:15 -05:00
parent 5a66a92969
commit 15a3f4c540
3 changed files with 35 additions and 0 deletions
+1
View File
@@ -62,6 +62,7 @@ extern char *strcasestr(const char *string, const char *searchString);
extern char *strdup(const char *string); extern char *strdup(const char *string);
extern char *strndup(const char* string, size_t size); extern char *strndup(const char* string, size_t size);
extern char *stpcpy(char *dest, const char *source); extern char *stpcpy(char *dest, const char *source);
extern char *stpncpy(char *dest, const char *source, size_t size);
extern size_t strlcat(char *dest, const char *source, size_t length); extern size_t strlcat(char *dest, const char *source, size_t length);
extern size_t strlcpy(char *dest, const char *source, size_t length); extern size_t strlcpy(char *dest, const char *source, size_t length);
@@ -10,6 +10,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
MergeObject <$(architecture)>posix_musl_string.o : MergeObject <$(architecture)>posix_musl_string.o :
memrchr.c memrchr.c
stpncpy.c
strchrnul.c strchrnul.c
strcspn.c strcspn.c
swab.c swab.c
@@ -0,0 +1,33 @@
#include <features.h>
#include <string.h>
#include <stdint.h>
#include <limits.h>
#define ALIGN (sizeof(size_t)-1)
#define ONES ((size_t)-1/UCHAR_MAX)
#define HIGHS (ONES * (UCHAR_MAX/2+1))
#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
char *__stpncpy(char *restrict d, const char *restrict s, size_t n)
{
#if defined(__GNUC__) && __GNUC__ > 3
typedef size_t __attribute__((__may_alias__)) word;
word *wd;
const word *ws;
if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) {
for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++);
if (!n || !*s) goto tail;
wd=(void *)d; ws=(const void *)s;
for (; n>=sizeof(size_t) && !HASZERO(*ws);
n-=sizeof(size_t), ws++, wd++) *wd = *ws;
d=(void *)wd; s=(const void *)ws;
}
#endif
for (; n && (*d=*s); n--, s++, d++);
tail:
memset(d, 0, n);
return d;
}
weak_alias(__stpncpy, stpncpy);