scripts/echo.c: fix NUL handling in "abc\0 def"

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2021-12-28 21:05:59 +01:00
parent f1d06462e8
commit 0fcc7f5f73

View File

@ -153,25 +153,32 @@ int main(int argc, char **argv)
if (!eflag) { if (!eflag) {
/* optimization for very common case */ /* optimization for very common case */
fputs(arg, stdout); fputs(arg, stdout);
} else while ((c = *arg++)) { } else
if (c == eflag) { /* Check for escape seq. */ while ((c = *arg++) != '\0') {
if (c == eflag) {
/* This is an "\x" sequence */
if (*arg == 'c') { if (*arg == 'c') {
/* '\c' means cancel newline and /* "\c" means cancel newline and
* ignore all subsequent chars. */ * ignore all subsequent chars. */
goto ret; goto ret;
} }
{ /* Since SUSv3 mandates a first digit of 0, 4-digit octals
/* Since SUSv3 mandates a first digit of 0, 4-digit octals * of the form \0### are accepted. */
* of the form \0### are accepted. */ if (*arg == '0') {
if (*arg == '0') { if ((unsigned char)(arg[1] - '0') < 8) {
/* NB: don't turn "...\0" into "...\" */ /* 2nd char is 0..7: skip leading '0' */
if (arg[1] && ((unsigned char)(arg[1]) - '0') < 8) { arg++;
arg++;
}
} }
/* bb_process_escape_sequence handles NUL correctly }
* ("...\" case. */ /* bb_process_escape_sequence handles NUL correctly
c = bb_process_escape_sequence(&arg); * ("...\" case). */
{
/* optimization: don't force arg to be on-stack,
* use another variable for that. ~30 bytes win */
const char *z = arg;
c = bb_process_escape_sequence(&z);
arg = z;
} }
} }
putchar(c); putchar(c);