Add strdupa(3) and strndupa(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2013-07-12 19:13:55 +02:00
parent fdbcea19dc
commit 0c43765bbf
1 changed files with 21 additions and 0 deletions

View File

@ -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