Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

following symbolic links in C

Tags:

c

symlink

I'm looking to write a C program which, given the name of symbolic link, will print the name of the file or directory the link points to. Any suggestions on how to start?

like image 324
Ralph Avatar asked Oct 14 '09 22:10

Ralph


2 Answers

The readlink() function that has been mentioned is part of the answer. However, you should be aware of its horrid interface (it does not null terminate the response string!).

You might also want to look at the realpath() function, the use of which was discussed in SO 1563186. You could also look at the code for 'linkpath' at the IIUG Software Archive. It analyzes the security of all the directories encountered as a symbolic link is resolved - it uses readlink() and lstat() and stat(); one of the checks when testing the program was to ensure that realpath() resolved the name to the same file.

like image 197
Jonathan Leffler Avatar answered Oct 01 '22 09:10

Jonathan Leffler


Make sure that you have an environment which supports POSIX functions, include unistd.h and then use the readlink function.

like image 42
Tim Avatar answered Oct 01 '22 11:10

Tim