The following symbolic links in C

I want to write a C program that, given the name of a symbolic link, will print the name of the file or directory that the link points to. Any suggestions on how to get started?

+3
source share
4 answers

Make sure you have an environment that supports POSIX functions, enable unistd.h , and then use readlink .

+3
source

The readlink() function mentioned is part of the answer. However, you should be aware of your terrible interface (this does not mean that the end of the response line is disabled!).

You can also see the realpath() function, the use of which was discussed in SO 1563186 . You can also see the code for linkpath 'in the IIUG software archive. It analyzes the security of all directories encountered when resolving a symbolic link - uses readlink() and lstat() and stat() ; One of the checks when testing the program was that realpath() resolve the name to the same file.

+5
source

Depending on the platform, stat() or fstat() are probably the first things to try. If you are running Linux or cygwin, then the stat program will give you a reasonable idea of ​​what to expect from a system API call (this will pretty much give you a text dump).

+1
source

The system call you want is readlink (). Selects the path to the link, returns a string ( not always a valid path in the file system!) Stored in the link. See the man page for details ("man 2 readlink").

Please note that there is some ambiguity in your question. Perhaps you are asking how to say the β€œreal” path in the file system, which is a bit more complicated.

0
source

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


All Articles