xref: /freebsd-13.1/lib/libc/sys/stat.2 (revision 06a31d6a)
1.\" Copyright (c) 1980, 1991, 1993, 1994
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)stat.2	8.4 (Berkeley) 5/1/95
33.\" $FreeBSD$
34.\"
35.Dd February 15, 2002
36.Dt STAT 2
37.Os
38.Sh NAME
39.Nm stat ,
40.Nm lstat ,
41.Nm fstat
42.Nd get file status
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In sys/types.h
47.In sys/stat.h
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
57system call 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
63The
64.Fn lstat
65system call is 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
77system call 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.Vt stat
85structure
86as defined by
87.Aq Pa sys/stat.h
88and into which information is placed concerning the file.
89.Pp
90The time-related fields of
91.Fa struct stat
92are as follows:
93.Bl -tag -width ".Va st_birthtime"
94.It Va st_atime
95Time when file data last accessed.
96Changed by the
97.Xr mknod 2 ,
98.Xr utimes 2
99and
100.Xr read 2
101system calls.
102.It Va st_mtime
103Time when file data last modified.
104Changed by the
105.Xr mknod 2 ,
106.Xr utimes 2
107and
108.Xr write 2
109system calls.
110.It Va st_ctime
111Time when file status was last changed (inode data modification).
112Changed by the
113.Xr chmod 2 ,
114.Xr chown 2 ,
115.Xr link 2 ,
116.Xr mknod 2 ,
117.Xr rename 2 ,
118.Xr unlink 2 ,
119.Xr utimes 2
120and
121.Xr write 2
122system calls.
123.It Va st_birthtime
124Time when the inode was created.
125.El
126.Pp
127If
128.Dv _POSIX_SOURCE
129is not defined, the time-related fields are defined as:
130.Bd -literal
131#ifndef _POSIX_SOURCE
132#define st_atime st_atimespec.tv_sec
133#define st_mtime st_mtimespec.tv_sec
134#define st_ctime st_ctimespec.tv_sec
135#endif
136.Ed
137.Pp
138The size-related fields of the
139.Vt "struct stat"
140are as follows:
141.Bl -tag -width XXXst_blksize
142.It st_blksize
143The optimal I/O block size for the file.
144.It st_blocks
145The actual number of blocks allocated for the file in 512-byte units.
146As short symbolic links are stored in the inode, this number may
147be zero.
148.El
149.Pp
150The status information word
151.Fa st_mode
152has the following bits:
153.Bd -literal
154#define S_IFMT   0170000  /* type of file */
155#define S_IFIFO  0010000  /* named pipe (fifo) */
156#define S_IFCHR  0020000  /* character special */
157#define S_IFDIR  0040000  /* directory */
158#define S_IFBLK  0060000  /* block special */
159#define S_IFREG  0100000  /* regular */
160#define S_IFLNK  0120000  /* symbolic link */
161#define S_IFSOCK 0140000  /* socket */
162#define S_IFWHT  0160000  /* whiteout */
163#define S_ISUID  0004000  /* set user id on execution */
164#define S_ISGID  0002000  /* set group id on execution */
165#define S_ISVTX  0001000  /* save swapped text even after use */
166#define S_IRUSR  0000400  /* read permission, owner */
167#define S_IWUSR  0000200  /* write permission, owner */
168#define S_IXUSR  0000100  /* execute/search permission, owner */
169.Ed
170.Pp
171For a list of access modes, see
172.Aq Pa sys/stat.h ,
173.Xr access 2
174and
175.Xr chmod 2 .
176.Sh RETURN VALUES
177.Rv -std
178.Sh COMPATIBILITY
179Previous versions of the system used different types for the
180.Li st_dev ,
181.Li st_uid ,
182.Li st_gid ,
183.Li st_rdev ,
184.Li st_size ,
185.Li st_blksize
186and
187.Li st_blocks
188fields.
189.Sh ERRORS
190The
191.Fn stat
192and
193.Fn lstat
194system calls will fail if:
195.Bl -tag -width Er
196.It Bq Er EACCES
197Search permission is denied for a component of the path prefix.
198.It Bq Er EFAULT
199The
200.Fa sb
201or
202.Fa path
203argument
204points to an invalid address.
205.It Bq Er EIO
206An I/O error occurred while reading from or writing to the file system.
207.It Bq Er ELOOP
208Too many symbolic links were encountered in translating the pathname.
209.It Bq Er ENAMETOOLONG
210A component of a pathname exceeded 255 characters,
211or an entire path name exceeded 1023 characters.
212.It Bq Er ENOENT
213The named file does not exist.
214.It Bq Er ENOTDIR
215A component of the path prefix is not a directory.
216.It Bq Er EOVERFLOW
217The file size in bytes cannot be
218represented correctly in the structure pointed to by
219.Fa sb .
220.El
221.Pp
222.Bl -tag -width Er
223The
224.Fn fstat
225system call will fail if:
226.It Bq Er EBADF
227The
228.Fa fd
229argument
230is not a valid open file descriptor.
231.It Bq Er EFAULT
232The
233.Fa sb
234argument
235points to an invalid address.
236.It Bq Er EIO
237An I/O error occurred while reading from or writing to the file system.
238.It Bq Er EOVERFLOW
239The file size in bytes cannot be
240represented correctly in the structure pointed to by
241.Fa sb .
242.El
243.Sh SEE ALSO
244.Xr access 2 ,
245.Xr chmod 2 ,
246.Xr chown 2 ,
247.Xr utimes 2 ,
248.Xr symlink 7
249.Sh BUGS
250Applying
251.Fn fstat
252to a socket (and thus to a pipe)
253returns a zeroed buffer,
254except for the blocksize field,
255and a unique device and inode number.
256.Sh STANDARDS
257The
258.Fn stat
259and
260.Fn fstat
261system calls are expected to conform to
262.St -p1003.1-90 .
263.Sh HISTORY
264The
265.Fn stat
266and
267.Fn fstat
268system calls appeared in
269.At v7 .
270The
271.Fn lstat
272system call appeared in
273.Bx 4.2 .
274