xref: /csrg-svn/lib/libc/sys/stat.2 (revision 69139)
166935Sbostic.\" Copyright (c) 1980, 1991, 1993, 1994
261185Sbostic.\"	The Regents of the University of California.  All rights reserved.
320089Smckusick.\"
447208Scael.\" %sccs.include.redist.man%
520089Smckusick.\"
6*69139Smckusick.\"     @(#)stat.2	8.4 (Berkeley) 05/01/95
747208Scael.\"
847208Scael.Dd
947208Scael.Dt STAT 2
1047208Scael.Os BSD 4
1147208Scael.Sh NAME
1247208Scael.Nm stat ,
1347208Scael.Nm lstat ,
1447208Scael.Nm fstat
1547208Scael.Nd get file status
1647208Scael.Sh SYNOPSIS
1747208Scael.Fd #include <sys/types.h>
1847208Scael.Fd #include <sys/stat.h>
1947208Scael.Ft int
2066591Sbostic.Fn stat "const char *path" "struct stat *sb"
2147208Scael.Ft int
2266591Sbostic.Fn lstat "const char *path" "struct stat *sb"
2347208Scael.Ft int
2466591Sbostic.Fn fstat "int fd" "struct stat *sb"
2547208Scael.Sh DESCRIPTION
2647208ScaelThe
2747208Scael.Fn stat
2847208Scaelfunction obtains information about the file pointed to by
2947208Scael.Fa path .
3020090SmckusickRead, write or execute
3120090Smckusickpermission of the named file is not required, but all directories
3266935Sbosticlisted in the path name leading to the file must be searchable.
3347208Scael.Pp
3447208Scael.Fn Lstat
3547208Scaelis like
3647208Scael.Fn stat
3747208Scaelexcept in the case where the named file is a symbolic link,
3820090Smckusickin which case
3947208Scael.Fn lstat
4020090Smckusickreturns information about the link,
4120090Smckusickwhile
4247208Scael.Fn stat
4320090Smckusickreturns information about the file the link references.
4460796SmckusickUnlike other filesystem objects,
4560796Smckusicksymbolic links do not have an owner, group, access mode, times, etc.
4660796SmckusickInstead, these attributes are taken from the directory that
4760796Smckusickcontains the link.
4860796SmckusickThe only attributes returned from an
4960796Smckusick.Fn lstat
5060796Smckusickthat refer to the symbolic link itself are the file type (S_IFLNK),
5160796Smckusicksize, blocks, and link count (always 1).
5247208Scael.Pp
5347208ScaelThe
5447208Scael.Fn fstat
5520089Smckusickobtains the same information about an open file
5647208Scaelknown by the file descriptor
5766591Sbostic.Fa fd .
5847208Scael.Pp
5966591SbosticThe
6066591Sbostic.Fa sb
6166591Sbosticargument is a pointer to a
6247208Scael.Fn stat
6347208Scaelstructure
6447208Scaelas defined by
6547208Scael.Aq Pa sys/stat.h
6647208Scael(shown below)
6747208Scaeland into which information is placed concerning the file.
6847208Scael.Bd -literal
6947208Scaelstruct stat {
7047208Scael    dev_t    st_dev;    /* device inode resides on */
7147208Scael    ino_t    st_ino;    /* inode's number */
7247208Scael    mode_t   st_mode;   /* inode protection mode */
7347208Scael    nlink_t  st_nlink;  /* number or hard links to the file */
7447208Scael    uid_t    st_uid;    /* user-id of owner */
7547208Scael    gid_t    st_gid;    /* group-id of owner */
7647208Scael    dev_t    st_rdev;   /* device type, for special file inode */
7766591Sbostic    struct timespec st_atimespec;  /* time of last access */
7866591Sbostic    struct timespec st_mtimespec;  /* time of last data modification */
7966591Sbostic    struct timespec st_ctimespec;  /* time of last file status change */
8047208Scael    off_t    st_size;   /* file size, in bytes */
8166591Sbostic    quad_t   st_blocks; /* blocks allocated for file */
8266591Sbostic    u_long   st_blksize;/* optimal file sys I/O ops blocksize */
8347208Scael    u_long   st_flags;  /* user defined flags for file */
8447208Scael    u_long   st_gen;    /* file generation number */
8547208Scael};
8647208Scael.Ed
8747208Scael.Pp
8847208ScaelThe time-related fields of
8947208Scael.Fa struct stat
9047208Scaelare as follows:
9166591Sbostic.Bl -tag -width XXXst_mtime
9247208Scael.It st_atime
9366591SbosticTime when file data last accessed.
9466591SbosticChanged by the
9547208Scael.Xr mknod 2 ,
9666591Sbostic.Xr utimes 2
9720090Smckusickand
9866591Sbostic.Xr read 2
9966591Sbosticsystem calls.
10047208Scael.It st_mtime
10147208ScaelTime when file data last modified.
10266591SbosticChanged by the
10347208Scael.Xr mknod 2 ,
10466591Sbostic.Xr utimes 2
10566591Sbosticand
10666591Sbostic.Xr write 2
10766591Sbosticsystem calls.
10847208Scael.It st_ctime
10947208ScaelTime when file status was last changed (inode data modification).
11066591SbosticChanged by the
11166591Sbostic.Xr chmod 2 ,
11247208Scael.Xr chown 2 ,
11347208Scael.Xr link 2 ,
11447208Scael.Xr mknod 2 ,
11547208Scael.Xr rename 2 ,
11647208Scael.Xr unlink 2 ,
11766591Sbostic.Xr utimes 2
11866591Sbosticand
11966591Sbostic.Xr write 2
12066591Sbosticsystem calls.
12166591Sbostic.El
12266591Sbostic.Pp
12366591SbosticThe size-related fields of the
12466591Sbostic.Fa struct stat
12566591Sbosticare as follows:
12666591Sbostic.Bl -tag -width XXXst_blksize
12766591Sbostic.It st_blksize
12866591SbosticThe optimal I/O block size for the file.
12947208Scael.It st_blocks
13038709SbosticThe actual number of blocks allocated for the file in 512-byte units.
13166591SbosticAs short symbolic links are stored in the inode, this number may
13266591Sbosticbe zero.
13347208Scael.El
13447208Scael.Pp
13547208ScaelThe status information word
13647208Scael.Fa st_mode
13766591Sbostichas the following bits:
13847208Scael.Bd -literal
13947208Scael#define S_IFMT 0170000           /* type of file */
14047208Scael#define        S_IFIFO  0010000  /* named pipe (fifo) */
14147208Scael#define        S_IFCHR  0020000  /* character special */
14247208Scael#define        S_IFDIR  0040000  /* directory */
14347208Scael#define        S_IFBLK  0060000  /* block special */
14447208Scael#define        S_IFREG  0100000  /* regular */
14547208Scael#define        S_IFLNK  0120000  /* symbolic link */
14647208Scael#define        S_IFSOCK 0140000  /* socket */
14747208Scael#define S_ISUID 0004000  /* set user id on execution */
14847208Scael#define S_ISGID 0002000  /* set group id on execution */
14947208Scael#define S_ISVTX 0001000  /* save swapped text even after use */
15047208Scael#define S_IRUSR 0000400  /* read permission, owner */
15147208Scael#define S_IWUSR 0000200  /* write permission, owner */
15247208Scael#define S_IXUSR 0000100  /* execute/search permission, owner */
15347208Scael.Ed
15447208Scael.Pp
15547208ScaelFor a list of access modes, see
15647208Scael.Aq Pa sys/stat.h ,
15747208Scael.Xr access 2
15847208Scaeland
15947208Scael.Xr chmod 2 .
16047208Scael.Sh RETURN VALUES
16120090SmckusickUpon successful completion a value of 0 is returned.
16247208ScaelOtherwise, a value of -1 is returned and
16347208Scael.Va errno
16420090Smckusickis set to indicate the error.
16566591Sbostic.Sh COMPATIBILITY
16666591SbosticPrevious versions of the system used different types for the
16766591Sbostic.Li st_dev ,
16866591Sbostic.Li st_uid ,
16966591Sbostic.Li st_gid ,
17066591Sbostic.Li st_rdev ,
17166591Sbostic.Li st_size ,
17266591Sbostic.Li st_blksize
17366591Sbosticand
17466591Sbostic.Li st_blocks
17566591Sbosticfields.
17647208Scael.Sh ERRORS
17747208Scael.Fn Stat
17820090Smckusickand
17947208Scael.Fn lstat
18047208Scaelwill fail if:
18147208Scael.Bl -tag -width ENAMETOOLONGAA
18247208Scael.It Bq Er ENOTDIR
18320090SmckusickA component of the path prefix is not a directory.
18447208Scael.It Bq Er EINVAL
18520090SmckusickThe pathname contains a character with the high-order bit set.
18647208Scael.It Bq Er ENAMETOOLONG
18721003SmckusickA component of a pathname exceeded 255 characters,
18821003Smckusickor an entire path name exceeded 1023 characters.
18947208Scael.It Bq Er ENOENT
19020090SmckusickThe named file does not exist.
19147208Scael.It Bq Er EACCES
19220090SmckusickSearch permission is denied for a component of the path prefix.
19347208Scael.It Bq Er ELOOP
19421003SmckusickToo many symbolic links were encountered in translating the pathname.
19547208Scael.It Bq Er EFAULT
19666591Sbostic.Fa Sb
19720090Smckusickor
19847208Scael.Em name
19920090Smckusickpoints to an invalid address.
20047208Scael.It Bq Er EIO
20124439SmckusickAn I/O error occurred while reading from or writing to the file system.
20247208Scael.El
20347208Scael.Pp
20447208Scael.Bl -tag -width [EFAULT]
20547208Scael.Fn Fstat
20647208Scaelwill fail if:
20747208Scael.It Bq Er EBADF
20847208Scael.Fa fd
20920090Smckusickis not a valid open file descriptor.
21047208Scael.It Bq Er EFAULT
21166591Sbostic.Fa Sb
21220090Smckusickpoints to an invalid address.
21347208Scael.It Bq Er EIO
21424439SmckusickAn I/O error occurred while reading from or writing to the file system.
21547208Scael.El
21647208Scael.Sh SEE ALSO
21747208Scael.Xr chmod 2 ,
21847208Scael.Xr chown 2 ,
21947208Scael.Xr utimes 2
22055728Sbostic.Xr symlink 7
22147208Scael.Sh BUGS
22220090SmckusickApplying
22347208Scael.Xr fstat
22420091Smckusickto a socket (and thus to a pipe)
22528045Smckusickreturns a zero'd buffer,
22628045Smckusickexcept for the blocksize field,
22728045Smckusickand a unique device and inode number.
22847208Scael.Sh STANDARDS
22947208ScaelThe
23047208Scael.Fn stat
23147208Scaeland
23247208Scael.Fn fstat
23347208Scaelfunction calls are expected to
23447208Scaelconform to IEEE Std 1003.1-1988
23547208Scael.Pq Dq Tn POSIX .
23647208Scael.Sh HISTORY
23747208ScaelA
23847208Scael.Nm lstat
23947208Scaelfunction call appeared in
24047208Scael.Bx 4.2 .
241