Difference between lstat fstat and stat in C
Asked Answered
R

3

41

Im writing a school assignment in C to search through a file system for directories, regular files and symlinks. For now i use lstat to get information about items.

So whats the difference between lstat fstat and stat system calls?

Reimers answered 1/10, 2015 at 19:6 Comment(3)
It's exactly what the manual says.Charry
C'mon people, you have to at least put forth some effort before askingHereinbefore
This is now the top hit on Google for this question. I really don't think it'd be so awful to actually answer it. Thankfully someone did.Bowman
I
86

I was also searching for stat vs lstat vs fstat and although there is already an answer to this question, I'd like to see it formatted like that:

lstat() is identical to stat(), except that if pathname is a symbolic link, then it returns information about the link itself, not the file that it refers to.

fstat() is identical to stat(), except that the file about which information is to be retrieved is specified by a file descriptor (instead of a file name).

http://man7.org/linux/man-pages/man2/stat.2.html

Irmine answered 17/5, 2016 at 6:30 Comment(0)
C
3

Similarity: They both take filename as arguments.

Difference: Whenever the file name is a symbolic link, stat() returns the attributes or inode information about the target file associated with the link. Whereas, lstat() return the attributes of only the link.

Refer the manpage for stat() vs lstat().

Caia answered 20/2, 2017 at 0:26 Comment(0)
S
0

googling the following: lstat v fstat v stat

the first link provided is a man page that describes these differences: http://manpages.ubuntu.com/manpages/hardy/man2/stat.2.html

listed on the page is the following simple answer: stat() stats the file pointed to by path and fills in buf. lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to. fstat() is identical to stat(), except that the file to be stat-ed is specified by the file descriptor fd.

Sibert answered 1/10, 2015 at 20:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.