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

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