From f259664f751492c97e1e89897a906348dbc30c0d 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
---
 CMakeLists.txt          |    9 +++++++++
 ConfigureChecks.cmake   |    6 ++++++
 include/libssh/libssh.h |    2 ++
 include/libssh/sftp.h   |   13 +++----------
 libssh_config.h.cmake   |   41 +++++++++++++++++++++++++++++++++++++++++
 src/sftp.c              |    4 ++--
 6 files changed, 63 insertions(+), 12 deletions(-)
 create mode 100644 libssh_config.h.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 53953b8..644f67d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -70,6 +70,15 @@ find_package(Threads)
 # config.h checks
 include(ConfigureChecks.cmake)
 configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)
+configure_file(libssh_config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/libssh_config.h)
+install(
+  FILES
+    ${CMAKE_CURRENT_BINARY_DIR}/libssh_config.h
+  DESTINATION
+    ${INCLUDE_INSTALL_DIR}/${APPLICATION_NAME}
+  COMPONENT
+    headers
+)
 
 # check subdirectories
 add_subdirectory(doc)
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/include/libssh/libssh.h b/include/libssh/libssh.h
index 40a32be..d5e815d 100644
--- a/include/libssh/libssh.h
+++ b/include/libssh/libssh.h
@@ -22,6 +22,8 @@
 #ifndef _LIBSSH_H
 #define _LIBSSH_H
 
+#include "libssh_config.h"
+
 #if defined _WIN32 || defined __CYGWIN__
   #ifdef LIBSSH_STATIC
     #define LIBSSH_API
diff --git a/include/libssh/sftp.h b/include/libssh/sftp.h
index e9fbae8..8575e86 100644
--- a/include/libssh/sftp.h
+++ b/include/libssh/sftp.h
@@ -38,8 +38,6 @@
 #ifndef SFTP_H
 #define SFTP_H
 
-#include <sys/types.h>
-
 #include "libssh.h"
 
 #ifdef __cplusplus
@@ -53,13 +51,8 @@ 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 */
-
+  
 #define LIBSFTP_VERSION 3
 
 typedef struct sftp_attributes_struct* sftp_attributes;
@@ -458,7 +451,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 +527,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/libssh_config.h.cmake b/libssh_config.h.cmake
new file mode 100644
index 0000000..e1f5d04
--- /dev/null
+++ b/libssh_config.h.cmake
@@ -0,0 +1,41 @@
+/*
+ * This file is part of the SSH Library
+ *
+ * The SSH Library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * The SSH Library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with the SSH Library; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*************************** 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}
+
+#ifdef HAVE_SSIZE_T_LIBSSH
+  #include <sys/types.h>
+  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 */
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

