awk: allow printf('%c') to output NUL, closes 13486
Treat the output of printf as binary rather than a null-terminated string so that NUL characters can be output. This is considered to be a GNU extension, though it's also available in mawk and FreeBSD's awk. function old new delta evaluate 3487 3504 +17 awk_printf 504 519 +15 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/0 up/down: 32/0) Total: 32 bytes Signed-off-by: Ron Yorston <rmy@pobox.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
committed by
Denys Vlasenko
parent
f3a55b306e
commit
e8fe9f9635
@ -2155,7 +2155,10 @@ static int fmt_num(char *b, int size, const char *format, double n, int int_as_i
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* formatted output into an allocated buffer, return ptr to buffer */
|
/* formatted output into an allocated buffer, return ptr to buffer */
|
||||||
static char *awk_printf(node *n)
|
#if !ENABLE_FEATURE_AWK_GNU_EXTENSIONS
|
||||||
|
# define awk_printf(a, b) awk_printf(a)
|
||||||
|
#endif
|
||||||
|
static char *awk_printf(node *n, int *len)
|
||||||
{
|
{
|
||||||
char *b = NULL;
|
char *b = NULL;
|
||||||
char *fmt, *s, *f;
|
char *fmt, *s, *f;
|
||||||
@ -2209,6 +2212,10 @@ static char *awk_printf(node *n)
|
|||||||
nvfree(v);
|
nvfree(v);
|
||||||
b = xrealloc(b, i + 1);
|
b = xrealloc(b, i + 1);
|
||||||
b[i] = '\0';
|
b[i] = '\0';
|
||||||
|
#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
|
||||||
|
if (len)
|
||||||
|
*len = i;
|
||||||
|
#endif
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2666,6 +2673,7 @@ static var *evaluate(node *op, var *res)
|
|||||||
case XC( OC_PRINT ):
|
case XC( OC_PRINT ):
|
||||||
case XC( OC_PRINTF ): {
|
case XC( OC_PRINTF ): {
|
||||||
FILE *F = stdout;
|
FILE *F = stdout;
|
||||||
|
IF_FEATURE_AWK_GNU_EXTENSIONS(int len;)
|
||||||
|
|
||||||
if (op->r.n) {
|
if (op->r.n) {
|
||||||
rstream *rsm = newfile(R.s);
|
rstream *rsm = newfile(R.s);
|
||||||
@ -2703,8 +2711,12 @@ static var *evaluate(node *op, var *res)
|
|||||||
fputs(getvar_s(intvar[ORS]), F);
|
fputs(getvar_s(intvar[ORS]), F);
|
||||||
|
|
||||||
} else { /* OC_PRINTF */
|
} else { /* OC_PRINTF */
|
||||||
char *s = awk_printf(op1);
|
char *s = awk_printf(op1, &len);
|
||||||
|
#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
|
||||||
|
fwrite(s, len, 1, F);
|
||||||
|
#else
|
||||||
fputs(s, F);
|
fputs(s, F);
|
||||||
|
#endif
|
||||||
free(s);
|
free(s);
|
||||||
}
|
}
|
||||||
fflush(F);
|
fflush(F);
|
||||||
@ -2978,7 +2990,7 @@ static var *evaluate(node *op, var *res)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case XC( OC_SPRINTF ):
|
case XC( OC_SPRINTF ):
|
||||||
setvar_p(res, awk_printf(op1));
|
setvar_p(res, awk_printf(op1, NULL));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case XC( OC_UNARY ): {
|
case XC( OC_UNARY ): {
|
||||||
|
@ -383,6 +383,11 @@ testing "awk errors on missing delete arg" \
|
|||||||
"awk -e '{delete}' 2>&1" "awk: cmd. line:1: Too few arguments\n" "" ""
|
"awk -e '{delete}' 2>&1" "awk: cmd. line:1: Too few arguments\n" "" ""
|
||||||
SKIP=
|
SKIP=
|
||||||
|
|
||||||
|
optional FEATURE_AWK_GNU_EXTENSIONS
|
||||||
|
testing "awk printf('%c') can output NUL" \
|
||||||
|
"awk '{printf(\"hello%c null\n\", 0)}'" "hello\0 null\n" "" "\n"
|
||||||
|
SKIP=
|
||||||
|
|
||||||
# testing "description" "command" "result" "infile" "stdin"
|
# testing "description" "command" "result" "infile" "stdin"
|
||||||
testing 'awk negative field access' \
|
testing 'awk negative field access' \
|
||||||
'awk 2>&1 -- '\''{ $(-1) }'\' \
|
'awk 2>&1 -- '\''{ $(-1) }'\' \
|
||||||
|
Reference in New Issue
Block a user