]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
build: tidy up and simplify `setmode()` detection and use
authorViktor Szakats <commit@vsz.me>
Sat, 7 Feb 2026 16:57:39 +0000 (17:57 +0100)
committerViktor Szakats <commit@vsz.me>
Sun, 8 Feb 2026 14:47:47 +0000 (15:47 +0100)
- move macro to `curl_setup.h` (from curlx), and rename.
  It's required by src, test servers, libtests. Also used by unit/tunit,
  (which is fixable but this patch doesn't touch it.)
- special-case it for Windows/Cygwin/MS-DOS.
- build: drop `setmode()`/`_setmode()` detection.
  This also avoids detecting the different `setmode()` on BSDs,
  and a lot of complexity and overhead.
- use `CURL_O_BINARY`.

Follow-up to 250d613763dfc29f73010696ee7948f19d07dba9 #15787
Follow-up to 5e70566094463b8463d4dc1bec9b66763a761c76 #15169

Closes #20539

19 files changed:
CMake/unix-cache.cmake
CMake/win32-cache.cmake
CMakeLists.txt
acinclude.m4
configure.ac
lib/Makefile.inc
lib/config-win32.h
lib/curl_config-cmake.h.in
lib/curl_setup.h
lib/curlx/binmode.h [deleted file]
lib/curlx/curlx.h
src/Makefile.inc
src/tool_formparse.c
src/tool_getparam.c
src/tool_operate.c
tests/libtest/first.c
tests/server/mqttd.c
tests/server/sockfilt.c
tests/server/socksd.c

index 1c6add8397ea78d8117cfda37f89bb06c81d34c8..bdde5f6a3d217d97a98b0de0d2917d5ee32bc662 100644 (file)
@@ -223,12 +223,6 @@ else()
 endif()
 set(HAVE_SENDMSG 1)
 set(HAVE_SETLOCALE 1)
-if(CYGWIN OR
-   CMAKE_SYSTEM_NAME STREQUAL "Linux")
-  set(HAVE_SETMODE 0)
-else()
-  set(HAVE_SETMODE 1)
-endif()
 set(HAVE_SETRLIMIT 1)
 set(HAVE_SETSOCKOPT_SO_NONBLOCK 0)
 set(HAVE_SIGACTION 1)
@@ -305,8 +299,5 @@ set(HAVE_UTIME 1)
 set(HAVE_UTIMES 1)
 set(HAVE_UTIME_H 1)
 set(HAVE_WRITABLE_ARGV 1)
-if(CYGWIN)
-  set(HAVE__SETMODE 1)
-endif()
 set(STDC_HEADERS 1)
 set(USE_UNIX_SOCKETS 1)
index c4ee6fd1b9b471aab9a264974b31d41ac29217e0..218d873b7dfad769eac2ca7ae55edbbafb022f5e 100644 (file)
@@ -144,7 +144,6 @@ set(HAVE_SEND 1)
 set(HAVE_SENDMMSG 0)
 set(HAVE_SENDMSG 0)
 set(HAVE_SETLOCALE 1)
-set(HAVE_SETMODE 1)
 set(HAVE_SETRLIMIT 0)
 set(HAVE_SETSOCKOPT_SO_NONBLOCK 0)
 set(HAVE_SIGACTION 0)
@@ -173,7 +172,6 @@ set(HAVE_TERMIO_H 0)
 set(HAVE_TIME_T_UNSIGNED 0)
 set(HAVE_UTIME 1)
 set(HAVE_UTIMES 0)
-set(HAVE__SETMODE 1)
 set(STDC_HEADERS 1)
 
 # Types and sizes
index e8e2897ef1fa9f93ebe923de65f554460884435c..dfcaf6a1525b3cd96bac5950bef9c0ef07a7e489 100644 (file)
@@ -1617,11 +1617,6 @@ if(NOT WIN32)
   check_symbol_exists("strcmpi"           "string.h" HAVE_STRCMPI)
 endif()
 
-check_function_exists("setmode" HAVE_SETMODE)
-if(WIN32 OR CYGWIN)
-  check_function_exists("_setmode" HAVE__SETMODE)
-endif()
-
 if(AMIGA)
   check_symbol_exists("CloseSocket" "${CURL_INCLUDES}" HAVE_CLOSESOCKET_CAMEL)  # sys/socket.h proto/bsdsocket.h
 endif()
index 638b04adf687a155e8f7aa3b9a4609e1edf589b9..0f88afd2dedb63389df1bbb53aecdcfca700b4dc 100644 (file)
@@ -1451,9 +1451,9 @@ AC_DEFUN([CURL_PREPARE_BUILDINFO], [
   if test "$curl_cv_winuwp" = "yes"; then
     curl_pflags="${curl_pflags} UWP"
   fi
-  if test "$curl_cv_cygwin" = "yes"; then
-    curl_pflags="${curl_pflags} CYGWIN"
-  fi
+  case $host_os in
+    cygwin*|msys*) curl_pflags="${curl_pflags} CYGWIN";;
+  esac
   case $host_os in
     msdos*) curl_pflags="${curl_pflags} DOS";;
     amiga*) curl_pflags="${curl_pflags} AMIGA";;
