stat (Unix)
From Wikipedia, the free encyclopedia
stat()
is a Unix system call that returns useful data about a file inode. The semantics of stat()
vary between operating systems. With the Unix command ls, one can gather information about
- mtime: time of last modification (
ls -l
), - ctime: time of last status change (
ls -lc
) and - atime: time of last access (
ls -lu
).
Note that ctime
is not the time of file creation. Writing to a file changes its mtime
, ctime
, and atime
. A change in file permissions or file ownership changes its ctime
and atime
. Reading a file changes its atime
. File systems mounted with the noatime
option do not update the atime on reads, and the relatime
option provides for updates only if the previous atime is older than the mtime or ctime.
Contents |
[edit] lstat()
lstat()
is a library function that retrieves the status of a file. It is identical to stat()
, except when the file is a symbolic link, in which case information about the link itself is returned instead of the linked-to file.
[edit] fstat()
fstat()
is a library function that retrieves the status of a file. It is identical to stat()
except that the file's identity is passed as a file descriptor instead of as a filename.
[edit] stat() functions
The following is a set of declarations from the POSIX library header sys/stat.h
typically found on most operating systems.
#include <sys/types.h> #include <sys/stat.h> int stat(const char *filename, struct stat *buf); int lstat(const char *filename, struct stat *buf); int fstat(int filedesc, struct stat *buf); struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for filesystem I/O */ blkcnt_t st_blocks; /* number of blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ };