Provide a fallback implementation of strndup() Some environments (e.g. Windows + MSVC) do not provide strndup(), this tries to detect its presence and provide a fallback implementation when not available. [ran: some tweaks]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
diff --git a/meson.build b/meson.build
index ad268d0..b7aaeee 100644
--- a/meson.build
+++ b/meson.build
@@ -99,6 +99,9 @@ endif
if cc.has_header_symbol('fcntl.h', 'posix_fallocate', prefix: system_ext_define)
configh_data.set('HAVE_POSIX_FALLOCATE', 1)
endif
+if cc.has_header_symbol('string.h', 'strndup', prefix: system_ext_define)
+ configh_data.set('HAVE_STRNDUP', 1)
+endif
if cc.has_header_symbol('stdlib.h', 'secure_getenv', prefix: system_ext_define)
configh_data.set('HAVE_SECURE_GETENV', 1)
elif cc.has_header_symbol('stdlib.h', '__secure_getenv', prefix: system_ext_define)
diff --git a/src/utils.h b/src/utils.h
index c012651..d796e10 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -110,6 +110,21 @@ memdup(const void *mem, size_t nmemb, size_t size)
return p;
}
+#if !(defined(HAVE_STRNDUP) && HAVE_STRNDUP)
+static inline char *
+strndup(const char *s, size_t n)
+{
+ size_t slen = strlen(s);
+ size_t len = MIN(slen, n);
+ char *p = malloc(len + 1);
+ if (!p)
+ return NULL;
+ memcpy(p, str, len);
+ p[len] = '\0';
+ return p;
+}
+#endif
+
/* ctype.h is locale-dependent and has other oddities. */
static inline bool
is_space(char ch)