On linux / unix, how do I know if a directory is a link using C ++?

I am trying to figure out how to find out if a directory is a symbolic link using C ++

Finding out if a file is a symbolic link is easy using lstat and S_ISLNK (fs.st_mode), but this seems to work only with files. S_ISDIR always says that a directory is a directory, even if its symbolic link points to a directory.

So, how do I do the same for a directory as a file?

+3
source share
2 answers

. lstat("/path/link/"), "" - . , lstat("/path/link") ( ). , python ( , , ):

$ ln -s /etc /tmp/foo

>>> from posix import lstat
>>> lstat("/tmp/foo")
(41471, 24591, 64769L, 1, 1095, 501, 4, 1298667395, 1298667395, 1298667395)
>>> lstat("/tmp/foo/")
(16877, 131073, 64768L, 119, 0, 0, 12288, 1298663835, 1298667375, 1298667375)
+7

, , - readlink:

char buffer[255];
int size = readlink("/my/path", buffer);
if (errno == EINVAL) {
    /* here you know: "/my/path" isn't a symlink */
}

, : /my/path/ ( ), : /my/path/., .

0

Source: https://habr.com/ru/post/1794812/


All Articles