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. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" @(#)stat.2 8.4 (Berkeley) 5/1/95 29.\" $FreeBSD$ 30.\" 31.Dd December 5, 2018 32.Dt STAT 2 33.Os 34.Sh NAME 35.Nm stat , 36.Nm lstat , 37.Nm fstat , 38.Nm fstatat 39.Nd get file status 40.Sh LIBRARY 41.Lb libc 42.Sh SYNOPSIS 43.In sys/stat.h 44.Ft int 45.Fn stat "const char * restrict path" "struct stat * restrict sb" 46.Ft int 47.Fn lstat "const char * restrict path" "struct stat * restrict sb" 48.Ft int 49.Fn fstat "int fd" "struct stat *sb" 50.Ft int 51.Fn fstatat "int fd" "const char *path" "struct stat *sb" "int flag" 52.Sh DESCRIPTION 53The 54.Fn stat 55system call obtains information about the file pointed to by 56.Fa path . 57Read, write or execute 58permission of the named file is not required, but all directories 59listed in the path name leading to the file must be searchable. 60.Pp 61The 62.Fn lstat 63system call is like 64.Fn stat 65except when the named file is a symbolic link, 66in which case 67.Fn lstat 68returns information about the link, 69while 70.Fn stat 71returns information about the file the link references. 72.Pp 73The 74.Fn fstat 75system call obtains the same information about an open file 76known by the file descriptor 77.Fa fd . 78.Pp 79The 80.Fn fstatat 81system call is equivalent to 82.Fn stat 83and 84.Fn lstat 85except when the 86.Fa path 87specifies a relative path. 88In this case the status is retrieved from a file relative to 89the directory associated with the file descriptor 90.Fa fd 91instead of the current working directory. 92.Pp 93The values for the 94.Fa flag 95are constructed by a bitwise-inclusive OR of flags from this list, 96defined in 97.In fcntl.h : 98.Bl -tag -width indent 99.It Dv AT_SYMLINK_NOFOLLOW 100If 101.Fa path 102names a symbolic link, the status of the symbolic link is returned. 103.El 104.Pp 105If 106.Fn fstatat 107is passed the special value 108.Dv AT_FDCWD 109in the 110.Fa fd 111parameter, the current working directory is used and the behavior is 112identical to a call to 113.Fn stat 114or 115.Fn lstat 116respectively, depending on whether or not the 117.Dv AT_SYMLINK_NOFOLLOW 118bit is set in 119.Fa flag . 120.Pp 121The 122.Fa sb 123argument is a pointer to a 124.Vt stat 125structure 126as defined by 127.In sys/stat.h 128and into which information is placed concerning the file. 129.Pp 130The fields of 131.Vt "struct stat" 132related to the file system are: 133.Bl -tag -width ".Va st_nlink" 134.It Va st_dev 135Numeric ID of the device containing the file. 136.It Va st_ino 137The file's inode number. 138.It Va st_nlink 139Number of hard links to the file. 140.It Va st_flags 141Flags enabled for the file. 142See 143.Xr chflags 2 144for the list of flags and their description. 145.El 146.Pp 147The 148.Va st_dev 149and 150.Va st_ino 151fields together identify the file uniquely within the system. 152.Pp 153The time-related fields of 154.Vt "struct stat" 155are: 156.Bl -tag -width ".Va st_birthtim" 157.It Va st_atim 158Time when file data was last accessed. 159Changed implicitly by syscalls such as 160.Xr read 2 161and 162.Xr readv 2 , 163and explicitly by 164.Xr utimes 2 . 165.It Va st_mtim 166Time when file data was last modified. 167Changed implicitly by syscalls such as 168.Xr truncate 2 , 169.Xr write 2 , 170and 171.Xr writev 2 , 172and explicitly by 173.Xr utimes 2 . 174Also, any syscall which modifies directory content changes the 175.Va st_mtim 176for the affected directory. 177For instance, 178.Xr creat 2 , 179.Xr mkdir 2 , 180.Xr rename 2 , 181.Xr link 2 , 182and 183.Xr unlink 2 . 184.It Va st_ctim 185Time when file status was last changed (inode data modification). 186Changed implicitly by any syscall that affects file metadata, including 187.Va st_mtim , 188such as 189.Xr chflags 2 , 190.Xr chmod 2 , 191.Xr chown 2 , 192.Xr truncate 2 , 193.Xr utimes 2 , 194and 195.Xr write 2 . 196Also, any syscall which modifies directory content changes the 197.Va st_ctim 198for the affected directory. 199For instance, 200.Xr creat 2 , 201.Xr mkdir 2 , 202.Xr rename 2 , 203.Xr link 2 , 204and 205.Xr unlink 2 . 206.It Va st_birthtim 207Time when the inode was created. 208.El 209.Pp 210These time-related macros are defined for compatibility: 211.Bd -literal 212#define st_atime st_atim.tv_sec 213#define st_mtime st_mtim.tv_sec 214#define st_ctime st_ctim.tv_sec 215#ifndef _POSIX_SOURCE 216#define st_birthtime st_birthtim.tv_sec 217#endif 218 219#ifndef _POSIX_SOURCE 220#define st_atimespec st_atim 221#define st_mtimespec st_mtim 222#define st_ctimespec st_ctim 223#define st_birthtimespec st_birthtim 224#endif 225.Ed 226.Pp 227Size-related fields of the 228.Vt "struct stat" 229are: 230.Bl -tag -width ".Va st_blksize" 231.It Va st_size 232File size in bytes. 233.It Va st_blksize 234Optimal I/O block size for the file. 235.It Va st_blocks 236Actual number of blocks allocated for the file in 512-byte units. 237As short symbolic links are stored in the inode, this number may 238be zero. 239.El 240.Pp 241The access-related fields of 242.Vt "struct stat" 243are: 244.Bl -tag -width ".Va st_mode" 245.It Va st_uid 246User ID of the file's owner. 247.It Va st_gid 248Group ID of the file. 249.It Va st_mode 250Status of the file (see below). 251.El 252.Pp 253The status information word 254.Fa st_mode 255has these bits: 256.Bd -literal 257#define S_IFMT 0170000 /* type of file mask */ 258#define S_IFIFO 0010000 /* named pipe (fifo) */ 259#define S_IFCHR 0020000 /* character special */ 260#define S_IFDIR 0040000 /* directory */ 261#define S_IFBLK 0060000 /* block special */ 262#define S_IFREG 0100000 /* regular */ 263#define S_IFLNK 0120000 /* symbolic link */ 264#define S_IFSOCK 0140000 /* socket */ 265#define S_IFWHT 0160000 /* whiteout */ 266#define S_ISUID 0004000 /* set user id on execution */ 267#define S_ISGID 0002000 /* set group id on execution */ 268#define S_ISVTX 0001000 /* save swapped text even after use */ 269#define S_IRWXU 0000700 /* RWX mask for owner */ 270#define S_IRUSR 0000400 /* read permission, owner */ 271#define S_IWUSR 0000200 /* write permission, owner */ 272#define S_IXUSR 0000100 /* execute/search permission, owner */ 273#define S_IRWXG 0000070 /* RWX mask for group */ 274#define S_IRGRP 0000040 /* read permission, group */ 275#define S_IWGRP 0000020 /* write permission, group */ 276#define S_IXGRP 0000010 /* execute/search permission, group */ 277#define S_IRWXO 0000007 /* RWX mask for other */ 278#define S_IROTH 0000004 /* read permission, other */ 279#define S_IWOTH 0000002 /* write permission, other */ 280#define S_IXOTH 0000001 /* execute/search permission, other */ 281.Ed 282.Pp 283For a list of access modes, see 284.In sys/stat.h , 285.Xr access 2 286and 287.Xr chmod 2 . 288These macros are available to test whether a 289.Va st_mode 290value passed in the 291.Fa m 292argument corresponds to a file of the specified type: 293.Bl -tag -width ".Fn S_ISFIFO m" 294.It Fn S_ISBLK m 295Test for a block special file. 296.It Fn S_ISCHR m 297Test for a character special file. 298.It Fn S_ISDIR m 299Test for a directory. 300.It Fn S_ISFIFO m 301Test for a pipe or FIFO special file. 302.It Fn S_ISLNK m 303Test for a symbolic link. 304.It Fn S_ISREG m 305Test for a regular file. 306.It Fn S_ISSOCK m 307Test for a socket. 308.It Fn S_ISWHT m 309Test for a whiteout. 310.El 311.Pp 312The macros evaluate to a non-zero value if the test is true 313or to the value 0 if the test is false. 314.Sh RETURN VALUES 315.Rv -std 316.Sh COMPATIBILITY 317Previous versions of the system used different types for the 318.Va st_dev , 319.Va st_uid , 320.Va st_gid , 321.Va st_rdev , 322.Va st_size , 323.Va st_blksize 324and 325.Va st_blocks 326fields. 327.Sh ERRORS 328The 329.Fn stat 330and 331.Fn lstat 332system calls will fail if: 333.Bl -tag -width Er 334.It Bq Er EACCES 335Search permission is denied for a component of the path prefix. 336.It Bq Er EFAULT 337The 338.Fa sb 339or 340.Fa path 341argument 342points to an invalid address. 343.It Bq Er EIO 344An I/O error occurred while reading from or writing to the file system. 345.It Bq Er ELOOP 346Too many symbolic links were encountered in translating the pathname. 347.It Bq Er ENAMETOOLONG 348A component of a pathname exceeded 255 characters, 349or an entire path name exceeded 1023 characters. 350.It Bq Er ENOENT 351The named file does not exist. 352.It Bq Er ENOTDIR 353A component of the path prefix is not a directory. 354.It Bq Er EOVERFLOW 355The file size in bytes cannot be 356represented correctly in the structure pointed to by 357.Fa sb . 358.El 359.Pp 360The 361.Fn fstat 362system call will fail if: 363.Bl -tag -width Er 364.It Bq Er EBADF 365The 366.Fa fd 367argument 368is not a valid open file descriptor. 369.It Bq Er EFAULT 370The 371.Fa sb 372argument 373points to an invalid address. 374.It Bq Er EIO 375An I/O error occurred while reading from or writing to the file system. 376.It Bq Er EOVERFLOW 377The file size in bytes cannot be 378represented correctly in the structure pointed to by 379.Fa sb . 380.El 381.Pp 382In addition to the errors returned by the 383.Fn lstat , 384the 385.Fn fstatat 386may fail if: 387.Bl -tag -width Er 388.It Bq Er EBADF 389The 390.Fa path 391argument does not specify an absolute path and the 392.Fa fd 393argument is neither 394.Dv AT_FDCWD 395nor a valid file descriptor open for searching. 396.It Bq Er EINVAL 397The value of the 398.Fa flag 399argument is not valid. 400.It Bq Er ENOTDIR 401The 402.Fa path 403argument is not an absolute path and 404.Fa fd 405is neither 406.Dv AT_FDCWD 407nor a file descriptor associated with a directory. 408.El 409.Sh SEE ALSO 410.Xr access 2 , 411.Xr chmod 2 , 412.Xr chown 2 , 413.Xr fhstat 2 , 414.Xr statfs 2 , 415.Xr utimes 2 , 416.Xr sticky 7 , 417.Xr symlink 7 418.Sh STANDARDS 419The 420.Fn stat 421and 422.Fn fstat 423system calls are expected to conform to 424.St -p1003.1-90 . 425The 426.Fn fstatat 427system call follows The Open Group Extended API Set 2 specification. 428.Sh HISTORY 429The 430.Fn stat 431and 432.Fn fstat 433system calls appeared in 434.At v1 . 435The 436.Fn lstat 437system call appeared in 438.Bx 4.2 . 439The 440.Fn fstatat 441system call appeared in 442.Fx 8.0 . 443.Sh BUGS 444Applying 445.Fn fstat 446to a socket 447returns a zeroed buffer, 448except for the blocksize field, 449and a unique device and inode number. 450