aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2018-08-29 18:41:15 +0200
committerAndreas Schneider <asn@cryptomilk.org>2018-10-17 08:23:15 +0200
commitf81ca6161223e3566ce78a427571235fb6848fe9 (patch)
tree5c9b342dab82e0bbdb466c18061259c825e7cb32
parentc20b360c96a098773755e82982ec6d7dfc7ff057 (diff)
downloadlibssh-f81ca6161223e3566ce78a427571235fb6848fe9.tar.gz
libssh-f81ca6161223e3566ce78a427571235fb6848fe9.tar.xz
libssh-f81ca6161223e3566ce78a427571235fb6848fe9.zip
misc: Add strndup implementation if not provides by the OS
Fixes T112 Signed-off-by: Andreas Schneider <asn@cryptomilk.org> (cherry picked from commit 247983e9820fd264cb5a59c14cc12846c028bd08)
-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: */