0

With c code, I want to know the given file (like test.c) is a symbolic link or not. I tried with stat() function. I am checking the

nlink_t   st_nlink;   /* number of hard links */

member of stat structure.

struct stat stbuf;
stat("test.c", &stbuf)

stbuf.st_nlink is giving one in case of hard link as well as in softlink.

Is that i am doing right. Or is there any other way to check the given file is a soft link or hard link.

Abhitesh khatri
  • 2,911
  • 3
  • 20
  • 29
  • 1
    See here for the answer: http://stackoverflow.com/questions/2635923/how-do-you-determine-using-stat-whether-a-file-is-a-symbolic-link – Brandin Feb 13 '14 at 13:33

1 Answers1

4

No, you need to use lstat() to be able to detect if a file is a soft link.

Also make sure that you understand that typically, there are not three different types of files: files, hard links to files, and soft links to files. Instead, there are only two: hard links to files and soft links. What you might think of as "the file" is in fact a hard link too, it's just typically the single link.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • I am using this code struct stat stbuf; stat("softlink_uname.c", &stbuf)) if (S_IFLNK == stbuf.st_mode & S_IFMT) printf("this is a soft link\n"); else if (S_IFREG == stbuf.st_mode & S_IFMT) printf("this is not a link\n"); But nothing is printing, Is i am did somthing wrong? – Abhitesh khatri Feb 13 '14 at 13:48
  • @Abhiteshkhatri Yes, you're using the wrong function, like I said you must use `lstat()`, **not** `stat()`, do detect soft links. – unwind Feb 13 '14 at 13:54
  • thanks, sorry it is typing mistake , now i am using lstat but nothing is printing. – Abhitesh khatri Feb 13 '14 at 13:58
  • thnx , I got the error..Now it is working. – Abhitesh khatri Feb 13 '14 at 14:06