>From 7af7d425ff7ef98a9e4994ce4309f07943a285c0 Mon Sep 17 00:00:00 2001 From: Assaf Gordon Date: Thu, 18 Oct 2018 16:16:53 -0600 Subject: [PATCH] sed: fix -b/--binary mode under windows/mingw Discussed in https://lists.gnu.org/r/sed-devel/2018-10/msg00001.html . * NEWS: Mention change. * bootstrap.conf: Add gnulib's binary-io module. * sed/sed.c (main): Set stdin/stdout to binary mode if needed. --- NEWS | 3 +++ bootstrap.conf | 1 + sed/sed.c | 20 ++++++++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index a03bb63..1464db8 100644 --- a/NEWS +++ b/NEWS @@ -13,6 +13,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/bootstrap.conf b/bootstrap.conf index 5227ee2..692ddfc 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -24,6 +24,7 @@ gnulib_modules=' acl alloca stdalign +binary-io btowc c-ctype closeout diff --git a/sed/sed.c b/sed/sed.c index 5ff49c0..2e52c74 100644 --- a/sed/sed.c +++ b/sed/sed.c @@ -24,6 +24,7 @@ #include #include #include +#include "binary-io.h" #include "getopt.h" #include "progname.h" #include "version.h" @@ -147,8 +148,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 O_BINARY fprintf (out, _(" -b, --binary\n\ open files in binary mode (CR+LFs are not" \ " processed specially)\n")); @@ -220,6 +220,9 @@ main (int argc, char **argv) int opt; int return_code; const char *cols = getenv ("COLS"); +#if O_BINARY + bool binary_mode = false; +#endif set_program_name (argv[0]); initialize_main (&argc, &argv); @@ -308,6 +311,9 @@ main (int argc, char **argv) case 'b': read_mode = "rb"; write_mode = "wb"; +#if O_BINARY + binary_mode = true; +#endif break; case 'E': @@ -356,6 +362,16 @@ main (int argc, char **argv) } check_final_program (the_program); +#if O_BINARY + if (binary_mode) + { + if (set_binary_mode ( fileno (stdin), O_BINARY) == -1) + panic (_("failed to set binary mode on STDIN")); + if (set_binary_mode ( fileno (stdout), O_BINARY) == -1) + panic (_("failed to set binary mode on STDOUT")); + } +#endif + if (debug) debug_print_program (the_program); -- 2.11.0