diff options
| author | 2025-03-15 19:51:11 -0400 | |
|---|---|---|
| committer | 2025-03-15 19:51:11 -0400 | |
| commit | 7feaa9ec9090129f78b7aed868b6a0b4359e686c (patch) | |
| tree | b31d17e4fff75a92200fd34c6e344e70bb44bea2 /include | |
| parent | 31715673a5b98991ef276decbd90f026559eaf4b (diff) | |
| download | libalabaster-7feaa9ec9090129f78b7aed868b6a0b4359e686c.tar.gz libalabaster-7feaa9ec9090129f78b7aed868b6a0b4359e686c.tar.bz2 libalabaster-7feaa9ec9090129f78b7aed868b6a0b4359e686c.zip | |
lib: Implement strndup
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'include')
| -rw-r--r-- | include/al/lib.h | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/include/al/lib.h b/include/al/lib.h index 4f03235..ac47896 100644 --- a/include/al/lib.h +++ b/include/al/lib.h @@ -75,7 +75,8 @@ void al_malloc_stats(size_t *current, size_t *peak, size_t *total); #define al_memset memset #define al_bzero bzero #define al_strlen strlen -#define al_strndup strndup +// strndup is _POSIX_C_SOURCE >= 200809L. +//#define al_strndup strndup #define al_snprintf snprintf #define al_vsnprintf vsnprintf #define al_fflush fflush @@ -103,6 +104,15 @@ static inline size_t al_strnlen(const char *s, size_t n) return p ? (size_t)(p - s) : n; } +static inline char *al_strndup(const char *s, size_t n) +{ + size_t len = al_strnlen(s, n); + char *ret = al_malloc(len + 1); + al_memcpy(ret, s, len); + ret[len] = '\0'; + return ret; +} + static inline s32 al_printf(const char *fmt, ...) { #ifdef AL_FORCE_DISABLE_OUTPUT @@ -229,8 +239,7 @@ AL_UNUSED_FUNCTION_POP #define MAX_ALLOC_32 ((INT32_MAX - 1) / 4) #endif -// Once a growing allocation expands past this number, -// only grow in multiples of it. +// Once a growing allocation expands past this number, only grow in multiples of it. extern u32 al_grow_step; u32 al_default_growing_allocation(void **ptr, u32 prev_size, u32 size, |