Commit 5c44cf4bb62590a8db514c8b2d0f9db953a43d19

humphreyj 2018-10-07T11:47:29

Replace all instances of ssize_t with long and rearranged unistd include, fixing compile errors on Windows VS.

diff --git a/src/smtp.c b/src/smtp.c
index 950b89a..b99d540 100644
--- a/src/smtp.c
+++ b/src/smtp.c
@@ -39,6 +39,7 @@
 # include <sys/select.h>
 # include <sys/socket.h>
 # include <netdb.h>
+# include <unistd.h>
 #endif /* SMTP_IS_WINDOWS */
 
 #include <errno.h>
@@ -48,7 +49,6 @@
 #include <string.h>
 #include <stdlib.h>
 #include <time.h>
-#include <unistd.h>
 
 #ifdef SMTP_OPENSSL
 # include <openssl/bio.h>
@@ -276,12 +276,12 @@ smtp_str_getdelimfd_read_timeout(struct smtp *const smtp){
  * @retval >=0 Number of bytes read.
  * @retval -1  Failed to read from the socket.
  */
-static ssize_t
+static long
 smtp_str_getdelimfd_read(struct str_getdelimfd *const gdfd,
                          void *buf,
                          size_t count){
   struct smtp *smtp;
-  ssize_t bytes_read;
+  long bytes_read;
 
   smtp = gdfd->user_data;
 
@@ -394,7 +394,7 @@ smtp_str_getdelimfd_free(struct str_getdelimfd *const gdfd){
 SMTP_LINKAGE enum str_getdelim_retcode
 smtp_str_getdelimfd(struct str_getdelimfd *const gdfd){
   size_t delim_pos;
-  ssize_t bytes_read;
+  long bytes_read;
   void *read_buf_ptr;
   char *buf_new;
   size_t buf_sz_remaining;
@@ -629,7 +629,7 @@ smtp_base64_encode_block(const char *const buf,
  */
 SMTP_LINKAGE char *
 smtp_base64_encode(const char *const buf,
-                   ssize_t buflen){
+                   long buflen){
   char *b64;
   size_t b64_sz;
   size_t buf_i;
@@ -781,13 +781,13 @@ smtp_base64_decode_block(const unsigned char *const buf,
  * @retval >=0 Length of the data stored in the decode parameter.
  * @retval -1  Memory allocation failure or invalid base64 byte sequences.
  */
-SMTP_LINKAGE ssize_t
+SMTP_LINKAGE long
 smtp_base64_decode(const char *const buf,
                    unsigned char **decode){
   size_t buf_len;
   size_t buf_i;
   unsigned char *b64_decode;
-  ssize_t decode_len;
+  long decode_len;
   int decode_block_len;
 
   *decode = NULL;
@@ -923,7 +923,7 @@ smtp_str_has_nonascii_utf8(const char *const s){
  * @retval maxlen    If length of s has more bytes than maxlen.
  * @retval -1        If @p s contains an invalid UTF-8 byte sequence.
  */
-SMTP_LINKAGE ssize_t
+SMTP_LINKAGE long
 smtp_strnlen_utf8(const char *s,
                   size_t maxlen){
   size_t i;
@@ -967,7 +967,7 @@ smtp_chunk_split(const char *const s,
   size_t chunk_i;
   size_t snew_i;
   size_t body_i;
-  ssize_t body_copy_len;
+  long body_copy_len;
 
   if(chunklen < 1){
     errno = EINVAL;
@@ -1338,11 +1338,16 @@ smtp_connect(struct smtp *const smtp,
     }
 
     if(connect(smtp->sock, res->ai_addr, res->ai_addrlen) < 0){
+#ifdef SMTP_IS_WINDOWS
+      closesocket(smtp->sock);
+#else /* POSIX */
       close(smtp->sock);
+#endif /* SMTP_IS_WINDOWS */
       smtp->sock = -1;
-      continue;
     }
-    break;
+    else{
+      break;
+    }
   }
 
   freeaddrinfo(res0);
@@ -1630,7 +1635,7 @@ smtp_auth_cram_md5(struct smtp *const smtp,
                    const char *const pass){
   struct smtp_command cmd;
   unsigned char *challenge_decoded;
-  ssize_t challenge_decoded_len;
+  long challenge_decoded_len;
   const char *key;
   int key_len;
   unsigned char hmac[EVP_MAX_MD_SIZE];
@@ -3091,7 +3096,7 @@ enum smtp_status_code
 smtp_attachment_add_mem(struct smtp *const smtp,
                         const char *const name,
                         const void *const data,
-                        ssize_t datasz){
+                        long datasz){
   size_t new_size;
   struct smtp_attachment *new_attachment_list;
   struct smtp_attachment *new_attachment;
diff --git a/src/smtp.h b/src/smtp.h
index 9db9446..e9d3cf1 100644
--- a/src/smtp.h
+++ b/src/smtp.h
@@ -271,7 +271,7 @@ enum smtp_status_code
 smtp_attachment_add_mem(struct smtp *const smtp,
                         const char *const name,
                         const void *const data,
-                        ssize_t datasz);
+                        long datasz);
 
 void smtp_attachment_clear_all(struct smtp *const smtp);
 
@@ -414,9 +414,9 @@ struct str_getdelimfd{
    * info from the @ref str_getdelimfd struct which can contain file pointer,
    * socket connection, etc.
    */
-  ssize_t (*getdelimfd_read)(struct str_getdelimfd *const gdfd,
-                             void *buf,
-                             size_t count);
+  long (*getdelimfd_read)(struct str_getdelimfd *const gdfd,
+                          void *buf,
+                          size_t count);
 
   /**
    * User data which gets sent to the read handler function.
diff --git a/test/seams.c b/test/seams.c
index 96a4536..7c3d20a 100644
--- a/test/seams.c
+++ b/test/seams.c
@@ -483,7 +483,7 @@ smtp_test_seam_realloc(void *ptr,
  * @retval >=0 Number of bytes received.
  * @retval  -1 Failed to receive bytes over the network.
  */
-ssize_t
+long
 smtp_test_seam_recv(int socket,
                     void *buffer,
                     size_t length,
@@ -545,7 +545,7 @@ smtp_test_seam_select(int nfds,
  * @retval >=0 Number of bytes sent.
  * @retval  -1 Failed to send bytes over the network.
  */
-ssize_t
+long
 smtp_test_seam_send(int socket,
                     const void *buffer,
                     size_t length,
diff --git a/test/test.c b/test/test.c
index ae21b7b..f0bc1fd 100644
--- a/test/test.c
+++ b/test/test.c
@@ -257,7 +257,7 @@ smtp_str_list_free(struct smtp_str_list *const list){
  */
 static int
 smtp_str_split(const char *const s,
-               ssize_t slen,
+               long slen,
                const char *const delimiter,
                int limit,
                struct smtp_str_list *slist){
@@ -328,8 +328,8 @@ smtp_str_split(const char *const s,
 static size_t
 smtp_ffile_put_contents(FILE *stream,
                         const void *const data,
-                        ssize_t datasz){
-  ssize_t bytes_written;
+                        long datasz){
+  long bytes_written;
 
   bytes_written = 0;
 
@@ -366,7 +366,7 @@ smtp_ffile_put_contents(FILE *stream,
 static size_t
 smtp_file_put_contents(const char *const filename,
                        const void *const data,
-                       ssize_t datasz,
+                       long datasz,
                        int flags){
   FILE *fp;
   size_t bytes_written;
@@ -420,9 +420,9 @@ smtp_test_sleep(unsigned int seconds){
 static void
 smtp_unit_test_base64_decode(const char *const buf,
                              const char *const expect_str,
-                             ssize_t expect_str_len){
+                             long expect_str_len){
   unsigned char *decode;
-  ssize_t str_len;
+  long str_len;
 
   str_len = smtp_base64_decode(buf, &decode);
   if(expect_str){
@@ -471,7 +471,7 @@ smtp_unit_test_all_base64_decode(void){
  */
 static void
 smtp_unit_test_base64_encode(const char *const buf,
-                             ssize_t buflen,
+                             long buflen,
                              const char *const expect){
   char *result;
 
@@ -1199,7 +1199,7 @@ static int g_smtp_test_getdelimfd_fp_fail = 0;
  * @retval >=0 Number of bytes read.
  * @retval -1  Failed to read from the socket.
  */
-static ssize_t
+static long
 smtp_unit_test_getdelimfd_fp(struct str_getdelimfd *const gdfd,
                              void *buf,
                              size_t count){
diff --git a/test/test.h b/test/test.h
index dce6105..97686ac 100644
--- a/test/test.h
+++ b/test/test.h
@@ -55,13 +55,13 @@ struct str_getdelimfd;
  */
 #define SMTP_DATE_MAX_SZ 32
 
-ssize_t
+long
 smtp_base64_decode(const char *const buf,
                    unsigned char **decode);
 
 char *
 smtp_base64_encode(const char *const buf,
-                   ssize_t buflen);
+                   long buflen);
 
 char *
 smtp_bin2hex(const unsigned char *const s,
@@ -91,7 +91,7 @@ smtp_utf8_charlen(unsigned char c);
 int
 smtp_str_has_nonascii_utf8(const char *const s);
 
-ssize_t
+long
 smtp_strnlen_utf8(const char *s,
                   size_t maxlen);
 
@@ -188,7 +188,7 @@ void *
 smtp_test_seam_realloc(void *ptr,
                        size_t size);
 
-ssize_t
+long
 smtp_test_seam_recv(int socket,
                     void *buffer,
                     size_t length,
@@ -201,7 +201,7 @@ smtp_test_seam_select(int nfds,
                       fd_set *errorfds,
                       struct timeval *timeout);
 
-ssize_t
+long
 smtp_test_seam_send(int socket,
                     const void *buffer,
                     size_t length,