[Top][All Lists]

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

Re: nonconformant behavior for printf(1) (you cannot interpret - as an o

From: Eric Blake
Subject: Re: nonconformant behavior for printf(1) (you cannot interpret - as an option char)
Date: Mon, 26 Nov 2007 21:54:52 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20071031 Thunderbird/ Mnenhy/

Hash: SHA1

Please keep replies on the list, so that others may chime in.

According to Rich Felker on 11/26/2007 9:41 PM:
>>> $ printf ---%s---\\n test
>>> bash: printf: --: invalid option
>> That's not a bug.  If you insist on printing with a format string that
>> starts with -, POSIX requires that you use -- to end arguments, as in:
>> $ printf -- ---%s---\\n test
>> - ---test---
>> If your non-builtin system printf behaves differently, that is a bug in
>> your system printf.
> This is simply not true. Read the synopsis for POSIX printf. It does
> not support options; usage is simply:
> printf format [argument ...]

POSIX merely states that a _conforming_ program cannot use any options;
however, it does not forbid the presence of options as extensions.
Furthermore, it states:

Guideline 10:
    The argument -- should be accepted as a delimiter indicating the end
of options. Any following arguments should be treated as operands, even if
they begin with the '-' character. The -- argument should not be used as
an option or as an operand.

The utilities in the Shell and Utilities volume of IEEE Std 1003.1-2001
that claim conformance to these guidelines shall conform completely to
these guidelines as if these guidelines contained the term "shall" instead
of "should".

> Using printf -- ---%s---\\n test is not valid at all and will produce
> unspecified output since there are no format specifiers in the format
> ("--") and yet there are arguments to be formatted.

You are wrong.  It is the _only_ POSIX-sanctioned way to print with a
format string starting with a hyphen, because the -- is required to be
interpreted as the end of options, and not as the format string.  The
format string is not an option, so it is the first word after the
end-of-options designator, or ---%s---\\n.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
Version: GnuPG v1.4.5 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org


reply via email to

[Prev in Thread] Current Thread [Next in Thread]