logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: 46ec041c11acdc8a5bdac738685a45e84941cfc0
parent 24930bf695d343c6644504f63769e9ed881d040c
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue,  3 Sep 2024 21:42:31 +0200

cmd/printf: add support for \xHH backslash-escape

Diffstat:

Mcmd/printf.112+++++++++++-
Mcmd/printf.c16++++++++++++++++
2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/cmd/printf.1 b/cmd/printf.1 @@ -99,15 +99,22 @@ Write a <vertical tab> character. Write a <single quote> character. .It Cm \e\e Write a backslash character. +.It Cm \ex Ns Ar hex-num +Write a byte whose +value is the 1- or 2-digits +hexadecimal number +.Ar hex-num . .It Cm \e Ns Ar num Write a byte whose value is the 1-, 2-, or 3-digit octal number .Ar num . +.El Multibyte characters can be constructed using multiple +.Cm \ex Ns Ar hex-num +or .Cm \e Ns Ar num sequences. -.El .Pp Each format specification is introduced by the percent character (``%''). @@ -368,6 +375,9 @@ The command is expected to be compatible with the .St -p1003.2 specification. +The +.Cm \ex Ns Ar hex-num +backslash-escape is an extension. .Sh HISTORY The .Nm diff --git a/cmd/printf.c b/cmd/printf.c @@ -568,6 +568,22 @@ escape(char *fmt, int percent, size_t *len) *store = '\v'; break; /* octal constant */ + case 'x': /* hex */ + c = 2; + fmt++; + for(value = 0; c-- && isxdigit(*fmt); ++fmt) + { + value <<= 4; + if(*fmt <= '9') + value += *fmt - '0'; + else if(*fmt <= 'F') + value += *fmt - 'A' + 10; + else + value += *fmt - 'a' + 10; + } + --fmt; + *store = (char)value; + break; case '0': case '1': case '2':