|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Minios-devel] [UNIKRAFT PATCH 2/3] lib/nolibc/string: import strlcpy and strlcat from musl
git tag v1.1.20
commit <0fa1e638e87cf257e9f96b4019b2076afd674a19>
Signed-off-by: Yuri Volchkov <yuri.volchkov@xxxxxxxxx>
---
lib/nolibc/exportsyms.uk | 2 ++
lib/nolibc/include/string.h | 2 ++
lib/nolibc/string.c | 32 ++++++++++++++++++++++++++++++++
3 files changed, 36 insertions(+)
diff --git a/lib/nolibc/exportsyms.uk b/lib/nolibc/exportsyms.uk
index 61d49c7..6dbfa72 100644
--- a/lib/nolibc/exportsyms.uk
+++ b/lib/nolibc/exportsyms.uk
@@ -64,6 +64,8 @@ strspn
strtok
strndup
strdup
+strlcpy
+strlcat
# time
nanosleep
diff --git a/lib/nolibc/include/string.h b/lib/nolibc/include/string.h
index b21e2c1..957efc3 100644
--- a/lib/nolibc/include/string.h
+++ b/lib/nolibc/include/string.h
@@ -52,6 +52,8 @@ void *memmove(void *dst, const void *src, size_t len);
char *strncpy(char *dst, const char *src, size_t len);
char *strcpy(char *dst, const char *src);
+size_t strlcpy(char *d, const char *s, size_t n);
+size_t strlcat(char *d, const char *s, size_t n);
size_t strnlen(const char *str, size_t maxlen);
size_t strlen(const char *str);
char *strchrnul(const char *s, int c);
diff --git a/lib/nolibc/string.c b/lib/nolibc/string.c
index fee2d10..0a22c1a 100644
--- a/lib/nolibc/string.c
+++ b/lib/nolibc/string.c
@@ -287,3 +287,35 @@ char *strdup(const char *str)
{
return strndup(str, SIZE_MAX);
}
+
+/* strlcpy has different ALIGN */
+#undef ALIGN
+#define ALIGN (sizeof(size_t)-1)
+size_t strlcpy(char *d, const char *s, size_t n)
+{
+ char *d0 = d;
+ size_t *wd;
+ const size_t *ws;
+
+ if (!n--) goto finish;
+ if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) {
+ for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++);
+ if (n && *s) {
+ 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;
+ }
+ }
+ for (; n && (*d=*s); n--, s++, d++);
+ *d = 0;
+finish:
+ return d-d0 + strlen(s);
+}
+
+size_t strlcat(char *d, const char *s, size_t n)
+{
+ size_t l = strnlen(d, n);
+ if (l == n) return l + strlen(s);
+ return l + strlcpy(d+l, s, n-l);
+}
--
2.19.2
_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |