1.\" $NetBSD: stat.2,v 1.56 2013/10/15 11:43:21 njoly 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.4 (Berkeley) 5/1/95 31.\" 32.Dd July 28, 2013 33.Dt STAT 2 34.Os 35.Sh NAME 36.Nm stat , 37.Nm lstat , 38.Nm fstat , 39.Nm fstatat , 40.Nd get file status 41.Sh LIBRARY 42.Lb libc 43.Sh SYNOPSIS 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.In sys/stat.h 52.In fcntl.h 53.Ft int 54.Fn fstatat "int fd" "const char *path" "struct stat *sb" "int flag" 55.Sh DESCRIPTION 56The 57.Fn stat 58function obtains information about the file pointed to by 59.Fa path . 60Read, write or execute 61permission of the named file is not required, but all directories 62listed in the path name leading to the file must be searchable. 63.Pp 64The function 65.Fn lstat 66is like 67.Fn stat 68except in the case where the named file is a symbolic link, 69in which case 70.Fn lstat 71returns information about the link, 72while 73.Fn stat 74returns information about the file the link references. 75The 76.Fn fstat 77function obtains the same information about an open file 78known by the file descriptor 79.Fa fd . 80.Pp 81.Fn fstatat 82works the same way as 83.Fn stat 84(or 85.Fn lstat 86if 87.Dv AT_SYMLINK_NOFOLLOW 88is set in 89.Fa flag ) 90except if 91.Fa path 92is relative. 93In that case, it is looked up from a directory whose file 94descriptor was passed as 95.Fa fd . 96Search permission is required on this directory. 97.\" (These alternatives await a decision about the semantics of O_SEARCH) 98.\" Search permission is required on this directory 99.\" except if 100.\" .Fa fd 101.\" was opened with the 102.\" .Dv O_SEARCH 103.\" flag. 104.\" - or - 105.\" This file descriptor must have been opened with the 106.\" .Dv O_SEARCH 107.\" flag. 108.Fa fd 109can be set to 110.Dv AT_FDCWD 111in order to specify the current directory. 112.Pp 113The 114.Fa sb 115argument is a pointer to a 116.Fa stat 117structure 118as defined by 119.In sys/stat.h 120and into which information is placed concerning the file. 121.Ss The Standard Structure 122The following standards-compliant fields are defined in the structure: 123.Bl -column -offset indent \ 124"nlink_t " "st_nlink " "Description" 125.It Sy Type Ta Sy Entry Ta Sy Description 126.It Vt dev_t Ta st_dev Ta device ID containing the file 127.It Vt ino_t Ta st_ino Ta serial number of the file 128.It Vt mode_t Ta st_mode Ta mode of the file 129.It Vt nlink_t Ta st_nlink Ta number of hard links to the file 130.It Vt uid_t Ta st_uid Ta user ID of the owner 131.It Vt gid_t Ta st_gid Ta group ID of the owner 132.It Vt dev_t Ta st_rdev Ta device type (character or block special) 133.It Vt off_t Ta st_size Ta size of the file in bytes 134.It Vt time_t Ta st_atime Ta time of last access 135.It Vt time_t Ta st_mtime Ta time of last data modification 136.It Vt time_t Ta st_ctime Ta time of last file status change 137.It Vt blksize_t Ta st_blksize Ta preferred I/O block size (fs-specific) 138.It Vt blkcnt_t Ta st_blocks Ta blocks allocated for the file 139.El 140.Pp 141These are specified in the 142.St -p1003.1-2004 143standard. 144The 145.Va st_ino 146and 147.Va st_dev 148fields taken together uniquely identify the file within the system. 149Most of the types are defined in 150.Xr types 3 . 151.Pp 152The time-related fields are: 153.Bl -tag -width st_blksize -offset indent 154.It Va st_atime 155Time when file data was last accessed. 156Changed by the 157.Xr mknod 2 , 158.Xr utimes 2 , 159and 160.Xr read 2 161system calls. 162.It Va st_mtime 163Time when file data was last modified. 164Changed by the 165.Xr mknod 2 , 166.Xr utimes 2 , 167and 168.Xr write 2 169system calls. 170.It Va st_ctime 171Time when file status was last changed (file metadata modification). 172Changed by the 173.Xr chflags 2 , 174.Xr chmod 2 , 175.Xr chown 2 , 176.Xr link 2 , 177.Xr mknod 2 , 178.Xr rename 2 , 179.Xr unlink 2 , 180.Xr utimes 2 , 181and 182.Xr write 2 183system calls. 184.El 185.Pp 186The size-related fields of the 187.Fa struct stat 188are as follows: 189.Bl -tag -width st_blksize -offset indent 190.It Va st_size 191The size of the file in bytes. 192The meaning of the size reported for a directory is file system 193dependent. 194Some file systems (e.g. FFS) return the total size used for the 195directory metadata, possibly including free slots; others (notably 196ZFS) return the number of entries in the directory. 197Some may also return other things or always report zero. 198.It Va st_blksize 199The optimal I/O block size for the file. 200.It Va st_blocks 201The actual number of blocks allocated for the file in 512-byte units. 202As short symbolic links are stored in the inode, this number may 203be zero. 204.El 205.Pp 206The status information word 207.Fa st_mode 208contains bits that define the access mode (see 209.Xr chmod 2 ) 210and the type (see 211.Xr dirent 3 ) 212of the file. 213The following macros can be used to test 214whether a file is of the specified type. 215The value 216.Fa m 217supplied to the macros is the value of 218.Va st_mode . 219.Bl -tag -width "S_ISSOCK(m)" -offset indent 220.It Fn S_ISBLK "m" 221Test for a block special file. 222.It Fn S_ISCHR "m" 223Test for a character special file. 224.It Fn S_ISDIR "m" 225Test for a directory. 226.It Fn S_ISFIFO "m" 227Test for a pipe or FIFO special file. 228.It Fn S_ISREG "m" 229Test for a regular file. 230.It Fn S_ISLNK "m" 231Test for a symbolic link. 232.It Fn S_ISSOCK "m" 233Test for a socket. 234.El 235.Pp 236The macros evaluate to a non-zero value if the test 237is true or to the value 0 if the test is false. 238.Ss NetBSD Extensions 239The following additional 240.Nx 241specific fields are present: 242.Bl -column -offset indent \ 243"uint32_t" "st_birthtimensec" "Description" 244.It Sy Type Ta Sy Entry Ta Sy Description 245.It Vt long Ta st_atimensec Ta last access (nanoseconds) 246.It Vt long Ta st_mtimensec Ta last modification (nanoseconds) 247.It Vt long Ta st_ctimensec Ta last status change (nanoseconds) 248.It Vt time_t Ta st_birthtime Ta time of inode creation 249.It Vt long Ta st_birthtimensec Ta inode creation (nanoseconds) 250.It Vt uint32_t Ta st_flags Ta user defined flags for the file 251.It Vt uint32_t Ta st_gen Ta file generation number 252.\" 253.\" XXX: What is this? 254.\" 255.It Vt uint32_t Ta st_spare[2] Ta implementation detail 256.El 257.Pp 258However, if 259_NETBSD_SOURCE 260is furthermore defined, instead of the above, 261the following are present in the structure: 262.Bl -column -offset indent \ 263"struct timespec " "st_birthtimensec" "Description" 264.It Sy Type Ta Sy Entry Ta Sy Description 265.It Vt struct timespec Ta st_atimespec Ta time of last access 266.It Vt struct timespec Ta st_mtimespec Ta time of last modification 267.It Vt struct timespec Ta st_birthtimespec Ta time of creation 268.It Vt uint32_t Ta st_flags Ta user defined flags 269.It Vt uint32_t Ta st_gen Ta file generation number 270.\" 271.\" XXX: What is this? 272.\" 273.It Vt uint32_t Ta st_spare[2] Ta implementation detail 274.El 275.Pp 276In this case the following macros are provided for convenience: 277.Bd -literal -offset indent 278#if defined(_NETBSD_SOURCE) 279 #define st_atime st_atimespec.tv_sec 280 #define st_atimensec st_atimespec.tv_nsec 281 #define st_mtime st_mtimespec.tv_sec 282 #define st_mtimensec st_mtimespec.tv_nsec 283 #define st_ctime st_ctimespec.tv_sec 284 #define st_ctimensec st_ctimespec.tv_nsec 285 #define st_birthtime st_birthtimespec.tv_sec 286 #define st_birthtimensec st_birthtimespec.tv_nsec 287#endif 288.Ed 289.Pp 290The status information word 291.Fa st_flags 292has the following bits: 293.Bl -column -offset indent \ 294"struct timespec " "st_birthtimensec" 295.It Sy Constant Ta Sy Description 296.It Dv UF_NODUMP Ta do not dump a file 297.It Dv UF_IMMUTABLE Ta file may not be changed 298.It Dv UF_APPEND Ta writes to file may only append 299.It Dv UF_OPAQUE Ta directory is opaque wrt. union 300.It Dv SF_ARCHIVED Ta file is archived 301.It Dv SF_IMMUTABLE Ta file may not be changed 302.It Dv SF_APPEND Ta writes to file may only append 303.El 304.Pp 305For a description of the flags, see 306.Xr chflags 2 . 307.Sh RETURN VALUES 308.Rv -std stat lstat fstat fstatat 309.Sh COMPATIBILITY 310Previous versions of the system used different types for the 311.Li st_dev , 312.Li st_uid , 313.Li st_gid , 314.Li st_rdev , 315.Li st_size , 316.Li st_blksize 317and 318.Li st_blocks 319fields. 320.Sh ERRORS 321.Fn stat , 322.Fn lstat 323and 324.Fn fstatat 325will fail if: 326.Bl -tag -width Er 327.It Bq Er EACCES 328Search permission is denied for a component of the path prefix. 329.It Bq Er EBADF 330A badly formed vnode was encountered. 331This can happen if a file system information node is incorrect. 332.It Bq Er EFAULT 333.Fa sb 334or 335.Fa path 336points to an invalid address. 337.It Bq Er EIO 338An I/O error occurred while reading from or writing to the file system. 339.It Bq Er ELOOP 340Too many symbolic links were encountered in translating the pathname. 341.It Bq Er ENAMETOOLONG 342A component of a pathname exceeded 343.Brq Dv NAME_MAX 344characters, or an entire path name exceeded 345.Brq Dv PATH_MAX 346characters. 347.It Bq Er ENOENT 348The named file does not exist. 349.It Bq Er ENOTDIR 350A component of the path prefix is not a directory. 351.It Bq Er ENXIO 352The named file is a character special or block 353special file, and the device associated with this special file 354does not exist. 355.El 356.Pp 357In addition, 358.Fn fstatat 359will fail if: 360.Bl -tag -width Er 361.It Bq Er EBADF 362.Fa path 363does not specify an absolute path and 364.Fa fd 365is neither 366.Dv AT_FDCWD 367nor a valid file descriptor open for reading or searching. 368.It Bq Er ENOTDIR 369.Fa path 370is not an absolute path and 371.Fa fd 372is a file descriptor associated with a non-directory file. 373.El 374.Pp 375.Fn fstat 376will fail if: 377.Bl -tag -width Er 378.It Bq Er EBADF 379.Fa fd 380is not a valid open file descriptor. 381.It Bq Er EFAULT 382.Fa sb 383points to an invalid address. 384.It Bq Er EIO 385An I/O error occurred while reading from or writing to the file system. 386.El 387.Sh SEE ALSO 388.Xr chflags 2 , 389.Xr chmod 2 , 390.Xr chown 2 , 391.Xr utimes 2 , 392.Xr dirent 3 , 393.Xr types 3 , 394.Xr symlink 7 395.Sh STANDARDS 396.Fn stat , 397.Fn lstat , 398and 399.Fn fstat 400conform to 401.St -p1003.1-2004 . 402.Fn fstatat 403conforms to 404.St -p1003.1-2008 . 405.Sh HISTORY 406A 407.Fn stat 408function call appeared in 409.At v2 . 410A 411.Fn lstat 412function call appeared in 413.Bx 4.2 . 414.Sh BUGS 415Applying 416.Fn fstat 417to a socket (and thus to a pipe) 418returns a zero'd buffer, 419except for the blocksize field, 420and a unique device and file serial number. 421