From 5446bac31b5f02eb3337e7e528c3c24ff006bd8d Mon Sep 17 00:00:00 2001
From: Johannes Schindelin <johannes.schindelin@gmx.de>
Date: Wed, 23 Aug 2017 13:31:16 +0100
Subject: sed: support the -b/--binary option

This option was introduced in Cygwin's and MSYS2's sed, to allow for
keeping Carriage Returns (otherwise, they would be stripped and the
output would always be LF-only).

Git for Windows' test suite relies on the presence of this option.

It was easy enough to implement, too.

Conditional compilation added by rmy.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Ron Yorston <rmy@pobox.com>
---
 editors/sed.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/editors/sed.c b/editors/sed.c
index d6e25aedc..e2c6690bb 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -77,6 +77,9 @@
 //usage:     "\n		Optionally back files up, appending SFX"
 //usage:     "\n	-n	Suppress automatic printing of pattern space"
 //usage:     "\n	-r,-E	Use extended regex syntax"
+//usage:     IF_PLATFORM_MINGW32(
+//usage:     "\n	-b 	Keep CR/LF (Windows-only)"
+//usage:     )
 //usage:     "\n"
 //usage:     "\nIf no -e or -f, the first non-option argument is the sed command string."
 //usage:     "\nRemaining arguments are input files (stdin if none)."
@@ -135,6 +138,9 @@ static const char semicolon_whitespace[] ALIGN1 = "; \n\r\t\v";
 struct globals {
 	/* options */
 	int be_quiet, regex_type;
+#if ENABLE_PLATFORM_MINGW32
+	int keep_cr;
+#endif
 
 	FILE *nonstdout;
 	char *outname, *hold_space;
@@ -1017,7 +1023,7 @@ static char *get_next_line(char *gets_char, char *last_puts_char)
 			if (c == '\n' || c == '\0') {
 				temp[len-1] = '\0';
 #if ENABLE_PLATFORM_MINGW32
-				if (c == '\n' && len > 1 && temp[len-2] == '\r') {
+				if (!G.keep_cr && c == '\n' && len > 1 && temp[len-2] == '\r') {
 					temp[len-2] = '\0';
 				}
 #endif
@@ -1495,7 +1501,12 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
 		"quiet\0"           No_argument         "n"
 		"silent\0"          No_argument         "n"
 		"expression\0"      Required_argument   "e"
+# if !ENABLE_PLATFORM_MINGW32
 		"file\0"            Required_argument   "f";
+# else
+		"file\0"            Required_argument   "f"
+		"binary\0"          No_argument         "b";
+# endif
 #endif
 
 	INIT_G();
@@ -1519,6 +1530,7 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
 	 */
 	opt = getopt32long(argv, "^"
 			"i::rEne:*f:*"
+			IF_PLATFORM_MINGW32("b")
 			"\0" "nn"/*count -n*/,
 			sed_longopts,
 			&opt_i, &opt_e, &opt_f,
@@ -1532,6 +1544,10 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
 		G.regex_type |= REG_EXTENDED; // -r or -E
 	//if (opt & 8)
 	//	G.be_quiet++; // -n (implemented with a counter instead)
+#if ENABLE_PLATFORM_MINGW32
+	if (opt & 0x40)
+		G.keep_cr = 1;
+#endif
 	while (opt_e) { // -e
 		add_cmd_block(llist_pop(&opt_e));
 	}
-- 
cgit v1.2.3-55-g6feb