summaryrefslogtreecommitdiff
path: root/tools/include
diff options
context:
space:
mode:
authorAmmar Faizi <ammarfaizi2@gnuweeb.org>2022-03-29 17:17:37 +0700
committerPaul E. McKenney <paulmck@kernel.org>2022-04-20 17:05:46 -0700
commit11dbdaeff41d9ec9376476889651fac4838bff99 (patch)
tree583df0651963450bbcb3c634dc091e43180f5a47 /tools/include
parentb26823c19a12d9a06207ad3051e3d1059a9e1005 (diff)
tools/nolibc/string: Implement `strdup()` and `strndup()`
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@1wt.eu> Signed-off-by: Ammar Faizi <ammarfaizi2@gnuweeb.org> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Diffstat (limited to 'tools/include')
-rw-r--r--tools/include/nolibc/string.h32
1 files changed, 32 insertions, 0 deletions
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.
*/
@@ -157,6 +159,36 @@ size_t strnlen(const char *str, size_t maxlen)
}
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)
{
size_t len;