On Sat, 6 Jul 2019 at 01:31, Eric Blake <ebl...@redhat.com> wrote:
>
> On 7/5/19 11:16 PM, Geyslan G. Bem wrote:
> >>From 99ed9353c43fc8682f99a70d8a0b97d75f78f3f8 Mon Sep 17 00:00:00 2001
> > From: "Geyslan G. Bem" <geys...@gmail.com>
> > Date: Sat, 6 Jul 2019 01:04:40 -0300
> > Subject: [PATCH] cat: open any file name after "--"
> >
> > * src/cat.c (main): Treat "-" after "--" as file, not as stdin.
>
> Sorry, but this patch violates POSIX.

I see.

12.2 Utility Syntax Guidelines, Guideline 10 says:
"The first -- argument that is not an option-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."

And Guideline 13 stands out with "For utilities that use operands to
represent files
to be opened for either reading or writing, the '-' operand should be
used to mean
only standard input (or standard output when it is clear from context
that an output file is being specified) or a file named -."

I think Guideline 13 makes clear that '-' is either stdin or a file
named '-' (xor).

Thus, my patch embarrassingly violates POSIX.

Thank you for point it out. :)

>
> https://pubs.opengroup.org/onlinepubs/9699919799/toc.htm
>
> "If a file is '-', the cat utility shall read from the standard input at
> that point in the sequence."
>
> Note that [file...] per the SYNOPSIS is NOT an option, but an argument.
> The '--' argument is an end-of-option marker, required only when you
> want the next command line word (or all subsequent command line words
> when you are not using POSIXLY_CORRECT to suppress GNU getopt argument
> permutation) to be recognized as an argument rather than an option.  But
> since there is no such thing as an empty-string option, '-' is not
> recognized as an option in the first place, regardless of whether or not
> '--' was present.

POSIX sometimes pinches our heels; see, for instance, sh man RATIONALE:

"Thus, this volume of POSIX.1‐2008 allows the single <hyphen> to mark
the end of the options, in addition to the use of the regular "--" argument,
because it was considered that the older practice was so pervasive."

When it comes to that piece of software POSIX agrees with a non-standard
utility syntax considering '-' as the same as "--". But that's crystal due
historical usage.

>
> If you wish to cat a file named '-' in the current directory, spell it
> './-' or use an absolute path to that file.  That's true of all command
> line utilities that treat '-' as stdin (not just cat).
>
> --
> Eric Blake, Principal Software Engineer
> Red Hat, Inc.           +1-919-301-3226
> Virtualization:  qemu.org | libvirt.org
>


-- 
Regards,

Geyslan G. Bem

Reply via email to