bug-coreutils
[Top][All Lists]
Advanced

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

bug#18726: dirname & basename


From: Eric Blake
Subject: bug#18726: dirname & basename
Date: Tue, 14 Oct 2014 16:34:58 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.1.1

tag 18726 notabug
thanks

On 10/14/2014 04:12 PM, Alan Stebbens wrote:
> If dirname is given an argument of "-ANYTHING", it should return the empty
> string.

Thanks for the report.  However, you are forgetting about the POSIX
rules on argument parsing; you are also wrong that dirname would return
the empty string (it returns '.', not the empty string, for a relative
name with no slash).

Here's the POSIX rules:

http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
...
"Guideline 10:
    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."
...
"Guideline 14:
    If an argument can be identified according to Guidelines 3 through
10 as an option, or as a group of options without option-arguments
behind one '-' delimiter, then it should be treated as such."
...
"The utilities in the Shell and Utilities volume of POSIX.1-2008 that
claim conformance to these guidelines shall conform completely to these
guidelines as if these guidelines contained the term "shall" instead of
"should". On some implementations, the utilities accept usage in
violation of these guidelines for backwards-compatibility as well as
accepting the required form."

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html
"OPTIONS
...
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.

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>, it could safely do it as:

foo -- -myfile

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

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/basename.html
"OPTIONS
    None."

Therefore, POSIX requires that 'basename' shall recognize '--' as the
end of options and the start of arguments, and that conforming uses MUST
pass '--' if the first argument has a leading dash and must not be
confused as an extension option.

Try this instead:

$ basename -- -ANYTHING
-ANYTHING
$ dirname -- -ANYTHING
.


> Currently, both programs return an error about '-X' where 'X' is an invalid
> option.

Which is the CORRECT behavior, given that POSIX allows implementations
to have extensions, and you forgot the use of -- to end options.

> 
> This error occurred in a script using "$0", eg: dirname $0, and the script
> was started with 'bash', and '$0' was '-bash'.

The bug is in your script, not coreutils; therefore, I'm closing this as
not a bug.  Feel free to respond if you have further comments or questions.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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