From 8cb57712fda0564317c96fd7037b1a5ce4d8fabd Mon Sep 17 00:00:00 2001
From: Brad King <brad.king@kitware.com>
Date: Wed, 4 Oct 2006 17:27:17 -0400
Subject: [PATCH] ENH: Adding tests KWSYS_C_HAS_PTRDIFF_T and
 KWSYS_C_HAS_SSIZE_T to help ProcessUNIX.c build everywhere without warnings.

---
 CMakeLists.txt        |  9 +++++++++
 ProcessUNIX.c         | 31 ++++++++++++++++++++++---------
 kwsysPlatformTestsC.c | 30 ++++++++++++++++++++++++++++--
 3 files changed, 59 insertions(+), 11 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 01891ac..6538561 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -621,6 +621,15 @@ IF(KWSYS_USE_Process)
   ELSE(NOT UNIX)
     # Use the UNIX implementation.
     SET(KWSYS_C_SRCS ${KWSYS_C_SRCS} ProcessUNIX.c)
+
+    # Help ProcessUNIX.c compile properly on all platforms.
+    KWSYS_PLATFORM_C_TEST(KWSYS_C_HAS_PTRDIFF_T
+      "Checking whether C compiler has ptrdiff_t in stddef.h" DIRECT)
+    KWSYS_PLATFORM_C_TEST(KWSYS_C_HAS_SSIZE_T
+      "Checking whether C compiler has ssize_t in unistd.h" DIRECT)
+    SET_SOURCE_FILES_PROPERTIES(ProcessUNIX.c PROPERTIES
+      COMPILE_FLAGS "-DKWSYS_C_HAS_PTRDIFF_T=${KWSYS_C_HAS_PTRDIFF_T} -DKWSYS_C_HAS_SSIZE_T=${KWSYS_C_HAS_SSIZE_T}"
+      )
   ENDIF(NOT UNIX)
 ENDIF(KWSYS_USE_Process)
 
diff --git a/ProcessUNIX.c b/ProcessUNIX.c
index 245ca77..ac07f1a 100644
--- a/ProcessUNIX.c
+++ b/ProcessUNIX.c
@@ -47,6 +47,7 @@ do.
 
 */
 
+#include <stddef.h>    /* ptrdiff_t */
 #include <stdio.h>     /* snprintf */
 #include <stdlib.h>    /* malloc, free */
 #include <string.h>    /* strdup, strerror, memset */
@@ -62,6 +63,18 @@ do.
 #include <dirent.h>    /* DIR, dirent */
 #include <ctype.h>     /* isspace */
 
+#if defined(KWSYS_C_HAS_PTRDIFF_T) && KWSYS_C_HAS_PTRDIFF_T
+typedef ptrdiff_t kwsysProcess_ptrdiff_t;
+#else
+typedef int kwsysProcess_ptrdiff_t;
+#endif
+
+#if defined(KWSYS_C_HAS_SSIZE_T) && KWSYS_C_HAS_SSIZE_T
+typedef ssize_t kwsysProcess_ssize_t;
+#else
+typedef int kwsysProcess_ssize_t;
+#endif
+
 /* The number of pipes for the child's output.  The standard stdout
    and stderr pipes are the first two.  One more pipe is used to
    detect when the child process has terminated.  The third pipe is
@@ -368,8 +381,8 @@ int kwsysProcess_AddCommand(kwsysProcess* cp, char const* const* command)
     {
     /* Copy each argument string individually.  */
     char const* const* c = command;
-    int n = 0;
-    int i = 0;
+    kwsysProcess_ptrdiff_t n = 0;
+    kwsysProcess_ptrdiff_t i = 0;
     while(*c++);
     n = c - command - 1;
     newCommands[cp->NumberOfCommands] = (char**)malloc((n+1)*sizeof(char*));
