[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220419004225.3952530-61-paulmck@kernel.org>
Date: Mon, 18 Apr 2022 17:42:25 -0700
From: "Paul E. McKenney" <paulmck@...nel.org>
To: linux-kernel@...r.kernel.org
Cc: gwml@...r.gnuweeb.org, kernel-team@...com, w@....eu,
Ammar Faizi <ammarfaizi2@...weeb.org>,
Willy Tarreau <w@....eu>,
"Paul E . McKenney" <paulmck@...nel.org>
Subject: [PATCH nolibc 61/61] tools/nolibc/string: Implement `strdup()` and `strndup()`
From: Ammar Faizi <ammarfaizi2@...weeb.org>
These functions are currently only available on architectures that have
my_syscall6() macro implemented. Since these functions use malloc(),
malloc() uses mmap(), mmap() depends on my_syscall6() macro.
On architectures that don't support my_syscall6(), these function will
always return NULL with errno set to ENOSYS.
Acked-by: Willy Tarreau <w@....eu>
Signed-off-by: Ammar Faizi <ammarfaizi2@...weeb.org>
Signed-off-by: Paul E. McKenney <paulmck@...nel.org>
---
tools/include/nolibc/string.h | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h
index f43d52a44d09..bef35bee9c44 100644
--- a/tools/include/nolibc/string.h
+++ b/tools/include/nolibc/string.h
@@ -9,6 +9,8 @@
#include "std.h"
+static void *malloc(size_t len);
+
/*
* As much as possible, please keep functions alphabetically sorted.
*/
@@ -156,6 +158,36 @@ size_t strnlen(const char *str, size_t maxlen)
return len;
}
+static __attribute__((unused))
+char *strdup(const char *str)
+{
+ size_t len;
+ char *ret;
+
+ len = strlen(str);
+ ret = malloc(len + 1);
+ if (__builtin_expect(ret != NULL, 1))
+ memcpy(ret, str, len + 1);
+
+ return ret;
+}
+
+static __attribute__((unused))
+char *strndup(const char *str, size_t maxlen)
+{
+ size_t len;
+ char *ret;
+
+ len = strnlen(str, maxlen);
+ ret = malloc(len + 1);
+ if (__builtin_expect(ret != NULL, 1)) {
+ memcpy(ret, str, len);
+ ret[len] = '\0';
+ }
+
+ return ret;
+}
+
static __attribute__((unused))
size_t strlcat(char *dst, const char *src, size_t size)
{
--
2.31.1.189.g2e36527f23
Powered by blists - more mailing lists