string/strdupa: support strdupa strndupa

refs:https://sites.uclouvain.be/SystInfo/manpages/man3/strdup.3.html

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1 2022-12-30 22:01:06 +08:00 committed by Xiang Xiao
parent 7660ece921
commit 191d453063
1 changed files with 10 additions and 0 deletions

View File

@ -29,15 +29,25 @@
#include <nuttx/compiler.h>
#include <stddef.h>
#include <alloca.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define strcoll_l(s1, s2, l) strcoll(s1, s2)
#define strdupa(x) strcpy(alloca(strlen(x) + 1), x)
#define strerror_l(e, l) strerror(e)
#define strxfrm_l(s1, s2, n, l) strxfrm(s1, s2, n)
#define strndupa(x, l) ({ \
FAR const char *__old = (x); \
size_t __len = strnlen(__old, (l)); \
FAR char *__new = alloca(__len + 1); \
__new[__len] = '\0'; \
(FAR char *)memcpy(__new, __old, __len); \
})
/****************************************************************************
* Public Function Prototypes
****************************************************************************/