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 250d613763 #15787
Follow-up to 5e70566094 #15169

Closes #20539
This commit is contained in:
Viktor Szakats 2026-02-07 17:57:39 +01:00
parent 2c0019b085
commit cdfc8dc7ad
No known key found for this signature in database
19 changed files with 32 additions and 103 deletions

View 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)

View 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

View 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()

View 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";;

View 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

View 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 \

View File

@ -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

View File

@ -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

View File

@ -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
*/

View File

@ -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 */

View 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() */

View 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 \

View 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. */

View 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");

View 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 */

View 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

View 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);

View 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);

View 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);