Mercurial > hg > toybox
changeset 449:d3544d2cdb26
Fix overflow in octal formatting in echo, add support for hexadecimal input, add tests for octal and hexadecimal formatting
author | Elie De Brauwer <eliedebrauwer@gmail.com> |
---|---|
date | Sat, 11 Feb 2012 14:58:50 +0100 |
parents | fa3293dda216 |
children | d8ff3b0980cf |
files | scripts/test/echo.test toys/echo.c |
diffstat | 2 files changed, 36 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/test/echo.test Sat Feb 11 13:46:49 2012 +0100 +++ b/scripts/test/echo.test Sat Feb 11 14:58:50 2012 +0100 @@ -23,3 +23,13 @@ testing "echo -e all" "$CMD -e '\a\b\c\f\n\r\t\v\\\0123'" \ "\a\b\c\f\n\r\t\v\\\0123\n" "" "" testing "echo -nex hello" "$CMD -nex hello" "-nex hello\n" "" "" + +# Octal formatting tests +testing "echo -e octal values" \ + "$CMD -ne '\01234 \0060 \060 \0130\0131\0132 \077\012'" \ + "S4 0 0 XYZ ?\n" "" "" + +# Hexadecimal value tests +testing "echo -e hexadecimal values" \ + "$CMD -ne '\x534 \x30 \x58\x59\x5a \x3F\x0A'"\ + "S4 0 XYZ ?\n" "" ""
--- a/toys/echo.c Sat Feb 11 13:46:49 2012 +0100 +++ b/toys/echo.c Sat Feb 11 14:58:50 2012 +0100 @@ -17,17 +17,19 @@ Write each argument to stdout, with one space between each, followed by a newline. - -n No trailing newline. - -e Process the following escape sequences: - \\ backslash - \a alert (beep/flash) - \b backspace - \c Stop output here (avoids trailing newline) - \f form feed - \n newline - \r carriage return - \t horizontal tab - \v vertical tab + -n No trailing newline. + -e Process the following escape sequences: + \\ backslash + \0NNN octal values (1 to 3 digits) + \a alert (beep/flash) + \b backspace + \c stop output here (avoids trailing newline) + \f form feed + \n newline + \r carriage return + \t horizontal tab + \v vertical tab + \xHH hexadecimal values (1 to 2 digits) */ #include "toys.h" @@ -60,10 +62,22 @@ if (found) c = to[found-from]; else if (d == 'c') goto done; else if (d == '0') { + int n = 0; c = 0; - while (arg[j]>='0' && arg[j]<='7') + while (arg[j]>='0' && arg[j]<='7' && n++<3) c = (c*8)+arg[j++]-'0'; } + else if (d == 'x') { + int n = 0; + c = 0; + while (n++<2) + if (arg[j]>='0' && arg[j]<='9') + c = (c*16)+arg[j++]-'0'; + else if (tolower(arg[j])>='a' && tolower(arg[j])<='f') + c = (c*16)+tolower(arg[j++])-'a'+10; + else + break; + } } } xputc(c);