Re: Making a path absolute portably

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

 



On 7/10/19 7:54 AM, Bob Friesenhahn wrote:
> On Wed, 10 Jul 2019, Sébastien Hinderer wrote:
> 
>> Dear all,
>>
>> I'd need to compute the absolute path of srcdir in a portable way (it's
>> okay if it includes symlinks).
>>
>> Currently I use
>>
>> abssrcdir=$(cd "${srcdir}"; echo $PWD)
>> which seems to work fine.
>>
>> Is that considered portable enough?
> 
> It appears that $PWD is a shell extension, so I don't think so.

No, POSIX requires that $PWD always be accurate, so that part is portable.

>  Even
> the shell syntax being used to execute the external command is not very
> portable.

The use of $() instead of `` is fairly portable except to old Solaris
/bin/sh - since you asked on the autoconf list, if your usage is inside
a configure script, then we have probably already happened to re-execute
your script to be running inside a shell that supports $(), (but
surprisingly, autoconf doesn't actually yet guarantee support for $() by
default).

>  In addition to that, if the cd "${srcdir}" fails, it would
> then capture the wrong directory.
> 
> This syntax is proven to be portable and reliable:
> 
> abssrcdir="`cd $srcdir && pwd`"

No, that still has problems if $srcdir contains whitespace or begins
with '-'; and also eats trailing newlines in an unusually-named
directory.  Do you have to worry about that in practice? Probably not.
But being truly portable vs. good enough can be surprisingly hard.


-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Autoconf mailing list
Autoconf@xxxxxxx
https://lists.gnu.org/mailman/listinfo/autoconf

[Index of Archives]     [GCC Help]     [Kernel Discussion]     [RPM Discussion]     [Red Hat Development]     [Yosemite News]     [Linux USB]     [Samba]

  Powered by Linux