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