GNU bug report logs -
#18987
the bourne shell printf-vs-\xHH portability trap
Previous Next
Reported by: Jim Meyering <jim <at> meyering.net>
Date: Fri, 7 Nov 2014 17:15:03 UTC
Severity: normal
Done: Jim Meyering <jim <at> meyering.net>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
I have some qualms about that patch. It assumes the C locale, and it's a bit
safer to spell it out as in '0-9abcdefABCDEF'. Also, the temporary streams
(i.e., the output of 'COMMAND inside '$(COMMAND)') are not text, and arguably
this does not conform to POSIX (POSIX is murky here) and anyway I suspect some
picky shells will complain. Third and most important, it'd be nicer if
hex_printf_ worked like 'printf', except with support for hexadecimal escapes.
How about something like the following instead? It's brute-force, but it should
be portable.
hex_printf_()
{
hex_printf_format=$(printf '%s\n' "$1" | sed '
s/^/_/
s/$/_/
s/\([^\\]\(\\\\\)*\\x\)\([0-9aAbBcCdDeEfF][^0-9aAbBcCdDeEfF]\)/\10\3/g
s/\([^\\]\(\\\\\)*\\x\)\([0-3]\)/\10\3/g
s/\([^\\]\(\\\\\)*\\x\)\([4-7]\)/\11\3/g
s/\([^\\]\(\\\\\)*\\x\)\([89aAbB]\)/\12\3/g
s/\([^\\]\(\\\\\)*\\x\)\([cCdDeEfF]\)/\13\3/g
s/\([^\\]\(\\\\\)*\\x[0-3]\)[048cC]\([0-7]\)/\1,0\3/g
s/\([^\\]\(\\\\\)*\\x[0-3]\)[048cC]\([89aAbBcCdDeEfF]\)/\1,1\3/g
s/\([^\\]\(\\\\\)*\\x[0-3]\)[159dD]\([0-7]\)/\1,2\3/g
s/\([^\\]\(\\\\\)*\\x[0-3]\)[159dD]\([89abcdef]\)/\1,3\3/g
s/\([^\\]\(\\\\\)*\\x[0-3]\)[26aAeE]\([0-7]\)/\1,4\3/g
s/\([^\\]\(\\\\\)*\\x[0-3]\)[26aAeE]\([89aAbBcCdDeEfF]\)/\1,5\3/g
s/\([^\\]\(\\\\\)*\\x[0-3]\)[37bBfF]\([0-7]\)/\1,6\3/g
s/\([^\\]\(\\\\\)*\\x[0-3]\)[37bBfF]\([89aAbBcCdDeEfF]\)/\1,7\3/g
s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[08]/\1\3\40/g
s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[19]/\1\3\41/g
s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[2aA]/\1\3\42/g
s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[3bB]/\1\3\43/g
s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[4cC]/\1\3\44/g
s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[5dD]/\1\3\45/g
s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[6eE]/\1\3\46/g
s/\([^\\]\(\\\\\)*\\\)x\([0-3]\),\([0-7]\)[7fF]/\1\3\47/g
s/^_//
s/_$//
')
shift
printf "$hex_printf_format" "$@"
}
hex_printf_ '\x34\\x%dX\x45\n' 100
This bug report was last modified 10 years and 198 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.