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 February 23, 2021 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. 88For 89.Fn fstatat 90and relative 91.Fa path , 92the status is retrieved from a file relative to 93the directory associated with the file descriptor 94.Fa fd 95instead of the current working directory. 96.Pp 97The values for the 98.Fa flag 99are constructed by a bitwise-inclusive OR of flags from this list, 100defined in 101.In fcntl.h : 102.Bl -tag -width indent 103.It Dv AT_SYMLINK_NOFOLLOW 104If 105.Fa path 106names a symbolic link, the status of the symbolic link is returned. 107.It Dv AT_RESOLVE_BENEATH 108Only walk paths below the starting directory. 109See the description of the 110.Dv O_RESOLVE_BENEATH 111flag in the 112.Xr open 2 113manual page. 114.El 115.Pp 116If 117.Fn fstatat 118is passed the special value 119.Dv AT_FDCWD 120in the 121.Fa fd 122parameter, the current working directory is used and the behavior is 123identical to a call to 124.Fn stat 125or 126.Fn lstat 127respectively, depending on whether or not the 128.Dv AT_SYMLINK_NOFOLLOW 129bit is set in 130.Fa flag . 131.Pp 132When 133.Fn fstatat 134is called with an absolute 135.Fa path , 136it ignores the 137.Fa fd 138argument. 139.Pp 140The 141.Fa sb 142argument is a pointer to a 143.Vt stat 144structure 145as defined by 146.In sys/stat.h 147and into which information is placed concerning the file. 148.Pp 149The fields of 150.Vt "struct stat" 151related to the file system are: 152.Bl -tag -width ".Va st_nlink" 153.It Va st_dev 154Numeric ID of the device containing the file. 155.It Va st_ino 156The file's inode number. 157.It Va st_nlink 158Number of hard links to the file. 159.It Va st_flags 160Flags enabled for the file. 161See 162.Xr chflags 2 163for the list of flags and their description. 164.El 165.Pp 166The 167.Va st_dev 168and 169.Va st_ino 170fields together identify the file uniquely within the system. 171.Pp 172The time-related fields of 173.Vt "struct stat" 174are: 175.Bl -tag -width ".Va st_birthtim" 176.It Va st_atim 177Time when file data was last accessed. 178Changed implicitly by syscalls such as 179.Xr read 2 180and 181.Xr readv 2 , 182and explicitly by 183.Xr utimes 2 . 184.It Va st_mtim 185Time when file data was last modified. 186Changed implicitly by syscalls such as 187.Xr truncate 2 , 188.Xr write 2 , 189and 190.Xr writev 2 , 191and explicitly by 192.Xr utimes 2 . 193Also, any syscall which modifies directory content changes the 194.Va st_mtim 195for the affected directory. 196For instance, 197.Xr creat 2 , 198.Xr mkdir 2 , 199.Xr rename 2 , 200.Xr link 2 , 201and 202.Xr unlink 2 . 203.It Va st_ctim 204Time when file status was last changed (inode data modification). 205Changed implicitly by any syscall that affects file metadata, including 206.Va st_mtim , 207such as 208.Xr chflags 2 , 209.Xr chmod 2 , 210.Xr chown 2 , 211.Xr truncate 2 , 212.Xr utimes 2 , 213and 214.Xr write 2 . 215Also, any syscall which modifies directory content changes the 216.Va st_ctim 217for the affected directory. 218For instance, 219.Xr creat 2 , 220.Xr mkdir 2 , 221.Xr rename 2 , 222.Xr link 2 , 223and 224.Xr unlink 2 . 225.It Va st_birthtim 226Time when the inode was created. 227.El 228.Pp 229These time-related macros are defined for compatibility: 230.Bd -literal 231#define st_atime st_atim.tv_sec 232#define st_mtime st_mtim.tv_sec 233#define st_ctime st_ctim.tv_sec 234#ifndef _POSIX_SOURCE 235#define st_birthtime st_birthtim.tv_sec 236#endif 237 238#ifndef _POSIX_SOURCE 239#define st_atimespec st_atim 240#define st_mtimespec st_mtim 241#define st_ctimespec st_ctim 242#define st_birthtimespec st_birthtim 243#endif 244.Ed 245.Pp 246Size-related fields of the 247.Vt "struct stat" 248are: 249.Bl -tag -width ".Va st_blksize" 250.It Va st_size 251File size in bytes. 252.It Va st_blksize 253Optimal I/O block size for the file. 254.It Va st_blocks 255Actual number of blocks allocated for the file in 512-byte units. 256As short symbolic links are stored in the inode, this number may 257be zero. 258.El 259.Pp 260The access-related fields of 261.Vt "struct stat" 262are: 263.Bl -tag -width ".Va st_mode" 264.It Va st_uid 265User ID of the file's owner. 266.It Va st_gid 267Group ID of the file. 268.It Va st_mode 269Status of the file (see below). 270.El 271.Pp 272The status information word 273.Fa st_mode 274has these bits: 275.Bd -literal 276#define S_IFMT 0170000 /* type of file mask */ 277#define S_IFIFO 0010000 /* named pipe (fifo) */ 278#define S_IFCHR 0020000 /* character special */ 279#define S_IFDIR 0040000 /* directory */ 280#define S_IFBLK 0060000 /* block special */ 281#define S_IFREG 0100000 /* regular */ 282#define S_IFLNK 0120000 /* symbolic link */ 283#define S_IFSOCK 0140000 /* socket */ 284#define S_IFWHT 0160000 /* whiteout */ 285#define S_ISUID 0004000 /* set user id on execution */ 286#define S_ISGID 0002000 /* set group id on execution */ 287#define S_ISVTX 0001000 /* save swapped text even after use */ 288#define S_IRWXU 0000700 /* RWX mask for owner */ 289#define S_IRUSR 0000400 /* read permission, owner */ 290#define S_IWUSR 0000200 /* write permission, owner */ 291#define S_IXUSR 0000100 /* execute/search permission, owner */ 292#define S_IRWXG 0000070 /* RWX mask for group */ 293#define S_IRGRP 0000040 /* read permission, group */ 294#define S_IWGRP 0000020 /* write permission, group */ 295#define S_IXGRP 0000010 /* execute/search permission, group */ 296#define S_IRWXO 0000007 /* RWX mask for other */ 297#define S_IROTH 0000004 /* read permission, other */ 298#define S_IWOTH 0000002 /* write permission, other */ 299#define S_IXOTH 0000001 /* execute/search permission, other */ 300.Ed 301.Pp 302For a list of access modes, see 303.In sys/stat.h , 304.Xr access 2 305and 306.Xr chmod 2 . 307These macros are available to test whether a 308.Va st_mode 309value passed in the 310.Fa m 311argument corresponds to a file of the specified type: 312.Bl -tag -width ".Fn S_ISFIFO m" 313.It Fn S_ISBLK m 314Test for a block special file. 315.It Fn S_ISCHR m 316Test for a character special file. 317.It Fn S_ISDIR m 318Test for a directory. 319.It Fn S_ISFIFO m 320Test for a pipe or FIFO special file. 321.It Fn S_ISLNK m 322Test for a symbolic link. 323.It Fn S_ISREG m 324Test for a regular file. 325.It Fn S_ISSOCK m 326Test for a socket. 327.It Fn S_ISWHT m 328Test for a whiteout. 329.El 330.Pp 331The macros evaluate to a non-zero value if the test is true 332or to the value 0 if the test is false. 333.Sh RETURN VALUES 334.Rv -std 335.Sh COMPATIBILITY 336Previous versions of the system used different types for the 337.Va st_dev , 338.Va st_uid , 339.Va st_gid , 340.Va st_rdev , 341.Va st_size , 342.Va st_blksize 343and 344.Va st_blocks 345fields. 346.Sh ERRORS 347The 348.Fn stat 349and 350.Fn lstat 351system calls will fail if: 352.Bl -tag -width Er 353.It Bq Er EACCES 354Search permission is denied for a component of the path prefix. 355.It Bq Er EFAULT 356The 357.Fa sb 358or 359.Fa path 360argument 361points to an invalid address. 362.It Bq Er EIO 363An I/O error occurred while reading from or writing to the file system. 364.It Bq Er EINTEGRITY 365Corrupted data was detected while reading from the file system. 366.It Bq Er ELOOP 367Too many symbolic links were encountered in translating the pathname. 368.It Bq Er ENAMETOOLONG 369A component of a pathname exceeded 255 characters, 370or an entire path name exceeded 1023 characters. 371.It Bq Er ENOENT 372The named file does not exist. 373.It Bq Er ENOTDIR 374A component of the path prefix is not a directory. 375.It Bq Er EOVERFLOW 376The file size in bytes cannot be 377represented correctly in the structure pointed to by 378.Fa sb . 379.El 380.Pp 381The 382.Fn fstat 383system call will fail if: 384.Bl -tag -width Er 385.It Bq Er EBADF 386The 387.Fa fd 388argument 389is not a valid open file descriptor. 390.It Bq Er EFAULT 391The 392.Fa sb 393argument 394points to an invalid address. 395.It Bq Er EIO 396An I/O error occurred while reading from or writing to the file system. 397.It Bq Er EINTEGRITY 398Corrupted data was detected while reading from the file system. 399.It Bq Er EOVERFLOW 400The file size in bytes cannot be 401represented correctly in the structure pointed to by 402.Fa sb . 403.El 404.Pp 405In addition to the errors returned by the 406.Fn lstat , 407the 408.Fn fstatat 409may fail if: 410.Bl -tag -width Er 411.It Bq Er EBADF 412The 413.Fa path 414argument does not specify an absolute path and the 415.Fa fd 416argument is neither 417.Dv AT_FDCWD 418nor a valid file descriptor open for searching. 419.It Bq Er EINVAL 420The value of the 421.Fa flag 422argument is not valid. 423.It Bq Er ENOTDIR 424The 425.Fa path 426argument is not an absolute path and 427.Fa fd 428is neither 429.Dv AT_FDCWD 430nor a file descriptor associated with a directory. 431.It Bq Er ENOTCAPABLE 432.Fa path 433is an absolute path, 434or contained a ".." component leading to a 435directory outside of the directory hierarchy specified by 436.Fa fd , 437and the process is in capability mode or the 438.Dv AT_RESOLVE_BENEATH 439flag was specified. 440.El 441.Sh SEE ALSO 442.Xr access 2 , 443.Xr chmod 2 , 444.Xr chown 2 , 445.Xr fhstat 2 , 446.Xr statfs 2 , 447.Xr utimes 2 , 448.Xr sticky 7 , 449.Xr symlink 7 450.Sh STANDARDS 451The 452.Fn stat 453and 454.Fn fstat 455system calls are expected to conform to 456.St -p1003.1-90 . 457The 458.Fn fstatat 459system call follows The Open Group Extended API Set 2 specification. 460.Sh HISTORY 461The 462.Fn stat 463and 464.Fn fstat 465system calls appeared in 466.At v1 . 467The 468.Fn lstat 469system call appeared in 470.Bx 4.2 . 471The 472.Fn fstatat 473system call appeared in 474.Fx 8.0 . 475.Sh BUGS 476Applying 477.Fn fstat 478to a socket 479returns a zeroed buffer, 480except for the blocksize field, 481and a unique device and inode number. 482