1.\" $NetBSD: stat.2,v 1.38 2009/05/13 12:51:13 wiz 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 June 9, 2007 33.Dt STAT 2 34.Os 35.Sh NAME 36.Nm stat , 37.Nm lstat , 38.Nm fstat 39.Nd get file status 40.Sh LIBRARY 41.Lb libc 42.Sh SYNOPSIS 43.In sys/stat.h 44.Ft int 45.Fn stat "const char *path" "struct stat *sb" 46.Ft int 47.Fn lstat "const char *path" "struct stat *sb" 48.Ft int 49.Fn fstat "int fd" "struct stat *sb" 50.Sh DESCRIPTION 51The 52.Fn stat 53function obtains information about the file pointed to by 54.Fa path . 55Read, write or execute 56permission of the named file is not required, but all directories 57listed in the path name leading to the file must be searchable. 58.Pp 59.Fn lstat 60is like 61.Fn stat 62except in the case where the named file is a symbolic link, 63in which case 64.Fn lstat 65returns information about the link, 66while 67.Fn stat 68returns information about the file the link references. 69.Pp 70The 71.Fn fstat 72function obtains the same information about an open file 73known by the file descriptor 74.Fa fd . 75.Pp 76The 77.Fa sb 78argument is a pointer to a 79.Fa stat 80structure 81as defined by 82.Aq Pa sys/stat.h 83(shown below) 84and into which information is placed concerning the file. 85.Bd -literal 86struct stat { 87 dev_t st_dev; /* device containing the file */ 88 ino_t st_ino; /* file's serial number */ 89 mode_t st_mode; /* file's mode (protection and type) */ 90 nlink_t st_nlink; /* number of hard links to the file */ 91 uid_t st_uid; /* user-id of owner */ 92 gid_t st_gid; /* group-id of owner */ 93 dev_t st_rdev; /* device type, for device special file */ 94#if defined(_NETBSD_SOURCE) 95 struct timespec st_atimespec; /* time of last access */ 96 struct timespec st_mtimespec; /* time of last data modification */ 97 struct timespec st_ctimespec; /* time of last file status change */ 98#else 99 time_t st_atime; /* time of last access */ 100 long st_atimensec; /* nsec of last access */ 101 time_t st_mtime; /* time of last data modification */ 102 long st_mtimensec; /* nsec of last data modification */ 103 time_t st_ctime; /* time of last file status change */ 104 long st_ctimensec; /* nsec of last file status change */ 105#endif 106 off_t st_size; /* file size, in bytes */ 107 blkcnt_t st_blocks; /* blocks allocated for file */ 108 blksize_t st_blksize; /* optimal file sys I/O ops blocksize */ 109 uint32_t st_flags; /* user defined flags for file */ 110 uint32_t st_gen; /* file generation number */ 111#if defined(_NETBSD_SOURCE) 112 struct timespec st_birthtimespec; /* time of inode creation */ 113#else 114 time_t st_birthtime; /* time of inode creation */ 115 long st_birthtimensec; /* nsec of inode creation */ 116#endif 117}; 118.Ed 119.Pp 120The time-related fields of 121.Fa struct stat 122are as follows: 123.Bl -tag -width XXXst_birthtime 124.It st_atime 125Time when file data was last accessed. 126Changed by the 127.Xr mknod 2 , 128.Xr utimes 2 129and 130.Xr read 2 131system calls. 132.It st_mtime 133Time when file data was last modified. 134Changed by the 135.Xr mknod 2 , 136.Xr utimes 2 137and 138.Xr write 2 139system calls. 140.It st_ctime 141Time when file status was last changed (file metadata modification). 142Changed by the 143.Xr chflags 2 , 144.Xr chmod 2 , 145.Xr chown 2 , 146.Xr link 2 , 147.Xr mknod 2 , 148.Xr rename 2 , 149.Xr unlink 2 , 150.Xr utimes 2 151and 152.Xr write 2 153system calls. 154.It st_birthtime 155Time when the inode was created. 156.El 157.Pp 158If 159.Dv _NETBSD_SOURCE 160is defined, the time-related fields are defined as: 161.Bd -literal 162#if defined(_NETBSD_SOURCE) 163#define st_atime st_atimespec.tv_sec 164#define st_atimensec st_atimespec.tv_nsec 165#define st_mtime st_mtimespec.tv_sec 166#define st_mtimensec st_mtimespec.tv_nsec 167#define st_ctime st_ctimespec.tv_sec 168#define st_ctimensec st_ctimespec.tv_nsec 169#define st_birthtime st_birthtimespec.tv_sec 170#define st_birthtimensec st_birthtimespec.tv_nsec 171#endif 172.Ed 173.Pp 174The size-related fields of the 175.Fa struct stat 176are as follows: 177.Bl -tag -width XXXst_blksize 178.It st_size 179The size of the file in bytes. 180A directory will be a multiple of the size of the 181.Xr dirent 5 182structure. 183Some filesystems (notably ZFS) return the number of enties in the directory 184instead of the size in bytes. 185.It st_blksize 186The optimal I/O block size for the file. 187.It st_blocks 188The actual number of blocks allocated for the file in 512-byte units. 189As short symbolic links are stored in the inode, this number may 190be zero. 191.El 192.Pp 193The status information word 194.Fa st_mode 195has the following bits: 196.Bd -literal 197#define S_IFMT 0170000 /* type of file */ 198#define S_IFIFO 0010000 /* named pipe (fifo) */ 199#define S_IFCHR 0020000 /* character special */ 200#define S_IFDIR 0040000 /* directory */ 201#define S_IFBLK 0060000 /* block special */ 202#define S_IFREG 0100000 /* regular */ 203#define S_IFLNK 0120000 /* symbolic link */ 204#define S_IFSOCK 0140000 /* socket */ 205#define S_IFWHT 0160000 /* whiteout */ 206#define S_ISUID 0004000 /* set user id on execution */ 207#define S_ISGID 0002000 /* set group id on execution */ 208#define S_ISVTX 0001000 /* save swapped text even after use */ 209#define S_IRUSR 0000400 /* read permission, owner */ 210#define S_IWUSR 0000200 /* write permission, owner */ 211#define S_IXUSR 0000100 /* execute/search permission, owner */ 212#define S_IRGRP 0000040 /* read permission, group */ 213#define S_IWGRP 0000020 /* write permission, group */ 214#define S_IXGRP 0000010 /* execute/search permission, group */ 215#define S_IROTH 0000004 /* read permission, other */ 216#define S_IWOTH 0000002 /* write permission, other */ 217#define S_IXOTH 0000001 /* execute/search permission, other */ 218.Ed 219.Pp 220For a list of access modes, see 221.Aq Pa sys/stat.h , 222.Xr access 2 223and 224.Xr chmod 2 . 225.Pp 226The status information word 227.Fa st_flags 228has the following bits: 229.Bd -literal 230#define UF_NODUMP 0x00000001 /* do not dump file */ 231#define UF_IMMUTABLE 0x00000002 /* file may not be changed */ 232#define UF_APPEND 0x00000004 /* writes to file may only append */ 233#define UF_OPAQUE 0x00000008 /* directory is opaque wrt. union */ 234#define SF_ARCHIVED 0x00010000 /* file is archived */ 235#define SF_IMMUTABLE 0x00020000 /* file may not be changed */ 236#define SF_APPEND 0x00040000 /* writes to file may only append */ 237.Ed 238.Pp 239For a description of the flags, see 240.Xr chflags 2 . 241.Sh RETURN VALUES 242Upon successful completion a value of 0 is returned. 243Otherwise, a value of \-1 is returned and 244.Va errno 245is set to indicate the error. 246.Sh COMPATIBILITY 247Previous versions of the system used different types for the 248.Li st_dev , 249.Li st_uid , 250.Li st_gid , 251.Li st_rdev , 252.Li st_size , 253.Li st_blksize 254and 255.Li st_blocks 256fields. 257.Sh ERRORS 258.Fn stat 259and 260.Fn lstat 261will fail if: 262.Bl -tag -width Er 263.It Bq Er EACCES 264Search permission is denied for a component of the path prefix. 265.It Bq Er EBADF 266A badly formed v-node was encountered. 267This can happen if a file system information node is incorrect. 268.It Bq Er EFAULT 269.Fa sb 270or 271.Em name 272points to an invalid address. 273.It Bq Er EIO 274An I/O error occurred while reading from or writing to the file system. 275.It Bq Er ELOOP 276Too many symbolic links were encountered in translating the pathname. 277.It Bq Er ENAMETOOLONG 278A component of a pathname exceeded 279.Dv { NAME_MAX } 280characters, or an entire path name exceeded 281.Dv { PATH_MAX } 282characters. 283.It Bq Er ENOENT 284The named file does not exist. 285.It Bq Er ENOTDIR 286A component of the path prefix is not a directory. 287.It Bq Er ENXIO 288The named file is a character special or block 289special file, and the device associated with this special file 290does not exist. 291.El 292.Pp 293.Fn fstat 294will fail if: 295.Bl -tag -width Er 296.It Bq Er EBADF 297.Fa fd 298is not a valid open file descriptor. 299.It Bq Er EFAULT 300.Fa sb 301points to an invalid address. 302.It Bq Er EIO 303An I/O error occurred while reading from or writing to the file system. 304.El 305.Sh SEE ALSO 306.Xr chflags 2 , 307.Xr chmod 2 , 308.Xr chown 2 , 309.Xr utimes 2 , 310.Xr dir 5 , 311.Xr symlink 7 312.Sh STANDARDS 313The 314.Fn stat 315and 316.Fn fstat 317functions conform to 318.St -p1003.1-90 . 319.Sh HISTORY 320A 321.Fn lstat 322function call appeared in 323.Bx 4.2 . 324.Sh BUGS 325Applying 326.Fn fstat 327to a socket (and thus to a pipe) 328returns a zero'd buffer, 329except for the blocksize field, 330and a unique device and file serial number. 331