od: fix "od -b"

This commit is contained in:
Denis Vlasenko 2008-04-14 19:50:06 +00:00
parent c7131c3e58
commit 7089c31d57
2 changed files with 27 additions and 12 deletions

View File

@ -508,10 +508,10 @@ check_and_close(void)
} }
/* If S points to a single valid modern od format string, put /* If S points to a single valid modern od format string, put
a description of that format in *TSPEC, make *NEXT point at the a description of that format in *TSPEC, return pointer to
character following the just-decoded format (if *NEXT is non-NULL), character following the just-decoded format.
and return zero. For example, if S were "d4afL" For example, if S were "d4afL", we will return a rtp to "afL"
*NEXT would be set to "afL" and *TSPEC would be and *TSPEC would be
{ {
fmt = SIGNED_DECIMAL; fmt = SIGNED_DECIMAL;
size = INT or LONG; (whichever integral_type_size[4] resolves to) size = INT or LONG; (whichever integral_type_size[4] resolves to)
@ -521,9 +521,8 @@ check_and_close(void)
S_ORIG is solely for reporting errors. It should be the full format S_ORIG is solely for reporting errors. It should be the full format
string argument. */ string argument. */
static void static const char *
decode_one_format(const char *s_orig, const char *s, const char **next, decode_one_format(const char *s_orig, const char *s, struct tspec *tspec)
struct tspec *tspec)
{ {
enum size_spec size_spec; enum size_spec size_spec;
unsigned size; unsigned size;
@ -536,7 +535,6 @@ decode_one_format(const char *s_orig, const char *s, const char **next,
unsigned field_width = 0; unsigned field_width = 0;
int pos; int pos;
assert(tspec != NULL);
switch (*s) { switch (*s) {
case 'd': case 'd':
@ -562,13 +560,14 @@ decode_one_format(const char *s_orig, const char *s, const char **next,
s = end; s = end;
} }
} else { } else {
static const uint8_t CSIL_sizeof[] = { static const uint8_t CSIL_sizeof[4] = {
sizeof(char), sizeof(char),
sizeof(short), sizeof(short),
sizeof(int), sizeof(int),
sizeof(long), sizeof(long),
}; };
size = CSIL_sizeof[p - CSIL]; size = CSIL_sizeof[p - CSIL];
s++; /* skip C/S/I/L */
} }
#define ISPEC_TO_FORMAT(Spec, Min_format, Long_format, Max_format) \ #define ISPEC_TO_FORMAT(Spec, Min_format, Long_format, Max_format) \
@ -716,8 +715,7 @@ decode_one_format(const char *s_orig, const char *s, const char **next,
if (tspec->hexl_mode_trailer) if (tspec->hexl_mode_trailer)
s++; s++;
if (next != NULL) return s;
*next = s;
} }
/* Decode the modern od format string S. Append the decoded /* Decode the modern od format string S. Append the decoded
@ -733,7 +731,7 @@ decode_format_string(const char *s)
struct tspec tspec; struct tspec tspec;
const char *next; const char *next;
decode_one_format(s_orig, s, &next, &tspec); next = decode_one_format(s_orig, s, &tspec);
assert(s != next); assert(s != next);
s = next; s = next;

17
testsuite/od.tests Executable file
View File

@ -0,0 +1,17 @@
#!/bin/sh
# Copyright 2008 by Denys Vlasenko
# Licensed under GPL v2, see file LICENSE for details.
. testing.sh
# testing "test name" "options" "expected result" "file input" "stdin"
testing "od -b" \
"echo HELLO | od -b" \
"\
0000000 110 105 114 114 117 012
0000006
" \
"" "HELLO"
exit $FAILCOUNT