[Toybox] Numeric values in dd operands

Robert Thompson robertt.thompson at gmail.com
Tue Feb 20 12:56:35 PST 2018


I've seen a fair number of scripts that use printf's ability to
interpret hex and octal arguments. This is often combined with
printf's ability to format outputs into decimal, octal, or hex, to
make a convenient base-shifter. For this discussion, it's the argument
conversion, not the formatting that's relevant...

Working from memory, I've seen patterns such as those below, often in
$() (or ` `) expansions. In some cases, the converted number was
roundtripped during the lifetime of the script.


printf "%u\n" 0377
255
(I've seen this one using both the "unsigned integer" and the various
printf(3) integer formatters)


printf "%o\n" 0x1b
33

printf "%#o\n" 0x1b
033

printf "%x\n" 77
4d

printf "0x%x\n" 77
0x4d

printf "%#x\n" 77
0x4d




On 2/20/18, enh <enh at google.com> wrote:
> On Tue, Feb 20, 2018 at 9:57 AM, Rob Landley <rob at landley.net> wrote:
>> On 02/20/2018 11:32 AM, enh wrote:
>>> On Tue, Feb 20, 2018 at 9:28 AM, Rob Landley <rob at landley.net> wrote:
>>>> A real user piped up and said their existing script doesn't work with my
>>>> tool.
>>>> That feedback is of interest to me.
>>>
>>> and as the person who set us down the strtol path
>>> (https://github.com/landley/toybox/commit/d5088a059649daf34e729995bb3daa3eb64fa432#diff-ce001a87e82f850a38fd93183e12b417),
>>> the original request i had was just for hex. like you say, no-one's
>>> used octal (on purpose) for anything other than mode for decades now.
>>
>> I'm tempted to have atolx() skip leading zeroes, and then base 16 if the
>> first
>> character is an x and base 10 otherwise. Except supporting the - basically
>> means
>> open coding the sucker...
>>
>> Anyway, my _real_ question is, if I'm yanking octal from dd= should I yank
>> it
>> from everywhere? I still think it's useful for "printf %d 0123" to be able
>> to
>> cope with it (especially since "printf %o 668" prints 1234). And maybe
>> $((0123))
>> needs it too?
>>
>> No clue where to draw the line on this one. Hmmm...
>
> you could make it opt-in and add octal support to places where its
> absence is actually noticed.
>
>> Rob
> _______________________________________________
> Toybox mailing list
> Toybox at lists.landley.net
> http://lists.landley.net/listinfo.cgi/toybox-landley.net
>



More information about the Toybox mailing list