From 60654fc501c573144286dcbf7090945966aadea8 Mon Sep 17 00:00:00 2001
From: Orgad Shaneh <orgads@gmail.com>
Date: Thu, 28 Dec 2017 09:28:38 +0200
Subject: [PATCH 3/5] Fix warnings on MinGW/GCC7

---
 src/commands.c             |  4 ++--
 src/function.c             |  8 ++++----
 src/getopt.c               |  5 +++++
 src/job.c                  |  6 ++++--
 src/main.c                 |  6 +++---
 src/output.c               |  2 ++
 src/w32/compat/posixfcn.c  | 11 ++++++-----
 src/w32/subproc/sub_proc.c | 14 +++++++-------
 src/w32/subproc/w32err.c   |  2 +-
 src/w32/w32os.c            | 15 +++++++++------
 10 files changed, 43 insertions(+), 30 deletions(-)

diff --git a/src/commands.c b/src/commands.c
index 75a9d2d..608a24c 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -515,12 +515,12 @@ fatal_error_signal (int sig)
       DWORD susp_count = SuspendThread (main_thread);
 
       if (susp_count != 0)
-        fprintf (stderr, "SuspendThread: suspend count = %ld\n", susp_count);
+        fprintf (stderr, "SuspendThread: suspend count = %lu\n", susp_count);
       else if (susp_count == (DWORD)-1)
         {
           DWORD ierr = GetLastError ();
 
-          fprintf (stderr, "SuspendThread: error %ld: %s\n",
+          fprintf (stderr, "SuspendThread: error %lu: %s\n",
                    ierr, map_windows32_error_to_string (ierr));
         }
     }
diff --git a/src/function.c b/src/function.c
index a22352f..1535cca 100644
--- a/src/function.c
+++ b/src/function.c
@@ -1530,11 +1530,11 @@ windows32_openpipe (int *pipedes, int errfd, pid_t *pid_p, char **command_argv,
       if (hIn == INVALID_HANDLE_VALUE)
         {
           ON (error, NILF,
-              _("windows32_openpipe: DuplicateHandle(In) failed (e=%ld)\n"), e);
+              _("windows32_openpipe: DuplicateHandle(In) failed (e=%lu)\n"), e);
           return -1;
         }
     }
-  tmpErr = (HANDLE)_get_osfhandle (errfd);
+  tmpErr = (HANDLE)(intptr_t)_get_osfhandle (errfd);
   if (DuplicateHandle (GetCurrentProcess (), tmpErr,
                        GetCurrentProcess (), &hErr,
                        0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE)
@@ -1554,14 +1554,14 @@ windows32_openpipe (int *pipedes, int errfd, pid_t *pid_p, char **command_argv,
       if (hErr == INVALID_HANDLE_VALUE)
         {
           ON (error, NILF,
-              _("windows32_openpipe: DuplicateHandle(Err) failed (e=%ld)\n"), e);
+              _("windows32_openpipe: DuplicateHandle(Err) failed (e=%lu)\n"), e);
           return -1;
         }
     }
 
   if (! CreatePipe (&hChildOutRd, &hChildOutWr, &saAttr, 0))
     {
-      ON (error, NILF, _("CreatePipe() failed (e=%ld)\n"), GetLastError());
+      ON (error, NILF, _("CreatePipe() failed (e=%lu)\n"), GetLastError());
       return -1;
     }
 
diff --git a/src/getopt.c b/src/getopt.c
index 5d4b89e..be212d7 100644
--- a/src/getopt.c
+++ b/src/getopt.c
@@ -434,6 +434,9 @@ _getopt_initialize (int argc, char *const *argv, const char *optstring)
     }
   else
     nonoption_flags_len = 0;
+#else
+  (void)argc;
+  (void)argv;
 #endif
 
   return optstring;
