For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > May 2004 > Final pathname following links









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Final pathname following links
KenW

2004-05-12, 9:08 pm

Is there a UNIX command that, given a pathname possibly containing
links, will return the "final" pathname with all links resolved? For
example:
/aa/bb
/aa/cc -> bb
/aa/dd -> /aa/cc
/aa/ee -> ../dd
Given /aa/ee, the command would return /aa/bb.
Thanks.
joe@invalid.address

2004-05-12, 9:08 pm

kwinieck@paymentech.com (KenW) writes:

> Is there a UNIX command that, given a pathname possibly containing
> links, will return the "final" pathname with all links resolved? For
> example:
> /aa/bb
> /aa/cc -> bb
> /aa/dd -> /aa/cc
> /aa/ee -> ../dd
> Given /aa/ee, the command would return /aa/bb.
> Thanks.


I don't know of any command line utility that would do this, but all
you need to do is write a little C program that calls readlink(2) in a
loop until you find the real file, or hit an error condition.

See http://www.opengroup.org/onlinepubs...s/readlink.html

Joe
--
"Surprise me"
- Yogi Berra when asked where he wanted to be buried.
Barry Margolin

2004-05-12, 9:08 pm

In article <m3hdut8cti.fsf@invalid.address>, joe@invalid.address wrote:

> kwinieck@paymentech.com (KenW) writes:
>
>
> I don't know of any command line utility that would do this, but all
> you need to do is write a little C program that calls readlink(2) in a
> loop until you find the real file, or hit an error condition.


The loop isn't quite so simple, since symlinks that contain relative
pathnames must be interpreted relative to the directory in which they're
found.

I think the function the OP is looking for is realpath().

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Adrian 'Dagurashibanipal' von Bidder

2004-05-12, 9:08 pm

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Clinging to sanity, Barry Margolin mumbled in his beard:

> I think the function the OP is looking for is realpath().


I've had a similar question recently - search this group, about two ws
ago, for postings from me.

summary: realpath is dangerous, but apparently there's no reliable
alternative atm.

greetings
- -- vbi

- --
The content of this message may or may not reflect the opinion of me, my
employer, my girlfriend, my cat or anybody else, regardless of the fact
whether such an employer, girlfriend, cat, or anybody else exists. I
(or my employer, girlfriend, cat or whoever) disclaim any legal
obligations resulting from the above message. You, as the reader of
this message, may or may not have the permission to redistribute this
message as a whole or in parts, verbatim or in modified form, or to
distribute any message at all.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: get my key from http://fortytwo.ch/gpg/92082481

iKcEARECAGcFAkCbNlZgGmh0dHA6Ly9mb3J0eXR3
by5jaC9sZWdhbC9ncGcvZW1h
aWwuMjAwMjA4MjI/ dmVyc2lvbj0xLjUmbWQ1c3VtPTVkZmY4NjhkMTE4
NDMyNzYw
NzFiMjVlYjcwMDZkYTNlAAoJEIukMYvlp/fWE6IAn0y2WhgIr+2Y0+tFF4pEibu/
6K0vAKDUEbUY2jsW4n5hswK0uDf7KBsSzQ==
=fr7/
-----END PGP SIGNATURE-----
KenW

2004-05-12, 9:08 pm

Thanks for the suggestion. I tried realpath(), but can't get it to
work (Solaris 8) for all the cases shown in my original message. For
case /aa/cc, it returns /aa/bb, which is good. But for the other cases
(/aa/dd, /aa/ee), it just returns the input argument pathname and
doesn't follow the links. E.g., for /aa/dd, it returns /aa/dd.
Ken
Barry Margolin

2004-05-12, 9:08 pm

In article <522ca62.0405070741.1efb1372@posting.google.com>,
kwinieck@paymentech.com (KenW) wrote:

> Thanks for the suggestion. I tried realpath(), but can't get it to
> work (Solaris 8) for all the cases shown in my original message. For
> case /aa/cc, it returns /aa/bb, which is good. But for the other cases
> (/aa/dd, /aa/ee), it just returns the input argument pathname and
> doesn't follow the links. E.g., for /aa/dd, it returns /aa/dd.
> Ken


I'm not sure why /aa/dd doesn't work, but /aa/ee points to a nonexisting
name.

/aa/ee -> ../dd

is equivalent to:

/aa/ee -> /dd

but there is no /dd. Maybe you meant for it to point to ./dd.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Eric Sosman

2004-05-12, 9:08 pm

KenW wrote:
>
> Thanks for the suggestion. I tried realpath(), but can't get it to
> work (Solaris 8) for all the cases shown in my original message. For
> case /aa/cc, it returns /aa/bb, which is good. But for the other cases
> (/aa/dd, /aa/ee), it just returns the input argument pathname and
> doesn't follow the links. E.g., for /aa/dd, it returns /aa/dd.
> Ken


I've just tried this on my own Solaris 8 system, and it
works as expected (that is, the first three cases all resolve
to /aa/bb, and the fourth reports "No such file or directory").
There may be an error in your code; would you care to post it?

--
Eric.Sosman@sun.com
KenW

2004-05-12, 9:08 pm

I tried again and got the same results as you. Thanks for the
encouragement to try again.

Ken
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com