STAT(2)

NAME
stat, lstat, fstat - get file status

SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>

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

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

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

DESCRIPTION
Stat puts detailed information about the file name in a structure whose address is buf. Lstat does the same except when name is a symbolic link (see link(2)), in which case it supplies information about the link itself. Fstat does the same for an open file known by a file descriptor.

It is unnecessary to have any permissions at all with respect to name, but all directories leading to the file must be searchable.
struct stat
{
	dev_t st_dev;	major/minor device numbers for the inode
	ino_t st_ino;	inode number
	unsigned short st_mode;	file mode encoded as below
	short st_nlink;	number of links (not symbolic links)
	short st_uid;	uid of owner
	short st_gid;	gid of owner
	dev_t st_rdev;	if special, the major/minor device numbers
	off_t st_size;	size in bytes
	time_t st_atime;	time file was last read or created
	time_t st_mtime;	time file was last written or created
	time_t st_ctime;	time file or inode was last written or created
};

For reasons of efficiency, st_atime is not set when a directory is searched, although this might be more logical.

The bits in st_mode are defined by
S_IFMT	0170000	file type
S_IFDIR	0040000	directory
S_IFCHR	0020000	special character device
S_IFBLK	0060000	special block device
S_IFREG	0100000	regular file
S_IFLNK	0120000	symbolic link
S_ISUID	0004000	set userid on execution
S_ISGID	0002000	set groupid on execution
S_ICCTYP	0007000	type of concurrency control
S_ISYNC	0001000	1 writer and n readers (synchronized access)
S_IEXCL	0003000	1 writer or n readers (exclusive access)
S_IAPPEND	0005000	append-only file
S_IBLIND	0007000	blind directory
	0000400	read by owner
	0000200	write by owner
	0000100	execute (search on directory) by owner
	0000070	read, write, execute (search) by group
	0000007	read, write, execute (search) by others

S_IFMT and S_ICCTYP are field masks; the other constants encode modes. Codes contained in the S_IFMT field are mutually exclusive. Codes containing bit 01000 are incompatible with other codes contained in the S_ICCTYP field.

Fstat, which reads the inode, and write(2) can be performed on the same file descriptor only if the security labels of file and process reach the same value or if the process has capability T_NOCHK.

SEE ALSO
chmod(1), ls(1), stat(1), chmod(2), filsys(5)

DIAGNOSTICS
EACCES, EBADF, EFAULT, EIO, ELAB, ELOOP, ENOENT, ENOTDIR