]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
binmode: convert to macro and use it from tests
authorViktor Szakats <commit@vsz.me>
Wed, 27 Nov 2024 12:52:30 +0000 (13:52 +0100)
committerViktor Szakats <commit@vsz.me>
Sat, 21 Dec 2024 12:29:24 +0000 (13:29 +0100)
And use it from src and tests.

Syncing this functionality between platforms and build targets.

Also: Stop redefining `O_BINARY` in src, and use a local macro with
the same effect. `O_BINARY` is used in `CURL_SET_BINMODE()` to decide
if this functionality is supported, and redefining it makes this check
pass always in unity builds. The check is required for Apple OS, because
it offers a `setmode()` function, successfully detected by both CMake
and autotools, but that function has a different functionality and
signature than that expected by `CURL_SET_BINMODE()`.

Also:
- drop MetaWare High C (MS-DOS) support for set binmode.
- tests/libtest/Makefile.inc: dedupe comments.
- lib/curl_setup_once.h: tidy up feature guards for `io.h`, `fcntl.h`.

Ref: #15652
Closes #15787

18 files changed:
lib/curl_setup_once.h
packages/vms/gnv_link_curl.com
src/Makefile.inc
src/tool_binmode.c [deleted file]
src/tool_binmode.h
src/tool_cb_wrt.c
src/tool_formparse.c
src/tool_getparam.c
src/tool_operate.c
tests/libtest/CMakeLists.txt
tests/libtest/Makefile.am
tests/libtest/Makefile.inc
tests/libtest/first.c
tests/server/CMakeLists.txt
tests/server/Makefile.am
tests/server/mqttd.c
tests/server/sockfilt.c
tests/server/socksd.c

index 32f176451209b807387c8b2fc416f893e056a583..3e1a69ab658b436e78737475497e74dd23013f04 100644 (file)
 #include <sys/time.h>
 #endif
 
-#ifdef _WIN32
+#ifdef HAVE_IO_H
 #include <io.h>
+#endif
+
+#ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
 
index 2acd49a6ad2a78ec23d651d028190316d5460312..7ce9fb934024e615dec899d4d7633dbc47694c1e 100644 (file)
@@ -396,7 +396,7 @@ $   if f$search("[.src]curl.exe") .eqs. ""
 $   then
 $       define/user gnv$libcurl 'gnv_libcurl_share'
 $       link'ldebug'/exe=[.src]curl.exe/dsf=[.src]curl.dsf -
-           [.src]curl-tool_main.o, [.src]curl-tool_binmode.o, -
+           [.src]curl-tool_main.o, -
            [.src]curl-tool_bname.o, [.src]curl-tool_cb_dbg.o, -
            [.src]curl-tool_cb_hdr.o, [.src]curl-tool_cb_prg.o, -
            [.src]curl-tool_cb_rea.o, [.src]curl-tool_cb_see.o, -
index 93bef122c9024a209e6930614b94d3e87bacfefb..cbf6d81c42c7d78bac7731fd7eb0e6cc766e35f0 100644 (file)
@@ -60,7 +60,6 @@ CURLX_HFILES = \
 CURL_CFILES = \
   slist_wc.c \
   terminal.c \
-  tool_binmode.c \
   tool_bname.c \
   tool_cb_dbg.c \
   tool_cb_hdr.c \
diff --git a/src/tool_binmode.c b/src/tool_binmode.c
deleted file mode 100644 (file)
index a925964..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-/***************************************************************************
- *                                  _   _ ____  _
- *  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 "tool_setup.h"
-
-#if defined(HAVE_SETMODE) || defined(HAVE__SETMODE)
-
-#ifdef HAVE_IO_H
-#  include <io.h>
-#endif
-
-#ifdef HAVE_FCNTL_H
-#  include <fcntl.h>
-#endif
-
-#include "tool_binmode.h"
-
-#include "memdebug.h" /* keep this as LAST include */
-
-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
-#else
-  (void)stream;
-#endif
-}
-
-#endif /* HAVE_SETMODE || HAVE__SETMODE */
index ce2f589e0ffcb16625bc56f12c9900cd41aa1d52..0c7e77c838a548d909b4dbadde72a9a8912ebb7d 100644 (file)
  ***************************************************************************/
 #include "tool_setup.h"
 
