Source-Changes-D archive

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

Re: CVS commit: src/bin/echo



    Date:        Wed, 15 May 2024 02:33:23 +0300
    From:        Valery Ushakov <uwe%stderr.spb.ru@localhost>
    Message-ID:  <ZkP0w-YCNx9YlSAK%snips.stderr.spb.ru@localhost>

  | I vaguely remember I read somewhere that printf(1) was specifically
  | conceived to take no options, but that can be planted memories.  May
  | be it's indeed induced by the old state of affairs in our version.

POSIX printf(1) has no options (it says in its page "OPTIONS: None")
and our printf has no options, but implementations are allowed to
extend the standard, and add some, POSIX says (XCU 1.4) that when it
says "OPTIONS: None" what it means is:

  Default Behavior: When this section is listed as ``None.'', it means that
  the implementation need not support any options. Standard utilities that
  do not accept options, but that do accept operands, shall recognize "--"
  as a first argument to be discarded.

That is more or less what we do, but for backwards compat reasons, not
always.

  The requirement for recognizing "--" is because conforming applications
  need a way to shield their operands from any arbitrary options that the
  implementation may provide as an extension. For example, if the standard
  utility foo is listed as taking no options, and the application needed
  to give it a pathname with a leading <hyphen-minus>, it could safely do
  it as:

        foo -- -myfile

  and avoid any problems with -m used as an extension.

Other versions of printf(1) do have options, or at least, an option.
bash, ksh93 & zsh all have a "-v var" option to cause the output from
(the builtin) printf to be stored in var, rather than written to stdout.

It might be time to adjust our ptintf to be fully POSIX, and always handle
the "--" end of options option (it doesn't seem useful as a printf format,
and if needed, can always be written as printf -- --) rather than only
doing it sometimes.   Currently our printf used as "printf --" writes "--"
to stdout.   It really shouldn't.   When there is just 1 arg, and it
contains no %, it is also treated as a format, even if it starts with a '-'.

So, as you demonstrated, in our printf, printf -V prints "-V", but
if you try 'printf -V A B C' what you get is:

  sh: unknown option -- V
  Usage: printf format [arg ...]

(It would do the same if given a script with one of the -v var options
used in it).

The comments at the start of main() in our printf(1) source are nonsense
(though for backwards compat, we should just check for "--" rather than
using getopt() to do that for us, as we currently do, when we do it.)

kre

ps: I do appreciate that some of the mess that is there now is my doing,
but back when I did that I didn't understand POSIX as well as I do now.




Home | Main Index | Thread Index | Old Index