Getting bash's pwd isn't possible where bash isn't used. What's your point? Are you implying that the code I posted isn't portable? If so, you'd be mistaken.
Update:
Every tool that gives you bash's pwd use $ENV{PWD}.
$ /bin/pwd GNU pwd
/tmp/ikegami
$ bash -c pwd bash's builtin
/home/ikegami/tmp
$ PWD=/ bash -c pwd bash's builtin uses $ENV{PWD}
/tmp/ikegami
The GNU pwd doesn't have any command line options and always gives the path returned by getcwd (with a fallback on error). Darwin's pwd goes a step further and does something that looks awfully familiar.
static char *
getcwd_logical(void)
{
struct stat lg, phy;
char *pwd;
/*
* Check that $PWD is an absolute logical pathname referring to
* the current working directory.
*/
if ((pwd = getenv("PWD")) != NULL && *pwd == '/') {
if (stat(pwd, &lg) == -1 || stat(".", &phy) == -1)
return (NULL);
if (lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
return (pwd);
}
errno = ENOENT;
return (NULL);
}
|