-#if defined(HAVE_SETMODE) || defined(HAVE__SETMODE)
-
-void set_binmode(FILE *stream);
-
+#if (defined(HAVE_SETMODE) || defined(HAVE__SETMODE)) && defined(O_BINARY)
+/* Requires io.h and/or fcntl.h when available */
+#ifdef HAVE__SETMODE
+#  define CURL_SET_BINMODE(stream)  (void)_setmode(fileno(stream), O_BINARY)
 #else
-
-#define set_binmode(x) Curl_nop_stmt
-
-#endif /* HAVE_SETMODE || HAVE__SETMODE */
+#  define CURL_SET_BINMODE(stream)  (void)setmode(fileno(stream), O_BINARY)
+#endif
+#else
+#  define CURL_SET_BINMODE(stream)  (void)stream; Curl_nop_stmt
+#endif
 
 #endif /* HEADER_CURL_TOOL_BINMODE_H */
index f25a481503479857ecae58499f6aeb112e5e10cb..cbf1f0a52c9d141a8767c9751cb7f7191a97027a 100644 (file)
 
 #include "memdebug.h" /* keep this as LAST include */
 
-#ifndef O_BINARY
-#define O_BINARY 0
+#ifdef O_BINARY
+#define CURL_O_BINARY O_BINARY
+#else
+#define CURL_O_BINARY 0
 #endif
 #ifdef _WIN32
 #define OPENMODE S_IREAD | S_IWRITE
