Go to the previous, next section.

fstat, stat and lstat

SYNOPSIS

int fstat(int fd, struct stat *buf);

int stat(char *path, struct stat *buf);

int lstat(char *path, struct stat *buf);

PARAMETERS

fd: [in] the file descriptor we want to get the information from.

path: [in] the file path we want to get the information from.

buf: [out] points to the buffer that will contain the information.

DESCRIPTION

Those calls return a stat structure in buf with the following format:

struct stat {
        dev_t           st_dev;       /* device */
        unsigned short  __pad1;       /* padding */
        ino_t           st_ino;       /* inode
        umode_t         st_mode;      /* access mode */
        nlink_t         st_nlink;     /* number of hard links */
        uid_t           st_uid;       /* uid */
        gid_t           st_gid;       /* gid */
        dev_t           st_rdev;      /* device type */
        unsigned short  __pad2;       /* padding */
        off_t           st_size;      /* size (in bytes) */
        unsigned long   st_blksize;   /* block size */
        unsigned long   st_blocks;    /* number of allocated blocks */
        time_t          st_atime;     /* last access time */
        unsigned long   __unused1;    /* unused */
        time_t          st_mtime;     /* last modification time */
        unsigned long   __unused2;    /* unused */
        time_t          st_ctime;     /* last change time */
        unsigned long   __unused3;    /* unused */
        unsigned long   __unused4;    /* unused */
        unsigned long   __unused5;    /* unused */
};

The change time is for modifications to the inode, whereas the modification time is for modifications to the content of the file.

fstat gets the information from a file descriptor. stat and lstat get the information from a file path. However, lstat used on a link will give get the information from the link itself instead of the file pointed by the link.

Note: the kernel contains a older stats functions. However, it would seem they are no longer used. (Maybe only by very old binaries.)

RETURN VALUE

On success zero is returned. On error, -1 is returned and errno is set to one of the following values:

for stat or lstat:

for fstat:

Go to the previous, next section.