xref: /freebsd-14.2/lib/libc/sys/stat.2 (revision 052d3c12)
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. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)stat.2	8.4 (Berkeley) 5/1/95
29.\" $FreeBSD$
30.\"
31.Dd December 1, 2017
32.Dt STAT 2
33.Os
34.Sh NAME
35.Nm stat ,
36.Nm lstat ,
37.Nm fstat ,
38.Nm fstatat
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 * restrict path" "struct stat * restrict sb"
46.Ft int
47.Fn lstat "const char * restrict path" "struct stat * restrict sb"
48.Ft int
49.Fn fstat "int fd" "struct stat *sb"
50.Ft int
51.Fn fstatat "int fd" "const char *path" "struct stat *buf" "int flag"
52.Sh DESCRIPTION
53The
54.Fn stat
55system call obtains information about the file pointed to by
56.Fa path .
57Read, write or execute
58permission of the named file is not required, but all directories
59listed in the path name leading to the file must be searchable.
60.Pp
61The
62.Fn lstat
63system call is like
64.Fn stat
65except when 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.
72.Pp
73The
74.Fn fstat
75system call obtains the same information about an open file
76known by the file descriptor
77.Fa fd .
78.Pp
79The
80.Fn fstatat
81system call is equivalent to
82.Fn stat
83and
84.Fn lstat
85except when the
86.Fa path
87specifies a relative path.
88In this case the status is retrieved from a file relative to
89the directory associated with the file descriptor
90.Fa fd
91instead of the current working directory.
92.Pp
93The values for the
94.Fa flag
95are constructed by a bitwise-inclusive OR of flags from this list,
96defined in
97.In fcntl.h :
98.Bl -tag -width indent
99.It Dv AT_SYMLINK_NOFOLLOW
100If
101.Fa path
102names a symbolic link, the status of the symbolic link is returned.
103.El
104.Pp
105If
106.Fn fstatat
107is passed the special value
108.Dv AT_FDCWD
109in the
110.Fa fd
111parameter, the current working directory is used and the behavior is
112identical to a call to
113.Fn stat
114or
115.Fn lstat
116respectively, depending on whether or not the
117.Dv AT_SYMLINK_NOFOLLOW
118bit is set in
119.Fa flag .
120.Pp
121The
122.Fa sb
123argument is a pointer to a
124.Vt stat
125structure
126as defined by
127.In sys/stat.h
128and into which information is placed concerning the file.
129.Pp
130The fields of
131.Vt "struct stat"
132related to the file system are:
133.Bl -tag -width ".Va st_nlink"
134.It Va st_dev
135Numeric ID of the device containing the file.
136.It Va st_ino
137The file's inode number.
138.It Va st_nlink
139Number of hard links to the file.
140.It Va st_flags
141Flags enabled for the file.
142See
143.Xr chflags 2
144for the list of flags and their description.
145.El
146.Pp
147The
148.Va st_dev
149and
150.Va st_ino
151fields together identify the file uniquely within the system.
152.Pp
153The time-related fields of
154.Vt "struct stat"
155are:
156.Bl -tag -width ".Va st_birthtim"
157.It Va st_atim
158Time when file data was last accessed.
159Changed by the
160.Xr mknod 2 ,
161.Xr utimes 2 ,
162.Xr read 2
163and
164.Xr readv 2
165system calls.
166.It Va st_mtim
167Time when file data was last modified.
168Changed by the
169.Xr mkdir 2 ,
170.Xr mkfifo 2 ,
171.Xr mknod 2 ,
172.Xr utimes 2 ,
173.Xr write 2
174and
175.Xr writev 2
176system calls.
177.It Va st_ctim
178Time when file status was last changed (inode data modification).
179Changed by the
180.Xr chflags 2 ,
181.Xr chmod 2 ,
182.Xr chown 2 ,
183.Xr creat 2 ,
184.Xr link 2 ,
185.Xr mkdir 2 ,
186.Xr mkfifo 2 ,
187.Xr mknod 2 ,
188.Xr rename 2 ,
189.Xr rmdir 2 ,
190.Xr symlink 2 ,
191.Xr truncate 2 ,
192.Xr unlink 2 ,
193.Xr utimes 2 ,
194.Xr write 2
195and
196.Xr writev 2
197system calls.
198.It Va st_birthtim
199Time when the inode was created.
200.El
201.Pp
202These time-related macros are defined for compatibility:
203.Bd -literal
204#define	st_atime		st_atim.tv_sec
205#define	st_mtime		st_mtim.tv_sec
206#define	st_ctime		st_ctim.tv_sec
207#ifndef _POSIX_SOURCE
208#define	st_birthtime		st_birthtim.tv_sec
209#endif
210
211#ifndef _POSIX_SOURCE
212#define	st_atimespec		st_atim
213#define	st_mtimespec		st_mtim
214#define	st_ctimespec		st_ctim
215#define	st_birthtimespec	st_birthtim
216#endif
217.Ed
218.Pp
219Size-related fields of the
220.Vt "struct stat"
221are:
222.Bl -tag -width ".Va st_blksize"
223.It Va st_size
224File size in bytes.
225.It Va st_blksize
226Optimal I/O block size for the file.
227.It Va st_blocks
228Actual number of blocks allocated for the file in 512-byte units.
229As short symbolic links are stored in the inode, this number may
230be zero.
231.El
232.Pp
233The access-related fields of
234.Vt "struct stat"
235are:
236.Bl -tag -width ".Va st_mode"
237.It Va st_uid
238User ID of the file's owner.
239.It Va st_gid
240Group ID of the file.
241.It Va st_mode
242Status of the file (see below).
243.El
244.Pp
245The status information word
246.Fa st_mode
247has these bits:
248.Bd -literal
249#define S_IFMT   0170000  /* type of file mask */
250#define S_IFIFO  0010000  /* named pipe (fifo) */
251#define S_IFCHR  0020000  /* character special */
252#define S_IFDIR  0040000  /* directory */
253#define S_IFBLK  0060000  /* block special */
254#define S_IFREG  0100000  /* regular */
255#define S_IFLNK  0120000  /* symbolic link */
256#define S_IFSOCK 0140000  /* socket */
257#define S_IFWHT  0160000  /* whiteout */
258#define S_ISUID  0004000  /* set user id on execution */
259#define S_ISGID  0002000  /* set group id on execution */
260#define S_ISVTX  0001000  /* save swapped text even after use */
261#define S_IRWXU  0000700  /* RWX mask for owner */
262#define S_IRUSR  0000400  /* read permission, owner */
263#define S_IWUSR  0000200  /* write permission, owner */
264#define S_IXUSR  0000100  /* execute/search permission, owner */
265#define S_IRWXG  0000070  /* RWX mask for group */
266#define S_IRGRP  0000040  /* read permission, group */
267#define S_IWGRP  0000020  /* write permission, group */
268#define S_IXGRP  0000010  /* execute/search permission, group */
269#define S_IRWXO  0000007  /* RWX mask for other */
270#define S_IROTH  0000004  /* read permission, other */
271#define S_IWOTH  0000002  /* write permission, other */
272#define S_IXOTH  0000001  /* execute/search permission, other */
273.Ed
274.Pp
275For a list of access modes, see
276.In sys/stat.h ,
277.Xr access 2
278and
279.Xr chmod 2 .
280These macros are available to test whether a
281.Va st_mode
282value passed in the
283.Fa m
284argument corresponds to a file of the specified type:
285.Bl -tag -width ".Fn S_ISFIFO m"
286.It Fn S_ISBLK m
287Test for a block special file.
288.It Fn S_ISCHR m
289Test for a character special file.
290.It Fn S_ISDIR m
291Test for a directory.
292.It Fn S_ISFIFO m
293Test for a pipe or FIFO special file.
294.It Fn S_ISLNK m
295Test for a symbolic link.
296.It Fn S_ISREG m
297Test for a regular file.
298.It Fn S_ISSOCK m
299Test for a socket.
300.It Fn S_ISWHT m
301Test for a whiteout.
302.El
303.Pp
304The macros evaluate to a non-zero value if the test is true
305or to the value 0 if the test is false.
306.Sh RETURN VALUES
307.Rv -std
308.Sh COMPATIBILITY
309Previous versions of the system used different types for the
310.Va st_dev ,
311.Va st_uid ,
312.Va st_gid ,
313.Va st_rdev ,
314.Va st_size ,
315.Va st_blksize
316and
317.Va st_blocks
318fields.
319.Sh ERRORS
320The
321.Fn stat
322and
323.Fn lstat
324system calls will fail if:
325.Bl -tag -width Er
326.It Bq Er EACCES
327Search permission is denied for a component of the path prefix.
328.It Bq Er EFAULT
329The
330.Fa sb
331or
332.Fa path
333argument
334points to an invalid address.
335.It Bq Er EIO
336An I/O error occurred while reading from or writing to the file system.
337.It Bq Er ELOOP
338Too many symbolic links were encountered in translating the pathname.
339.It Bq Er ENAMETOOLONG
340A component of a pathname exceeded 255 characters,
341or an entire path name exceeded 1023 characters.
342.It Bq Er ENOENT
343The named file does not exist.
344.It Bq Er ENOTDIR
345A component of the path prefix is not a directory.
346.It Bq Er EOVERFLOW
347The file size in bytes cannot be
348represented correctly in the structure pointed to by
349.Fa sb .
350.El
351.Pp
352The
353.Fn fstat
354system call will fail if:
355.Bl -tag -width Er
356.It Bq Er EBADF
357The
358.Fa fd
359argument
360is not a valid open file descriptor.
361.It Bq Er EFAULT
362The
363.Fa sb
364argument
365points to an invalid address.
366.It Bq Er EIO
367An I/O error occurred while reading from or writing to the file system.
368.It Bq Er EOVERFLOW
369The file size in bytes cannot be
370represented correctly in the structure pointed to by
371.Fa sb .
372.El
373.Pp
374In addition to the errors returned by the
375.Fn lstat ,
376the
377.Fn fstatat
378may fail if:
379.Bl -tag -width Er
380.It Bq Er EBADF
381The
382.Fa path
383argument does not specify an absolute path and the
384.Fa fd
385argument is neither
386.Dv AT_FDCWD
387nor a valid file descriptor open for searching.
388.It Bq Er EINVAL
389The value of the
390.Fa flag
391argument is not valid.
392.It Bq Er ENOTDIR
393The
394.Fa path
395argument is not an absolute path and
396.Fa fd
397is neither
398.Dv AT_FDCWD
399nor a file descriptor associated with a directory.
400.El
401.Sh SEE ALSO
402.Xr access 2 ,
403.Xr chmod 2 ,
404.Xr chown 2 ,
405.Xr fhstat 2 ,
406.Xr statfs 2 ,
407.Xr utimes 2 ,
408.Xr sticky 7 ,
409.Xr symlink 7
410.Sh STANDARDS
411The
412.Fn stat
413and
414.Fn fstat
415system calls are expected to conform to
416.St -p1003.1-90 .
417The
418.Fn fstatat
419system call follows The Open Group Extended API Set 2 specification.
420.Sh HISTORY
421The
422.Fn stat
423and
424.Fn fstat
425system calls appeared in
426.At v1 .
427The
428.Fn lstat
429system call appeared in
430.Bx 4.2 .
431The
432.Fn fstatat
433system call appeared in
434.Fx 8.0 .
435.Sh BUGS
436Applying
437.Fn fstat
438to a socket
439returns a zeroed buffer,
440except for the blocksize field,
441and a unique device and inode number.
442