index 827f5f17a05dc72fbfef43f5cf9b614a5664e61c..7697cdb154ffe2b74ebb6d6182e22b54ee4c8d47 100644 (file)
@@ -697,11 +697,6 @@ if test "$curl_cv_apple" = "yes"; then
   CURL_SUPPORTS_BUILTIN_AVAILABLE
 fi
 
-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}"])
 
@@ -4207,11 +4202,6 @@ if test "$curl_cv_native_windows" != "yes"; then
   CURL_CHECK_FUNC_STRICMP
 fi
 
-AC_CHECK_FUNCS([setmode])
-if test "$curl_cv_native_windows" = "yes" || test "$curl_cv_cygwin" = "yes"; then
-  AC_CHECK_FUNCS([_setmode])
-fi
-
 if test -z "$ssl_backends"; then
   AC_CHECK_FUNCS([arc4random])
 fi
index e5f7457e4306f769ff639e7a19586b8a4f422aa5..08145210eb670c6e7a904df18bb535dc92978c01 100644 (file)
@@ -45,7 +45,6 @@ LIB_CURLX_CFILES =      \
 
 LIB_CURLX_HFILES =      \
   curlx/base64.h        \
-  curlx/binmode.h       \
   curlx/basename.h      \
   curlx/curlx.h         \
   curlx/dynbuf.h        \
index d6a045d22ea0a4545d511de618b8deac8f2d9c44..25409d02f3ed860276b5ec3550189252acb5e5c1 100644 (file)
 /* Define if you have the setlocale function. */
 #define HAVE_SETLOCALE 1
 
-/* 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 9f9c60015e95ead23545675df77b8f22c6e4cd49..e35b372ff198ee14bd005152ee0b0b001c94db95 100644 (file)
 /* Define to 1 if you have the `setlocale' function. */
 #cmakedefine HAVE_SETLOCALE 1
 
-/* 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 0402e733067a75a2f2607219e66d0e2c4922825e..5f02d7a32f8b8518e6c7d6ef112d0997c5be3cdd 100644 (file)
 
 /* Since O_BINARY is used in bitmasks, setting it to zero makes it usable in
    source code but yet it does not ruin anything */
-#ifdef _O_BINARY  /* for _WIN32 */
+#ifdef _O_BINARY  /* for _WIN32 || MSDOS */
 #define CURL_O_BINARY _O_BINARY
-#elif defined(O_BINARY)
+#elif defined(O_BINARY)  /* __CYGWIN__ */
 #define CURL_O_BINARY O_BINARY
 #else
 #define CURL_O_BINARY 0
 #endif
 
+/* Requires io.h when available */
+#ifdef MSDOS
+#define CURL_BINMODE(stream) (void)setmode(fileno(stream), CURL_O_BINARY)
+#elif defined(_WIN32) || defined(__CYGWIN__)
+#define CURL_BINMODE(stream) (void)_setmode(fileno(stream), CURL_O_BINARY)
+#else
+#define CURL_BINMODE(stream) (void)stream
+#endif
+
 /* In Windows the default file mode is text but an application can override it.
 Therefore we specify it explicitly. https://github.com/curl/curl/pull/258
 */
