]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
build: detect and use `_setmode()` with Cygwin/MSYS, also use on Windows
authorViktor Szakats <commit@vsz.me>
Sun, 6 Oct 2024 13:41:48 +0000 (15:41 +0200)
committerViktor Szakats <commit@vsz.me>
Mon, 7 Oct 2024 10:31:21 +0000 (12:31 +0200)
Before this patch `setmode()` was not detected with Cygwin/MSYS, because
it's a macro, not a function, and detection is looking for a function.

Switching to symbol detection doesn't work because it mis-detects it on
BSD systems which features a function with the same name but different
functionality and arguments.

Fix it by looking for a `_setmode()` function on Cygwin/MSYS, and use it
if available.

`_setmode()` is recommended over `setmode()` by Windows documentation so
use that on Windows too. It seems to be available on all supported
compilers, so omit detection.

https://learn.microsoft.com/cpp/c-runtime-library/reference/posix-setmode
https://learn.microsoft.com/cpp/c-runtime-library/reference/setmode

Officially Windows requires argument `_O_BINARY` with an underscore.
`O_BINARY` is also supported but bound to conditions. Continue to use it
for simplicity. Cygwin supports `O_BINARY` (no underscore).

Closes #15169

CMake/Platforms/WindowsCache.cmake
CMakeLists.txt
acinclude.m4
configure.ac
lib/config-win32.h
lib/curl_config.h.cmake
src/tool_binmode.c
src/tool_binmode.h

index 1cedad3fd79d93cf1e67753fd0a211283b72b485..668bba215a19dc80b1fd0203850ed659a8dabc50 100644 (file)
@@ -105,6 +105,7 @@ set(HAVE_FSETXATTR 0)
 set(HAVE_LIBSOCKET 0)
 set(HAVE_SETLOCALE 1)
 set(HAVE_SETMODE 1)
+set(HAVE__SETMODE 1)
 set(HAVE_GETPEERNAME 1)
 set(HAVE_GETSOCKNAME 1)
 set(HAVE_GETHOSTNAME 1)
index f75c1ee7d7e17f34290913b5fd7c348c28e29eb0..0e2ae7a1b96641756dded023f2d62802e3673aaf 100644 (file)
@@ -1524,6 +1524,10 @@ check_symbol_exists("setlocale"       "${CURL_INCLUDES}" HAVE_SETLOCALE)
 check_symbol_exists("setmode"         "${CURL_INCLUDES}" HAVE_SETMODE)
 check_symbol_exists("setrlimit"       "${CURL_INCLUDES}" HAVE_SETRLIMIT)
 
+if(WIN32 OR CYGWIN)
+  check_function_exists("_setmode" HAVE__SETMODE)
+endif()
+
 if(NOT _ssl_enabled)
   check_symbol_exists("arc4random" "${CURL_INCLUDES};stdlib.h" HAVE_ARC4RANDOM)
 endif()
index df893e05b82b97da6dd6ade3a5dab338735b1498..9afc1774dae429e977869dcde867993e51ccbf47 100644 (file)
@@ -1553,9 +1553,9 @@ AC_DEFUN([CURL_PREPARE_BUILDINFO], [
         curl_pflags="${curl_pflags} UNIX";;
     esac
   fi
-  case $host_os in
-    cygwin*|msys*) curl_pflags="${curl_pflags} CYGWIN";;
-  esac
+  if test "$curl_cv_cygwin" = 'yes'; then
+    curl_pflags="${curl_pflags} CYGWIN"
+  fi
   case $host_os in
     msys*) curl_pflags="${curl_pflags} MSYS";;
   esac
index e5998a106283444fd12a3dbeab8efc88448f49c9..9bf2f51b3faad31c5f9ffa5fe38854a4e758c21d 100644 (file)
@@ -605,6 +605,11 @@ case $host in
     ;;
 esac
 
+curl_cv_cygwin='no'
+case $host_os in
+  cygwin*|msys*) curl_cv_cygwin='yes';;
+esac
+
 AM_CONDITIONAL([HAVE_WINDRES],
   [test "$curl_cv_native_windows" = "yes" && test -n "${RC}"])
 
@@ -4120,6 +4125,10 @@ AC_CHECK_FUNCS([\
   utimes \
 ])
 
+if test "$curl_cv_native_windows" = 'yes' -o "$curl_cv_cygwin" = 'yes'; then
+  AC_CHECK_FUNCS([_setmode])
+fi
+
 if test -z "$ssl_backends"; then
   AC_CHECK_FUNCS([arc4random])
 fi
index b855e5c635f75fb7f45bfd8d4196e1c9529cbca4..c8fdf6df09585e51a7f826b4d2b7dc12c7d3fc41 100644 (file)
 /* Define if you have the setmode function. */
 #define HAVE_SETMODE 1
 
+/* Define if you have the _setmode function. */
+#define HAVE__SETMODE 1
+
 /* Define if you have the socket function. */
 #define HAVE_SOCKET 1
 
index dd806da9bca3bbbdf7b5e2d19f36453fb9868ee1..e69fc50073de31ac6deb6de659e2121591f5e73a 100644 (file)
 /* Define to 1 if you have the `setmode' function. */
 #cmakedefine HAVE_SETMODE 1
 
+/* Define to 1 if you have the `_setmode' function. */
+#cmakedefine HAVE__SETMODE 1
+
 /* Define to 1 if you have the `setrlimit' function. */
 #cmakedefine HAVE_SETRLIMIT 1
 
index e27ce9663c9b8402ecb8ce713b76cf565746b669..a92596416ec23e30a90a6af9045615737a1152fc 100644 (file)
@@ -23,7 +23,7 @@
  ***************************************************************************/
 #include "tool_setup.h"
 
-#ifdef HAVE_SETMODE
+#if defined(HAVE_SETMODE) || defined(HAVE__SETMODE)
 
 #ifdef HAVE_IO_H
 #  include <io.h>
@@ -42,6 +42,8 @@ void set_binmode(FILE *stream)
 #ifdef O_BINARY
 #  ifdef __HIGHC__
   _setmode(stream, O_BINARY);
+#  elif defined(HAVE__SETMODE)
+  (void)_setmode(fileno(stream), O_BINARY);
 #  else
   (void)setmode(fileno(stream), O_BINARY);
 #  endif
@@ -50,4 +52,4 @@ void set_binmode(FILE *stream)
 #endif
 }
 
-#endif /* HAVE_SETMODE */
+#endif /* HAVE_SETMODE || HAVE__SETMODE */
index bee837b0001b295b220a67058abb8b80b8d89cf5..ce2f589e0ffcb16625bc56f12c9900cd41aa1d52 100644 (file)
@@ -25,7 +25,7 @@
  ***************************************************************************/
 #include "tool_setup.h"
 
-#ifdef HAVE_SETMODE
+#if defined(HAVE_SETMODE) || defined(HAVE__SETMODE)
 
 void set_binmode(FILE *stream);
 
@@ -33,6 +33,6 @@ void set_binmode(FILE *stream);
 
 #define set_binmode(x) Curl_nop_stmt
 
-#endif /* HAVE_SETMODE */
+#endif /* HAVE_SETMODE || HAVE__SETMODE */
 
 #endif /* HEADER_CURL_TOOL_BINMODE_H */