You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.0 KiB

  1. From f81ca6161223e3566ce78a427571235fb6848fe9 Mon Sep 17 00:00:00 2001
  2. From: Andreas Schneider <asn@cryptomilk.org>
  3. Date: Wed, 29 Aug 2018 18:41:15 +0200
  4. Subject: [PATCH 1/8] misc: Add strndup implementation if not provides by the
  5. OS
  6. Fixes T112
  7. Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
  8. (cherry picked from commit 247983e9820fd264cb5a59c14cc12846c028bd08)
  9. Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
  10. ---
  11. ConfigureChecks.cmake | 1 +
  12. config.h.cmake | 3 +++
  13. include/libssh/priv.h | 4 ++++
  14. src/misc.c | 21 +++++++++++++++++++++
  15. 4 files changed, 29 insertions(+)
  16. --- a/ConfigureChecks.cmake
  17. +++ b/ConfigureChecks.cmake
  18. @@ -115,6 +115,7 @@ endif (NOT WITH_GCRYPT)
  19. check_function_exists(isblank HAVE_ISBLANK)
  20. check_function_exists(strncpy HAVE_STRNCPY)
  21. +check_function_exists(strndup HAVE_STRNDUP)
  22. check_function_exists(strtoull HAVE_STRTOULL)
  23. if (NOT WIN32)
  24. --- a/config.h.cmake
  25. +++ b/config.h.cmake
  26. @@ -103,6 +103,9 @@
  27. /* Define to 1 if you have the `strncpy' function. */
  28. #cmakedefine HAVE_STRNCPY 1
  29. +/* Define to 1 if you have the `strndup' function. */
  30. +#cmakedefine HAVE_STRNDUP 1
  31. +
  32. /* Define to 1 if you have the `cfmakeraw' function. */
  33. #cmakedefine HAVE_CFMAKERAW 1
  34. --- a/include/libssh/priv.h
  35. +++ b/include/libssh/priv.h
  36. @@ -43,6 +43,10 @@
  37. # endif
  38. #endif /* !defined(HAVE_STRTOULL) */
  39. +#if !defined(HAVE_STRNDUP)
  40. +char *strndup(const char *s, size_t n);
  41. +#endif /* ! HAVE_STRNDUP */
  42. +
  43. #ifdef HAVE_BYTESWAP_H
  44. #include <byteswap.h>
  45. #endif
  46. --- a/src/misc.c
  47. +++ b/src/misc.c
  48. @@ -1028,6 +1028,27 @@ int ssh_match_group(const char *group, c
  49. return 0;
  50. }
  51. +#if !defined(HAVE_STRNDUP)
  52. +char *strndup(const char *s, size_t n)
  53. +{
  54. + char *x = NULL;
  55. +
  56. + if (n + 1 < n) {
  57. + return NULL;
  58. + }
  59. +
  60. + x = malloc(n + 1);
  61. + if (x == NULL) {
  62. + return NULL;
  63. + }
  64. +
  65. + memcpy(x, s, n);
  66. + x[n] = '\0';
  67. +
  68. + return x;
  69. +}
  70. +#endif /* ! HAVE_STRNDUP */
  71. +
  72. /** @} */
  73. /* vim: set ts=4 sw=4 et cindent: */