in reply to Re^2: Surprising behavior of Cwd module on Unix with symlinks
in thread Surprising behavior of Cwd module on Unix with symlinks
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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Surprising behavior of Cwd module on Unix with symlinks
by DrHyde (Prior) on Feb 09, 2011 at 11:03 UTC | |
by ikegami (Patriarch) on Feb 09, 2011 at 15:22 UTC |