Skip to content

Commit

Permalink
string/strdupa: support strdupa strndupa
Browse files Browse the repository at this point in the history
refs:https://sites.uclouvain.be/SystInfo/manpages/man3/strdup.3.html

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
  • Loading branch information
Donny9 authored and xiaoxiang781216 committed Dec 30, 2022
1 parent 7660ece commit 191d453
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
****************************************************************************/
Expand Down

0 comments on commit 191d453

Please sign in to comment.