From 137ac1f8000b1c50b2485912de6e8f553a498230 Mon Sep 17 00:00:00 2001
From: Rob Landley <rob@landley.net>
Date: Sun, 5 Jul 2015 02:36:51 -0500
Subject: Kylie McClain pointed out that -z affects both input and output.

Can't say I'm happy with the API (-z and -Z to keep them independent would
be nice), but compatibility with what exists trumps having a good API...
---
 toys/posix/sort.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/toys/posix/sort.c b/toys/posix/sort.c
index b7e16c97..49e68c74 100644
--- a/toys/posix/sort.c
+++ b/toys/posix/sort.c
@@ -33,7 +33,7 @@ config SORT_BIG
     -M	month sort (jan, feb, etc).
     -x	Hexadecimal numerical sort
     -s	skip fallback sort (only sort with keys)
-    -z	zero (null) terminated input
+    -z	zero (null) terminated lines
     -k	sort by "key" (see below)
     -t	use a key separator other than whitespace
     -o	output to FILE instead of stdout
@@ -384,9 +384,11 @@ void sort_main(void)
   // Output result
   for (idx = 0; idx<TT.linecount; idx++) {
     char *s = TT.lines[idx];
-    xwrite(fd, s, strlen(s));
+    int i = strlen(s);
+
+    if (!(toys.optflags&FLAG_z)) s[i] = '\n';
+    xwrite(fd, s, i+1);
     if (CFG_TOYBOX_FREE) free(s);
-    xwrite(fd, "\n", 1);
   }
 
 exit_now:
-- 
cgit v1.2.3