From 0c43765bbfd565be647254ebf20052ea725e8229 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Fri, 12 Jul 2013 19:13:55 +0200 Subject: [PATCH] Add strdupa(3) and strndupa(3). --- libc/include/string.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/libc/include/string.h b/libc/include/string.h index 057926b0..9a2bfabd 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -93,6 +93,27 @@ char* strerror_l(int, locale_t); char* strsignal(int signum); #endif +/* Duplicate S, returning an identical alloca'd string. */ +#define strdupa(s) \ + (__extension__ \ + ({ \ + const char* __old = (s); \ + size_t __len = strlen(__old) + 1; \ + char* __new = (char*) __builtin_alloca(__len); \ + (char*) memcpy(__new, __old, __len); \ + })) + +/* Return an alloca'd copy of at most N bytes of string. */ +#define strndupa(s, n) \ + (__extension__ \ + ({ \ + __const char* __old = (s); \ + size_t __len = strnlen(__old, (n)); \ + char* __new = (char*) __builtin_alloca(__len + 1); \ + __new[__len] = '\0'; \ + (char*) memcpy(__new, __old, __len); \ + })) + __END_DECLS #endif