Date:        Sat, 22 Feb 2025 09:48:29 +0100
    From:        Andreas Schwab <sch...@linux-m68k.org>
    Message-ID:  <87r03q5pr6....@linux-m68k.org>

  | On Feb 22 2025, Phi Debian wrote:
  | > I forgot to mention your trick to nuke the fmt reuse still works
  | > $ printf '%s %s %s %999$s' A B C D E F G
  | > A B C

Using %999$.0s rather than just %999$s was deliberate, it ensures that
if there happen to be 999 args, nothing is printed (the aim isn't to
do anything with the arg, just consider it used).   Also, sticking a space
before it is not a good idea, your output would be "A B C " - and while
that might be what you wanted, it usually would not be.

I should also mention that this isn't guaranteed to work according to
the POSIX spec - it says:

        10. The format operand shall be reused as often as necessary
            to satisfy the argument operands.  If conversion specifications
            beginning with a "%n$" sequence are used, on format reuse the
            value of n shall refer to the nth argument operand following the
            highest numbered argument operand consumed by the previous use
            of the format operand.

Note, it says "consumed" not "referenced" there, an argument that does
not exist (as we're presuming, or perhaps hoping, is the case with the
999th arg being selected) cannot really be said to be consumed, as it does
not exist.

Further, this spec also violates Chet's rule:

chet.ra...@case.edu said:
  | There is no user who would think that using a numbered conversion specifier
  | is not an absolute position in the original argument list.

which does not apply when the format string is reused -- format string
reuse in the presence of numbered conversions would make a lot more
sense if it did, and %2$ *always* (on every iteration of the format
string) meant the 2nd arg.   Then mixing numbered and unnumbered
conversion specs would make sense, with only the unnumbered ones
consuming the excess args (and would make them make absolutely no
sense with only numbered conversions of course).

And finally, to Andreas' comment which was what inspired me to continue
this relatively meaningless thread:

  | As long as NL_ARGMAX >= 999.

No, there is no such restriction.  printf(3) (for implementation reasons)
needs to have such a limit, printf(1) does not (and doesn't).   It should
be possible to do %999999999999999999999999$s if one wanted, though that's
likely to blow someone's string->integer conversion limits, that there can
never be, or at least not in any implementation we're going to see any time
soon, anywhere near that number of args is irrelevant, the idea is to name
one which doesn't exist, not one which might.

kre


Reply via email to