What can I do if getcwd() and getenv("PWD") don't match?

Carl Norum picture Carl Norum · Jul 17, 2010 · Viewed 7.6k times · Source

I have a build system tool that is using getcwd() to get the current working directory. That's great, except that sometimes people have spaces in their paths, which isn't supported by the build system. You'd think that you could just make a symbolic link:

ln -s "Directory With Spaces" DirectoryWithoutSpaces

And then be happy. But unfortunately for me, getcwd() resolves all the symbolic links. I tried to use getenv("PWD"), but it is not pointing at the same path as I get back from getcwd(). I blame make -C for not updating the environment variable, I think. Right now, getcwd() gives me back a path like this:

/Users/carl/Directory With Spaces/Some/Other/Directories

And getenv("PWD") gives me:

/Users/carl/DirectoryWithoutSpaces

So - is there any function like getcwd() that doesn't resolve the symbolic links?

Edit:

I changed

make -C Some/Other/Directories

to

cd Some/Other/Directories ; make

And then getenv("PWD") works.. If there's no other solution, I can use that.

Answer

eruciform picture eruciform · Jul 17, 2010

According to the Advanced Programming in the UNIX Environment bible by Stevens, p.112:

Since the kernel must maintain knowledge of the current working directory, we should be able to fetch its current value. Unfortunately, all the kernel maintains for each process is the i-node number and device identification for the current working directory. The kernel does not maintain the full pathname of the directory.

Sorry, looks like you do need to work around this in another way.