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

@ -267,6 +267,10 @@ if cc.has_type('ptrdiff_t', prefix : '#include <stddef.h>')
cdata.set('HAVE_PTRDIFF_T', 1)
endif
if cc.has_function('strndupa', prefix : '#include <string.h>', args : [ '-D_GNU_SOURCE' ])
cdata.set('HAVE_STRNDUPA', 1)
endif
if cc.has_function('mkstemp', prefix : '#include <stdlib.h>')
cdata.set('HAVE_MKSTEMP', 1)
endif

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