1.\" $NetBSD: stat.2,v 1.19 2000/06/29 03:26:57 sommerfeld 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. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)stat.2 8.4 (Berkeley) 5/1/95 35.\" 36.Dd May 1, 1995 37.Dt STAT 2 38.Os 39.Sh NAME 40.Nm stat , 41.Nm lstat , 42.Nm fstat 43.Nd get file status 44.Sh LIBRARY 45.Lb libc 46.Sh SYNOPSIS 47.Fd #include <sys/types.h> 48.Fd #include <sys/stat.h> 49.Ft int 50.Fn stat "const char *path" "struct stat *sb" 51.Ft int 52.Fn lstat "const char *path" "struct stat *sb" 53.Ft int 54.Fn fstat "int fd" "struct stat *sb" 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 64.Fn lstat 65is 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 77function 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.Fn stat 85structure 86as defined by 87.Aq Pa sys/stat.h 88(shown below) 89and into which information is placed concerning the file. 90.Bd -literal 91struct stat { 92 dev_t st_dev; /* inode's device */ 93 ino_t st_ino; /* inode's number */ 94 mode_t st_mode; /* inode's mode (protection and type) */ 95 nlink_t st_nlink; /* number of hard links to the file */ 96 uid_t st_uid; /* user-id of owner */ 97 gid_t st_gid; /* group-id of owner */ 98 dev_t st_rdev; /* device type, for special file inode */ 99 struct timespec st_atimespec; /* time of last access */ 100 struct timespec st_mtimespec; /* time of last data modification */ 101 struct timespec st_ctimespec; /* time of last file status change */ 102 off_t st_size; /* file size, in bytes */ 103 int64_t st_blocks; /* blocks allocated for file */ 104 u_int32_t st_blksize; /* optimal file sys I/O ops blocksize */ 105 u_int32_t st_flags; /* user defined flags for file */ 106 u_int32_t st_gen; /* file generation number */ 107}; 108.Ed 109.Pp 110The time-related fields of 111.Fa struct stat 112are as follows: 113.Bl -tag -width XXXst_mtime 114.It st_atime 115Time when file data was last accessed. 116Changed by the 117.Xr mknod 2 , 118.Xr utimes 2 119and 120.Xr read 2 121system calls. 122.It st_mtime 123Time when file data was last modified. 124Changed by the 125.Xr mknod 2 , 126.Xr utimes 2 127and 128.Xr write 2 129system calls. 130.It st_ctime 131Time when file status was last changed (inode data modification). 132Changed by the 133.Xr chflags 2 , 134.Xr chmod 2 , 135.Xr chown 2 , 136.Xr link 2 , 137.Xr mknod 2 , 138.Xr rename 2 , 139.Xr unlink 2 , 140.Xr utimes 2 141and 142.Xr write 2 143system calls. 144.El 145.Pp 146The size-related fields of the 147.Fa struct stat 148are as follows: 149.Bl -tag -width XXXst_blksize 150.It st_blksize 151The optimal I/O block size for the file. 152.It st_blocks 153The actual number of blocks allocated for the file in 512-byte units. 154As short symbolic links are stored in the inode, this number may 155be zero. 156.El 157.Pp 158The status information word 159.Fa st_mode 160has the following bits: 161.Bd -literal 162#define S_IFMT 0170000 /* type of file */ 163#define S_IFIFO 0010000 /* named pipe (fifo) */ 164#define S_IFCHR 0020000 /* character special */ 165#define S_IFDIR 0040000 /* directory */ 166#define S_IFBLK 0060000 /* block special */ 167#define S_IFREG 0100000 /* regular */ 168#define S_IFLNK 0120000 /* symbolic link */ 169#define S_IFSOCK 0140000 /* socket */ 170#define S_IFWHT 0160000 /* whiteout */ 171#define S_ISUID 0004000 /* set user id on execution */ 172#define S_ISGID 0002000 /* set group id on execution */ 173#define S_ISVTX 0001000 /* save swapped text even after use */ 174#define S_IRUSR 0000400 /* read permission, owner */ 175#define S_IWUSR 0000200 /* write permission, owner */ 176#define S_IXUSR 0000100 /* execute/search permission, owner */ 177#define S_IRGRP 0000040 /* read permission, group */ 178#define S_IWGRP 0000020 /* write permission, group */ 179#define S_IXGRP 0000010 /* execute/search permission, group */ 180#define S_IROTH 0000004 /* read permission, other */ 181#define S_IWOTH 0000002 /* write permission, other */ 182#define S_IXOTH 0000001 /* execute/search permission, other */ 183.Ed 184.Pp 185For a list of access modes, see 186.Aq Pa sys/stat.h , 187.Xr access 2 188and 189.Xr chmod 2 . 190.Pp 191The status information word 192.Fa st_flags 193has the following bits: 194.Bd -literal 195#define UF_NODUMP 0x00000001 /* do not dump file */ 196#define UF_IMMUTABLE 0x00000002 /* file may not be changed */ 197#define UF_APPEND 0x00000004 /* writes to file may only append */ 198#define UF_OPAQUE 0x00000008 /* directory is opaque wrt. union */ 199#define SF_ARCHIVED 0x00010000 /* file is archived */ 200#define SF_IMMUTABLE 0x00020000 /* file may not be changed */ 201#define SF_APPEND 0x00040000 /* writes to file may only append */ 202.Ed 203.Pp 204For a description of the flags, see 205.Xr chflags 2 . 206.Sh RETURN VALUES 207Upon successful completion a value of 0 is returned. 208Otherwise, a value of -1 is returned and 209.Va errno 210is set to indicate the error. 211.Sh COMPATIBILITY 212Previous versions of the system used different types for the 213.Li st_dev , 214.Li st_uid , 215.Li st_gid , 216.Li st_rdev , 217.Li st_size , 218.Li st_blksize 219and 220.Li st_blocks 221fields. 222.Sh ERRORS 223.Fn stat 224and 225.Fn lstat 226will fail if: 227.Bl -tag -width Er 228.It Bq Er ENOTDIR 229A component of the path prefix is not a directory. 230.It Bq Er ENAMETOOLONG 231A component of a pathname exceeded 232.Dv {NAME_MAX} 233characters, or an entire path name exceeded 234.Dv {PATH_MAX} 235characters. 236.It Bq Er ENOENT 237The named file does not exist. 238.It Bq Er EACCES 239Search permission is denied for a component of the path prefix. 240.It Bq Er ELOOP 241Too many symbolic links were encountered in translating the pathname. 242.It Bq Er EFAULT 243.Fa sb 244or 245.Em name 246points to an invalid address. 247.It Bq Er EIO 248An I/O error occurred while reading from or writing to the file system. 249.El 250.Pp 251.Bl -tag -width Er 252.Fn fstat 253will fail if: 254.It Bq Er EBADF 255.Fa fd 256is not a valid open file descriptor. 257.It Bq Er EFAULT 258.Fa sb 259points to an invalid address. 260.It Bq Er EIO 261An I/O error occurred while reading from or writing to the file system. 262.El 263.Sh SEE ALSO 264.Xr chflags 2 , 265.Xr chmod 2 , 266.Xr chown 2 , 267.Xr utimes 2 , 268.Xr symlink 7 269.Sh BUGS 270Applying 271.Fn fstat 272to a socket (and thus to a pipe) 273returns a zero'd buffer, 274except for the blocksize field, 275and a unique device and inode number. 276.Sh STANDARDS 277The 278.Fn stat 279and 280.Fn fstat 281functions conform to 282.St -p1003.1-90 . 283.Sh HISTORY 284A 285.Fn lstat 286function call appeared in 287.Bx 4.2 . 288