aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2018-08-29 18:41:15 +0200
committerAndreas Schneider <asn@cryptomilk.org>2018-08-29 19:04:44 +0200
commit247983e9820fd264cb5a59c14cc12846c028bd08 (patch)
treed29b27aadcab16aa587df64c1972266bec994267
parentf0e99961b6d04b6fc7d4054a6c111223d8ff2894 (diff)
downloadlibssh-247983e9820fd264cb5a59c14cc12846c028bd08.tar.gz
libssh-247983e9820fd264cb5a59c14cc12846c028bd08.tar.xz
libssh-247983e9820fd264cb5a59c14cc12846c028bd08.zip
misc: Add strndup implementation if not provides by the OS
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r--ConfigureChecks.cmake1
-rw-r--r--config.h.cmake3
-rw-r--r--include/libssh/priv.h4
-rw-r--r--src/misc.c21
4 files changed, 29 insertions, 0 deletions
diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake
index 57cd0811..f91bd038 100644
--- a/ConfigureChecks.cmake
+++ b/ConfigureChecks.cmake
@@ -150,6 +150,7 @@ endif (NOT WITH_MBEDTLS)
check_function_exists(isblank HAVE_ISBLANK)
check_function_exists(strncpy HAVE_STRNCPY)
+check_function_exists(strndup HAVE_STRNDUP)
check_function_exists(strtoull HAVE_STRTOULL)
check_function_exists(explicit_bzero HAVE_EXPLICIT_BZERO)
check_function_exists(memset_s HAVE_MEMSET_S)
diff --git a/config.h.cmake b/config.h.cmake
index 8c469b6c..96dd4a87 100644
--- a/config.h.cmake
+++ b/config.h.cmake
@@ -130,6 +130,9 @@
/* Define to 1 if you have the `strncpy' function. */
#cmakedefine HAVE_STRNCPY 1
+/* Define to 1 if you have the `strndup' function. */
+#cmakedefine HAVE_STRNDUP 1
+
/* Define to 1 if you have the `cfmakeraw' function. */
#cmakedefine HAVE_CFMAKERAW 1
diff --git a/include/libssh/priv.h b/include/libssh/priv.h
index 0aab4edb..2123fc03 100644
--- a/include/libssh/priv.h
+++ b/include/libssh/priv.h
@@ -44,6 +44,10 @@
# endif
#endif /* !defined(HAVE_STRTOULL) */
+#if !defined(HAVE_STRNDUP)
+char *strndup(const char *s, size_t n);
+#endif /* ! HAVE_STRNDUP */
+
#ifdef HAVE_BYTESWAP_H
#include <byteswap.h>
#endif
diff --git a/src/misc.c b/src/misc.c
index 5f606044..89934900 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -1085,4 +1085,25 @@ void explicit_bzero(void *s, size_t n)
}
#endif /* !HAVE_EXPLICIT_BZERO */
+#if !defined(HAVE_STRNDUP)
+char *strndup(const char *s, size_t n)
+{
+ char *x = NULL;
+
+ if (n + 1 < n) {
+ return NULL;
+ }
+
+ x = malloc(n + 1);
+ if (x == NULL) {
+ return NULL;
+ }
+
+ memcpy(x, s, n);
+ x[n] = '\0';
+
+ return x;
+}
+#endif /* ! HAVE_STRNDUP */
+
/** @} */