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 October 12, 2006 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 mask */ 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_IRWXU 0000700 /* RWX mask for owner */ 211#define S_IRUSR 0000400 /* read permission, owner */ 212#define S_IWUSR 0000200 /* write permission, owner */ 213#define S_IXUSR 0000100 /* execute/search permission, owner */ 214#define S_IRWXG 0000070 /* RWX mask for group */ 215#define S_IRGRP 0000040 /* read permission, group */ 216#define S_IWGRP 0000020 /* write permission, group */ 217#define S_IXGRP 0000010 /* execute/search permission, group */ 218#define S_IRWXO 0000007 /* RWX mask for other */ 219#define S_IROTH 0000004 /* read permission, other */ 220#define S_IWOTH 0000002 /* write permission, other */ 221#define S_IXOTH 0000001 /* execute/search permission, other */ 222.Ed 223.Pp 224For a list of access modes, see 225.In sys/stat.h , 226.Xr access 2 227and 228.Xr chmod 2 . 229The following macros are available to test whether a 230.Va st_mode 231value passed in the 232.Fa m 233argument corresponds to a file of the specified type: 234.Bl -tag -width ".Fn S_ISFIFO m" 235.It Fn S_ISBLK m 236Test for a block special file. 237.It Fn S_ISCHR m 238Test for a character special file. 239.It Fn S_ISDIR m 240Test for a directory. 241.It Fn S_ISFIFO m 242Test for a pipe or FIFO special file. 243.It Fn S_ISLNK m 244Test for a symbolic link. 245.It Fn S_ISREG m 246Test for a regular file. 247.It Fn S_ISSOCK m 248Test for a socket. 249.It Fn S_ISWHT m 250Test for a whiteout. 251.El 252.Pp 253The macros evaluate to a non-zero value if the test is true 254or to the value 0 if the test is false. 255.Sh RETURN VALUES 256.Rv -std 257.Sh COMPATIBILITY 258Previous versions of the system used different types for the 259.Va st_dev , 260.Va st_uid , 261.Va st_gid , 262.Va st_rdev , 263.Va st_size , 264.Va st_blksize 265and 266.Va st_blocks 267fields. 268.Sh ERRORS 269The 270.Fn stat 271and 272.Fn lstat 273system calls will fail if: 274.Bl -tag -width Er 275.It Bq Er EACCES 276Search permission is denied for a component of the path prefix. 277.It Bq Er EFAULT 278The 279.Fa sb 280or 281.Fa path 282argument 283points to an invalid address. 284.It Bq Er EIO 285An I/O error occurred while reading from or writing to the file system. 286.It Bq Er ELOOP 287Too many symbolic links were encountered in translating the pathname. 288.It Bq Er ENAMETOOLONG 289A component of a pathname exceeded 255 characters, 290or an entire path name exceeded 1023 characters. 291.It Bq Er ENOENT 292The named file does not exist. 293.It Bq Er ENOTDIR 294A component of the path prefix is not a directory. 295.It Bq Er EOVERFLOW 296The file size in bytes cannot be 297represented correctly in the structure pointed to by 298.Fa sb . 299.El 300.Pp 301.Bl -tag -width Er 302The 303.Fn fstat 304system call will fail if: 305.It Bq Er EBADF 306The 307.Fa fd 308argument 309is not a valid open file descriptor. 310.It Bq Er EFAULT 311The 312.Fa sb 313argument 314points to an invalid address. 315.It Bq Er EIO 316An I/O error occurred while reading from or writing to the file system. 317.It Bq Er EOVERFLOW 318The file size in bytes cannot be 319represented correctly in the structure pointed to by 320.Fa sb . 321.El 322.Sh SEE ALSO 323.Xr access 2 , 324.Xr chmod 2 , 325.Xr chown 2 , 326.Xr fhstat 2 , 327.Xr statfs 2 , 328.Xr utimes 2 , 329.Xr symlink 7 , 330.Xr sticky 8 331.Sh STANDARDS 332The 333.Fn stat 334and 335.Fn fstat 336system calls are expected to conform to 337.St -p1003.1-90 . 338.Sh HISTORY 339The 340.Fn stat 341and 342.Fn fstat 343system calls appeared in 344.At v7 . 345The 346.Fn lstat 347system call appeared in 348.Bx 4.2 . 349.Sh BUGS 350Applying 351.Fn fstat 352to a socket (and thus to a pipe) 353returns a zeroed buffer, 354except for the blocksize field, 355and a unique device and inode number. 356