aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 0e2c2cc7..0a53c5b1 100644
--- a/ConfigureChecks.cmake
+++ b/ConfigureChecks.cmake
@@ -115,6 +115,7 @@ endif (NOT WITH_GCRYPT)
check_function_exists(isblank HAVE_ISBLANK)
check_function_exists(strncpy HAVE_STRNCPY)
+check_function_exists(strndup HAVE_STRNDUP)
check_function_exists(strtoull HAVE_STRTOULL)
if (NOT WIN32)
diff --git a/config.h.cmake b/config.h.cmake
index f2288b60..3e7f7939 100644
--- a/config.h.cmake
+++ b/config.h.cmake
@@ -103,6 +103,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 5a74915e..8c01f9c2 100644
--- a/include/libssh/priv.h
+++ b/include/libssh/priv.h
@@ -43,6 +43,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 2d4fa17f..233f2724 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -1028,6 +1028,27 @@ int ssh_match_group(const char *group, const char *object)
return 0;
}
+#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 */
+
/** @} */
/* vim: set ts=4 sw=4 et cindent: */