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 September 22, 2026 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_bsdflags" 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.It Va st_bsdflags 176Miscellaneous file flags (see below). 177.El 178.Pp 179The 180.Va st_dev 181and 182.Va st_ino 183fields together identify the file uniquely within the system. 184.Pp 185The time-related fields of 186.Vt "struct stat" 187are: 188.Bl -tag -width ".Va st_birthtim" 189.It Va st_atim 190Time when file data was last accessed. 191Changed implicitly by syscalls such as 192.Xr read 2 193and 194.Xr readv 2 , 195and explicitly by 196.Xr utimes 2 . 197.It Va st_mtim 198Time when file data was last modified. 199Changed implicitly by syscalls such as 200.Xr truncate 2 , 201.Xr write 2 , 202and 203.Xr writev 2 , 204and explicitly by 205.Xr utimes 2 . 206Also, any syscall which modifies directory content changes the 207.Va st_mtim 208for the affected directory. 209For instance, 210.Xr creat 2 , 211.Xr mkdir 2 , 212.Xr rename 2 , 213.Xr link 2 , 214and 215.Xr unlink 2 . 216.It Va st_ctim 217Time when file status was last changed (inode data modification). 218Changed implicitly by any syscall that affects file metadata, including 219.Va st_mtim , 220such as 221.Xr chflags 2 , 222.Xr chmod 2 , 223.Xr chown 2 , 224.Xr truncate 2 , 225.Xr utimes 2 , 226and 227.Xr write 2 . 228Also, any syscall which modifies directory content changes the 229.Va st_ctim 230for the affected directory. 231For instance, 232.Xr creat 2 , 233.Xr mkdir 2 , 234.Xr rename 2 , 235.Xr link 2 , 236and 237.Xr unlink 2 . 238.It Va st_birthtim 239Time when the inode was created. 240.El 241.Pp 242These time-related macros are defined for compatibility: 243.Bd -literal 244#define st_atime st_atim.tv_sec 245#define st_mtime st_mtim.tv_sec 246#define st_ctime st_ctim.tv_sec 247#ifndef _POSIX_SOURCE 248#define st_birthtime st_birthtim.tv_sec 249#endif 250 251#ifndef _POSIX_SOURCE 252#define st_atimespec st_atim 253#define st_mtimespec st_mtim 254#define st_ctimespec st_ctim 255#define st_birthtimespec st_birthtim 256#endif 257.Ed 258.Pp 259Size-related fields of the 260.Vt "struct stat" 261are: 262.Bl -tag -width ".Va st_blksize" 263.It Va st_size 264File size in bytes. 265.It Va st_blksize 266Optimal I/O block size for the file. 267.It Va st_blocks 268Actual number of blocks allocated for the file. 269.Pp 270For files located on non-synthetic filesystems, the block size is 512 bytes. 271As short symbolic links are stored in the inode, this number may 272be zero. 273.Pp 274For file descriptors referencing specific non-file objects, 275the field is type-specific. 276.Pp 277For POSIX shared memory segments 278.Pq see Xr shm_open 2 , 279the block size is 512 bytes. 280For older versions of 281.Fx 282.Pq before 15.2 and 16.0 283the block size was equal to the platform page size 284.Pq Xr getpagesize 2 . 285.El 286.Pp 287The access-related fields of 288.Vt "struct stat" 289are: 290.Bl -tag -width ".Va st_mode" 291.It Va st_uid 292User ID of the file's owner. 293.It Va st_gid 294Group ID of the file. 295.It Va st_mode 296Status of the file (see below). 297.El 298.Pp 299The status information word 300.Fa st_mode 301has these bits: 302.Bd -literal 303#define S_IFMT 0170000 /* type of file mask */ 304#define S_IFIFO 0010000 /* named pipe (fifo) */ 305#define S_IFCHR 0020000 /* character special */ 306#define S_IFDIR 0040000 /* directory */ 307#define S_IFBLK 0060000 /* block special */ 308#define S_IFREG 0100000 /* regular */ 309#define S_IFLNK 0120000 /* symbolic link */ 310#define S_IFSOCK 0140000 /* socket */ 311#define S_IFWHT 0160000 /* whiteout */ 312#define S_ISUID 0004000 /* set user id on execution */ 313#define S_ISGID 0002000 /* set group id on execution */ 314#define S_ISVTX 0001000 /* sticky(7) bit is set */ 315#define S_IRWXU 0000700 /* RWX mask for owner */ 316#define S_IRUSR 0000400 /* read permission, owner */ 317#define S_IWUSR 0000200 /* write permission, owner */ 318#define S_IXUSR 0000100 /* execute/search permission, owner */ 319#define S_IRWXG 0000070 /* RWX mask for group */ 320#define S_IRGRP 0000040 /* read permission, group */ 321#define S_IWGRP 0000020 /* write permission, group */ 322#define S_IXGRP 0000010 /* execute/search permission, group */ 323#define S_IRWXO 0000007 /* RWX mask for other */ 324#define S_IROTH 0000004 /* read permission, other */ 325#define S_IWOTH 0000002 /* write permission, other */ 326#define S_IXOTH 0000001 /* execute/search permission, other */ 327.Ed 328.Pp 329For a list of access modes, see 330.In sys/stat.h , 331.Xr access 2 332and 333.Xr chmod 2 . 334These macros are available to test whether a 335.Va st_mode 336value passed in the 337.Fa m 338argument corresponds to a file of the specified type: 339.Bl -tag -width ".Fn S_ISFIFO m" 340.It Fn S_ISBLK m 341Test for a block special file. 342.It Fn S_ISCHR m 343Test for a character special file. 344.It Fn S_ISDIR m 345Test for a directory. 346.It Fn S_ISFIFO m 347Test for a pipe or FIFO special file. 348.It Fn S_ISLNK m 349Test for a symbolic link. 350.It Fn S_ISREG m 351Test for a regular file. 352.It Fn S_ISSOCK m 353Test for a socket. 354.It Fn S_ISWHT m 355Test for a whiteout. 356.El 357.Pp 358The macros evaluate to a non-zero value if the test is true 359or to the value 0 if the test is false. 360.Pp 361The 362.Va st_bsdflags 363field contains the following flags, defined in 364.In sys/stat.h : 365.Bl -tag -width indent 366.It Dv SFBSD_NAMEDATTR 367The file is a named attribute or a named attribute directory. 368.It Dv SFBSD_MNTPOINT 369The file is a mount point. 370The reported file attributes describe the root node of the file system 371mounted on it. 372.El 373.Sh RETURN VALUES 374.Rv -std 375.Sh COMPATIBILITY 376Previous versions of the system used different types for the 377.Va st_dev , 378.Va st_uid , 379.Va st_gid , 380.Va st_rdev , 381.Va st_size , 382.Va st_blksize 383and 384.Va st_blocks 385fields. 386.Sh ERRORS 387The 388.Fn stat 389and 390.Fn lstat 391system calls will fail if: 392.Bl -tag -width Er 393.It Bq Er EACCES 394Search permission is denied for a component of the path prefix. 395.It Bq Er EFAULT 396The 397.Fa sb 398or 399.Fa path 400argument 401points to an invalid address. 402.It Bq Er EIO 403An I/O error occurred while reading from or writing to the file system. 404.It Bq Er EINTEGRITY 405Corrupted data was detected while reading from the file system. 406.It Bq Er ELOOP 407Too many symbolic links were encountered in translating the pathname. 408.It Bq Er ENAMETOOLONG 409A component of a pathname exceeded 255 characters, 410or an entire path name exceeded 1023 characters. 411.It Bq Er ENOENT 412The named file does not exist. 413.It Bq Er ENOTDIR 414A component of the path prefix is not a directory. 415.It Bq Er EOVERFLOW 416The file size in bytes cannot be 417represented correctly in the structure pointed to by 418.Fa sb . 419.El 420.Pp 421The 422.Fn fstat 423system call will fail if: 424.Bl -tag -width Er 425.It Bq Er EBADF 426The 427.Fa fd 428argument 429is not a valid open file descriptor. 430.It Bq Er EFAULT 431The 432.Fa sb 433argument 434points to an invalid address. 435.It Bq Er EIO 436An I/O error occurred while reading from or writing to the file system. 437.It Bq Er EINTEGRITY 438Corrupted data was detected while reading from the file system. 439.It Bq Er EOVERFLOW 440The file size in bytes cannot be 441represented correctly in the structure pointed to by 442.Fa sb . 443.El 444.Pp 445In addition to the errors returned by the 446.Fn lstat , 447the 448.Fn fstatat 449may fail if: 450.Bl -tag -width Er 451.It Bq Er EBADF 452The 453.Fa path 454argument does not specify an absolute path and the 455.Fa fd 456argument is neither 457.Dv AT_FDCWD 458nor a valid file descriptor open for searching. 459.It Bq Er EINVAL 460The value of the 461.Fa flag 462argument is not valid. 463.It Bq Er ENOTDIR 464The 465.Fa path 466argument is not an absolute path and 467.Fa fd 468is neither 469.Dv AT_FDCWD 470nor a file descriptor associated with a directory. 471.It Bq Er ENOTCAPABLE 472.Fa path 473is an absolute path, 474or contained a ".." component leading to a 475directory outside of the directory hierarchy specified by 476.Fa fd , 477and the process is in capability mode or the 478.Dv AT_RESOLVE_BENEATH 479flag was specified. 480.El 481.Sh SEE ALSO 482.Xr access 2 , 483.Xr chmod 2 , 484.Xr chown 2 , 485.Xr fhstat 2 , 486.Xr statfs 2 , 487.Xr utimes 2 , 488.Xr sticky 7 , 489.Xr symlink 7 490.Sh STANDARDS 491The 492.Fn stat 493and 494.Fn fstat 495system calls are expected to conform to 496.St -p1003.1-90 . 497The 498.Fn fstatat 499system call follows The Open Group Extended API Set 2 specification. 500.Pp 501The 502.Va st_bsdflags 503field is a non-portable 504.Fx Ns -only 505extension. 506.Sh HISTORY 507The 508.Fn stat 509and 510.Fn fstat 511system calls appeared in 512.At v1 . 513The 514.Fn lstat 515system call appeared in 516.Bx 4.2 . 517The 518.Fn fstatat 519system call appeared in 520.Fx 8.0 . 521.Sh BUGS 522Applying 523.Fn fstat 524to a socket 525returns a zeroed buffer, 526except for the blocksize field, 527and a unique device and inode number. 528