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