From: Viktor Szakats Date: Sat, 7 Feb 2026 16:57:39 +0000 (+0100) Subject: build: tidy up and simplify `setmode()` detection and use X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cdfc8dc7ad2e573dcfc08f2c5a0a67910ef9c0a6;p=thirdparty%2Fcurl.git build: tidy up and simplify `setmode()` detection and use - 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 --- diff --git a/CMake/unix-cache.cmake b/CMake/unix-cache.cmake index 1c6add8397..bdde5f6a3d 100644 --- a/CMake/unix-cache.cmake +++ b/CMake/unix-cache.cmake @@ -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) diff --git a/CMake/win32-cache.cmake b/CMake/win32-cache.cmake index c4ee6fd1b9..218d873b7d 100644 --- a/CMake/win32-cache.cmake +++ b/CMake/win32-cache.cmake @@ -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 diff --git a/CMakeLists.txt b/CMakeLists.txt index e8e2897ef1..dfcaf6a152 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() diff --git a/acinclude.m4 b/acinclude.m4 index 638b04adf6..0f88afd2de 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -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";; diff --git a/configure.ac b/configure.ac index 827f5f17a0..7697cdb154 100644 --- a/configure.ac +++ b/configure.ac @@ -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 diff --git a/lib/Makefile.inc b/lib/Makefile.inc index e5f7457e43..08145210eb 100644 --- a/lib/Makefile.inc +++ b/lib/Makefile.inc @@ -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 \ diff --git a/lib/config-win32.h b/lib/config-win32.h index d6a045d22e..25409d02f3 100644 --- a/lib/config-win32.h +++ b/lib/config-win32.h @@ -117,12 +117,6 @@ /* 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 diff --git a/lib/curl_config-cmake.h.in b/lib/curl_config-cmake.h.in index 9f9c60015e..e35b372ff1 100644 --- a/lib/curl_config-cmake.h.in +++ b/lib/curl_config-cmake.h.in @@ -484,12 +484,6 @@ /* 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 diff --git a/lib/curl_setup.h b/lib/curl_setup.h index 0402e73306..5f02d7a32f 100644 --- a/lib/curl_setup.h +++ b/lib/curl_setup.h @@ -868,14 +868,23 @@ /* 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 index 991cc89045..0000000000 --- a/lib/curlx/binmode.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef HEADER_CURL_TOOL_BINMODE_H -#define HEADER_CURL_TOOL_BINMODE_H -/*************************************************************************** - * _ _ ____ _ - * Project ___| | | | _ \| | - * / __| | | | |_) | | - * | (__| |_| | _ <| |___ - * \___|\___/|_| \_\_____| - * - * Copyright (C) Daniel Stenberg, , 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 */ diff --git a/lib/curlx/curlx.h b/lib/curlx/curlx.h index 80da456f57..1ee35fa1a0 100644 --- a/lib/curlx/curlx.h +++ b/lib/curlx/curlx.h @@ -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() */ diff --git a/src/Makefile.inc b/src/Makefile.inc index 92cfa1467c..fe2bae5c32 100644 --- a/src/Makefile.inc +++ b/src/Makefile.inc @@ -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 \ diff --git a/src/tool_formparse.c b/src/tool_formparse.c index 11ef318b3e..0ec434e2de 100644 --- a/src/tool_formparse.c +++ b/src/tool_formparse.c @@ -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. */ diff --git a/src/tool_getparam.c b/src/tool_getparam.c index 543c4083de..86ebed13e8 100644 --- a/src/tool_getparam.c +++ b/src/tool_getparam.c @@ -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"); diff --git a/src/tool_operate.c b/src/tool_operate.c index 5b7dd231a5..73be9a7360 100644 --- a/src/tool_operate.c +++ b/src/tool_operate.c @@ -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 */ diff --git a/tests/libtest/first.c b/tests/libtest/first.c index 0a5ee2047f..eff83aefab 100644 --- a/tests/libtest/first.c +++ b/tests/libtest/first.c @@ -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 diff --git a/tests/server/mqttd.c b/tests/server/mqttd.c index 02ee592042..05e58d28bb 100644 --- a/tests/server/mqttd.c +++ b/tests/server/mqttd.c @@ -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); diff --git a/tests/server/sockfilt.c b/tests/server/sockfilt.c index 39b02c5ce4..9bb6c41847 100644 --- a/tests/server/sockfilt.c +++ b/tests/server/sockfilt.c @@ -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); diff --git a/tests/server/socksd.c b/tests/server/socksd.c index bdd89c4743..85001340c2 100644 --- a/tests/server/socksd.c +++ b/tests/server/socksd.c @@ -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);