Stats
Headers
#include <sys/types.h>
#include <sys/stat.h>
Function Prototype
int stat(const char *pathname, struct stat *buf);
int fstat(int filedes, struct stat *buf)
int lstat(const char *pathname, struct stat *buf)Function
These three functions all return a structure which contains the metadata of files stored in i-node table
Detail
stat
Takes file path and fill the metadata in buffer
fstat
Takes file descriptor and fill the metadata in buffer
lstat
Similar to stat(). However, when encountering a symlink, stat() will trace back to the original file and display metadata of the original file. lstat(), on the other hand, will return metadata of the link
This system call is used to implement ls -l
struct stat
struct stat {
mode_t st_mode; /* file type & mode (permissions) */
ino_t st_ino; /* i-node number (serial number) */
dev_t st_dev; /* device number (file system) */
dev_t st_rdev; /* device number for special files */
nlink_t st_nlink; /* number of links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
off_t st_size; /* size in bytes, for regular files */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last file status change */
blksize_t st_blksize; /* best I/O block size */
blkcnt_t st_blocks; /* number of disk blocks allocated (typically 512-byte units) */
};