Hi Andreas,

What do you think of the following two patches?

The first removes some unnecessary obsolete Autoconf macros. AC_PROG_CC
has handled all of those checks for a few years.

The second adds a dependency to 'xbinary-io' from Gnulib. I assume
'patch --binary' was written before that module existed. It should be
a no-op on most platforms but I think it is better to have Gnulib deal
with that portability stuff.

Collin
From dd4f8902808c5b8d1b4342f7d0052af51c9b29b5 Mon Sep 17 00:00:00 2001
From: Collin Funk <[email protected]>
Date: Sat, 15 Jun 2024 15:54:46 -0700
Subject: [PATCH 1/2] build: Remove obsolete autoconf macros.

* configure.ac (AC_PROG_CC_STDC, AC_HEADER_STDC): Remove long obsolete
autoconf macros. Autconf warns about both and gnulib-tool complains
about AC_PROG_CC_STDC.
---
 configure.ac | 2 --
 1 file changed, 2 deletions(-)

diff --git a/configure.ac b/configure.ac
index 0fb9c5e..8db7e51 100644
--- a/configure.ac
+++ b/configure.ac
@@ -39,10 +39,8 @@ AM_SILENT_RULES([yes])
 AC_CONFIG_HEADERS([config.h:config.hin])
 
 AC_PROG_CC
-AC_PROG_CC_STDC
 gl_EARLY
 gl_USE_SYSTEM_EXTENSIONS
-AC_HEADER_STDC
 gl_INIT
 AM_PROG_AR
 
-- 
2.45.2

From d9befb706e46ecf962e42930249a8fd25dfbd825 Mon Sep 17 00:00:00 2001
From: Collin Funk <[email protected]>
Date: Sat, 15 Jun 2024 16:23:39 -0700
Subject: [PATCH 2/2] maint: Use gnulib's xbinary-io module.

* bootstrap.conf (gnulib_modules): Depend on xbinary-io.
* m4/setmode.m4: Remove file.
* configure.ac (AC_FUNC_SETMODE_DOS): Remove macro invocation.
* src/common.h (binary_transput): Define as a bool unconditionally.
* src/inp.c (plan_a, plan_b): Adjust use of binary_transput to reflect
change to bool.
* src/patch.c (main, get_some_switches, apply_hunk, init_reject):
Likewise.
* src/pch.c (open_patch_file): Use xset_binary_mode.
---
 bootstrap.conf |  1 +
 configure.ac   |  1 -
 m4/setmode.m4  | 38 --------------------------------------
 src/common.h   |  6 +-----
 src/inp.c      |  4 ++--
 src/patch.c    | 16 +++++++---------
 src/pch.c      | 12 ++----------
 7 files changed, 13 insertions(+), 65 deletions(-)
 delete mode 100644 m4/setmode.m4

diff --git a/bootstrap.conf b/bootstrap.conf
index bb89b0f..a749519 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -76,6 +76,7 @@ update-copyright
 utimensat
 verror
 xalloc
+xbinary-io
 xlist
 xmemdup0
 "
diff --git a/configure.ac b/configure.ac
index 8db7e51..468114c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -142,7 +142,6 @@ gl_SIZE_MAX
 gl_FUNC_XATTR
 
 AC_CHECK_FUNCS(geteuid getuid raise sigaction sigprocmask sigsetmask)
-AC_FUNC_SETMODE_DOS
 
 AC_PATH_PROG([ED], [ed], [ed])
 AC_DEFINE_UNQUOTED([EDITOR_PROGRAM], ["$ED"], [Name of editor program.])
