diff options
| author | Konstantin Belousov <kib@FreeBSD.org> | 2025-12-03 23:56:26 +0000 |
|---|---|---|
| committer | Konstantin Belousov <kib@FreeBSD.org> | 2025-12-16 04:57:01 +0000 |
| commit | b2e649a0b98d110aa70c2d2d6cab919b9dd8e9af (patch) | |
| tree | 7fe35148c6f4ffa7a736fcae9d4b00af8c485456 /include | |
| parent | 477c4645bbcc4dce0e88587ffa6b583c6ff6899f (diff) | |
libc/string: add strdupa(3) and strndupa(3)
(cherry picked from commit a98e5d78500193dc7aa352e1f60ac2c6529e2c38)
Diffstat (limited to 'include')
| -rw-r--r-- | include/string.h | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/include/string.h b/include/string.h index a3fa96ebb449..e9aa1b55281a 100644 --- a/include/string.h +++ b/include/string.h @@ -154,6 +154,34 @@ void swab(const void * __restrict, void * __restrict, ssize_t); int timingsafe_bcmp(const void *, const void *, size_t); int timingsafe_memcmp(const void *, const void *, size_t); + +#if __has_builtin(__builtin_alloca) +#define strdupa(_Str) (__extension__({ \ + const char *_Str1; \ + size_t _Len; \ + char *_Copy; \ + \ + _Str1 = (_Str); \ + _Len = strlen(_Str1) + 1; \ + _Copy = (char *)__builtin_alloca(_Len); \ + memcpy(_Copy, _Str1, _Len); \ + _Copy; \ +})) + +#define strndupa(_Str, _Maxlen) (__extension__({ \ + const char *_Str1; \ + char *_Copy; \ + size_t _Len; \ + \ + _Str1 = (_Str); \ + _Len = strnlen((_Str1), (_Maxlen)); \ + _Copy = __builtin_alloca(_Len + 1); \ + (void)memcpy(_Copy, _Str1, _Len); \ + _Copy[_Len] = '\0'; \ + _Copy; \ +})) +#endif + #endif /* __BSD_VISIBLE */ #if __POSIX_VISIBLE >= 200112 || defined(_XLOCALE_H_) |
