Subject: Re: bug#6124: basename/dirname are not POSIX compliant on arg starting with a dash

From: Eric Blake <[email-masked]>
Date: Thu, 06 May 2010 08:02:04 -0600

On 05/06/2010 07:23 AM, Vincent Lefevre wrote:
> If one follows POSIX, the correct way to do it is:
> 
>   base=$(basename "$path")
> 
> and this is robust because the first argument is necessarily the path
> (it cannot be an option, even "--").

POSIX does not forbid applications from adding extension options.  And
BSD basename has done just that:

SYNOPSIS
     basename string [suffix]
     basename [-a] [-s suffix] string [...]
     dirname string [...]

And since POSIX allows FreeBSD's extensions to exist (although a
portable script will not use them), it must also require that a portable
app be able to guarantee a way to bypass those extensions (ergo
base=$(basename -- "$path") is a POSIX requirement).

> According to
> 
>   grep 'basename --' /usr/bin/*
>   grep 'basename \$' /usr/bin/*
> 
> on my Debian machine, almost all scripts use basename without "--".
> The only exceptions are autoconf (but I'm not sure this counts, as
> there's a test for "basename --" first,

Can you find an actual example of this?  I think you read it
incorrectly, as really, autoconf first tests for 'basename -- /', but if
you indeed found an instance of autoconf testing for 'basename --', that
would be a bug in autoconf that needs to be reported to that list.

>> Note solaris behaves like busybox and openbsd behaves like coreutils.
> 
> Perhaps they're copying coreutils without looking at what POSIX says.

Or, more likely, perhaps they are independently obeying what POSIX says,
without reference to either coreutils or busybox.

-- 
Eric Blake   [email-masked]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org