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.\" 4. 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 April 10, 2008 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/types.h 44.In sys/stat.h 45.Ft int 46.Fn stat "const char *path" "struct stat *sb" 47.Ft int 48.Fn lstat "const char *path" "struct stat *sb" 49.Ft int 50.Fn fstat "int fd" "struct stat *sb" 51.Ft int 52.Fn fstatat "int fd" "const char *path" "struct stat *buf" "int flag" 53.Sh DESCRIPTION 54The 55.Fn stat 56system call obtains information about the file pointed to by 57.Fa path . 58Read, write or execute 59permission of the named file is not required, but all directories 60listed in the path name leading to the file must be searchable. 61.Pp 62The 63.Fn lstat 64system call is like 65.Fn stat 66except in the case where the named file is a symbolic link, 67in which case 68.Fn lstat 69returns information about the link, 70while 71.Fn stat 72returns information about the file the link references. 73.Pp 74The 75.Fn fstat 76system call obtains the same information about an open file 77known by the file descriptor 78.Fa fd . 79.Pp 80The 81.Fn fstatat 82system call is equivalent to 83.Fn stat 84and 85.Fn lstat 86except in the case where the 87.Fa path 88specifies a relative path. 89In this case the 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 the following 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.El 105.Pp 106If 107.Fn fstatat 108is passed the special value 109.Dv AT_FDCWD 110in the 111.Fa fd 112parameter, the current working directory is used and the behavior is 113identical to a call to 114.Fn stat 115or 116.Fn lstat 117respectively, depending on whether or not the 118.Dv AT_SYMLINK_NOFOLLOW 119bit is set in 120.Fa flag . 121.Pp 122The 123.Fa sb 124argument is a pointer to a 125.Vt stat 126structure 127as defined by 128.In sys/stat.h 129and into which information is placed concerning the file. 130.Pp 131The fields of 132.Vt "struct stat" 133related to the file system are as follows: 134.Bl -tag -width ".Va st_nlink" 135.It Va st_dev 136The numeric ID of the device containing the file. 137.It Va st_ino 138The file's inode number. 139.It Va st_nlink 140The number of hard links to the file. 141.El 142.Pp 143The 144.Va st_dev 145and 146.Va st_ino 147fields together identify the file uniquely within the system. 148.Pp 149The time-related fields of 150.Vt "struct stat" 151are as follows: 152.Bl -tag -width ".Va st_birthtime" 153.It Va st_atime 154Time when file data last accessed. 155Changed by the 156.Xr mknod 2 , 157.Xr utimes 2 , 158.Xr read 2 159and 160.Xr readv 2 161system calls. 162.It Va st_mtime 163Time when file data last modified. 164Changed by the 165.Xr mkdir 2 , 166.Xr mkfifo 2 , 167.Xr mknod 2 , 168.Xr utimes 2 , 169.Xr write 2 170and 171.Xr writev 2 172system calls. 173.It Va st_ctime 174Time when file status was last changed (inode data modification). 175Changed by the 176.Xr chflags 2 , 177.Xr chmod 2 , 178.Xr chown 2 , 179.Xr creat 2 , 180.Xr link 2 , 181.Xr mkdir 2 , 182.Xr mkfifo 2 , 183.Xr mknod 2 , 184.Xr rename 2 , 185.Xr rmdir 2 , 186.Xr symlink 2 , 187.Xr truncate 2 , 188.Xr unlink 2 , 189.Xr utimes 2 , 190.Xr write 2 191and 192.Xr writev 2 193system calls. 194.It Va st_birthtime 195Time when the inode was created. 196.El 197.Pp 198If 199.Dv _POSIX_SOURCE 200is not defined, the time-related fields are defined as: 201.Bd -literal 202#ifndef _POSIX_SOURCE 203#define st_atime st_atimespec.tv_sec 204#define st_mtime st_mtimespec.tv_sec 205#define st_ctime st_ctimespec.tv_sec 206#endif 207.Ed 208.Pp 209The size-related fields of the 210.Vt "struct stat" 211are as follows: 212.Bl -tag -width ".Va st_blksize" 213.It Va st_size 214The file size in bytes. 215.It Va st_blksize 216The optimal I/O block size for the file. 217.It Va st_blocks 218The actual number of blocks allocated for the file in 512-byte units. 219As short symbolic links are stored in the inode, this number may 220be zero. 221.El 222.Pp 223The access-related fields of 224.Vt "struct stat" 225are as follows: 226.Bl -tag -width ".Va st_mode" 227.It Va st_uid 228The user ID of the file's owner. 229.It Va st_gid 230The group ID of the file. 231.It Va st_mode 232Status of the file (see below). 233.El 234.Pp 235The status information word 236.Fa st_mode 237has the following bits: 238.Bd -literal 239#define S_IFMT 0170000 /* type of file mask */ 240#define S_IFIFO 0010000 /* named pipe (fifo) */ 241#define S_IFCHR 0020000 /* character special */ 242#define S_IFDIR 0040000 /* directory */ 243#define S_IFBLK 0060000 /* block special */ 244#define S_IFREG 0100000 /* regular */ 245#define S_IFLNK 0120000 /* symbolic link */ 246#define S_IFSOCK 0140000 /* socket */ 247#define S_IFWHT 0160000 /* whiteout */ 248#define S_ISUID 0004000 /* set user id on execution */ 249#define S_ISGID 0002000 /* set group id on execution */ 250#define S_ISVTX 0001000 /* save swapped text even after use */ 251#define S_IRWXU 0000700 /* RWX mask for owner */ 252#define S_IRUSR 0000400 /* read permission, owner */ 253#define S_IWUSR 0000200 /* write permission, owner */ 254#define S_IXUSR 0000100 /* execute/search permission, owner */ 255#define S_IRWXG 0000070 /* RWX mask for group */ 256#define S_IRGRP 0000040 /* read permission, group */ 257#define S_IWGRP 0000020 /* write permission, group */ 258#define S_IXGRP 0000010 /* execute/search permission, group */ 259#define S_IRWXO 0000007 /* RWX mask for other */ 260#define S_IROTH 0000004 /* read permission, other */ 261#define S_IWOTH 0000002 /* write permission, other */ 262#define S_IXOTH 0000001 /* execute/search permission, other */ 263.Ed 264.Pp 265For a list of access modes, see 266.In sys/stat.h , 267.Xr access 2 268and 269.Xr chmod 2 . 270The following macros are available to test whether a 271.Va st_mode 272value passed in the 273.Fa m 274argument corresponds to a file of the specified type: 275.Bl -tag -width ".Fn S_ISFIFO m" 276.It Fn S_ISBLK m 277Test for a block special file. 278.It Fn S_ISCHR m 279Test for a character special file. 280.It Fn S_ISDIR m 281Test for a directory. 282.It Fn S_ISFIFO m 283Test for a pipe or FIFO special file. 284.It Fn S_ISLNK m 285Test for a symbolic link. 286.It Fn S_ISREG m 287Test for a regular file. 288.It Fn S_ISSOCK m 289Test for a socket. 290.It Fn S_ISWHT m 291Test for a whiteout. 292.El 293.Pp 294The macros evaluate to a non-zero value if the test is true 295or to the value 0 if the test is false. 296.Sh RETURN VALUES 297.Rv -std 298.Sh COMPATIBILITY 299Previous versions of the system used different types for the 300.Va st_dev , 301.Va st_uid , 302.Va st_gid , 303.Va st_rdev , 304.Va st_size , 305.Va st_blksize 306and 307.Va st_blocks 308fields. 309.Sh ERRORS 310The 311.Fn stat 312and 313.Fn lstat 314system calls will fail if: 315.Bl -tag -width Er 316.It Bq Er EACCES 317Search permission is denied for a component of the path prefix. 318.It Bq Er EFAULT 319The 320.Fa sb 321or 322.Fa path 323argument 324points to an invalid address. 325.It Bq Er EIO 326An I/O error occurred while reading from or writing to the file system. 327.It Bq Er ELOOP 328Too many symbolic links were encountered in translating the pathname. 329.It Bq Er ENAMETOOLONG 330A component of a pathname exceeded 255 characters, 331or an entire path name exceeded 1023 characters. 332.It Bq Er ENOENT 333The named file does not exist. 334.It Bq Er ENOTDIR 335A component of the path prefix is not a directory. 336.It Bq Er EOVERFLOW 337The file size in bytes cannot be 338represented correctly in the structure pointed to by 339.Fa sb . 340.El 341.Pp 342.Bl -tag -width Er 343The 344.Fn fstat 345system call will fail if: 346.It Bq Er EBADF 347The 348.Fa fd 349argument 350is not a valid open file descriptor. 351.It Bq Er EFAULT 352The 353.Fa sb 354argument 355points to an invalid address. 356.It Bq Er EIO 357An I/O error occurred while reading from or writing to the file system. 358.It Bq Er EOVERFLOW 359The file size in bytes cannot be 360represented correctly in the structure pointed to by 361.Fa sb . 362.El 363.Pp 364In addition to the errors returned by the 365.Fn lstat , 366the 367.Fn fstatat 368may fail if: 369.Bl -tag -width Er 370.It Bq Er EBADF 371The 372.Fa path 373argument does not specify an absolute path and the 374.Fa fd 375argument is neither 376.Dv AT_FDCWD 377nor a valid file descriptor open for searching. 378.It Bq Er EINVAL 379The value of the 380.Fa flag 381argument is not valid. 382.It Bq Er ENOTDIR 383The 384.Fa path 385argument is not an absolute path and 386.Fa fd 387is neither 388.Dv AT_FDCWD 389nor a file descriptor associated with a directory. 390.El 391.Sh SEE ALSO 392.Xr access 2 , 393.Xr chmod 2 , 394.Xr chown 2 , 395.Xr fhstat 2 , 396.Xr statfs 2 , 397.Xr utimes 2 , 398.Xr symlink 7 , 399.Xr sticky 8 400.Sh STANDARDS 401The 402.Fn stat 403and 404.Fn fstat 405system calls are expected to conform to 406.St -p1003.1-90 . 407The 408.Fn fchownat 409system call follows The Open Group Extended API Set 2 specification. 410.Sh HISTORY 411The 412.Fn stat 413and 414.Fn fstat 415system calls appeared in 416.At v7 . 417The 418.Fn lstat 419system call appeared in 420.Bx 4.2 . 421The 422.Fn fstatat 423system call appeared in 424.Fx 8.0 . 425.Sh BUGS 426Applying 427.Fn fstat 428to a socket (and thus to a pipe) 429returns a zeroed buffer, 430except for the blocksize field, 431and a unique device and inode number. 432