Question

I have a symbolic link /home/me/symlink -> /home/me/realdir when I try to getcwd in the directory /home/me/symlink I get /home/me/realdir with the following program:

 int main(int argc, char **argv)
{
    int ret;
    char path[PATH_MAX];
    getcwd(path, PATH_MAX);
    printf("path %s\n", path);
    return 0;
}

Is there a way to get the directory /home/me/symlink? And, is there a way to set the current working directory to a symlinked directory?

Was it helpful?

Solution

I. No (docs) :

The pathname shall contain no components that are dot or dot-dot, or are symbolic links.

However, there's a workaround:

char *cwd = getenv("PWD");

II. Yes (docs) :

chdir("/path/to/newcwd");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top