diff --git a/lib/curlx/binmode.h b/lib/curlx/binmode.h
deleted file mode 100644 (file)
index 991cc89..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-#ifndef HEADER_CURL_TOOL_BINMODE_H
-#define HEADER_CURL_TOOL_BINMODE_H
-/***************************************************************************
- *                                  _   _ ____  _
- *  Project                     ___| | | |  _ \| |
- *                             / __| | | | |_) | |
- *                            | (__| |_| |  _ <| |___
- *                             \___|\___/|_| \_\_____|
- *
- * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
- *
- * This software is licensed as described in the file COPYING, which
- * you should have received as part of this distribution. The terms
- * are also available at https://curl.se/docs/copyright.html.
- *
- * You may opt to use, copy, modify, merge, publish, distribute and/or sell
- * copies of the Software, and permit persons to whom the Software is
- * furnished to do so, under the terms of the COPYING file.
- *
- * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
- * KIND, either express or implied.
- *
- * SPDX-License-Identifier: curl
- *
- ***************************************************************************/
-#include "../curl_setup.h"
-
-#if (defined(HAVE_SETMODE) || defined(HAVE__SETMODE)) && defined(O_BINARY)
-/* Requires io.h and/or fcntl.h when available */
-#ifdef HAVE__SETMODE
-#  define CURLX_SET_BINMODE(stream)  (void)_setmode(fileno(stream), O_BINARY)
-#else
-#  define CURLX_SET_BINMODE(stream)  (void)setmode(fileno(stream), O_BINARY)
-#endif
-#else
-#  define CURLX_SET_BINMODE(stream)  (void)stream
-#endif
-
-#endif /* HEADER_CURL_TOOL_BINMODE_H */
index 80da456f57e993efd662d72d542eb5130e37872d..1ee35fa1a029667d290f599fbdcc57264d3b4a0f 100644 (file)
@@ -33,7 +33,6 @@
 
 #include "base64.h" /* for curlx_base64* */
 #include "basename.h" /* for curlx_basename() */
-#include "binmode.h" /* for macro CURLX_SET_BINMODE() */
 #include "dynbuf.h" /* for curlx_dyn_*() */
 #include "fopen.h" /* for curlx_f*() */
 #include "inet_ntop.h" /* for curlx_inet_ntop() */
index 92cfa1467ca46222150a83b8c8e3af172f33a20e..fe2bae5c32e35054c8b688d0c6efbaae48f39d6b 100644 (file)
@@ -54,7 +54,6 @@ CURLX_HFILES = \
   ../lib/curl_setup.h \
   ../lib/curlx/base64.h \
   ../lib/curlx/basename.h \
-  ../lib/curlx/binmode.h \
   ../lib/curlx/dynbuf.h \
   ../lib/curlx/fopen.h \
   ../lib/curlx/multibyte.h \