@@ -69,7 +71,7 @@ bool tool_create_output_file(struct OutStruct *outs,
   else {
     int fd;
     do {
-      fd = open(fname, O_CREAT | O_WRONLY | O_EXCL | O_BINARY, OPENMODE);
+      fd = open(fname, O_CREAT | O_WRONLY | O_EXCL | CURL_O_BINARY, OPENMODE);
       /* Keep retrying in the hope that it is not interrupted sometime */
     } while(fd == -1 && errno == EINTR);
     if(config->file_clobber_mode == CLOBBER_NEVER && fd == -1) {
@@ -96,7 +98,8 @@ bool tool_create_output_file(struct OutStruct *outs,
         msnprintf(newname + len + 1, 12, "%d", next_num);
         next_num++;
         do {
-          fd = open(newname, O_CREAT | O_WRONLY | O_EXCL | O_BINARY, OPENMODE);
+          fd = open(newname, O_CREAT | O_WRONLY | O_EXCL | CURL_O_BINARY,
+                             OPENMODE);
           /* Keep retrying in the hope that it is not interrupted sometime */
         } while(fd == -1 && errno == EINTR);
       }
index 814f240e66b18c1b8d820937ca19c25256c49be9..7216233aa47e0a2e8122d50c91f53dcfbca52ccc 100644 (file)
@@ -106,7 +106,7 @@ static struct tool_mime *tool_mime_new_filedata(struct tool_mime *parent,
     curl_off_t origin;
     struct_stat sbuf;
 
-    set_binmode(stdin);
+    CURL_SET_BINMODE(stdin);
     origin = ftell(stdin);
     /* If stdin is a regular file, do not buffer data but read it
        when needed. */
index 8754f0bc6f98649ec232c20b47df8abcfc5802bd..81dbbb883521549e51733d15041cc90b455c7b3b 100644 (file)
@@ -600,7 +600,7 @@ static ParameterError data_urlencode(struct GlobalConfig *global,
     /* a '@' letter, it means that a filename or - (stdin) follows */
     if(!strcmp("-", p)) {
       file = stdin;
-      set_binmode(stdin);
+      CURL_SET_BINMODE(stdin);
     }
     else {
       file = fopen(p, "rb");
@@ -866,7 +866,7 @@ static ParameterError set_data(cmdline_t cmd,
     if(!strcmp("-", nextarg)) {
       file = stdin;
       if(cmd == C_DATA_BINARY) /* forced data-binary */
-        set_binmode(stdin);
+        CURL_SET_BINMODE(stdin);
     }
     else {
       file = fopen(nextarg, "rb");
index ff28d77b39aa7f3ffdd4877450dc6ede767bc0e5..a2ceeac3dd4ed1c51ab473f62d7a8c869d22b61a 100644 (file)
@@ -111,10 +111,12 @@ extern const unsigned char curl_ca_embed[];
 #endif
 #endif
 
-#ifndef O_BINARY
-/* since O_BINARY as used in bitmasks, setting it to zero makes it usable in
+/* since O_BINARY is used in bitmasks, setting it to zero makes it usable in
    source code but yet it does not ruin anything */
-#  define O_BINARY 0
+#ifdef O_BINARY
+#define CURL_O_BINARY O_BINARY
+#else
+#define CURL_O_BINARY 0
 #endif
 
 #ifndef SOL_IP
@@ -379,16 +381,16 @@ static CURLcode pre_transfer(struct GlobalConfig *global,
       case FAB$C_VAR:
       case FAB$C_VFC:
       case FAB$C_STMCR:
-        per->infd = open(per->uploadfile, O_RDONLY | O_BINARY);
+        per->infd = open(per->uploadfile, O_RDONLY | CURL_O_BINARY);
         break;
       default:
-        per->infd = open(per->uploadfile, O_RDONLY | O_BINARY,
+        per->infd = open(per->uploadfile, O_RDONLY | CURL_O_BINARY,
                          "rfm=stmlf", "ctx=stm");
       }
     }
     if(per->infd == -1)
 #else
-      per->infd = open(per->uploadfile, O_RDONLY | O_BINARY);
+      per->infd = open(per->uploadfile, O_RDONLY | CURL_O_BINARY);
     if((per->infd == -1) || fstat(per->infd, &fileinfo))
 #endif
     {
@@ -1993,7 +1995,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
         }
         else {
           /* always use binary mode for protocol header output */
-          set_binmode(etag_save->stream);
+          CURL_SET_BINMODE(etag_save->stream);
         }
       }
 
@@ -2038,7 +2040,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
         if(!strcmp(config->headerfile, "%")) {
           heads->stream = stderr;
           /* use binary mode for protocol header output */
-          set_binmode(heads->stream);
+          CURL_SET_BINMODE(heads->stream);
         }
         else if(strcmp(config->headerfile, "-")) {
           FILE *newfile;
@@ -2079,7 +2081,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
         }
         else {
           /* always use binary mode for protocol header output */
-          set_binmode(heads->stream);
+          CURL_SET_BINMODE(heads->stream);
         }
       }
 
@@ -2266,7 +2268,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
         DEBUGASSERT(per->infdopen == FALSE);
         DEBUGASSERT(per->infd == STDIN_FILENO);
 
-        set_binmode(stdin);
+        CURL_SET_BINMODE(stdin);
         if(!strcmp(per->uploadfile, ".")) {
           if(curlx_nonblock((curl_socket_t)per->infd, TRUE) < 0)
             warnf(global,
@@ -2300,7 +2302,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
          !config->use_ascii) {
         /* We get the output to stdout and we have not got the ASCII/text
            flag, then set stdout to be binary */
-        set_binmode(stdout);
+        CURL_SET_BINMODE(stdout);
       }
 
       /* explicitly passed to stdout means okaying binary gunk */
index 0e43c7f2ab5e36509d784ab8d42dad7137404d9c..6c1d8e9323f53de5342f40e74850c784d8d05dea 100644 (file)
@@ -75,6 +75,7 @@ foreach(_target IN LISTS LIBTESTPROGS)
   target_include_directories(${_target_name} PRIVATE
     "${PROJECT_BINARY_DIR}/lib"            # for "curl_config.h"
     "${PROJECT_SOURCE_DIR}/lib"            # for "curl_setup.h"
+    "${PROJECT_SOURCE_DIR}/src"            # for "tool_binmode.h"
     "${PROJECT_SOURCE_DIR}/tests/libtest"  # to be able to build generated tests
   )
   if(NOT CURL_TEST_BUNDLES)
index a2288da5fa4a7bdc7155cce696d552d7fe7cd81f..45eb20a866aa767c546be65319979e044cb1dd8d 100644 (file)
@@ -35,6 +35,7 @@ AUTOMAKE_OPTIONS = foreign nostdinc
 AM_CPPFLAGS = -I$(top_srcdir)/include        \
               -I$(top_builddir)/lib          \
               -I$(top_srcdir)/lib            \
+              -I$(top_srcdir)/src            \
               -I$(top_srcdir)/tests/libtest
 
 EXTRA_DIST = test307.pl test610.pl test613.pl test1013.pl test1022.pl   \
index 4824def74a158e5223ec9c74054a6bf5ab3a61d2..7d41954df3df2f8fee22b93683bbbb4af4277427 100644 (file)
 ###########################################################################
 # files used only in some libcurl test programs
 TESTUTIL = testutil.c testutil.h
-
-# files used only in some libcurl test programs
 TSTTRACE = testtrace.c testtrace.h
-
-# files used only in some libcurl test programs
 WARNLESS = ../../lib/warnless.c ../../lib/warnless.h
-
-# files used only in some libcurl test programs
 MULTIBYTE = ../../lib/curl_multibyte.c ../../lib/curl_multibyte.h
-
-# files used only in some libcurl test programs
 INET_PTON = ../../lib/inet_pton.c ../../lib/inet_pton.h
+THREADS = ../../lib/curl_threads.c ../../lib/curl_threads.h
 
 # these files are used in every single test program below
 TIMEDIFF = ../../lib/timediff.c ../../lib/timediff.h
 FIRSTFILES = first.c first.h
 SUPPORTFILES = $(TIMEDIFF) $(FIRSTFILES) test.h
 
-THREADS = ../../lib/curl_threads.c ../../lib/curl_threads.h
-
 # These are all libcurl test programs
 LIBTESTPROGS = libauthretry libntlmconnect libprereq                     \
  lib500 lib501 lib502 lib503 lib504 lib505 lib506 lib507 lib508 lib509   \
index 68c649e08bd237148bf9db49ad904279ac98795e..937c66c4db510b4373fd9b6ee4c9121dc229a387 100644 (file)
 #  include <locale.h> /* for setlocale() */
 #endif
 
-#ifdef HAVE_IO_H
-#  include <io.h> /* for setmode() */
-#endif
-
-#ifdef HAVE_FCNTL_H
-#  include <fcntl.h> /* for setmode() */
-#endif
-
 #ifdef CURLDEBUG
 #  define MEMDEBUG_NODEFINES
 #  include "memdebug.h"
@@ -43,6 +35,8 @@
 
 #include "timediff.h"
 
+#include "tool_binmode.h"
+
 int select_wrapper(int nfds, fd_set *rd, fd_set *wr, fd_set *exc,
                    struct timeval *tv)
 {
@@ -141,13 +135,7 @@ int main(int argc, char **argv)
   int basearg;
   test_func_t test_func;
 
-#ifdef O_BINARY
-#  ifdef __HIGHC__
-  _setmode(stdout, O_BINARY);
-#  else
-  setmode(fileno(stdout), O_BINARY);
-#  endif
-#endif
+  CURL_SET_BINMODE(stdout);
 
   memory_tracking_init();
 
index 939a003e5a5dd0cae5fc9e8d6bed7777781e15be..2ad8b5eed0d265b3d54254c7f5d16b11d73d1f2e 100644 (file)
@@ -35,7 +35,7 @@ foreach(_target IN LISTS noinst_PROGRAMS)
   target_include_directories(${_target_name} PRIVATE
     "${PROJECT_BINARY_DIR}/lib"  # for "curl_config.h"
     "${PROJECT_SOURCE_DIR}/lib"  # for "curl_setup.h"
-    "${PROJECT_SOURCE_DIR}/src"  # for "tool_xattr.h" in disabled_SOURCES
+    "${PROJECT_SOURCE_DIR}/src"  # for "tool_binmod.h", "tool_xattr.h"
   )
   target_link_libraries(${_target_name} ${CURL_LIBS})
   # Test servers simply are standalone programs that do not use libcurl
index 1a844f121bd3d30a3b9bd0e74782799c7a31e22f..40433f8c5c9cba04acb8d6d49b2ff335b3315894 100644 (file)
@@ -34,9 +34,7 @@ AUTOMAKE_OPTIONS = foreign nostdinc
 
 AM_CPPFLAGS = -I$(top_srcdir)/include        \
               -I$(top_builddir)/lib          \
-              -I$(top_srcdir)/lib
-
-disabled_CPPFLAGS = $(AM_CPPFLAGS) \
+              -I$(top_srcdir)/lib            \
               -I$(top_srcdir)/src
 
 # Prevent LIBS from being used for all link targets
index 233d03dbf0e6335b1438fc4ecd1597c183033b53..76e8c3d28af57ca9e40ed571095551876c7930e6 100644 (file)
@@ -60,6 +60,8 @@
 #include "server_sockaddr.h"
 #include "warnless.h"
 
+#include "tool_binmode.h"
+
 /* include memdebug.h last */
 #include "memdebug.h"
 
@@ -1028,12 +1030,12 @@ int main(int argc, char *argv[])
 #ifdef _WIN32
   win32_init();
   atexit(win32_cleanup);
-
-  setmode(fileno(stdin), O_BINARY);
-  setmode(fileno(stdout), O_BINARY);
-  setmode(fileno(stderr), O_BINARY);
 #endif
 
+  CURL_SET_BINMODE(stdin);
+  CURL_SET_BINMODE(stdout);
+  CURL_SET_BINMODE(stderr);
+
   install_signal_handlers(FALSE);
 
 #ifdef USE_IPV6
index 3c401113acaf05a24ccd58f45c3f2abd0966b34e..37931b64efdcf85f51fc746ac43228e70604c674 100644 (file)
 #include "timediff.h"
 #include "warnless.h"
 
+#include "tool_binmode.h"
+
 /* include memdebug.h last */
 #include "memdebug.h"
 
@@ -1497,12 +1499,12 @@ int main(int argc, char *argv[])
 #ifdef _WIN32
   win32_init();
   atexit(win32_cleanup);
-
-  setmode(fileno(stdin), O_BINARY);
-  setmode(fileno(stdout), O_BINARY);
-  setmode(fileno(stderr), O_BINARY);
 #endif
 
+  CURL_SET_BINMODE(stdin);
+  CURL_SET_BINMODE(stdout);
+  CURL_SET_BINMODE(stderr);
+
   install_signal_handlers(false);
 
 #ifdef USE_IPV6
index 6df6bd4cdf354c35044c00ca85ee42adfd0b2e01..295df423a2c98df7166c5275e7c70eb037fe97ca 100644 (file)
@@ -78,6 +78,8 @@
 #include "server_sockaddr.h"
 #include "warnless.h"
 
+#include "tool_binmode.h"
+
 /* include memdebug.h last */
 #include "memdebug.h"
 
@@ -1098,12 +1100,12 @@ int main(int argc, char *argv[])
 #ifdef _WIN32
   win32_init();
   atexit(win32_cleanup);
-
-  setmode(fileno(stdin), O_BINARY);
-  setmode(fileno(stdout), O_BINARY);
-  setmode(fileno(stderr), O_BINARY);
 #endif
 
+  CURL_SET_BINMODE(stdin);
+  CURL_SET_BINMODE(stdout);
+  CURL_SET_BINMODE(stderr);
+
   install_signal_handlers(false);
 
   sock = socket(socket_domain, SOCK_STREAM, 0);