@@ -677,6 +680,7 @@ _getopt_internal (int argc, char *const *argv, const char *optstring,
 	      else
 		{
 		  if (opterr)
+		   {
 		   if (argv[optind - 1][1] == '-')
 		    /* --option */
 		    fprintf (stderr,
@@ -687,6 +691,7 @@ _getopt_internal (int argc, char *const *argv, const char *optstring,
 		    fprintf (stderr,
 		     _("%s: option '%c%s' doesn't allow an argument\n"),
 		     argv[0], argv[optind - 1][0], pfound->name);
+		   }
 
 		  nextchar += strlen (nextchar);
 
diff --git a/src/job.c b/src/job.c
index a13aada..e02fc0e 100644
--- a/src/job.c
+++ b/src/job.c
@@ -364,7 +364,7 @@ create_batch_file (char const *base, int unixy, int *fd)
   *fd = -1;
   if (error_string == NULL)
     error_string = _("Cannot create a temporary file\n");
-  O (fatal, NILF, error_string);
+  OS (fatal, NILF, "%s", error_string);
 
   /* not reached */
   return NULL;
@@ -708,7 +708,9 @@ reap_children (int block, int err)
       else if (pid < 0)
         {
           /* A remote status command failed miserably.  Punt.  */
+#if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
         remote_status_lose:
+#endif
           pfatal_with_name ("remote_status");
         }
       else
@@ -816,7 +818,7 @@ reap_children (int block, int err)
                   {
                     DWORD e = GetLastError ();
                     fprintf (stderr,
-                             "Determine main thread ID (Error %ld: %s)\n",
+                             "Determine main thread ID (Error %lu: %s)\n",
                              e, map_windows32_error_to_string (e));
                   }
                 else
diff --git a/src/main.c b/src/main.c
index 9fa2c84..e538727 100644
--- a/src/main.c
+++ b/src/main.c
@@ -853,7 +853,7 @@ handle_runtime_exceptions (struct _EXCEPTION_POINTERS *exinfo)
       sprintf (errmsg,
                _("%s: Interrupt/Exception caught (code = 0x%lx, addr = 0x%p)\n"),
                prg, exrec->ExceptionCode, exrec->ExceptionAddress);
-      fprintf (stderr, errmsg);
+      fprintf (stderr, "%s", errmsg);
       exit (255);
     }
 
@@ -892,7 +892,7 @@ handle_runtime_exceptions (struct _EXCEPTION_POINTERS *exinfo)
 #endif
 
   /* Write the error to stderr too */
-  fprintf (stderr, errmsg);
+  fprintf (stderr, "%s", errmsg);
 
 #ifdef DEBUG
   return EXCEPTION_CONTINUE_SEARCH;
@@ -1521,7 +1521,7 @@ main (int argc, char **argv, char **envp)
 #ifdef WINDOWS32
   if (suspend_flag)
     {
-      fprintf (stderr, "%s (pid = %ld)\n", argv[0], GetCurrentProcessId ());
+      fprintf (stderr, "%s (pid = %lu)\n", argv[0], GetCurrentProcessId ());
       fprintf (stderr, _("%s is suspending for 30 seconds..."), argv[0]);
       Sleep (30 * 1000);
       fprintf (stderr, _("done sleep(30). Continuing.\n"));
diff --git a/src/output.c b/src/output.c
index 93b3dc7..5a5ff91 100644
--- a/src/output.c
+++ b/src/output.c
@@ -177,6 +177,8 @@ set_append_mode (int fd)
       int r;
       EINTRLOOP(r, fcntl (fd, F_SETFL, flags | O_APPEND));
     }
+#else
+  (void)fd;
 #endif
 }
 
diff --git a/src/w32/compat/posixfcn.c b/src/w32/compat/posixfcn.c
index 7b9a577..fac4535 100644
--- a/src/w32/compat/posixfcn.c
+++ b/src/w32/compat/posixfcn.c
@@ -183,7 +183,7 @@ create_mutex (void)
   secattr.lpSecurityDescriptor = NULL; /* use default security descriptor */
   secattr.bInheritHandle = TRUE;
 
-  hmutex = (intptr_t)CreateMutex (&secattr, FALSE, NULL);
+  hmutex = (intptr_t)(HANDLE)CreateMutex (&secattr, FALSE, NULL);
   if (!hmutex)
     {
       DWORD err = GetLastError ();
@@ -202,8 +202,8 @@ create_mutex (void)
 int
 same_stream (FILE *f1, FILE *f2)
 {
-  HANDLE fh1 = (HANDLE)_get_osfhandle (fileno (f1));
-  HANDLE fh2 = (HANDLE)_get_osfhandle (fileno (f2));
+  HANDLE fh1 = (HANDLE)(intptr_t)_get_osfhandle (fileno (f1));
+  HANDLE fh2 = (HANDLE)(intptr_t)_get_osfhandle (fileno (f2));
 
   /* Invalid file descriptors get treated as different streams.  */
   if (fh1 && fh1 != INVALID_HANDLE_VALUE
@@ -460,7 +460,7 @@ dlclose (void *handle)
 int
 isatty (int fd)
 {
-  HANDLE fh = (HANDLE) _get_osfhandle (fd);
+  HANDLE fh = (HANDLE)(intptr_t)_get_osfhandle (fd);
   DWORD con_mode;
 
   if (fh == INVALID_HANDLE_VALUE)
@@ -478,9 +478,10 @@ isatty (int fd)
 char *
 ttyname (int fd)
 {
+  (void)fd;
   /* This "knows" that Make only asks about stdout and stderr.  A more
      sophisticated implementation should test whether FD is open for
      input or output.  We can do that by looking at the mode returned
      by GetConsoleMode.  */
-  return "CONOUT$";
+  return (char*)"CONOUT$";
 }
diff --git a/src/w32/subproc/sub_proc.c b/src/w32/subproc/sub_proc.c
index e8552be..8d6ed81 100644
--- a/src/w32/subproc/sub_proc.c
+++ b/src/w32/subproc/sub_proc.c
@@ -108,7 +108,7 @@ DWORD process_wait_for_multiple_objects(
 	    continue;
 	    break;
 	  case WAIT_FAILED:
-	    fprintf(stderr,"WaitForMultipleOjbects failed waiting with error %d\n", GetLastError());
+	    fprintf(stderr,"WaitForMultipleOjbects failed waiting with error %lu\n", GetLastError());
 	    break;
 	  default:
 	    if (retVal >= WAIT_ABANDONED_0) {
@@ -244,7 +244,7 @@ process_table_full()
   extern int shell_function_pid;
 
   /* Reserve slots for jobserver_semaphore if we have one and the shell function if not active */
-  return(proc_index >= GMAKE_MAXIMUM_WAIT_OBJECTS - jobserver_enabled() - (shell_function_pid == 0));
+  return(proc_index >= (int)(GMAKE_MAXIMUM_WAIT_OBJECTS - jobserver_enabled() - (shell_function_pid == 0)));
 }
 
 /*
@@ -1476,12 +1476,12 @@ process_easy(
         CloseHandle(tmpIn);
     }
     if (hIn == INVALID_HANDLE_VALUE) {
-      fprintf(stderr, "process_easy: DuplicateHandle(In) failed (e=%ld)\n", e);
+      fprintf(stderr, "process_easy: DuplicateHandle(In) failed (e=%lu)\n", e);
       return INVALID_HANDLE_VALUE;
     }
   }
   if (outfd >= 0)
-    tmpOut = (HANDLE)_get_osfhandle (outfd);
+    tmpOut = (HANDLE)(intptr_t)_get_osfhandle (outfd);
   else
     tmpOut = GetStdHandle (STD_OUTPUT_HANDLE);
   if (DuplicateHandle(GetCurrentProcess(),
@@ -1506,12 +1506,12 @@ process_easy(
         CloseHandle(tmpOut);
     }
     if (hOut == INVALID_HANDLE_VALUE) {
-      fprintf(stderr, "process_easy: DuplicateHandle(Out) failed (e=%ld)\n", e);
+      fprintf(stderr, "process_easy: DuplicateHandle(Out) failed (e=%lu)\n", e);
       return INVALID_HANDLE_VALUE;
     }
   }
   if (errfd >= 0)
-    tmpErr = (HANDLE)_get_osfhandle (errfd);
+    tmpErr = (HANDLE)(intptr_t)_get_osfhandle (errfd);
   else
     tmpErr = GetStdHandle(STD_ERROR_HANDLE);
   if (DuplicateHandle(GetCurrentProcess(),
@@ -1536,7 +1536,7 @@ process_easy(
         CloseHandle(tmpErr);
     }
     if (hErr == INVALID_HANDLE_VALUE) {
-      fprintf(stderr, "process_easy: DuplicateHandle(Err) failed (e=%ld)\n", e);
+      fprintf(stderr, "process_easy: DuplicateHandle(Err) failed (e=%lu)\n", e);
       return INVALID_HANDLE_VALUE;
     }
   }
diff --git a/src/w32/subproc/w32err.c b/src/w32/subproc/w32err.c
index 16537d2..c1f1fc4 100644
--- a/src/w32/subproc/w32err.c
+++ b/src/w32/subproc/w32err.c
@@ -67,7 +67,7 @@ map_windows32_error_to_string (DWORD ercode) {
                         FreeLibrary(hModule);
                 }
 #else
-                O (fatal, NILF, szMessageBuffer);
+                OS (fatal, NILF, "%s", szMessageBuffer);
 #endif
         } else {
                 /*
diff --git a/src/w32/w32os.c b/src/w32/w32os.c
index 0023e79..f99bd9a 100644
--- a/src/w32/w32os.c
+++ b/src/w32/w32os.c
@@ -57,7 +57,7 @@ jobserver_setup (int slots)
       DWORD err = GetLastError ();
       const char *estr = map_windows32_error_to_string (err);
       ONS (fatal, NILF,
-           _("creating jobserver semaphore: (Error %ld: %s)"), err, estr);
+           _("creating jobserver semaphore: (Error %lu: %s)"), err, estr);
     }
 
   return 1;
@@ -76,7 +76,7 @@ jobserver_parse_auth (const char *auth)
       DWORD err = GetLastError ();
       const char *estr = map_windows32_error_to_string (err);
       fatal (NILF, strlen (auth) + INTSTR_LENGTH + strlen (estr),
-             _("internal error: unable to open jobserver semaphore '%s': (Error %ld: %s)"),
+             _("internal error: unable to open jobserver semaphore '%s': (Error %lu: %s)"),
              auth, err, estr);
     }
   DB (DB_JOBS, (_("Jobserver client (semaphore %s)\n"), auth));
@@ -120,7 +120,7 @@ jobserver_release (int is_fatal)
           DWORD err = GetLastError ();
           const char *estr = map_windows32_error_to_string (err);
           ONS (fatal, NILF,
-               _("release jobserver semaphore: (Error %ld: %s)"), err, estr);
+               _("release jobserver semaphore: (Error %lu: %s)"), err, estr);
         }
       perror_with_name ("release_jobserver_semaphore", "");
     }
@@ -150,10 +150,12 @@ jobserver_signal ()
 
 void jobserver_pre_child (int recursive)
 {
+  (void)recursive;
 }
 
 void jobserver_post_child (int recursive)
 {
+  (void)recursive;
 }
 
 void
@@ -170,6 +172,7 @@ jobserver_acquire (int timeout)
     DWORD dwHandleCount;
     DWORD dwEvent;
 
+    (void)timeout;
     handles = xmalloc(process_table_actual_size() * sizeof(HANDLE));
 
     /* Add jobserver semaphore to first slot. */
@@ -191,7 +194,7 @@ jobserver_acquire (int timeout)
         DWORD err = GetLastError ();
         const char *estr = map_windows32_error_to_string (err);
         ONS (fatal, NILF,
-             _("semaphore or child process wait: (Error %ld: %s)"),
+             _("semaphore or child process wait: (Error %lu: %s)"),
              err, estr);
       }
 
@@ -202,7 +205,7 @@ jobserver_acquire (int timeout)
 void
 fd_inherit(int fd)
 {
-  HANDLE fh = (HANDLE)_get_osfhandle(fd);
+  HANDLE fh = (HANDLE)(intptr_t)_get_osfhandle(fd);
 
   if (fh && fh != INVALID_HANDLE_VALUE)
         SetHandleInformation(fh, HANDLE_FLAG_INHERIT, 1);
@@ -211,7 +214,7 @@ fd_inherit(int fd)
 void
 fd_noinherit(int fd)
 {
-  HANDLE fh = (HANDLE)_get_osfhandle(fd);
+  HANDLE fh = (HANDLE)(intptr_t)_get_osfhandle(fd);
 
   if (fh && fh != INVALID_HANDLE_VALUE)
         SetHandleInformation(fh, HANDLE_FLAG_INHERIT, 0);
-- 
2.15.1.windows.2

