From 81518f643d4d53b035b2cefbce25f06bd3cb5079 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Sat, 17 Aug 2019 19:53:56 -0700 Subject: echo/printf: expand test cases, fix \x corner cases. The behavior with "\xAV" (where the second hex digit is invalid) is different from the behavior with "\xVA", and echo and printf differ from each other. --- toys/posix/echo.c | 12 +++++++++--- toys/posix/printf.c | 11 ++++------- 2 files changed, 13 insertions(+), 10 deletions(-) (limited to 'toys/posix') diff --git a/toys/posix/echo.c b/toys/posix/echo.c index 94546ecd..70f4ce5e 100644 --- a/toys/posix/echo.c +++ b/toys/posix/echo.c @@ -51,7 +51,7 @@ void echo_main(void) // Should we output arg verbatim? - if (!(toys.optflags & FLAG_e)) { + if (!FLAG(e)) { xprintf("%s", arg); continue; } @@ -79,7 +79,13 @@ void echo_main(void) if (temp>='a' && temp<='f') { out = (out*16)+temp-'a'+10; c++; - } else break; + } else { + if (n==1) { + --c; + out = '\\'; + } + break; + } } } // Slash in front of unknown character, print literal. @@ -90,5 +96,5 @@ void echo_main(void) } // Output "\n" if no -n - if (!(toys.optflags&FLAG_n)) putchar('\n'); + if (!FLAG(n)) putchar('\n'); } diff --git a/toys/posix/printf.c b/toys/posix/printf.c index 5448d8c7..b51eddc0 100644 --- a/toys/posix/printf.c +++ b/toys/posix/printf.c @@ -9,12 +9,12 @@ USE_PRINTF(NEWTOY(printf, "<1?^", TOYFLAG_USR|TOYFLAG_BIN)) -config PRINTF +config PRINTF bool "printf" default y help usage: printf FORMAT [ARGUMENT...] - + Format and print ARGUMENT(s) according to FORMAT, using C printf syntax (% escapes for cdeEfgGiosuxX, \ escapes for abefnrtv0 or \OCTAL or \xHEX). */ @@ -61,11 +61,8 @@ static int handle_slash(char **esc_val, int posix) num = tolower(*ptr) - '0'; if (num >= 'a'-'0') num += '0'-'a'+10; if (num >= base) { - // Don't parse invalid hex value ala "\xvd", print it verbatim - if (base == 16 && len == 2) { - ptr--; - result = '\\'; - } + // "\xav" is "\xa"+"v", but "\xva" is an error. + if (base == 16 && len == 2) error_exit("bad \\x"); break; } result = (result*base)+num; -- cgit v1.2.3