I think when I wrote this I tested the named escapes like \n and hex
escapes, and when I found \x wasn't supported I didn't even think of
octal. And I only learned about \c when I was looking at echo and printf
to compare their escape implementations a few weeks back.

Add the missing escapes and corresponding tests.

Fixes #139.
---
 tests/find.test   |  7 +++++++
 toys/posix/find.c | 10 +++++++++-
 2 files changed, 16 insertions(+), 1 deletion(-)
From 4ad8050b3af94a5630c89d506094bde325e3b348 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Sat, 31 Aug 2019 21:27:55 -0700
Subject: [PATCH] find: support -printf \0 octal escapes and \c.

I think when I wrote this I tested the named escapes like \n and hex
escapes, and when I found \x wasn't supported I didn't even think of
octal. And I only learned about \c when I was looking at echo and printf
to compare their escape implementations a few weeks back.

Add the missing escapes and corresponding tests.

Fixes #139.
---
 tests/find.test   |  7 +++++++
 toys/posix/find.c | 10 +++++++++-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/tests/find.test b/tests/find.test
index 1f59149b..9129a605 100755
--- a/tests/find.test
+++ b/tests/find.test
@@ -101,6 +101,13 @@ testing "-iwholename glob" "find dir -iwholename 'dIr*E'" "dir/file\n" "" ""
 testing "-printf" "find dir -name file -printf '%f %p %P %s'" \
   "file dir/file file 0" "" ""
 testing "-printf .N" "find dir -name file -printf %.2f" "fi" "" ""
+# findutils find supports C letter escapes and \0 octal, but not \x or \u.
+testing "-printf escapes" \
+  "find dir -name file -printf '\0 \007 \t \079' | xxd -p" \
+  "0020072009200739\n" "" ""
+# findutils find treats \c as "no more output from this -printf", not "no more
+# output from find".
+testing "-printf \\c escape" "find dir -name f* -printf 'x\cy'" "xx" "" ""
 
 # No error message for a dangling link.
 ln -s does-not-exist dir/dangler
diff --git a/toys/posix/find.c b/toys/posix/find.c
index 02fdf3c4..4ae83ec3 100644
--- a/toys/posix/find.c
+++ b/toys/posix/find.c
@@ -569,7 +569,15 @@ static int do_find(struct dirtree *new)
         if (check) for (fmt = ss[1]; *fmt; fmt++) {
           // Print the parts that aren't escapes
           if (*fmt == '\\') {
-            if (!(ch = unescape(*++fmt))) error_exit("bad \\%c", *fmt);
+            int slash = *(++fmt), n = unescape(slash);
+
+            if (n) ch = n;
+            else if (slash=='c') break;
+            else if (slash=='0') {
+              ch = 0;
+              while (*fmt>='0' && *fmt<='7' && n++<3) ch=(ch*8)+*(fmt++)-'0';
+              --fmt;
+            } else error_exit("bad \\%c", *fmt);
             putchar(ch);
           } else if (*fmt != '%') putchar(*fmt);
           else if (*++fmt == '%') putchar('%');
-- 
2.23.0.187.g17f5b7556c-goog

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

Reply via email to