diff --git a/m4/setmode.m4 b/m4/setmode.m4
deleted file mode 100644
index 07b529b..0000000
--- a/m4/setmode.m4
+++ /dev/null
@@ -1,38 +0,0 @@
-# Check for setmode, DOS style.
-
-# Copyright (C) 2001-2002, 2011-2012 Free Software Foundation, Inc.
-
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2, or (at your option)
-# any later version.
-
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
-# USA.
-
-AC_DEFUN([AC_FUNC_SETMODE_DOS],
-  [AC_CHECK_HEADERS([fcntl.h unistd.h])
-   AC_CACHE_CHECK([for DOS-style setmode],
-     [ac_cv_func_setmode_dos],
-     [AC_TRY_LINK(
-	[#include <io.h>
-	 #if HAVE_FCNTL_H
-	 # include <fcntl.h>
-	 #endif
-	 #if HAVE_UNISTD_H
-	 # include <unistd.h>
-	 #endif],
-	[int ret = setmode && setmode (1, O_BINARY);],
-	[ac_cv_func_setmode_dos=yes],
-	[ac_cv_func_setmode_dos=no])])
-   if test $ac_cv_func_setmode_dos = yes; then
-     AC_DEFINE([HAVE_SETMODE_DOS], [1],
-       [Define to 1 if you have the DOS-style `setmode' function.])
-   fi])
diff --git a/src/common.h b/src/common.h
index 53c5e32..a1b77e8 100644
--- a/src/common.h
+++ b/src/common.h
@@ -177,11 +177,7 @@ extern int errno;
 
 #include <fcntl.h>
 
-#ifdef HAVE_SETMODE_DOS
-  XTERN int binary_transput;	/* O_BINARY if binary i/o is desired */
-#else
-# define binary_transput 0
-#endif
+XTERN bool binary_transput;	/* O_BINARY if binary i/o is desired */
 
 /* Disable the CR stripping heuristic?  */
 XTERN bool no_strip_trailing_cr;
diff --git a/src/inp.c b/src/inp.c
index 22d7473..74fccf8 100644
--- a/src/inp.c
+++ b/src/inp.c
@@ -238,7 +238,7 @@ plan_a (char const *filename)
     {
       if (S_ISREG (instat.st_mode))
         {
-	  int flags = O_RDONLY | binary_transput;
+	  int flags = O_RDONLY | (binary_transput ? O_BINARY : 0);
 	  size_t buffered = 0, n;
 	  int ifd;
 
@@ -345,7 +345,7 @@ plan_a (char const *filename)
 static void
 plan_b (char const *filename)
 {
-  int flags = O_RDONLY | binary_transput;
+  int flags = O_RDONLY | (binary_transput ? O_BINARY : 0);
   int ifd;
   FILE *ifp;
   int c;
diff --git a/src/patch.c b/src/patch.c
index d5f456c..06d9b61 100644
--- a/src/patch.c
+++ b/src/patch.c
@@ -320,7 +320,7 @@ main (int argc, char **argv)
 
       tmpoutst.st_size = -1;
       outfd = make_tempfile (&TMPOUTNAME, 'o', outname,
-			     O_WRONLY | binary_transput,
+			     O_WRONLY | (binary_transput ? O_BINARY : 0),
 			     instat.st_mode & S_IRWXUGO);
       if (outfd == -1)
 	{
@@ -363,7 +363,7 @@ main (int argc, char **argv)
 	if (! skip_rest_of_patch && ! outfile)
 	  {
 	    init_output (&outstate);
-	    outstate.ofp = fdopen(outfd, binary_transput ? "wb" : "w");
+	    outstate.ofp = fdopen (outfd, binary_transput ? "wb" : "w");
 	    if (! outstate.ofp)
 	      pfatal ("%s", TMPOUTNAME);
 	    /* outstate.ofp now owns the file descriptor */
@@ -1033,9 +1033,7 @@ get_some_switches (void)
 		break;
 	    case CHAR_MAX + 3:
 		no_strip_trailing_cr = true;
-#if HAVE_SETMODE_DOS
-		binary_transput = O_BINARY;
-#endif
+		binary_transput = true;
 		break;
 	    case CHAR_MAX + 4:
 		usage (stdout, 0);
@@ -1583,8 +1581,8 @@ apply_hunk (struct outstate *outstate, lin where)
 static FILE *
 create_output_file (char const *name, int open_flags)
 {
-  int fd = create_file (name, O_WRONLY | binary_transput | open_flags,
-			instat.st_mode, true);
+  int fd = create_file (name, O_WRONLY | (binary_transput ? O_BINARY : 0)
+                        | open_flags, instat.st_mode, true);
   FILE *f = fdopen (fd, binary_transput ? "wb" : "w");
   if (! f)
     pfatal ("Can't create file %s", quotearg (name));
@@ -1628,8 +1626,8 @@ static void
 init_reject (char const *outname)
 {
   int fd;
-  fd = make_tempfile (&TMPREJNAME, 'r', outname, O_WRONLY | binary_transput,
-		      0666);
+  fd = make_tempfile (&TMPREJNAME, 'r', outname, O_WRONLY
+                      | (binary_transput ? O_BINARY : 0), 0666);
   if (fd == -1)
     pfatal ("Can't create temporary file %s", TMPREJNAME);
   TMPREJNAME_needs_removal = true;
diff --git a/src/pch.c b/src/pch.c
index fd9c480..4f20dcd 100644
--- a/src/pch.c
+++ b/src/pch.c
@@ -29,12 +29,10 @@
 #undef XTERN
 #define XTERN
 #include <pch.h>
-#if HAVE_SETMODE_DOS
-# include <io.h>
-#endif
 #include <safe.h>
 #include <alloca.h>
 #include "execute.h"
+#include "xbinary-io.h"
 
 #define INITHUNKMAX 125			/* initial dynamic allocation size */
 
@@ -125,14 +123,8 @@ open_patch_file (char const *filename)
 	if (!pfp)
 	  pfatal ("Can't open patch file %s", quotearg (filename));
       }
-#if HAVE_SETMODE_DOS
     if (binary_transput)
-      {
-	if (isatty (fileno (pfp)))
-	  fatal ("cannot read binary data from tty on this platform");
-	setmode (fileno (pfp), O_BINARY);
-      }
-#endif
+      xset_binary_mode (fileno (pfp), O_BINARY);
     if (fstat (fileno (pfp), &st) != 0)
       pfatal ("fstat");
     if (S_ISREG (st.st_mode) && (pos = file_tell (pfp)) != -1)
-- 
2.45.2

Reply via email to