aboutsummaryrefslogtreecommitdiff
path: root/toys/echo.c
diff options
context:
space:
mode:
authorElie De Brauwer <eliedebrauwer@gmail.com>2012-02-11 14:58:50 +0100
committerElie De Brauwer <eliedebrauwer@gmail.com>2012-02-11 14:58:50 +0100
commitef3e5284bf078ef3cb0bd21c4883b341f8a2cb96 (patch)
tree41c3caed036bc84035728df8e1fd3ab4943e0217 /toys/echo.c
parentd2ad17b2e50924233b589fa04fbf0462b9998ed0 (diff)
downloadtoybox-ef3e5284bf078ef3cb0bd21c4883b341f8a2cb96.tar.gz
Fix overflow in octal formatting in echo, add support for hexadecimal input, add tests for octal and hexadecimal formatting
Diffstat (limited to 'toys/echo.c')
-rw-r--r--toys/echo.c38
1 files changed, 26 insertions, 12 deletions
diff --git a/toys/echo.c b/toys/echo.c
index 5feb9899..f7c423cf 100644
--- a/toys/echo.c
+++ b/toys/echo.c
@@ -17,17 +17,19 @@ config ECHO
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 @@ void echo_main(void)
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);