Provide a strndupa implementation when it is absent

strndupa is a glibc exclusive, not even musl implements it
This commit is contained in:
Greg V 2020-11-09 00:40:40 +03:00 committed by Wim Taymans
parent cc0386e1c5
commit 90ade199e6
2 changed files with 20 additions and 0 deletions

View file

@ -29,6 +29,8 @@
extern "C" {
#endif
#include "config.h"
#include <spa/utils/defs.h>
#include <spa/pod/pod.h>
@ -52,6 +54,20 @@ pw_free_strv(char **str);
char *
pw_strip(char *str, const char *whitespace);
#if defined(HAVE_STRNDUPA)
#include <string.h>
#else
# define strndupa(s, n) \
({ \
const char *__old = (s); \
size_t __len = strnlen(__old, (n)); \
char *__new = (char *) __builtin_alloca(__len + 1); \
memcpy(__new, __old, __len); \
__new[__len] = '\0'; \
__new; \
})
#endif
#ifdef __cplusplus
} /* extern "C" */
#endif