Commit 11dbdaef authored by Ammar Faizi's avatar Ammar Faizi Committed by Paul E. McKenney
Browse files

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: default avatarWilly Tarreau <w@1wt.eu>
Signed-off-by: default avatarAmmar Faizi <ammarfaizi2@gnuweeb.org>
Signed-off-by: default avatarPaul E. McKenney <paulmck@kernel.org>
parent b26823c1
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
@@ -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)
{