1.\" $OpenBSD: stat.2,v 1.49 2021/11/21 23:44:55 jan Exp $ 2.\" 3.\" Copyright (c) 1980, 1991, 1993, 1994 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)stat.2 8.3 (Berkeley) 4/19/94 31.\" 32.Dd $Mdocdate: November 21 2021 $ 33.Dt STAT 2 34.Os 35.Sh NAME 36.Nm stat , 37.Nm lstat , 38.Nm fstatat , 39.Nm fstat , 40.Nm S_ISBLK , 41.Nm S_ISCHR , 42.Nm S_ISDIR , 43.Nm S_ISFIFO , 44.Nm S_ISLNK , 45.Nm S_ISREG , 46.Nm S_ISSOCK 47.Nd get file status 48.Sh SYNOPSIS 49.In sys/stat.h 50.Ft int 51.Fn stat "const char *path" "struct stat *sb" 52.Ft int 53.Fn lstat "const char *path" "struct stat *sb" 54.Ft int 55.Fn fstat "int fd" "struct stat *sb" 56.In sys/stat.h 57.In fcntl.h 58.Ft int 59.Fn fstatat "int fd" "const char *path" "struct stat *sb" "int flag" 60.Sh DESCRIPTION 61The 62.Fn stat 63function obtains information about the file pointed to by 64.Fa path . 65Read, write, or execute 66permission of the named file is not required, but all directories 67listed in the pathname leading to the file must be searchable. 68.Pp 69The 70.Fn lstat 71function is identical to 72.Fn stat 73except when the named file is a symbolic link, 74in which case 75.Fn lstat 76returns information about the link itself, not the file the link references. 77.Pp 78The 79.Fn fstatat 80function is equivalent to either the 81.Fn stat 82or 83.Fn lstat 84function depending on the value of 85.Fa flag 86(see below), except that where 87.Fa path 88specifies a relative path, 89the file whose information is returned is determined relative to 90the directory associated with file descriptor 91.Fa fd 92instead of the current working directory. 93.Pp 94If 95.Fn fstatat 96is passed the special value 97.Dv AT_FDCWD 98(defined in 99.In fcntl.h ) 100in the 101.Fa fd 102parameter, the current working directory is used 103and the behavior is identical to a call to 104.Fn stat 105or 106.Fn lstat , 107depending on whether or not the 108.Dv AT_SYMLINK_NOFOLLOW 109bit is set in 110.Fa flag . 111.Pp 112The 113.Fa flag 114argument is the bitwise OR of zero or more of the following values: 115.Pp 116.Bl -tag -width AT_SYMLINK_NOFOLLOW -offset indent -compact 117.It Dv AT_SYMLINK_NOFOLLOW 118If 119.Fa path 120names a symbolic link, then the status of the symbolic link is returned. 121.El 122.Pp 123The 124.Fn fstat 125function obtains the same information about an open file 126known by the file descriptor 127.Fa fd . 128.Pp 129The 130.Fa sb 131argument is a pointer to a 132.Vt stat 133structure 134as defined by 135.In sys/stat.h 136(shown below) 137and into which information is placed concerning the file. 138.Bd -literal 139struct stat { 140 dev_t st_dev; /* inode's device */ 141 ino_t st_ino; /* inode's number */ 142 mode_t st_mode; /* inode protection mode */ 143 nlink_t st_nlink; /* number of hard links */ 144 uid_t st_uid; /* user ID of the file's owner */ 145 gid_t st_gid; /* group ID of the file's group */ 146 dev_t st_rdev; /* device type */ 147 struct timespec st_atim; /* time of last access */ 148 struct timespec st_mtim; /* time of last data modification */ 149 struct timespec st_ctim; /* time of last file status change */ 150 off_t st_size; /* file size, in bytes */ 151 blkcnt_t st_blocks; /* blocks allocated for file */ 152 blksize_t st_blksize; /* optimal blocksize for I/O */ 153 u_int32_t st_flags; /* user defined flags for file */ 154 u_int32_t st_gen; /* file generation number */ 155}; 156.Ed 157.Pp 158The time-related fields of 159.Vt struct stat 160are represented in 161.Vt struct timespec 162format, which has nanosecond precision. 163However, the actual precision is generally limited by the file 164system holding the file. 165The fields are as follows: 166.Bl -tag -width XXXst_mtim 167.It Fa st_atim 168Time when file data was last accessed. 169Set when the file system object was created and updated by the 170.Xr utimes 2 171and 172.Xr read 2 173system calls. 174.It Fa st_mtim 175Time when file data was last modified. 176Changed by the 177.Xr truncate 2 , 178.Xr utimes 2 , 179and 180.Xr write 2 181system calls. 182For directories, changed by any system call that alters which files are 183in the directory, such as the 184.Xr unlink 2 , 185.Xr rename 2 , 186.Xr mkdir 2 , 187and 188.Xr symlink 2 189system calls. 190.It Fa st_ctim 191Time when file status was last changed (inode data modification). 192Changed by the 193.Xr chmod 2 , 194.Xr chown 2 , 195.Xr link 2 , 196.Xr rename 2 , 197.Xr unlink 2 , 198.Xr utimes 2 , 199and 200.Xr write 2 201system calls. 202.El 203.Pp 204In addition, all the time fields are set to the current time when 205a file system object is first created by the 206.Xr mkdir 2 , 207.Xr mkfifo 2 , 208.Xr mknod 2 , 209.Xr open 2 , 210and 211.Xr symlink 2 212system calls. 213.Pp 214For compatibility with previous standards, 215.Fa st_atime , 216.Fa st_mtime , 217and 218.Fa st_ctime 219macros are provided that expand to the 220.Fa tv_secs 221member of their respective 222.Vt struct timespec 223member. 224Deprecated macros are also provided for some transitional names: 225.Fa st_atimensec , 226.Fa st_mtimensec , 227.Fa st_ctimensec , 228.Fa st_atimespec , 229.Fa st_mtimespec , 230and 231.Fa st_ctimespec . 232.Pp 233The size-related fields of the 234.Vt struct stat 235are as follows: 236.Bl -tag -width XXXst_blksize 237.It Fa st_blksize 238The optimal I/O block size for the file. 239.It Fa st_blocks 240The actual number of blocks allocated for the file in 512-byte units. 241As short symbolic links are stored in the inode, this number may 242be zero. 243.El 244.Pp 245The status information word 246.Fa st_mode 247has the following bits: 248.Bd -literal -offset indent 249#define S_IFMT 0170000 /* type of file mask */ 250#define S_IFIFO 0010000 /* named pipe (fifo) */ 251#define S_IFCHR 0020000 /* character special */ 252#define S_IFDIR 0040000 /* directory */ 253#define S_IFBLK 0060000 /* block special */ 254#define S_IFREG 0100000 /* regular */ 255#define S_IFLNK 0120000 /* symbolic link */ 256#define S_IFSOCK 0140000 /* socket */ 257#define S_ISUID 0004000 /* set-user-ID on execution */ 258#define S_ISGID 0002000 /* set-group-ID on execution */ 259#define S_ISVTX 0001000 /* save swapped text even after use */ 260#define S_IRWXU 0000700 /* RWX mask for owner */ 261#define S_IRUSR 0000400 /* R for owner */ 262#define S_IWUSR 0000200 /* W for owner */ 263#define S_IXUSR 0000100 /* X for owner */ 264#define S_IRWXG 0000070 /* RWX mask for group */ 265#define S_IRGRP 0000040 /* R for group */ 266#define S_IWGRP 0000020 /* W for group */ 267#define S_IXGRP 0000010 /* X for group */ 268#define S_IRWXO 0000007 /* RWX mask for other */ 269#define S_IROTH 0000004 /* R for other */ 270#define S_IWOTH 0000002 /* W for other */ 271#define S_IXOTH 0000001 /* X for other */ 272.Ed 273.Pp 274The following macros test a file's type. 275If the file is of that type, a non-zero value is returned; 276otherwise, 0 is returned. 277.Bd -literal -offset indent 278S_ISBLK(st_mode m) /* block special */ 279S_ISCHR(st_mode m) /* char special */ 280S_ISDIR(st_mode m) /* directory */ 281S_ISFIFO(st_mode m) /* fifo */ 282S_ISLNK(st_mode m) /* symbolic link */ 283S_ISREG(st_mode m) /* regular file */ 284S_ISSOCK(st_mode m) /* socket */ 285.Ed 286.Pp 287For a list of access modes, see 288.In sys/stat.h , 289.Xr access 2 , 290and 291.Xr chmod 2 . 292.Sh RETURN VALUES 293.Rv -std 294.Sh ERRORS 295.Fn stat , 296.Fn lstat , 297and 298.Fn fstatat 299will fail if: 300.Bl -tag -width Er 301.It Bq Er ENOTDIR 302A component of the path prefix is not a directory. 303.It Bq Er ENAMETOOLONG 304A component of a pathname exceeded 305.Dv NAME_MAX 306characters, or an entire pathname (including the terminating NUL) 307exceeded 308.Dv PATH_MAX 309bytes. 310.It Bq Er ENOENT 311A component of 312.Fa path 313does not exist or 314.Fa path 315is an empty string. 316.It Bq Er EACCES 317Search permission is denied for a component of the 318.Fa path . 319.It Bq Er ELOOP 320Too many symbolic links were encountered in translating the 321.Fa path . 322.It Bq Er EFAULT 323.Fa sb 324or 325.Fa path 326points to an invalid address. 327.It Bq Er EIO 328An I/O error occurred while reading from or writing to the file system. 329.El 330.Pp 331Additionally, 332.Fn fstatat 333will fail if: 334.Bl -tag -width Er 335.It Bq Er EINVAL 336The value of the 337.Fa flag 338argument was neither zero nor 339.Dv AT_SYMLINK_NOFOLLOW . 340.It Bq Er EBADF 341The 342.Fa path 343argument specifies a relative path and the 344.Fa fd 345argument is neither 346.Dv AT_FDCWD 347nor a valid file descriptor. 348.It Bq Er ENOTDIR 349The 350.Fa path 351argument specifies a relative path and the 352.Fa fd 353argument is a valid file descriptor but it does not reference a directory. 354.It Bq Er EACCES 355The 356.Fa path 357argument specifies a relative path but search permission is denied 358for the directory which the 359.Fa fd 360file descriptor references. 361.El 362.Pp 363.Fn fstat 364will fail if: 365.Bl -tag -width Er 366.It Bq Er EBADF 367.Fa fd 368is not a valid open file descriptor. 369.It Bq Er EFAULT 370.Fa sb 371points to an invalid address. 372.It Bq Er EIO 373An I/O error occurred while reading from the file system. 374.El 375.Sh SEE ALSO 376.Xr chmod 2 , 377.Xr chown 2 , 378.Xr clock_gettime 2 , 379.Xr utimes 2 , 380.Xr symlink 7 381.Sh STANDARDS 382Previous versions of the system used different types for the 383.Fa st_dev , 384.Fa st_uid , 385.Fa st_gid , 386.Fa st_rdev , 387.Fa st_size , 388.Fa st_blksize , 389and 390.Fa st_blocks 391fields. 392.Pp 393The 394.Fn fstat , 395.Fn fstatat , 396.Fn lstat , 397and 398.Fn stat 399functions are intended to conform to 400.St -p1003.1-2008 . 401.Sh HISTORY 402The 403.Fn stat 404and 405.Fn fstat 406system calls first appeared in 407.At v1 . 408The 409.In sys/stat.h 410header file and the 411.Vt "struct stat" 412were introduced in 413.At v7 . 414.Pp 415An 416.Fn lstat 417function call appeared in 418.Bx 4.2 . 419The 420.Fn fstatat 421function appeared in 422.Ox 5.0 . 423.Sh CAVEATS 424The file generation number, 425.Fa st_gen , 426is only available to the superuser. 427.Pp 428Certain programs written when the timestamps were just of type 429.Vt time_t 430assumed that the members were consecutive (and could therefore 431be treated as an array and have their address passed directly to 432.Xr utime 3 ) . 433The transition to timestamps of type 434.Vt struct timespec 435broke them irrevocably. 436.Sh BUGS 437Applying 438.Fn fstat 439to a pipe or socket 440fails to fill in a unique device and inode number pair. 441Applying 442.Fn fstat 443to a socket 444also fails to fill in the time fields. 445