1.\" Copyright (c) 1980, 1991, 1993, 1994 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)stat.2 8.4 (Berkeley) 5/1/95 33.\" $FreeBSD$ 34.\" 35.Dd February 15, 2002 36.Dt STAT 2 37.Os 38.Sh NAME 39.Nm stat , 40.Nm lstat , 41.Nm fstat 42.Nd get file status 43.Sh LIBRARY 44.Lb libc 45.Sh SYNOPSIS 46.In sys/types.h 47.In sys/stat.h 48.Ft int 49.Fn stat "const char *path" "struct stat *sb" 50.Ft int 51.Fn lstat "const char *path" "struct stat *sb" 52.Ft int 53.Fn fstat "int fd" "struct stat *sb" 54.Sh DESCRIPTION 55The 56.Fn stat 57function obtains information about the file pointed to by 58.Fa path . 59Read, write or execute 60permission of the named file is not required, but all directories 61listed in the path name leading to the file must be searchable. 62.Pp 63The 64.Fn lstat 65function is like 66.Fn stat 67except in the case where the named file is a symbolic link, 68in which case 69.Fn lstat 70returns information about the link, 71while 72.Fn stat 73returns information about the file the link references. 74.Pp 75The 76.Fn fstat 77function obtains the same information about an open file 78known by the file descriptor 79.Fa fd . 80.Pp 81The 82.Fa sb 83argument is a pointer to a 84.Vt stat 85structure 86as defined by 87.Aq Pa sys/stat.h 88(shown below) 89and into which information is placed concerning the file. 90.Bd -literal 91struct stat { 92 dev_t st_dev; /* inode's device */ 93 ino_t st_ino; /* inode's number */ 94 mode_t st_mode; /* inode protection mode */ 95 nlink_t st_nlink; /* number of hard links */ 96 uid_t st_uid; /* user ID of the file's owner */ 97 gid_t st_gid; /* group ID of the file's group */ 98 dev_t st_rdev; /* device type */ 99#ifndef _POSIX_SOURCE 100 struct timespec st_atimespec; /* time of last access */ 101 struct timespec st_mtimespec; /* time of last data modification */ 102 struct timespec st_ctimespec; /* time of last file status change */ 103#else 104 time_t st_atime; /* time of last access */ 105 long st_atimensec; /* nsec of last access */ 106 time_t st_mtime; /* time of last data modification */ 107 long st_mtimensec; /* nsec of last data modification */ 108 time_t st_ctime; /* time of last file status change */ 109 long st_ctimensec; /* nsec of last file status change */ 110#endif 111 off_t st_size; /* file size, in bytes */ 112 int64_t st_blocks; /* blocks allocated for file */ 113 u_int32_t st_blksize; /* optimal blocksize for I/O */ 114 fflags_t st_flags; /* user defined flags for file */ 115 u_int32_t st_gen; /* file generation number */ 116}; 117.Ed 118.Pp 119The time-related fields of 120.Fa struct stat 121are as follows: 122.Bl -tag -width XXXst_mtime 123.It st_atime 124Time when file data last accessed. 125Changed by the 126.Xr mknod 2 , 127.Xr utimes 2 128and 129.Xr read 2 130system calls. 131.It st_mtime 132Time when file data last modified. 133Changed by the 134.Xr mknod 2 , 135.Xr utimes 2 136and 137.Xr write 2 138system calls. 139.It st_ctime 140Time when file status was last changed (inode data modification). 141Changed by the 142.Xr chmod 2 , 143.Xr chown 2 , 144.Xr link 2 , 145.Xr mknod 2 , 146.Xr rename 2 , 147.Xr unlink 2 , 148.Xr utimes 2 149and 150.Xr write 2 151system calls. 152.El 153.Pp 154If 155.Dv _POSIX_SOURCE 156is not defined, the time-related fields are defined as: 157.Bd -literal 158#ifndef _POSIX_SOURCE 159#define st_atime st_atimespec.tv_sec 160#define st_mtime st_mtimespec.tv_sec 161#define st_ctime st_ctimespec.tv_sec 162#endif 163.Ed 164.Pp 165The size-related fields of the 166.Fa struct stat 167are as follows: 168.Bl -tag -width XXXst_blksize 169.It st_blksize 170The optimal I/O block size for the file. 171.It st_blocks 172The actual number of blocks allocated for the file in 512-byte units. 173As short symbolic links are stored in the inode, this number may 174be zero. 175.El 176.Pp 177The status information word 178.Fa st_mode 179has the following bits: 180.Bd -literal 181#define S_IFMT 0170000 /* type of file */ 182#define S_IFIFO 0010000 /* named pipe (fifo) */ 183#define S_IFCHR 0020000 /* character special */ 184#define S_IFDIR 0040000 /* directory */ 185#define S_IFBLK 0060000 /* block special */ 186#define S_IFREG 0100000 /* regular */ 187#define S_IFLNK 0120000 /* symbolic link */ 188#define S_IFSOCK 0140000 /* socket */ 189#define S_IFWHT 0160000 /* whiteout */ 190#define S_ISUID 0004000 /* set user id on execution */ 191#define S_ISGID 0002000 /* set group id on execution */ 192#define S_ISVTX 0001000 /* save swapped text even after use */ 193#define S_IRUSR 0000400 /* read permission, owner */ 194#define S_IWUSR 0000200 /* write permission, owner */ 195#define S_IXUSR 0000100 /* execute/search permission, owner */ 196.Ed 197.Pp 198For a list of access modes, see 199.Aq Pa sys/stat.h , 200.Xr access 2 201and 202.Xr chmod 2 . 203.Sh RETURN VALUES 204.Rv -std 205.Sh COMPATIBILITY 206Previous versions of the system used different types for the 207.Li st_dev , 208.Li st_uid , 209.Li st_gid , 210.Li st_rdev , 211.Li st_size , 212.Li st_blksize 213and 214.Li st_blocks 215fields. 216.Sh ERRORS 217The 218.Fn stat 219and 220.Fn lstat 221functions will fail if: 222.Bl -tag -width Er 223.It Bq Er EACCES 224Search permission is denied for a component of the path prefix. 225.It Bq Er EFAULT 226.Fa sb 227or 228.Em name 229points to an invalid address. 230.It Bq Er EIO 231An I/O error occurred while reading from or writing to the file system. 232.It Bq Er ELOOP 233Too many symbolic links were encountered in translating the pathname. 234.It Bq Er ENAMETOOLONG 235A component of a pathname exceeded 255 characters, 236or an entire path name exceeded 1023 characters. 237.It Bq Er ENOENT 238The named file does not exist. 239.It Bq Er ENOTDIR 240A component of the path prefix is not a directory. 241.It Bq Er EOVERFLOW 242The file size in bytes cannot be 243represented correctly in the structure pointed to by 244.Fa sb . 245.El 246.Pp 247.Bl -tag -width Er 248The 249.Fn fstat 250function will fail if: 251.It Bq Er EBADF 252.Fa fd 253is not a valid open file descriptor. 254.It Bq Er EFAULT 255.Fa sb 256points to an invalid address. 257.It Bq Er EIO 258An I/O error occurred while reading from or writing to the file system. 259.It Bq Er EOVERFLOW 260The file size in bytes cannot be 261represented correctly in the structure pointed to by 262.Fa sb . 263.El 264.Sh SEE ALSO 265.Xr access 2 , 266.Xr chmod 2 , 267.Xr chown 2 , 268.Xr utimes 2 , 269.Xr symlink 7 270.Sh BUGS 271Applying 272.Fn fstat 273to a socket (and thus to a pipe) 274returns a zeroed buffer, 275except for the blocksize field, 276and a unique device and inode number. 277.Sh STANDARDS 278The 279.Fn stat 280and 281.Fn fstat 282function calls are expected to conform to 283.St -p1003.1-90 . 284.Sh HISTORY 285A 286.Fn stat 287and a 288.Fn fstat 289function call appeared in 290.At v7 . 291A 292.Fn lstat 293function call appeared in 294.Bx 4.2 . 295