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