Given a directory name represented as a pathname or namestring, is there
a platform- and implementation-independent way to get the parent
directory of that directory?
If not, is there a way that mostly works on modern platforms?
Thanks,
--
Peter Heslin (http://www.dur.ac.uk/p.j.heslin)
······@heslin.eclipse.co.uk writes:
> Given a directory name represented as a pathname or namestring, is there
> a platform- and implementation-independent way to get the parent
> directory of that directory?
>
> If not, is there a way that mostly works on modern platforms?
(merge-pathnames (make-pathname :directory '(:relative :up) :defaults dirpath)
dirpath)
--
__Pascal Bourguignon__ http://www.informatimago.com/
NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
Pascal Bourguignon <······@informatimago.com> wrote:
+---------------
| ······@heslin.eclipse.co.uk writes:
| > Given a directory name represented as a pathname or namestring, is there
| > a platform- and implementation-independent way to get the parent
| > directory of that directory?
| >
| > If not, is there a way that mostly works on modern platforms?
|
| (merge-pathnames (make-pathname :directory '(:relative :up)
| :defaults dirpath)
| dirpath)
+---------------
While technically correct, what he might get on some implementations
is, I suspect, not quite what he was hoping for, e.g., on CMUCL-19c:
> (default-directory)
#p"/usr/u/rpw3/src/cmd/misc/"
> (let ((dirpath (default-directory)))
(merge-pathnames (make-pathname :directory '(:relative :up)
:defaults dirpath)
dirpath))
#p"/usr/u/rpw3/src/cmd/misc/../"
>
-Rob
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607
····@rpw3.org (Rob Warnock) writes:
> Pascal Bourguignon <······@informatimago.com> wrote:
> +---------------
> | ······@heslin.eclipse.co.uk writes:
> | > Given a directory name represented as a pathname or namestring, is there
> | > a platform- and implementation-independent way to get the parent
> | > directory of that directory?
> | >
> | > If not, is there a way that mostly works on modern platforms?
> |
> | (merge-pathnames (make-pathname :directory '(:relative :up)
> | :defaults dirpath)
> | dirpath)
> +---------------
>
> While technically correct, what he might get on some implementations
> is, I suspect, not quite what he was hoping for, e.g., on CMUCL-19c:
>
> > (default-directory)
>
> #p"/usr/u/rpw3/src/cmd/misc/"
> > (let ((dirpath (default-directory)))
> (merge-pathnames (make-pathname :directory '(:relative :up)
> :defaults dirpath)
> dirpath))
>
> #p"/usr/u/rpw3/src/cmd/misc/../"
Then you can use truename:
* (let ((dirpath (ext:default-directory)))
(values dirpath
(truename
(merge-pathnames (make-pathname :directory '(:relative :up)
:defaults dirpath)
dirpath))))
#p"/local/users/pjb/src/"
#p"/local/users/pjb/"
--
__Pascal Bourguignon__ http://www.informatimago.com/
WARNING: This product warps space and time in its vicinity.
Pascal Bourguignon wrote:
> ······@heslin.eclipse.co.uk writes:
>
> > Given a directory name represented as a pathname or namestring, is there
> > a platform- and implementation-independent way to get the parent
> > directory of that directory?
> >
> > If not, is there a way that mostly works on modern platforms?
>
> (merge-pathnames (make-pathname :directory '(:relative :up) :defaults dirpath)
> dirpath)
I don't mean to be dense, but can you explain why :defaults dirpath and
the dirpath argument to merge-pathnames are not redundant?
i.e. can I not simply use
(merge-pathnames (make-pathname :directory '(:relative :up)) dirpath)
···············@hotmail.com" <············@gmail.com> writes:
> Pascal Bourguignon wrote:
>> ······@heslin.eclipse.co.uk writes:
>>
>> > Given a directory name represented as a pathname or namestring, is there
>> > a platform- and implementation-independent way to get the parent
>> > directory of that directory?
>> >
>> > If not, is there a way that mostly works on modern platforms?
>>
>> (merge-pathnames (make-pathname :directory '(:relative :up) :defaults dirpath)
>> dirpath)
>
> I don't mean to be dense, but can you explain why :defaults dirpath and
> the dirpath argument to merge-pathnames are not redundant?
>
> i.e. can I not simply use
>
> (merge-pathnames (make-pathname :directory '(:relative :up)) dirpath)
In general on unix, it'll be the same.
However, with other file systems, notably when there is a host or a device,
on some implementations, it can make a difference.
--
__Pascal Bourguignon__ http://www.informatimago.com/
NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
From: Richard M Kreuter
Subject: Re: How to get the parent directory of a pathname?
Date:
Message-ID: <87veuo4azo.fsf@progn.net>
Pascal Bourguignon <······@informatimago.com> writes:
> ···············@hotmail.com" <············@gmail.com> writes:
>> I don't mean to be dense, but can you explain why :defaults dirpath
>> and the dirpath argument to merge-pathnames are not redundant?
>>
>> i.e. can I not simply use
>>
>> (merge-pathnames (make-pathname :directory '(:relative :up)) dirpath)
>
> In general on unix, it'll be the same.
> However, with other file systems, notably when there is a host or a
> device, on some implementations, it can make a difference.
Are such implementations conformant? From the dictionary entry for
MERGE-PATHNAMES:
| If PATHNAME does not specify a host, device, directory, name, or
| type, each such component is copied from DEFAULT-PATHNAME.
I suppose one could read the condition as "if PATHNAME specifies none
of host, device, directory, name and type", but I thought it meant
"for each component of host, device, directory, name, and type, if
PATHNAME does not specify the component...", since it seems pretty
odd to have a pathname with just a version.
--
RmK
Richard M Kreuter <·······@progn.net> writes:
> Pascal Bourguignon <······@informatimago.com> writes:
>> ···············@hotmail.com" <············@gmail.com> writes:
>
>>> I don't mean to be dense, but can you explain why :defaults dirpath
>>> and the dirpath argument to merge-pathnames are not redundant?
>>>
>>> i.e. can I not simply use
>>>
>>> (merge-pathnames (make-pathname :directory '(:relative :up)) dirpath)
>>
>> In general on unix, it'll be the same.
>> However, with other file systems, notably when there is a host or a
>> device, on some implementations, it can make a difference.
>
> Are such implementations conformant? From the dictionary entry for
> MERGE-PATHNAMES:
>
> | If PATHNAME does not specify a host, device, directory, name, or
> | type, each such component is copied from DEFAULT-PATHNAME.
>
> I suppose one could read the condition as "if PATHNAME specifies none
> of host, device, directory, name and type", but I thought it meant
> "for each component of host, device, directory, name, and type, if
> PATHNAME does not specify the component...", since it seems pretty
> odd to have a pathname with just a version.
Perhaps I've been overly cautious.
--
__Pascal Bourguignon__ http://www.informatimago.com/
"Remember, Information is not knowledge; Knowledge is not Wisdom;
Wisdom is not truth; Truth is not beauty; Beauty is not love;
Love is not music; Music is the best." -- Frank Zappa