the Android Studio folks noticed that `ls foo\ bar` changed from
returning "foo bar" to "foo\ bar" recently.

testing a little bit, -b and -q seem to be wrong, and it seems like a
tty should imply -q rather than -b. (`info ls` agrees about the
latter, but doesn't distinguish between the two demonstrably different
sets of "non-graphic" characters for -b and -q.)

the attached patch includes tests and a fix, though it's not obvious
from the git history why you've gone back and forth here over time,
nor are there any existing tests to capture whatever it was. (the
patch doesn't include a test for the defaults, because i don't know
how to write one, but it does at least test -b and -q in two obvious
cases.)
From 1c0920dd55e27fb32822c792b5707a518dbfe1b7 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Fri, 8 Dec 2017 10:53:57 -0800
Subject: [PATCH] Fix -b/-q, and the default for a tty.

Includes tests that pass with TEST_HOST and don't pass for toybox without
this patch.
---
 tests/ls.test   | 8 ++++++++
 toys/posix/ls.c | 6 +++---
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/tests/ls.test b/tests/ls.test
index 56f7403..ac172bf 100755
--- a/tests/ls.test
+++ b/tests/ls.test
@@ -14,6 +14,9 @@ echo "test file1" > lstest/file1.txt
 echo "test file2" > lstest/file2.txt
 echo "hidden file1" > lstest/.hfile1
 
+touch foo\ bar
+touch foobar
+
 IN="cd lstest"
 OUT="cd .. "
 
@@ -32,6 +35,11 @@ testing "with -m" "$IN && ls -m; $OUT" "dir1, dir2, file1.txt, file2.txt\n" "" "
 testing "with -F" "$IN && ls -F; $OUT" "dir1/\ndir2/\nfile1.txt\nfile2.txt\n" "" ""
 testing "with -dk *" "$IN && ls -dk *; $OUT" "dir1\ndir2\nfile1.txt\nfile2.txt\n" "" ""
 
+testing "-b space" "ls -b foo\ bar" "foo\\ bar\n" "" ""
+testing "-b BEL" "ls -b foobar" "foo\\\\abar\n" "" ""
+testing "-q space" "ls -q foo\ bar" "foo bar\n" "" ""
+testing "-q BEL" "ls -q foobar" "foo?bar\n" "" ""
+
 ln -s file1.txt lstest/slink
 testing "-l symlink" \
     "$IN && ls -l slink | grep -q -- ' slink -> file1.txt' && echo ok ; $OUT" \
diff --git a/toys/posix/ls.c b/toys/posix/ls.c
index 009653b..5b30c26 100644
--- a/toys/posix/ls.c
+++ b/toys/posix/ls.c
@@ -71,9 +71,9 @@ static int crunch_qb(FILE *out, int cols, int wc)
   unsigned len = 1;
   char buf[32];
 
-  if (toys.optflags&FLAG_q) *buf = '?';
+  if (toys.optflags&FLAG_q) *buf = (wc==' ')?' ':'?';
   else {
-    if (wc<256) *buf = wc;
+    if (wc<256 && wc!=' ') *buf = wc;
     // scrute the inscrutable, eff the ineffable, print the unprintable
     else len = wcrtomb(buf, wc, 0);
     if (toys.optflags&FLAG_b) {
@@ -539,7 +539,7 @@ void ls_main(void)
 
   // Do we have an implied -1
   if (isatty(1)) {
-    if (!(toys.optflags&FLAG_show_control_chars)) toys.optflags |= FLAG_b;
+    if (!(toys.optflags&FLAG_b)) toys.optflags |= FLAG_q;
     if (toys.optflags&(FLAG_l|FLAG_o|FLAG_n|FLAG_g)) toys.optflags |= FLAG_1;
     else if (!(toys.optflags&(FLAG_1|FLAG_x|FLAG_m))) toys.optflags |= FLAG_C;
   } else {
-- 
2.15.1.424.g9478a66081-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to