Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between lstat fstat and stat in C

Tags:

c

stat

fstat

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?

like image 723
uzr Avatar asked Oct 01 '15 19:10

uzr


People also ask

What is the difference between the functions stat () and fstat ()?

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).

What is Linux fstat?

The fstat() function shall obtain information about an open file associated with the file descriptor fildes, and shall write it to the area pointed to by buf.

What is struct stat in C?

struct stat is a system struct that is defined to store information about files. It is used in several system calls, including fstat, lstat, and stat.


2 Answers

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().

like image 32
bharat nc Avatar answered Sep 20 '22 14:09

bharat nc


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

like image 103
user619271 Avatar answered Sep 17 '22 14:09

user619271