| Author |
Symbolic links and real paths
|
|
| Grumble 2004-03-26, 11:46 am |
| Assume I have the following configuration:
/foo
/bar
mysymlink -> /toto/tutu (a symbolic link)
/toto
/tutu
exe1 (either a shell script or a binary)
/baba
exe2
Assume exe1 calls ../baba/exe2
If I run /toto/tutu/exe1 then everything works:
exe1 calls /toto/tutu/../baba/exe2
(i.e. /toto/baba/exe2)
But if I run /foo/bar/mysymlink/exe1 then it breaks
exe1 tries to call /foo/bar/mysymlink/../baba/exe2
which is taken as /foo/bar/baba/exe2
which does not exist.
In the second case, if exe1 is a shell script then
$0 = /foo/bar/mysymlink/exe1
How can I "translate" /foo/bar/mysymlink/exe1 to the real path
/toto/tutu/exe1 with shell commands?
If exe2 is a C program then
argv[0] = "/foo/bar/mysymlink/exe1"
How can I "translate" /foo/bar/mysymlink/exe1 to the real path
/toto/tutu/exe1 with C system calls?
--
Regards, Grumble
| |
| Pascal Bourguignon 2004-03-26, 11:46 am |
| Grumble <invalid@kma.eu.org> writes:
> How can I "translate" /foo/bar/mysymlink/exe1 to the real path
> /toto/tutu/exe1 with shell commands?
It depends on the shell used. (Some shells maintain the path followed
instead of the path reached). In bash, the easiest way is to use:
cd /foo/bar/mysymlink/
real=$(pwd)/exe1
> If exe2 is a C program then
> argv[0] = "/foo/bar/mysymlink/exe1"
Not necessarily! It depends on how exe2 is called. argv[0] may as
well contain:
"../mysymlink/exe1"
"exe1"
"Hello World!"
> How can I "translate" /foo/bar/mysymlink/exe1 to the real path
> /toto/tutu/exe1 with C system calls?
Similarly:
chdir("/foo/bar/mysymlink");
real_dir=getwd(buffer);
real=strcat(real_dir,"/exe1");
--
__Pascal_Bourguignon__ http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he doesn't
want merely because you think it would be good for him.--Robert Heinlein
http://www.theadvocates.org/
| |
| Marc Rochkind 2004-03-26, 11:46 am |
|
"Grumble" <invalid@kma.eu.org> wrote in message
news:c41ai6$l0p$1@news-rocq.inria.fr...
> Assume I have the following configuration:
>
> /foo
> /bar
> mysymlink -> /toto/tutu (a symbolic link)
>
> /toto
> /tutu
> exe1 (either a shell script or a binary)
> /baba
> exe2
>
>
> Assume exe1 calls ../baba/exe2
>
> If I run /toto/tutu/exe1 then everything works:
> exe1 calls /toto/tutu/../baba/exe2
> (i.e. /toto/baba/exe2)
>
> But if I run /foo/bar/mysymlink/exe1 then it breaks
> exe1 tries to call /foo/bar/mysymlink/../baba/exe2
> which is taken as /foo/bar/baba/exe2
> which does not exist.
>
> In the second case, if exe1 is a shell script then
> $0 = /foo/bar/mysymlink/exe1
>
> How can I "translate" /foo/bar/mysymlink/exe1 to the real path
> /toto/tutu/exe1 with shell commands?
>
> If exe2 is a C program then
> argv[0] = "/foo/bar/mysymlink/exe1"
>
> How can I "translate" /foo/bar/mysymlink/exe1 to the real path
> /toto/tutu/exe1 with C system calls?
>
> --
> Regards, Grumble
>
I may be missing something, but it appears that you have a relative path
built into exe1, so it's meaning depends on the current directory for the
process running exe1. Merely running a program doesn't set the current
directory.
--
Marc Rochkind
"Advanced UNIX Programming" (publishing April 2004)
www.basepath.com/aup
| |
| Valentin Nechayev 2004-03-27, 11:35 am |
| >>> Grumble wrote:
G> How can I "translate" /foo/bar/mysymlink/exe1 to the real path
G> /toto/tutu/exe1 with shell commands?
F=/foo/bar/mysymlink/exe1
cd "`dirname $F`"
REALDIR="`pwd -P`"
echo "$REALDIR/`basename $F`"
This doesn't include situation when last path part (i.e. file name) is also
symlink, but works when symlink is in directories.
Otherwise you should implement full namei lookup logic of unix kernel;
this means cycle of adding symlink contents to file path until path has
no symlinks.
G> If exe2 is a C program then
G> argv[0] = "/foo/bar/mysymlink/exe1"
G>
G> How can I "translate" /foo/bar/mysymlink/exe1 to the real path
G> /toto/tutu/exe1 with C system calls?
As in shell example, but getcwd() instead of `pwd -P`.
-netch-
| |
| Mohun Biswas 2004-03-27, 3:37 pm |
|
> How can I "translate" /foo/bar/mysymlink/exe1 to the real path /toto/tutu/exe1 with C system calls?
The short answer is "man realpath". Realpath is in SUS and should be
supported by all modern Unices.
MB
| |
| Grumble 2004-03-30, 6:36 am |
| Grumble wrote:
[snip]
Thanks to all.
|
|
|
|