how to check the path to a symbolic link, but not to what it indicates, but to the link itself. IN C

821 views Asked by At

I've seen a lot of similar subject, I've read a lot of readlink, and realpath but problem is I need path to pliksoft, not where this link point.

for example:

I have symbolic link: pliksoft->abc

resultat should be:

/users/staff/trl/am/SK/pliksoft

not

/users/staff/trl/am/SK/abc

I do it, in this way:

char buf[PATH_MAX], buf_2[PATH_MAX]; 

readlink(argv[1], buf, PATH_MAX); 
realpath(buf, buf_2);

printf("%s\n", buf_2);

but thats a bad result.

1

There are 1 answers

0
William Pursell On

I suppose you're trying to do:

#include <libgen.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int
main(int argc, char **argv)
{
        char buf[PATH_MAX];
        char *path = argc > 1 ? argv[1] : "foo";

        realpath(dirname(path), buf);
        printf("%s/%s -> ", buf, basename(path));
        realpath(path, buf);
        printf("%s\n", buf);

        return 0;
}