a simple but strange problem

a simple but strange problem

am 14.11.2007 08:12:32 von david

First i create a symbol link to directory,then i change to the
directory the link point.
Now i should in the directory which the link point, right? But there's
a problem :

$ ln -s /home/david/Ubuntu/testdir /home/david/dir
$ cd /home/david/dir
$ pwd
/home/david/dir
$ /bin/pwd
/home/david/Ubuntu/testdir

Why the results returned by pwd and /bin/pwd are different?
And i have checked that pwd==/bin/pwd using the command which.

Re: a simple but strange problem

am 14.11.2007 08:39:07 von Joachim Schmitz

"david" schrieb im Newsbeitrag
news:1195024352.814012.193630@v29g2000prd.googlegroups.com.. .
> First i create a symbol link to directory,then i change to the
> directory the link point.
> Now i should in the directory which the link point, right? But there's
> a problem :
>
> $ ln -s /home/david/Ubuntu/testdir /home/david/dir
> $ cd /home/david/dir
> $ pwd
> /home/david/dir
> $ /bin/pwd
> /home/david/Ubuntu/testdir
>
> Why the results returned by pwd and /bin/pwd are different?
> And i have checked that pwd==/bin/pwd using the command which.
Well, I guess it's not, usually pwd is built into the shell and
_additionally_ available as a program.
Try pwd -L, pwd -P, /bin/pwd -L and /bin/pwd -P

I guess you'll find that pwd display the same as pwd -L and /bin/pwd
dispalyes the same as pwd -P, /bin/pwd -L and /bin/pwd -P

Why is that a problem to you?

Bye, Jojo

Re: a simple but strange problem

am 14.11.2007 09:09:41 von david

On 11 14 , 3 39 , "Joachim Schmitz"
wrote:
> "david" schrieb im Newsbeitragnews:1195024352.814012.193630@v29g2000prd.googleg roups.com...> First i create a symbol link to directory,then i change to the
> > directory the link point.
> > Now i should in the directory which the link point, right? But there's
> > a problem :
>
> > $ ln -s /home/david/Ubuntu/testdir /home/david/dir
> > $ cd /home/david/dir
> > $ pwd
> > /home/david/dir
> > $ /bin/pwd
> > /home/david/Ubuntu/testdir
>
> > Why the results returned by pwd and /bin/pwd are different?
> > And i have checked that pwd==/bin/pwd using the command which.
>
> Well, I guess it's not, usually pwd is built into the shell and
> _additionally_ available as a program.
> Try pwd -L, pwd -P, /bin/pwd -L and /bin/pwd -P
>
> I guess you'll find that pwd display the same as pwd -L and /bin/pwd
> dispalyes the same as pwd -P, /bin/pwd -L and /bin/pwd -P
>
> Why is that a problem to you?
>
> Bye, Jojo

Thanks.pwd==pwd -L and /bin/pwd==pwd -P
I want to see help page like man about pwd,but man page display /bin/
pwd not the command pwd built into the shell.
How to get the man page about pwd?

I think /bin/pwd use the API getcwd().Which API do pwd use to get
current directory?

Re: a simple but strange problem

am 14.11.2007 09:49:32 von wayne

david wrote:
> On 11 14 , 3 39 , "Joachim Schmitz"
> wrote:
>> "david" schrieb im Newsbeitragnews:1195024352.814012.193630@v29g2000prd.googleg roups.com...> First i create a symbol link to directory,then i change to the
>>> directory the link point.
>>> Now i should in the directory which the link point, right? But there's
>>> a problem :
>>> $ ln -s /home/david/Ubuntu/testdir /home/david/dir
>>> $ cd /home/david/dir
>>> $ pwd
>>> /home/david/dir
>>> $ /bin/pwd
>>> /home/david/Ubuntu/testdir
>>> Why the results returned by pwd and /bin/pwd are different?
>>> And i have checked that pwd==/bin/pwd using the command which.
>> Well, I guess it's not, usually pwd is built into the shell and
>> _additionally_ available as a program.
>> Try pwd -L, pwd -P, /bin/pwd -L and /bin/pwd -P
>>
>> I guess you'll find that pwd display the same as pwd -L and /bin/pwd
>> dispalyes the same as pwd -P, /bin/pwd -L and /bin/pwd -P
>>
>> Why is that a problem to you?
>>
>> Bye, Jojo
>
> Thanks.pwd==pwd -L and /bin/pwd==pwd -P
> I want to see help page like man about pwd,but man page display /bin/
> pwd not the command pwd built into the shell.
> How to get the man page about pwd?
>
> I think /bin/pwd use the API getcwd().Which API do pwd use to get
> current directory?
>

They both use the same API, but /bin/pwd runs as a separate process
and hence has no access to the shell's environment. So /bin/pwd
always reports the physical pathname of the CWD, whereas the shell's
pwd may not. If you cd into a directory using an absolute pathname
you will get the same results for both. But if you cd into some
directory via a symlink, the shell remembers how you got there,
and will treat "cd .." differently than the physical ".." directory
entry, and report the CWD using the logical path (the symlink).

As "Jojo" said, you can use arguments to the shell's pwd to use
either the real, physical path or the logical path. You'll find
the cd command (which is built into the shell, unlink pwd there
is no separate program) also supports those options.

Shell built-ins may or may not have their own man pages. Try
"man sh" or whatever shell you're using. (Bash and some other
shells have a built-in "help" command you can use.)

Finally, a related but obscure (to me anyway) utility is the
Gnu readlink. It has a "-f path" option that canonizes path.
POSIX doesn't provide a comparable utility (does Solaris?). But
POSIX does require realpath(3) library function which can be used
in a short C program to replace Gnu readlink if that is unavailable,
like this (comments welcome):