@@ -904,7 +917,7 @@ int kwsysProcess_WaitForData(kwsysProcess* cp, char** data, int* length,
       if(cp->PipeReadEnds[i] >= 0 &&
          FD_ISSET(cp->PipeReadEnds[i], &cp->PipeSet))
         {
-        int n;
+        kwsysProcess_ssize_t n;
 
         /* We are handling this pipe now.  Remove it from the set.  */
         FD_CLR(cp->PipeReadEnds[i], &cp->PipeSet);
@@ -1501,8 +1514,8 @@ static int kwsysProcessCreate(kwsysProcess* cp, int prIndex,
   /* Block until the child's exec call succeeds and closes the error
      pipe or writes data to the pipe to report an error.  */
   {
-  int total = 0;
-  int n = 1;
+  kwsysProcess_ssize_t total = 0;
+  kwsysProcess_ssize_t n = 1;
   /* Read the entire error message up to the length of our buffer.  */
   while(total < KWSYSPE_PIPE_BUFFER_SIZE && n > 0)
     {
@@ -2404,7 +2417,7 @@ static int kwsysProcessAppendByte(char* local,
   /* Allocate space for the character.  */
   if((*end - *begin) >= *size)
     {
-    int length = *end - *begin;
+    kwsysProcess_ptrdiff_t length = *end - *begin;
     char* newBuffer = (char*)malloc(*size*2);
     if(!newBuffer)
       {
@@ -2442,7 +2455,7 @@ static int kwsysProcessAppendArgument(char** local,
   /* Allocate space for the argument pointer.  */
   if((*end - *begin) >= *size)
     {
-    int length = *end - *begin;
+    kwsysProcess_ptrdiff_t length = *end - *begin;
     char** newPointers = (char**)malloc(*size*2*sizeof(char*));
     if(!newPointers)
       {
@@ -2615,14 +2628,14 @@ static char** kwsysProcessParseVerbatimCommand(const char* command)
      buffer.  */
   if(!failed)
     {
-    int n = pointer_end - pointer_begin;
+    kwsysProcess_ptrdiff_t n = pointer_end - pointer_begin;
     newCommand = (char**)malloc((n+1)*sizeof(char*));
     }
 
   if(newCommand)
     {
     /* Copy the arguments into the new command buffer.  */
-    int n = pointer_end - pointer_begin;
+    kwsysProcess_ptrdiff_t n = pointer_end - pointer_begin;
     memcpy(newCommand, pointer_begin, sizeof(char*)*n);
     newCommand[n] = 0;
     }
diff --git a/kwsysPlatformTestsC.c b/kwsysPlatformTestsC.c
index 36c5f4d..58b22f3 100644
--- a/kwsysPlatformTestsC.c
+++ b/kwsysPlatformTestsC.c
@@ -1,5 +1,7 @@
-/* Macros to define main() in a cross-platform way.
-   Usage:
+/*
+  Macros to define main() in a cross-platform way.
+
+  Usage:
 
     int KWSYS_PLATFORM_TEST_C_MAIN()
     {
@@ -23,3 +25,27 @@
 # define KWSYS_PLATFORM_TEST_C_MAIN_ARGS(argc, argv) \
   main(int argc, char* argv[])
 #endif
+
+/*--------------------------------------------------------------------------*/
+#ifdef TEST_KWSYS_C_HAS_PTRDIFF_T
+#include <stddef.h>
+int f(ptrdiff_t n) { return n > 0; }
+int KWSYS_PLATFORM_TEST_C_MAIN()
+{
+  char* p = 0;
+  ptrdiff_t d = p - p;
+  (void)d;
+  return f(p - p);
+}
+#endif
+
+/*--------------------------------------------------------------------------*/
+#ifdef TEST_KWSYS_C_HAS_SSIZE_T
+#include <unistd.h>
+int f(ssize_t n) { return (int)n; }
+int KWSYS_PLATFORM_TEST_C_MAIN()
+{
+  ssize_t n = 0;
+  return f(n);
+}
+#endif
-- 
GitLab