tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

printf(1), sh(1), POSIX.2 and octal escape sequences



When refactoring and rewriting the scanning/parsing code for inetd(8), I
wanted to add, too, the possibility to pass octal escape sequences
(hex were already added) in order to be less surprising and to,
actually, support whatever an admin is acustomed to use when invoking
utilities.

Since this is unfortunately variable length, I first implemented it as
if escape and digit between 0 and 7 -> octal; if next digit between
0 and 7, shift left by 3 and add the decimal value; repeat for next.

The problem is that \777 is accepted -> 0377; \677 -> 0277;
\577 -> 0177; \477 -> 0077.

So I had the curiosity to use printf(1) and sh(1) to see how they
handled them.

Well: this way.

But isn't it incorrect? POSIX 2018 says:

'"\ddd", where ddd is a one, two, or three-digit octal number, shall be
written as a byte with the numeric value specified by the octal number.'

since 477 -> 777 are not byte values, shouldn't \777 be interpreted as
'\77' octal then the digit 7; \677 -> \67 octal then 7 etc. ?
-- 
        Thierry Laronde <tlaronde +AT+ polynum +dot+ com>
                     http://www.kergis.com/
                    http://kertex.kergis.com/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89  250D 52B1 AE95 6006 F40C


Home | Main Index | Thread Index | Old Index