Stat (system call)

 is a Unix system call that returns file attributes about an inode. The semantics of vary between operating systems. As an example, Unix command uses this system call to retrieve information on files that includes:
codice_1 appeared in Version 1 Unix. It is among the few original Unix system calls to change, with Version 4's addition of group permissions and larger file size.
stat() functions.
The C POSIX library header , found on POSIX and other Unix-like operating systems, declares the codice_2 functions, as well as related functions called codice_3 and codice_4. The functions take a codice_5 buffer argument, which is used to return the file attributes. On success, the functions return zero, and on error, −1 is returned and errno is set appropriately.
The codice_2 and codice_4 functions take a filename argument. If the file is a symbolic link, codice_2 returns attributes of the eventual target of the link, while codice_4 returns attributes of the link itself. The codice_3 function takes a file descriptor argument instead, and returns attributes of the file that it identifies.
The family of functions was extended to implement large file support. Functions named codice_11, codice_12 and codice_13 return attributes in a codice_14 structure, which represents file sizes with a 64-bit type, allowing the functions to work on files 2 GiB and larger (up to 8 EiB). When the codice_15 macro is defined to 64, these 64-bit functions are available under the original names.
The functions are defined as:
int stat(const char *filename, struct stat *buf);
int lstat(const char *filename, struct stat *buf);
int fstat(int filedesc, struct stat *buf);
stat structure.
This structure is defined in header file as follows, although implementations are free to define additional fields:
struct stat {
 mode_t st_mode;
 ino_t st_ino;
 dev_t st_dev;
 dev_t st_rdev;
 nlink_t st_nlink;
 uid_t st_uid;
 gid_t st_gid;
 off_t st_size;
 struct timespec st_atim;
 struct timespec st_mtim;
 struct timespec st_ctim;
 blksize_t st_blksize;
 blkcnt_t st_blocks;
POSIX.1 does not require codice_16, codice_17 and codice_18 members; these fields are defined as part of XSI option in the Single Unix Specification.
In older versions of POSIX.1 standard, the time-related fields were defined as codice_19, codice_20 and codice_21, and were of type codice_22. Since the 2008 version of the standard, these fields were renamed to codice_23, codice_24 and codice_25, respectively, of type struct codice_26, since this structure provides a higher resolution time unit. For the sake of compatibility, implementations can define the old names in terms of the codice_27 member of codice_28. For example, codice_19 can be defined as codice_30.
The codice_5 structure includes at least the following members:
The codice_34 field is a bit field. It combines the file access modes and also indicates any special file type. There are many macros to work with the different mode flags and file types.
Criticism of atime.
Reading a file changes its eventually requiring a disk "write," which has been criticized as it is inconsistent with a read only file system. File system cache may significantly reduce this activity to one disk write per cache flush.
Linux kernel developer Ingo Molnár publicly criticized the concept and performance impact of atime in 2007, and in 2009, the mount option had become the default, which addresses this criticism. The behavior behind the mount option offers sufficient performance for most purposes and should not break any significant applications, as it has been extensively discussed. Initially, only updated atime if atime < mtime or atime < ctime; that was subsequently modified to update atimes that were 24 hours old or older, so that and Debian's popularity counter (popcon) would behave properly.
Current versions of the Linux kernel support four mount options, which can be specified in fstab:
Current versions of Linux, macOS, Solaris, FreeBSD, and NetBSD support a mount option in /etc/fstab, which causes the atime field never to be updated. Turning off atime updating breaks POSIX compliance, and some applications, such as mbox-driven "new mail" notifications, and some file usage watching utilities, notably tmpwatch.
The option on OpenBSD behaves more like Linux .
Version 4.0 of the Linux kernel mainline, which was released on April 12, 2015, introduced the new mount option . It allows POSIX-style atime updates to be performed in-memory and flushed to disk together with some non-time-related I/O operations on the same file; atime updates are also flushed to disk when some of the sync system calls are executed, or before the file's in-memory inode is evicted from the filesystem cache. Additionally, it is possible to configure for how long atime modifications can remain unflushed. That way, lazytime retains POSIX compatibility while offering performance improvements.
ctime.
It is tempting to believe that originally meant creation time; however, while early Unix did have modification and creation times, the latter was changed to be access time before there was any C structure in which to call anything . The file systems retained just the access time () and modification time () through 6th edition Unix. The timestamp was added in the file system restructuring that occurred with 7th edition Unix, and has always referred to inode change time. It is updated any time file metadata stored in the inode changes, such as file permissions, file ownership, and creation and deletion of hard links. In some implementations, is affected by renaming a file: Both original Unix, which implemented a renaming by making a link (updating ) and then unlinking the old name (updating again) and modern Linux tend to do this.
Unlike and , cannot be set to an arbitrary value with , as used by the utility, for example. Instead, when is used, or for any other change to the inode other than an update to caused by
accessing the file, the value is set to the current time.
Example.
int
main(int argc, char *argv[])
 struct stat sb;
 struct passwd *pwuser;
 struct group *grpnam;
 if (argc < 2)
 fprintf(stderr, "Usage: %s: file ...\n", argv[0]);
 exit(EXIT_FAILURE);
 for (int i = 1; i < argc; i++)
 if (-1 == stat(argv[i], &sb))
 perror("stat()");
 exit(EXIT_FAILURE);
 if (NULL == (pwuser = getpwuid(sb.st_uid)))
 perror("getpwuid()");
 exit(EXIT_FAILURE);
 if (NULL == (grpnam = getgrgid(sb.st_gid)))
 perror("getgrgid()");
 exit(EXIT_FAILURE);
 printf("%s:\n", argv[i]);
 printf("\tinode: %u\n", sb.st_ino);
 printf("\towner: %u (%s)\n", sb.st_uid, pwuser->pw_name);
 printf("\tgroup: %u (%s)\n", sb.st_gid, grpnam->gr_name);
 printf("\tperms: %o\n", sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
 printf("\tlinks: %d\n", sb.st_nlink);
 printf("\tsize: %ld\n", sb.st_size); /* you may use %lld */
 printf("\tatime: %s", ctime(&sb.st_atim.tv_sec));
 printf("\tmtime: %s", ctime(&sb.st_mtim.tv_sec));
 printf("\tctime: %s", ctime(&sb.st_ctim.tv_sec));
 printf("\n");
 return 0;