printf: fix this case: printf "%b" '\0057usr\0057bin\n'
authorDenys Vlasenko <vda.linux@googlemail.com>
Wed, 7 Mar 2012 10:57:47 +0000 (11:57 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Wed, 7 Mar 2012 10:57:47 +0000 (11:57 +0100)
It was not accepting \0NNN. Standard printf tool does.

function                                             old     new   delta
printf_main                                          869     886     +17

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
coreutils/printf.c

index 1437951a8fde41fcf77a65ff8776278d30b254c9..3dd43a978205f2ebcbfc3c3f49d7e5e0130264d2 100644 (file)
@@ -131,13 +131,28 @@ static double my_xstrtod(const char *arg)
        return result;
 }
 
+/* Handles %b */
 static void print_esc_string(const char *str)
 {
        char c;
        while ((c = *str) != '\0') {
                str++;
-               if (c == '\\')
-                       c = bb_process_escape_sequence(&str);
+               if (c == '\\') {
+                       /* %b also accepts 4-digit octals of the form \0### */
+                       if (*str == '0') {
+                               if ((unsigned char)(str[1] - '0') < 8) {
+                                       /* 2nd char is 0..7: skip leading '0' */
+                                       str++;
+                               }
+                       }
+                       {
+                               /* optimization: don't force arg to be on-stack,
+                                * use another variable for that. */
+                               const char *z = str;
+                               c = bb_process_escape_sequence(&z);
+                               str = z;
+                       }
+               }
                putchar(c);
        }
 }