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