1.\" $NetBSD: stat.2,v 1.9 1996/01/17 02:15:28 jtc 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.3 (Berkeley) 4/19/94 35.\" 36.Dd April 19, 1994 37.Dt STAT 2 38.Os BSD 4 39.Sh NAME 40.Nm stat , 41.Nm lstat , 42.Nm fstat 43.Nd get file status 44.Sh SYNOPSIS 45.Fd #include <sys/types.h> 46.Fd #include <sys/stat.h> 47.Ft int 48.Fn stat "const char *path" "struct stat *sb" 49.Ft int 50.Fn lstat "const char *path" "struct stat *sb" 51.Ft int 52.Fn fstat "int fd" "struct stat *sb" 53.Sh DESCRIPTION 54The 55.Fn stat 56function obtains information about the file pointed to by 57.Fa path . 58Read, write or execute 59permission of the named file is not required, but all directories 60listed in the path name leading to the file must be searchable. 61.Pp 62.Fn Lstat 63is like 64.Fn stat 65except in the case where the named file is a symbolic link, 66in which case 67.Fn lstat 68returns information about the link, 69while 70.Fn stat 71returns information about the file the link references. 72Unlike other filesystem objects, 73symbolic links do not have an owner, group, access mode, times, etc. 74Instead, these attributes are taken from the directory that 75contains the link. 76The only attributes returned from an 77.Fn lstat 78that refer to the symbolic link itself are the file type (S_IFLNK), 79size, blocks, and link count (always 1). 80.Pp 81The 82.Fn fstat 83obtains the same information about an open file 84known by the file descriptor 85.Fa fd . 86.Pp 87The 88.Fa sb 89argument is a pointer to a 90.Fn stat 91structure 92as defined by 93.Aq Pa sys/stat.h 94(shown below) 95and into which information is placed concerning the file. 96.Bd -literal 97struct stat { 98 dev_t st_dev; /* device inode resides on */ 99 ino_t st_ino; /* inode's number */ 100 mode_t st_mode; /* inode protection mode */ 101 nlink_t st_nlink; /* number or hard links to the file */ 102 uid_t st_uid; /* user-id of owner */ 103 gid_t st_gid; /* group-id of owner */ 104 dev_t st_rdev; /* device type, for special file inode */ 105 struct timespec st_atimespec; /* time of last access */ 106 struct timespec st_mtimespec; /* time of last data modification */ 107 struct timespec st_ctimespec; /* time of last file status change */ 108 off_t st_size; /* file size, in bytes */ 109 quad_t st_blocks; /* blocks allocated for file */ 110 u_long st_blksize;/* optimal file sys I/O ops blocksize */ 111 u_long st_flags; /* user defined flags for file */ 112 u_long st_gen; /* file generation number */ 113}; 114.Ed 115.Pp 116The time-related fields of 117.Fa struct stat 118are as follows: 119.Bl -tag -width XXXst_mtime 120.It st_atime 121Time when file data last accessed. 122Changed by the 123.Xr mknod 2 , 124.Xr utimes 2 125and 126.Xr read 2 127system calls. 128.It st_mtime 129Time when file data last modified. 130Changed by the 131.Xr mknod 2 , 132.Xr utimes 2 133and 134.Xr write 2 135system calls. 136.It st_ctime 137Time when file status was last changed (inode data modification). 138Changed by the 139.Xr chmod 2 , 140.Xr chown 2 , 141.Xr link 2 , 142.Xr mknod 2 , 143.Xr rename 2 , 144.Xr unlink 2 , 145.Xr utimes 2 146and 147.Xr write 2 148system calls. 149.El 150.Pp 151The size-related fields of the 152.Fa struct stat 153are as follows: 154.Bl -tag -width XXXst_blksize 155.It st_blksize 156The optimal I/O block size for the file. 157.It st_blocks 158The actual number of blocks allocated for the file in 512-byte units. 159As short symbolic links are stored in the inode, this number may 160be zero. 161.El 162.Pp 163The status information word 164.Fa st_mode 165has the following bits: 166.Bd -literal 167#define S_IFMT 0170000 /* type of file */ 168#define S_IFIFO 0010000 /* named pipe (fifo) */ 169#define S_IFCHR 0020000 /* character special */ 170#define S_IFDIR 0040000 /* directory */ 171#define S_IFBLK 0060000 /* block special */ 172#define S_IFREG 0100000 /* regular */ 173#define S_IFLNK 0120000 /* symbolic link */ 174#define S_IFSOCK 0140000 /* socket */ 175#define S_IFWHT 0160000 /* whiteout */ 176#define S_ISUID 0004000 /* set user id on execution */ 177#define S_ISGID 0002000 /* set group id on execution */ 178#define S_ISVTX 0001000 /* save swapped text even after use */ 179#define S_IRUSR 0000400 /* read permission, owner */ 180#define S_IWUSR 0000200 /* write permission, owner */ 181#define S_IXUSR 0000100 /* execute/search permission, owner */ 182.Ed 183.Pp 184For a list of access modes, see 185.Aq Pa sys/stat.h , 186.Xr access 2 187and 188.Xr chmod 2 . 189.Sh RETURN VALUES 190Upon successful completion a value of 0 is returned. 191Otherwise, a value of -1 is returned and 192.Va errno 193is set to indicate the error. 194.Sh COMPATIBILITY 195Previous versions of the system used different types for the 196.Li st_dev , 197.Li st_uid , 198.Li st_gid , 199.Li st_rdev , 200.Li st_size , 201.Li st_blksize 202and 203.Li st_blocks 204fields. 205.Sh ERRORS 206.Fn Stat 207and 208.Fn lstat 209will fail if: 210.Bl -tag -width Er 211.It Bq Er ENOTDIR 212A component of the path prefix is not a directory. 213.It Bq Er ENAMETOOLONG 214A component of a pathname exceeded 215.Dv {NAME_MAX} 216characters, or an entire path name exceeded 217.Dv {PATH_MAX} 218characters. 219.It Bq Er ENOENT 220The named file does not exist. 221.It Bq Er EACCES 222Search permission is denied for a component of the path prefix. 223.It Bq Er ELOOP 224Too many symbolic links were encountered in translating the pathname. 225.It Bq Er EFAULT 226.Fa Sb 227or 228.Em name 229points to an invalid address. 230.It Bq Er EIO 231An I/O error occurred while reading from or writing to the file system. 232.El 233.Pp 234.Bl -tag -width Er 235.Fn Fstat 236will fail if: 237.It Bq Er EBADF 238.Fa fd 239is not a valid open file descriptor. 240.It Bq Er EFAULT 241.Fa Sb 242points to an invalid address. 243.It Bq Er EIO 244An I/O error occurred while reading from or writing to the file system. 245.El 246.Sh CAVEAT 247The fields in the stat structure currently marked 248.Fa st_spare1 , 249.Fa st_spare2 , 250and 251.Fa st_spare3 252are present in preparation for inode time stamps expanding 253to 64 bits. This, however, can break certain programs that 254depend on the time stamps being contiguous (in calls to 255.Xr utimes 2 ) . 256.Sh SEE ALSO 257.Xr chmod 2 , 258.Xr chown 2 , 259.Xr utimes 2 260.Xr symlink 7 261.Sh BUGS 262Applying 263.Xr fstat 264to a socket (and thus to a pipe) 265returns a zero'd buffer, 266except for the blocksize field, 267and a unique device and inode number. 268.Sh STANDARDS 269The 270.Fn stat 271and 272.Fn fstat 273function calls are expected to conform to 274.St -p1003.1-88 . 275.Sh HISTORY 276A 277.Fn lstat 278function call appeared in 279.Bx 4.2 . 280