Paco Muñoz Martinez Paco Muñoz Martinez - 23 days ago 7
Linux Question

dinstinguishing a symbolic link

I am dealing with symbolic links using C posix in linux.

I detect a file and i have to differenciate between a regular file and a symbolic link.

Now i do the next:

if(S_ISREG(direct_info.st_mode)) {
deal with regular file
}
else if(S_ISLNK(direct_info.st_mode)) {
deal with symbolic link
}


the problem is that when the file is a symbolic link the flow of the execution enter at the first if. That is, when is a symbolic link also enter in the S_ISREG condition. So, what can i do to distinguish between both?

thank you

Answer

You are probably using stat() to get the file modes. stat() actually returns the info about the target, not the link itself. So, in order to get the information about the link itself you need to use lstat().