Fix GCC compile warning for "bad-function-cast" on Windows Trick the compiler by hiding the cast in a function call. * src/os.h: Declare the new function. * src/w32/w32os.c (get_handle_for_fd): Convert and cast a file descriptor into a Windows HANDLE. (check_io_state): Call the new function rather than casting. (osync_release): Ditto. (fd_inherit): Ditto. (fd_noinherit): Ditto. * src/function.c (windows32_openpipe): Ditto. * src/w32/compat/posixfcn.c (isatty): Ditto. * src/w32/subproc/sub_proc.c (process_easy): Ditto.
diff --git a/src/function.c b/src/function.c index 3b355a3..a705c8a 100644 --- a/src/function.c +++ b/src/function.c
@@ -1674,7 +1674,7 @@ return -1; } } - tmpErr = (HANDLE)_get_osfhandle (errfd); + tmpErr = get_handle_for_fd (errfd); if (DuplicateHandle (GetCurrentProcess (), tmpErr, GetCurrentProcess (), &hErr, 0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE)
diff --git a/src/os.h b/src/os.h index cce9d34..888e116 100644 --- a/src/os.h +++ b/src/os.h
@@ -156,3 +156,8 @@ #else int get_bad_stdin (void); #endif + +#if MK_OS_W32 +#include <windows.h> /* Needed for HANDLE */ +HANDLE get_handle_for_fd (int); +#endif
diff --git a/src/w32/compat/posixfcn.c b/src/w32/compat/posixfcn.c index 72fe5f2..7ad0fe0 100644 --- a/src/w32/compat/posixfcn.c +++ b/src/w32/compat/posixfcn.c
@@ -22,6 +22,7 @@ #include <stdarg.h> #include <errno.h> #include <windows.h> +#include "os.h" #include "dlfcn.h" @@ -125,7 +126,7 @@ int isatty (int fd) { - HANDLE fh = (HANDLE) _get_osfhandle (fd); + HANDLE fh = get_handle_for_fd (fd); DWORD con_mode; if (fh == INVALID_HANDLE_VALUE)
diff --git a/src/w32/subproc/sub_proc.c b/src/w32/subproc/sub_proc.c index 8cfa533..f5d7fe0 100644 --- a/src/w32/subproc/sub_proc.c +++ b/src/w32/subproc/sub_proc.c
@@ -1481,7 +1481,7 @@ } } if (outfd >= 0) - tmpOut = (HANDLE)_get_osfhandle (outfd); + tmpOut = get_handle_for_fd (outfd); else tmpOut = GetStdHandle (STD_OUTPUT_HANDLE); if (DuplicateHandle(GetCurrentProcess(), @@ -1511,7 +1511,7 @@ } } if (errfd >= 0) - tmpErr = (HANDLE)_get_osfhandle (errfd); + tmpErr = get_handle_for_fd (errfd); else tmpErr = GetStdHandle(STD_ERROR_HANDLE); if (DuplicateHandle(GetCurrentProcess(),
diff --git a/src/w32/w32os.c b/src/w32/w32os.c index 9d6d184..180f421 100644 --- a/src/w32/w32os.c +++ b/src/w32/w32os.c
@@ -43,10 +43,10 @@ /* Could have used GetHandleInformation, but that isn't supported on Windows 9X. */ - outfd = (HANDLE)_get_osfhandle (fileno (stdout)); - errfd = (HANDLE)_get_osfhandle (fileno (stderr)); + outfd = get_handle_for_fd (fileno (stdout)); + errfd = get_handle_for_fd (fileno (stderr)); - if ((HANDLE)_get_osfhandle (fileno (stdin)) != INVALID_HANDLE_VALUE) + if (get_handle_for_fd (fileno (stdin)) != INVALID_HANDLE_VALUE) state |= IO_STDIN_OK; if (outfd != INVALID_HANDLE_VALUE) state |= IO_STDOUT_OK; @@ -497,21 +497,31 @@ void fd_inherit(int fd) { - HANDLE fh = (HANDLE)_get_osfhandle(fd); + HANDLE fh = get_handle_for_fd (fd); if (fh && fh != INVALID_HANDLE_VALUE) - SetHandleInformation(fh, HANDLE_FLAG_INHERIT, 1); + SetHandleInformation (fh, HANDLE_FLAG_INHERIT, 1); } void fd_noinherit(int fd) { - HANDLE fh = (HANDLE)_get_osfhandle(fd); + HANDLE fh = get_handle_for_fd (fd); if (fh && fh != INVALID_HANDLE_VALUE) - SetHandleInformation(fh, HANDLE_FLAG_INHERIT, 0); + SetHandleInformation (fh, HANDLE_FLAG_INHERIT, 0); } void fd_set_append (int fd UNUSED) {} + + +HANDLE +get_handle_for_fd (int fd) +{ + /* This funcion call is needed to get around the "bad-function-cast"-warning + emitted by GCC when casting and assigning in the same statement. */ + intptr_t fh = _get_osfhandle (fd); + return (HANDLE) fh; +}