1.\" $NetBSD: stat.2,v 1.35 2005/12/26 19:40:15 perry 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 May 1, 1995 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. 183.It st_blksize 184The optimal I/O block size for the file. 185.It st_blocks 186The actual number of blocks allocated for the file in 512-byte units. 187As short symbolic links are stored in the inode, this number may 188be zero. 189.El 190.Pp 191The status information word 192.Fa st_mode 193has the following bits: 194.Bd -literal 195#define S_IFMT 0170000 /* type of file */ 196#define S_IFIFO 0010000 /* named pipe (fifo) */ 197#define S_IFCHR 0020000 /* character special */ 198#define S_IFDIR 0040000 /* directory */ 199#define S_IFBLK 0060000 /* block special */ 200#define S_IFREG 0100000 /* regular */ 201#define S_IFLNK 0120000 /* symbolic link */ 202#define S_IFSOCK 0140000 /* socket */ 203#define S_IFWHT 0160000 /* whiteout */ 204#define S_ISUID 0004000 /* set user id on execution */ 205#define S_ISGID 0002000 /* set group id on execution */ 206#define S_ISVTX 0001000 /* save swapped text even after use */ 207#define S_IRUSR 0000400 /* read permission, owner */ 208#define S_IWUSR 0000200 /* write permission, owner */ 209#define S_IXUSR 0000100 /* execute/search permission, owner */ 210#define S_IRGRP 0000040 /* read permission, group */ 211#define S_IWGRP 0000020 /* write permission, group */ 212#define S_IXGRP 0000010 /* execute/search permission, group */ 213#define S_IROTH 0000004 /* read permission, other */ 214#define S_IWOTH 0000002 /* write permission, other */ 215#define S_IXOTH 0000001 /* execute/search permission, other */ 216.Ed 217.Pp 218For a list of access modes, see 219.Aq Pa sys/stat.h , 220.Xr access 2 221and 222.Xr chmod 2 . 223.Pp 224The status information word 225.Fa st_flags 226has the following bits: 227.Bd -literal 228#define UF_NODUMP 0x00000001 /* do not dump file */ 229#define UF_IMMUTABLE 0x00000002 /* file may not be changed */ 230#define UF_APPEND 0x00000004 /* writes to file may only append */ 231#define UF_OPAQUE 0x00000008 /* directory is opaque wrt. union */ 232#define SF_ARCHIVED 0x00010000 /* file is archived */ 233#define SF_IMMUTABLE 0x00020000 /* file may not be changed */ 234#define SF_APPEND 0x00040000 /* writes to file may only append */ 235.Ed 236.Pp 237For a description of the flags, see 238.Xr chflags 2 . 239.Sh RETURN VALUES 240Upon successful completion a value of 0 is returned. 241Otherwise, a value of \-1 is returned and 242.Va errno 243is set to indicate the error. 244.Sh COMPATIBILITY 245Previous versions of the system used different types for the 246.Li st_dev , 247.Li st_uid , 248.Li st_gid , 249.Li st_rdev , 250.Li st_size , 251.Li st_blksize 252and 253.Li st_blocks 254fields. 255.Sh ERRORS 256.Fn stat 257and 258.Fn lstat 259will fail if: 260.Bl -tag -width Er 261.It Bq Er ENOTDIR 262A component of the path prefix is not a directory. 263.It Bq Er ENAMETOOLONG 264A component of a pathname exceeded 265.Dv {NAME_MAX} 266characters, or an entire path name exceeded 267.Dv {PATH_MAX} 268characters. 269.It Bq Er ENOENT 270The named file does not exist. 271.It Bq Er EACCES 272Search permission is denied for a component of the path prefix. 273.It Bq Er ELOOP 274Too many symbolic links were encountered in translating the pathname. 275.It Bq Er EFAULT 276.Fa sb 277or 278.Em name 279points to an invalid address. 280.It Bq Er ENXIO 281The named file is a character special or block 282special file, and the device associated with this special file 283does not exist. 284.It Bq Er EIO 285An I/O error occurred while reading from or writing to the file system. 286.It Bq Er EBADF 287A badly formed v-node was encountered. 288This can happen if a file system information node is incorrect. 289.El 290.Pp 291.Bl -tag -width Er 292.Fn fstat 293will fail if: 294.It Bq Er EBADF 295.Fa fd 296is not a valid open file descriptor. 297.It Bq Er EFAULT 298.Fa sb 299points to an invalid address. 300.It Bq Er EIO 301An I/O error occurred while reading from or writing to the file system. 302.El 303.Sh SEE ALSO 304.Xr chflags 2 , 305.Xr chmod 2 , 306.Xr chown 2 , 307.Xr utimes 2 , 308.Xr dir 5 , 309.Xr symlink 7 310.Sh STANDARDS 311The 312.Fn stat 313and 314.Fn fstat 315functions conform to 316.St -p1003.1-90 . 317.Sh HISTORY 318A 319.Fn lstat 320function call appeared in 321.Bx 4.2 . 322.Sh BUGS 323Applying 324.Fn fstat 325to a socket (and thus to a pipe) 326returns a zero'd buffer, 327except for the blocksize field, 328and a unique device and file serial number. 329