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