summaryrefslogtreecommitdiff
path: root/src/utils.h
diff options
context:
space:
mode:
authorAdrian Perez de Castro <aperez@igalia.com>2019-08-05 16:07:57 +0300
committerRan Benita <ran@unusedvar.com>2019-12-27 12:45:14 +0200
commit93a13050d64cbd98168df6a4257cf84a7139542f (patch)
tree28fefc5ef1c2be1838db1d9fda3f18fd981419d7 /src/utils.h
parenta8acc2ff5cd7eb4e2501f2ef529b1d8f21471f37 (diff)
downloadxorg-lib-libxkbcommon-93a13050d64cbd98168df6a4257cf84a7139542f.tar.gz
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]
Diffstat (limited to 'src/utils.h')
-rw-r--r--src/utils.h15
1 files changed, 15 insertions, 0 deletions
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)