From 063a41a85f8caadb4a9e4fac124b1d754a0e5ccc Mon Sep 17 00:00:00 2001
From: Christophe Baribaud <christophe.baribaud@gmail.com>
Date: Wed, 26 Sep 2012 12:03:05 +0200
Subject: [PATCH 3/3] provide fallback definition of ssize_t

Unlike size_t, ssize_t is not required by the ISO C standard
To avoid incompatible definitions between libraries,
ssize_t is renamed to the private data type ssh_ssize_t
---
 ConfigureChecks.cmake |    6 ++++++
 config.h.cmake        |    8 ++++++++
 include/libssh/sftp.h |   24 +++++++++++++++++-------
 src/sftp.c            |    4 ++--
 4 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake
index f20fac9..200bd09 100644
--- a/ConfigureChecks.cmake
+++ b/ConfigureChecks.cmake
@@ -140,6 +140,12 @@ endif (UNIX)
 
 set(LIBSSH_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CACHE INTERNAL "libssh required system libraries")
 
+CHECK_TYPE_SIZE(size_t SIZE_T_LIBSSH)
+CHECK_TYPE_SIZE(ssize_t SSIZE_T_LIBSSH)
+CHECK_TYPE_SIZE(int INT_LIBSSH)
+CHECK_TYPE_SIZE(long LONG_LIBSSH)
+CHECK_TYPE_SIZE("long long" LONG_LONG_LIBSSH)
+
 # LIBRARIES
 if (OPENSSL_FOUND)
   set(HAVE_LIBCRYPTO 1)
diff --git a/config.h.cmake b/config.h.cmake
index 03843e3..cd894a3 100644
--- a/config.h.cmake
+++ b/config.h.cmake
@@ -137,3 +137,11 @@
 /* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
    significant byte first (like Motorola and SPARC, unlike Intel). */
 #cmakedefine WORDS_BIGENDIAN 1
+
+/*************************** SIZE TYPES *************************/
+
+#cmakedefine HAVE_SSIZE_T_LIBSSH 1
+#cmakedefine SIZE_T_LIBSSH ${SIZE_T_LIBSSH}
+#cmakedefine INT_LIBSSH ${INT_LIBSSH}
+#cmakedefine LONG_LIBSSH ${LONG_LIBSSH}
+#cmakedefine LONG_LONG_LIBSSH ${LONG_LONG_LIBSSH}
diff --git a/include/libssh/sftp.h b/include/libssh/sftp.h
index e9fbae8..384cfae 100644
--- a/include/libssh/sftp.h
+++ b/include/libssh/sftp.h
@@ -53,12 +53,22 @@ extern "C" {
 #ifndef gid_t
   typedef uint32_t gid_t;
 #endif /* gid_t */
-#ifdef _MSC_VER
-#ifndef ssize_t
-  typedef _W64 SSIZE_T ssize_t;
-#endif /* ssize_t */
-#endif /* _MSC_VER */
 #endif /* _WIN32 */
+  
+#ifdef HAVE_SSIZE_T_LIBSSH
+  typedef ssize_t ssh_ssize_t;
+#else
+  #if SIZE_T_LIBSSH == INT_LIBSSH
+    typedef int ssh_ssize_t;
+  #elif SIZE_T_LIBSSH == LONG_LIBSSH
+    typedef long ssh_ssize_t;
+  #elif SIZE_T_LIBSSH == LONG_LONG_LIBSSH
+    typedef long long ssh_ssize_t;
+  #else
+    #error no suitable type for ssh_ssize_t found
+  #endif
+#endif /* HAVE_SSIZE_T_LIBSSH */
+
 
 #define LIBSFTP_VERSION 3
 
@@ -458,7 +468,7 @@ LIBSSH_API void sftp_file_set_blocking(sftp_file handle);
  *
  * @see sftp_get_error()
  */
-LIBSSH_API ssize_t sftp_read(sftp_file file, void *buf, size_t count);
+LIBSSH_API ssh_ssize_t sftp_read(sftp_file file, void *buf, size_t count);
 
 /**
  * @brief Start an asynchronous read from a file using an opened sftp file handle.
@@ -534,7 +544,7 @@ LIBSSH_API int sftp_async_read(sftp_file file, void *data, uint32_t len, uint32_
  * @see                 sftp_read()
  * @see                 sftp_close()
  */
-LIBSSH_API ssize_t sftp_write(sftp_file file, const void *buf, size_t count);
+LIBSSH_API ssh_ssize_t sftp_write(sftp_file file, const void *buf, size_t count);
 
 /**
  * @brief Seek to a specific location in a file.
diff --git a/src/sftp.c b/src/sftp.c
index 456f94a..e38e823 100644
--- a/src/sftp.c
+++ b/src/sftp.c
@@ -1693,7 +1693,7 @@ void sftp_file_set_blocking(sftp_file handle){
 }
 
 /* Read from a file using an opened sftp file handle. */
-ssize_t sftp_read(sftp_file handle, void *buf, size_t count) {
+ssh_ssize_t sftp_read(sftp_file handle, void *buf, size_t count) {
   sftp_session sftp = handle->sftp;
   sftp_message msg = NULL;
   sftp_status_message status;
@@ -1901,7 +1901,7 @@ int sftp_async_read(sftp_file file, void *data, uint32_t size, uint32_t id){
   return SSH_ERROR;
 }
 
-ssize_t sftp_write(sftp_file file, const void *buf, size_t count) {
+ssh_ssize_t sftp_write(sftp_file file, const void *buf, size_t count) {
   sftp_session sftp = file->sftp;
   sftp_message msg = NULL;
   sftp_status_message status;
-- 
1.7.7

