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. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)stat.2 8.4 (Berkeley) 5/1/95 33.\" $FreeBSD$ 34.\" 35.Dd November 15, 2004 36.Dt STAT 2 37.Os 38.Sh NAME 39.Nm stat , 40.Nm lstat , 41.Nm fstat 42.Nd get file status 43.Sh LIBRARY 44.Lb libc 45.Sh SYNOPSIS 46.In sys/types.h 47.In sys/stat.h 48.Ft int 49.Fn stat "const char *path" "struct stat *sb" 50.Ft int 51.Fn lstat "const char *path" "struct stat *sb" 52.Ft int 53.Fn fstat "int fd" "struct stat *sb" 54.Sh DESCRIPTION 55The 56.Fn stat 57system call obtains information about the file pointed to by 58.Fa path . 59Read, write or execute 60permission of the named file is not required, but all directories 61listed in the path name leading to the file must be searchable. 62.Pp 63The 64.Fn lstat 65system call is like 66.Fn stat 67except in the case where the named file is a symbolic link, 68in which case 69.Fn lstat 70returns information about the link, 71while 72.Fn stat 73returns information about the file the link references. 74.Pp 75The 76.Fn fstat 77system call obtains the same information about an open file 78known by the file descriptor 79.Fa fd . 80.Pp 81The 82.Fa sb 83argument is a pointer to a 84.Vt stat 85structure 86as defined by 87.In sys/stat.h 88and into which information is placed concerning the file. 89.Pp 90The fields of 91.Vt "struct stat" 92related to the file system are as follows: 93.Bl -tag -width ".Va st_nlink" 94.It Va st_dev 95The numeric ID of the device containing the file. 96.It Va st_ino 97The file's inode number. 98.It Va st_nlink 99The number of hard links to the file. 100.El 101.Pp 102The 103.Va st_dev 104and 105.Va st_ino 106fields together identify the file uniquely within the system. 107.Pp 108The time-related fields of 109.Vt "struct stat" 110are as follows: 111.Bl -tag -width ".Va st_birthtime" 112.It Va st_atime 113Time when file data last accessed. 114Changed by the 115.Xr mknod 2 , 116.Xr utimes 2 , 117.Xr read 2 118and 119.Xr readv 2 120system calls. 121.It Va st_mtime 122Time when file data last modified. 123Changed by the 124.Xr mkdir 2 , 125.Xr mkfifo 2 , 126.Xr mknod 2 , 127.Xr utimes 2 , 128.Xr write 2 129and 130.Xr writev 2 131system calls. 132.It Va st_ctime 133Time when file status was last changed (inode data modification). 134Changed by the 135.Xr chflags 2 , 136.Xr chmod 2 , 137.Xr chown 2 , 138.Xr creat 2 , 139.Xr link 2 , 140.Xr mkdir 2 , 141.Xr mkfifo 2 , 142.Xr mknod 2 , 143.Xr rename 2 , 144.Xr rmdir 2 , 145.Xr symlink 2 , 146.Xr truncate 2 , 147.Xr unlink 2 , 148.Xr utimes 2 , 149.Xr write 2 150and 151.Xr writev 2 152system calls. 153.It Va st_birthtime 154Time when the inode was created. 155.El 156.Pp 157If 158.Dv _POSIX_SOURCE 159is not defined, the time-related fields are defined as: 160.Bd -literal 161#ifndef _POSIX_SOURCE 162#define st_atime st_atimespec.tv_sec 163#define st_mtime st_mtimespec.tv_sec 164#define st_ctime st_ctimespec.tv_sec 165#endif 166.Ed 167.Pp 168The size-related fields of the 169.Vt "struct stat" 170are as follows: 171.Bl -tag -width ".Va st_blksize" 172.It Va st_size 173The file size in bytes. 174.It Va st_blksize 175The optimal I/O block size for the file. 176.It Va st_blocks 177The actual number of blocks allocated for the file in 512-byte units. 178As short symbolic links are stored in the inode, this number may 179be zero. 180.El 181.Pp 182The access-related fields of 183.Vt "struct stat" 184are as follows: 185.Bl -tag -width ".Va st_mode" 186.It Va st_uid 187The user ID of the file's owner. 188.It Va st_gid 189The group ID of the file. 190.It Va st_mode 191Status of the file (see below). 192.El 193.Pp 194The status information word 195.Fa st_mode 196has the following bits: 197.Bd -literal 198#define S_IFMT 0170000 /* type of file */ 199#define S_IFIFO 0010000 /* named pipe (fifo) */ 200#define S_IFCHR 0020000 /* character special */ 201#define S_IFDIR 0040000 /* directory */ 202#define S_IFBLK 0060000 /* block special */ 203#define S_IFREG 0100000 /* regular */ 204#define S_IFLNK 0120000 /* symbolic link */ 205#define S_IFSOCK 0140000 /* socket */ 206#define S_IFWHT 0160000 /* whiteout */ 207#define S_ISUID 0004000 /* set user id on execution */ 208#define S_ISGID 0002000 /* set group id on execution */ 209#define S_ISVTX 0001000 /* save swapped text even after use */ 210#define S_IRUSR 0000400 /* read permission, owner */ 211#define S_IWUSR 0000200 /* write permission, owner */ 212#define S_IXUSR 0000100 /* execute/search permission, owner */ 213.Ed 214.Pp 215For a list of access modes, see 216.In sys/stat.h , 217.Xr access 2 218and 219.Xr chmod 2 . 220The following macros are available to test whether a 221.Va st_mode 222value passed in the 223.Fa m 224argument corresponds to a file of the specified type: 225.Bl -tag -width ".Fn S_ISFIFO m" 226.It Fn S_ISBLK m 227Test for a block special file. 228.It Fn S_ISCHR m 229Test for a character special file. 230.It Fn S_ISDIR m 231Test for a directory. 232.It Fn S_ISFIFO m 233Test for a pipe or FIFO special file. 234.It Fn S_ISLNK m 235Test for a symbolic link. 236.It Fn S_ISREG m 237Test for a regular file. 238.It Fn S_ISSOCK m 239Test for a socket. 240.It Fn S_ISWHT m 241Test for a whiteout. 242.El 243.Pp 244The macros evaluate to a non-zero value if the test is true 245or to the value 0 if the test is false. 246.Sh RETURN VALUES 247.Rv -std 248.Sh COMPATIBILITY 249Previous versions of the system used different types for the 250.Va st_dev , 251.Va st_uid , 252.Va st_gid , 253.Va st_rdev , 254.Va st_size , 255.Va st_blksize 256and 257.Va st_blocks 258fields. 259.Sh ERRORS 260The 261.Fn stat 262and 263.Fn lstat 264system calls will fail if: 265.Bl -tag -width Er 266.It Bq Er EACCES 267Search permission is denied for a component of the path prefix. 268.It Bq Er EFAULT 269The 270.Fa sb 271or 272.Fa path 273argument 274points to an invalid address. 275.It Bq Er EIO 276An I/O error occurred while reading from or writing to the file system. 277.It Bq Er ELOOP 278Too many symbolic links were encountered in translating the pathname. 279.It Bq Er ENAMETOOLONG 280A component of a pathname exceeded 255 characters, 281or an entire path name exceeded 1023 characters. 282.It Bq Er ENOENT 283The named file does not exist. 284.It Bq Er ENOTDIR 285A component of the path prefix is not a directory. 286.It Bq Er EOVERFLOW 287The file size in bytes cannot be 288represented correctly in the structure pointed to by 289.Fa sb . 290.El 291.Pp 292.Bl -tag -width Er 293The 294.Fn fstat 295system call will fail if: 296.It Bq Er EBADF 297The 298.Fa fd 299argument 300is not a valid open file descriptor. 301.It Bq Er EFAULT 302The 303.Fa sb 304argument 305points to an invalid address. 306.It Bq Er EIO 307An I/O error occurred while reading from or writing to the file system. 308.It Bq Er EOVERFLOW 309The file size in bytes cannot be 310represented correctly in the structure pointed to by 311.Fa sb . 312.El 313.Sh SEE ALSO 314.Xr access 2 , 315.Xr chmod 2 , 316.Xr chown 2 , 317.Xr fhstat 2 , 318.Xr utimes 2 , 319.Xr symlink 7 , 320.Xr sticky 8 321.Sh STANDARDS 322The 323.Fn stat 324and 325.Fn fstat 326system calls are expected to conform to 327.St -p1003.1-90 . 328.Sh HISTORY 329The 330.Fn stat 331and 332.Fn fstat 333system calls appeared in 334.At v7 . 335The 336.Fn lstat 337system call appeared in 338.Bx 4.2 . 339.Sh BUGS 340Applying 341.Fn fstat 342to a socket (and thus to a pipe) 343returns a zeroed buffer, 344except for the blocksize field, 345and a unique device and inode number. 346