index 11ef318b3e6b50541fe37c4bbf5fd81a045cf35c..0ec434e2def65be84c10bd3341c07023bd8aedd4 100644 (file)
@@ -124,7 +124,7 @@ static struct tool_mime *tool_mime_new_filedata(struct tool_mime *parent,
     curl_off_t origin;
     curlx_struct_stat sbuf;
 
-    CURLX_SET_BINMODE(stdin);
+    CURL_BINMODE(stdin);
     origin = ftell(stdin);
     /* If stdin is a regular file, do not buffer data but read it
        when needed. */
index 543c4083dea1b9332eb89317d31546671b6b2184..86ebed13e8a4e9a1ee673edfe126f998e710b8bd 100644 (file)
@@ -675,7 +675,7 @@ static ParameterError data_urlencode(const char *nextarg,
     /* a '@' letter, it means that a filename or - (stdin) follows */
     if(!strcmp("-", p)) {
       file = stdin;
-      CURLX_SET_BINMODE(stdin);
+      CURL_BINMODE(stdin);
     }
     else {
       file = curlx_fopen(p, "rb");
@@ -949,7 +949,7 @@ static ParameterError set_data(cmdline_t cmd,
     if(!strcmp("-", nextarg)) {
       file = stdin;
       if(cmd == C_DATA_BINARY) /* forced data-binary */
-        CURLX_SET_BINMODE(stdin);
+        CURL_BINMODE(stdin);
     }
     else {
       file = curlx_fopen(nextarg, "rb");
index 5b7dd231a5481f1455089190d2202687840ff879..73be9a7360cca31c9684a96fea36a5507cdedd08 100644 (file)
@@ -910,7 +910,7 @@ static CURLcode etag_store(struct OperationConfig *config,
   }
   else {
     /* always use binary mode for protocol header output */
-    CURLX_SET_BINMODE(etag_save->stream);
+    CURL_BINMODE(etag_save->stream);
   }
   return CURLE_OK;
 }
@@ -923,7 +923,7 @@ static CURLcode setup_headerfile(struct OperationConfig *config,
   if(!strcmp(config->headerfile, "%")) {
     heads->stream = stderr;
     /* use binary mode for protocol header output */
-    CURLX_SET_BINMODE(heads->stream);
+    CURL_BINMODE(heads->stream);
   }
   else if(strcmp(config->headerfile, "-")) {
     FILE *newfile;
@@ -963,7 +963,7 @@ static CURLcode setup_headerfile(struct OperationConfig *config,
   }
   else {
     /* always use binary mode for protocol header output */
-    CURLX_SET_BINMODE(heads->stream);
+    CURL_BINMODE(heads->stream);
   }
   return CURLE_OK;
 }
@@ -1118,7 +1118,7 @@ static void check_stdin_upload(struct OperationConfig *config,
   DEBUGASSERT(per->infdopen == FALSE);
   DEBUGASSERT(per->infd == STDIN_FILENO);
 
-  CURLX_SET_BINMODE(stdin);
+  CURL_BINMODE(stdin);
   if(!strcmp(per->uploadfile, ".")) {
 #if defined(USE_WINSOCK) && !defined(CURL_WINDOWS_UWP)
     /* non-blocking stdin behavior on Windows is challenging
@@ -1350,7 +1350,7 @@ static CURLcode create_single(struct OperationConfig *config,
        !config->use_ascii) {
       /* We get the output to stdout and we have not got the ASCII/text flag,
          then set stdout to be binary */
-      CURLX_SET_BINMODE(stdout);
+      CURL_BINMODE(stdout);
     }
 
     /* explicitly passed to stdout means okaying binary gunk */
index 0a5ee2047f2fe6bc8d7f9cb0cf4cb253f523d23a..eff83aefab61e91ace26dba0daea01d21aee69dd 100644 (file)
@@ -215,7 +215,7 @@ int main(int argc, const char **argv)
   const char *env;
   size_t tmp;
 
-  CURLX_SET_BINMODE(stdout);
+  CURL_BINMODE(stdout);
 
   memory_tracking_init();
 #ifdef _WIN32
index 02ee592042776f36b3a51bb320b36114d7e18d88..05e58d28bb18bf7aca14c2575e390576a6636583 100644 (file)
@@ -824,9 +824,9 @@ static int test_mqttd(int argc, const char *argv[])
   snprintf(loglockfile, sizeof(loglockfile), "%s/%s/mqtt-%s.lock",
            logdir, SERVERLOGS_LOCKDIR, ipv_inuse);
 
-  CURLX_SET_BINMODE(stdin);
-  CURLX_SET_BINMODE(stdout);
-  CURLX_SET_BINMODE(stderr);
+  CURL_BINMODE(stdin);
+  CURL_BINMODE(stdout);
+  CURL_BINMODE(stderr);
 
   install_signal_handlers(FALSE);
 
index 39b02c5ce476f1aee87f3153df2001c8364d9c62..9bb6c41847d0ff4a054643034e76cfaeb93d70e0 100644 (file)
@@ -1290,9 +1290,9 @@ static int test_sockfilt(int argc, const char *argv[])
     }
   }
 
-  CURLX_SET_BINMODE(stdin);
-  CURLX_SET_BINMODE(stdout);
-  CURLX_SET_BINMODE(stderr);
+  CURL_BINMODE(stdin);
+  CURL_BINMODE(stdout);
+  CURL_BINMODE(stderr);
 
   install_signal_handlers(false);
 
index bdd89c47436334291594b6b5251fac3e10d7b7ff..85001340c20b00648ef143aeaf7e50a71b76e0dc 100644 (file)
@@ -858,9 +858,9 @@ static int test_socksd(int argc, const char *argv[])
     }
   }
 
-  CURLX_SET_BINMODE(stdin);
-  CURLX_SET_BINMODE(stdout);
-  CURLX_SET_BINMODE(stderr);
+  CURL_BINMODE(stdin);
+  CURL_BINMODE(stdout);
+  CURL_BINMODE(stderr);
 
   install_signal_handlers(false);