[PATCH] od: print valid octal values with -c flag

Ron Yorston rmy at tigress.co.uk
Thu Mar 20 21:43:23 UTC 2014


For characters with the top bit set od_bloaty produces silly octal
values with the -c flag:

$ echo £ | busybox od -c   # od_bloaty.c (CONFIG_DESKTOP set)
0000000 0H2 0D3  \n
0000003
$ echo £ | busybox od -c   # od.c (CONFIG_DESKTOP not set)
0000000  302 243  \n
0000003
$ echo £ | od -c           # coreutils
0000000 302 243  \n
0000003

Signed-off-by: Ron Yorston <rmy at tigress.co.uk>
---
 coreutils/od_bloaty.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/coreutils/od_bloaty.c b/coreutils/od_bloaty.c
index 2c26dda..337d132 100644
--- a/coreutils/od_bloaty.c
+++ b/coreutils/od_bloaty.c
@@ -387,8 +387,8 @@ print_named_ascii(size_t n_bytes, const char *block,
 		" sp"
 	};
 	// buf[N] pos:  01234 56789
-	char buf[12] = "   x\0 0xx\0";
-	// actually "   x\0 xxx\0", but want to share string with print_ascii.
+	char buf[12] = "   x\0 xxx\0";
+	// share string with print_ascii.
 	// [12] because we take three 32bit stack slots anyway, and
 	// gcc is too dumb to initialize with constant stores,
 	// it copies initializer from rodata. Oh well.
@@ -419,7 +419,7 @@ print_ascii(size_t n_bytes, const char *block,
 		const char *unused_fmt_string UNUSED_PARAM)
 {
 	// buf[N] pos:  01234 56789
-	char buf[12] = "   x\0 0xx\0";
+	char buf[12] = "   x\0 xxx\0";
 
 	while (n_bytes--) {
 		const char *s;
@@ -458,8 +458,9 @@ print_ascii(size_t n_bytes, const char *block,
 		case '\x7f':
 			s = " 177";
 			break;
-		default: /* c is never larger than 040 */
-			buf[7] = (c >> 3) + '0';
+		default:
+			buf[6] = (c >> 6 & 3) + '0';
+			buf[7] = (c >> 3 & 7) + '0';
 			buf[8] = (c & 7) + '0';
 			s = buf + 5;
 		}
-- 
1.8.5.3



More information about the busybox mailing list