STAT(2)
NAME
stat, fstat − get file status
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
stat(name, buf)
char *name;
struct stat *buf;
fstat(fildes, buf)
struct stat *buf;
DESCRIPTION
Stat obtains detailed information about a named file. Fstat obtains the same information about an open file known by the file descriptor from a successful open, creat, dup or pipe(2) call.
Name points to a null-terminated string naming a file; buf is the address of a buffer into which information is placed concerning the file. It is unnecessary to have any permissions at all with respect to the file, but all directories leading to the file must be searchable. The layout of the structure pointed to by buf as defined in <stat.h> is given below. St_mode is encoded according to the ’#define’ statements.
/*
* SCCSID: @(#)stat.h1.011/12/83
*/
structstat
{
dev_tst_dev;
ino_tst_ino;
unsignedshort st_mode;
shortst_nlink;
short st_uid;
short st_gid;
dev_tst_rdev;
off_tst_size;
time_tst_atime;
time_tst_mtime;
time_tst_ctime;
};
#defineS_IFMT 0170000 /* type of file */
#defineS_IFDIR 0040000 /* directory */
#defineS_IFCHR 0020000 /* character special */
#defineS_IFBLK 0060000 /* block special */
#defineS_IFREG 0100000 /* regular */
#defineS_IFMPC 0030000 /* multiplexed char special */
#defineS_IFMPB 0070000 /* multiplexed block special */
#defineS_ISUID 0004000 /* set user id on execution */
#defineS_ISGID 0002000 /* set group id on execution */
#defineS_ISVTX 0001000 /* save swapped text even
after use */
#defineS_IREAD 0000400 /* read permission, owner */
#defineS_IWRITE 0000200 /* write permission, owner */
#defineS_IEXEC 0000100 /* execute/search permission,
owner */
The mode bits 0000070 and 0000007 encode group and others permissions (see chmod(2)). The defined types, ino_t, off_t, time_t, name various width integer values; dev_t encodes major and minor device numbers; their exact definitions are in the include file <sys/types.h> (see types(5).
When fildes is associated with a pipe, fstat reports an ordinary file with restricted permissions. The size is the number of bytes queued in the pipe.
st_atime is the file was last read. For reasons of efficiency, it is not set when a directory is searched, although this would be more logical. st_mtime is the time the file was last written or created. It is not set by changes of owner, group, link count, or mode. st_ctime is set both by writing and changing the i-node.
DIAGNOSTICS
Stat will fail if one or more of the following is true:
[EACCES] Search permission is denied for a component of the path prefix.
[EFAULT] Name or buf points to an invalid address.
[ENFILE] Insufficient system space to contain i-node.
[ENOENT] The named file, or an element within the named file, does not exist.
[ENOTDIR] A component of the path prefix is not a directory.
Fstat will fail if one or both of the following is true:
[EBADF] Fildes is not a valid open file descriptor.
[EFAULT] Buf points to an address outside of the process’s allocated address space.
SEE ALSO
ASSEMBLER
(stat = 18.)
sys stat; name; buf
(fstat = 28.)
(file descriptor in r0)
sys fstat; buf