Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 17 Sep 2010 00:47:48 +0200
From:      Jilles Tjoelker <jilles@stack.nl>
To:        Juli Mallett <jmallett@FreeBSD.org>
Cc:        svn-src-head@freebsd.org, svn-src-all@freebsd.org, src-committers@freebsd.org, David O'Brien <obrien@freebsd.org>, Bruce Evans <brde@optusnet.com.au>
Subject:   Re: svn commit: r212374 - head/usr.bin/printf
Message-ID:  <20100916224748.GA60291@stack.nl>
In-Reply-To: <AANLkTimJfeXdP_9caoqBg3WPxpbgLRutDzBw%2BXM6veo5@mail.gmail.com>
References:  <201009091927.o89JReXm022426@svn.freebsd.org> <AANLkTi=1KXtmwVobbnk8vCkQKDN01RxWh8Q5ZZqzU0tq@mail.gmail.com> <20100916084442.GB21622@dragon.NUXI.org> <20100916212043.N724@delplex.bde.org> <AANLkTimJfeXdP_9caoqBg3WPxpbgLRutDzBw%2BXM6veo5@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Sep 16, 2010 at 01:10:33PM -0700, Juli Mallett wrote:
> On Thu, Sep 16, 2010 at 04:36, Bruce Evans <brde@optusnet.com.au> wrote:
> > WHat about the lesser burder on a character or two for putting -- in the
> > synopsis where it is more visible:

> >        printf [--] format [arguments ...]

> Right, that's what I've been thinking about.  For that matter, does
> printf(1) need to use getopt(3) at all?  It seems like simply checking
> for "--" could be sufficient.  Does POSIX require that it exit with an
> error if it's given any options?

It does not: implementations may add options as an extension and future
versions of POSIX may add options. So while it would be a valid
extension to allow format strings starting with "-" without "--"
protection (other than "--" itself), I think it would be unwise.

Most implementations of printf(1), other than the zsh builtin, require
"--" protection for format strings starting with "-". Some of them also
accept a few options: ksh93's builtin and GNU coreutils accept common
options like --help, bash's builtin has a -v option to write the output
directly into a variable (which seems bad to me by the way; I think
printf(1) should usually be a builtin but it should be possible to use
an external version with only performance and ARG_MAX as downsides).

It is preferable if attempts to use such options fail loudly rather than
silently writing garbage.

I do not like "frivolous" extensions either, extensions that cause
scripts written for FreeBSD to break on other systems without a good
reason.

Also note that the strict treatment of "-" is possible because printf(1)
has historically recognized and required "--". Utilities that have not
historically recognized "--" such as most of the special builtins may
need a weaker approach. For example, in -current I have changed . and
exec to treat "--" specially but not any other string starting with "-".
The strict approach is cleaner but may cause compatibility problems in
those cases.

> At the very least, it seems like it would be more productive to have
> printf(1) give unusually-helpful error messages (perhaps merely
> changing its usage output to match that synopsis) if it encounters a
> string beginning with '-' other than "--".

That would be acceptable.

-- 
Jilles Tjoelker



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20100916224748.GA60291>