>From f5330d22155a1f77a16fee945e22d8ac23fbb142 Mon Sep 17 00:00:00 2001 From: Assaf Gordon Date: Thu, 18 Oct 2018 14:42:41 -0600 Subject: [PATCH] sed: fix -b/--binary mode under windows/mingw * NEWS: Mention change. * doc/sed.texi (options): Mention -b/--binary option does not apply under cygwin. * sed/sed.h (WINDOWS_BINARY_MODE_SUPPORT): Define symbol under mingw. * sed/sed.c (main): Set stdin/stdout to binary mode if needed. --- NEWS | 3 +++ doc/sed.texi | 5 +++-- sed/sed.c | 23 +++++++++++++++++++++-- sed/sed.h | 5 +++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index e25d26b..1766260 100644 --- a/NEWS +++ b/NEWS @@ -9,6 +9,9 @@ GNU sed NEWS -*- outline -*- and other write commands. Buffering can be disabled (as before) with "sed -u". + sed in non-cygwin windows environments (e.g. mingw) now properly handles + '\n' newlines in -b/--binary mode. + ** Bug fixes sed no longer accesses invalid memory (heap overflow) when given invalid diff --git a/doc/sed.texi b/doc/sed.texi index 8fc9dbb..b6b0618 100644 --- a/doc/sed.texi +++ b/doc/sed.texi @@ -363,8 +363,9 @@ to a non-empty value. @opindex --binary This option is available on every platform, but is only effective where the operating system makes a distinction between text files and binary files. -When such a distinction is made---as is the case for MS-DOS, Windows, -Cygwin---text files are composed of lines separated by a carriage return +When such a distinction is made---as is the case for MS-DOS, Windows +(but not modern Cygwin)---text files are composed of lines separated by a +carriage return @emph{and} a line feed character, and @command{sed} does not see the ending CR. When this option is specified, @command{sed} will open input files in binary mode, thus not requesting this special processing diff --git a/sed/sed.c b/sed/sed.c index 1508f38..e3b3efd 100644 --- a/sed/sed.c +++ b/sed/sed.c @@ -31,6 +31,10 @@ #include "version-etc.h" +#ifdef WINDOWS_BINARY_MODE_SUPPORT +#include +#endif + #define AUTHORS \ _("Jay Fenlason"), \ _("Tom Lord"), \ @@ -142,8 +146,7 @@ Usage: %s [OPTION]... {script-only-if-no-other-script} [input-file]...\n\ #endif fprintf (out, _(" -i[SUFFIX], --in-place[=SUFFIX]\n\ edit files in place (makes backup if SUFFIX supplied)\n")); -#if defined WIN32 || defined _WIN32 || defined __CYGWIN__ \ - || defined MSDOS || defined __EMX__ +#if WINDOWS_BINARY_MODE_SUPPORT || defined MSDOS || defined __EMX__ fprintf (out, _(" -b, --binary\n\ open files in binary mode (CR+LFs are not" \ " processed specially)\n")); @@ -212,6 +215,9 @@ main (int argc, char **argv) int opt; int return_code; const char *cols = getenv ("COLS"); +#ifdef WINDOWS_BINARY_MODE_SUPPORT + bool binary_mode = false; +#endif set_program_name (argv[0]); initialize_main (&argc, &argv); @@ -300,6 +306,9 @@ main (int argc, char **argv) case 'b': read_mode = "rb"; write_mode = "wb"; +#ifdef WINDOWS_BINARY_MODE_SUPPORT + binary_mode = true; +#endif break; case 'E': @@ -344,6 +353,16 @@ main (int argc, char **argv) } check_final_program (the_program); +#ifdef WINDOWS_BINARY_MODE_SUPPORT + if (binary_mode) + { + if (_setmode ( _fileno (stdin), _O_BINARY) == -1) + panic (_("failed to set binary mode on STDIN")); + if (_setmode ( _fileno (stdout), _O_BINARY) == -1) + panic (_("failed to set binary mode on STDOUT")); + } +#endif + return_code = process_files (the_program, argv+optind); finish_program (the_program); diff --git a/sed/sed.h b/sed/sed.h index be26511..d2a030e 100644 --- a/sed/sed.h +++ b/sed/sed.h @@ -277,3 +277,8 @@ extern void cancel_cleanup (void); # define FALLTHROUGH __attribute__ ((__fallthrough__)) # endif #endif + +/* see: https://lists.gnu.org/r/bug-gnulib/2018-10/msg00055.html */ +#if defined _WIN32 && ! defined __CYGWIN__ +# define WINDOWS_BINARY_MODE_SUPPORT +#endif -- 2.11.0