// Prints the canonical pathname of the given single argument
// Written 11/2007 by Wayne _________
// See also Gnu "readlink -f" command, which does this too.
// $Id: realpath.c,v 1.0 2007/11/08 01:15:49 wayne Exp $

#include
#include
#include
#include

int errno;

int main ( int argc, char* argv[] )
{
char buf[PATH_MAX+1] = "";
char* progname = "realpath";

// Find the basename of this program:
if ( (progname = strrchr(argv[0], '/')) != NULL )
++progname;
else
progname = argv[0];

// Print usage message unless only arg. (Fix to skip "--".)
if ( argc != 2 || argv[1][0] == '-' )
{ fprintf( stderr, "%s ARG: returns the canonical absolute pathname "
"for a given pathname ARG.\nThere are no options.\n", progname );
return 1;
}

realpath( argv[1], buf );

if ( errno )
{ sprintf( buf, "%s: \"%s\"", progname, argv[1] );
perror(buf);
return 1;
}

printf( "%s\n", buf );
return 0;
}

Hope this helps!

-Wayne

Re: a simple but strange problem

am 14.11.2007 11:02:29 von david

On 11 14 , 4 49 , Wayne wrote:
> david wrote:
> > On 11 14 , 3 39 , "Joachim Schmitz"
> > wrote:
> >> "david" schrieb im Newsbeitragnews:1195024352.814012.193630@v29g2000prd.googleg roups.com...> First i create a symbol link to directory,then i change to the
> >>> directory the link point.
> >>> Now i should in the directory which the link point, right? But there's
> >>> a problem :
> >>> $ ln -s /home/david/Ubuntu/testdir /home/david/dir
> >>> $ cd /home/david/dir
> >>> $ pwd
> >>> /home/david/dir
> >>> $ /bin/pwd
> >>> /home/david/Ubuntu/testdir
> >>> Why the results returned by pwd and /bin/pwd are different?
> >>> And i have checked that pwd==/bin/pwd using the command which.
> >> Well, I guess it's not, usually pwd is built into the shell and
> >> _additionally_ available as a program.
> >> Try pwd -L, pwd -P, /bin/pwd -L and /bin/pwd -P
>
> >> I guess you'll find that pwd display the same as pwd -L and /bin/pwd
> >> dispalyes the same as pwd -P, /bin/pwd -L and /bin/pwd -P
>
> >> Why is that a problem to you?
>
> >> Bye, Jojo
>
> > Thanks.pwd==pwd -L and /bin/pwd==pwd -P
> > I want to see help page like man about pwd,but man page display /bin/
> > pwd not the command pwd built into the shell.
> > How to get the man page about pwd?
>
> > I think /bin/pwd use the API getcwd().Which API do pwd use to get
> > current directory?
>
> They both use the same API, but /bin/pwd runs as a separate process
> and hence has no access to the shell's environment. So /bin/pwd
> always reports the physical pathname of the CWD, whereas the shell's
> pwd may not. If you cd into a directory using an absolute pathname
> you will get the same results for both. But if you cd into some
> directory via a symlink, the shell remembers how you got there,
> and will treat "cd .." differently than the physical ".." directory
> entry, and report the CWD using the logical path (the symlink).
>
> As "Jojo" said, you can use arguments to the shell's pwd to use
> either the real, physical path or the logical path. You'll find
> the cd command (which is built into the shell, unlink pwd there
> is no separate program) also supports those options.
>
> Shell built-ins may or may not have their own man pages. Try
> "man sh" or whatever shell you're using. (Bash and some other
> shells have a built-in "help" command you can use.)
>
> Finally, a related but obscure (to me anyway) utility is the
> Gnu readlink. It has a "-f path" option that canonizes path.
> POSIX doesn't provide a comparable utility (does Solaris?). But
> POSIX does require realpath(3) library function which can be used
> in a short C program to replace Gnu readlink if that is unavailable,
> like this (comments welcome):
>
> // Prints the canonical pathname of the given single argument
> // Written 11/2007 by Wayne _________
> // See also Gnu "readlink -f" command, which does this too.
> // $Id: realpath.c,v 1.0 2007/11/08 01:15:49 wayne Exp $
>
> #include
> #include
> #include
> #include
>
> int errno;
>
> int main ( int argc, char* argv[] )
> {
> char buf[PATH_MAX+1] = "";
> char* progname = "realpath";
>
> // Find the basename of this program:
> if ( (progname = strrchr(argv[0], '/')) != NULL )
> ++progname;
> else
> progname = argv[0];
>
> // Print usage message unless only arg. (Fix to skip "--".)
> if ( argc != 2 || argv[1][0] == '-' )
> { fprintf( stderr, "%s ARG: returns the canonical absolute pathname "
> "for a given pathname ARG.\nThere are no options.\n", progname );
> return 1;
> }
>
> realpath( argv[1], buf );
>
> if ( errno )
> { sprintf( buf, "%s: \"%s\"", progname, argv[1] );
> perror(buf);
> return 1;
> }
>
> printf( "%s\n", buf );
> return 0;
>
> }
>
> Hope this helps!
>
> -Wayne

Thanks. This helps a lot.

Re: a simple but strange problem

am 14.11.2007 14:07:26 von Glenn Jackman

At 2007-11-14 02:12AM, "david" wrote:
[...]
> Why the results returned by pwd and /bin/pwd are different?
> And i have checked that pwd==/bin/pwd using the command which.

What does "command -V pwd" tell you?


--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry