xref: /dragonfly/lib/libc/sys/stat.2 (revision 73610d44)
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: src/lib/libc/sys/stat.2,v 1.16.2.7 2001/12/14 18:34:01 ru Exp $
30.\" $DragonFly: src/lib/libc/sys/stat.2,v 1.6 2008/09/28 16:33:35 swildner Exp $
31.\"
32.Dd August 1, 2009
33.Dt STAT 2
34.Os
35.Sh NAME
36.Nm stat ,
37.Nm lstat ,
38.Nm fstat ,
39.Nm fstatat
40.Nd get file status
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In sys/types.h
45.In sys/stat.h
46.Ft int
47.Fn stat "const char *path" "struct stat *sb"
48.Ft int
49.Fn lstat "const char *path" "struct stat *sb"
50.Ft int
51.Fn fstat "int fd" "struct stat *sb"
52.Ft int
53.Fn fstatat "int fd" "const char *path" "struct stat *buf" "int flag"
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
63.Fn Lstat
64is like
65.Fn stat
66except in the case where the named file is a symbolic link,
67in which case
68.Fn lstat
69returns information about the link,
70while
71.Fn stat
72returns information about the file the link references.
73.Pp
74The
75.Fn fstat
76system call obtains the same information about an open file
77known by the file descriptor
78.Fa fd .
79.Pp
80The
81.Fn fstatat
82system call is equivalent to
83.Fn stat
84and
85.Fn lstat
86except in the case where the
87.Fa path
88specifies a relative path.
89In this case the status is retrieved from a file relative to
90the directory associated with the file descriptor
91.Fa fd
92instead of the current working directory.
93.Pp
94The values for the
95.Fa flag
96are constructed by a bitwise-inclusive OR of flags from the following list,
97defined in
98.In fcntl.h :
99.Bl -tag -width indent
100.It Dv AT_SYMLINK_NOFOLLOW
101If
102.Fa path
103names a symbolic link, the status of the symbolic link is returned.
104.El
105.Pp
106If
107.Fn fstatat
108is passed the special value
109.Dv AT_FDCWD
110in the
111.Fa fd
112parameter, the current working directory is used and the behavior is
113identical to a call to
114.Fn stat
115or
116.Fn lstat
117respectively, depending on whether or not the
118.Dv AT_SYMLINK_NOFOLLOW
119bit is set in
120.Fa flag .
121.Pp
122The
123.Fa sb
124argument is a pointer to a
125.Vt stat
126structure
127as defined by
128.In sys/stat.h
129(shown below)
130and into which information is placed concerning the file.
131.Bd -literal
132struct stat {
133    dev_t     st_dev;               /* inode's device */
134    ino_t     st_ino;               /* inode's number */
135    mode_t    st_mode;              /* inode protection mode */
136    nlink_t   st_nlink;             /* number of hard links */
137    uid_t     st_uid;               /* user ID of the file's owner */
138    gid_t     st_gid;               /* group ID of the file's group */
139    dev_t     st_rdev;              /* device type */
140#ifndef _POSIX_SOURCE
141    struct timespec st_atimespec;  /* time of last access */
142    struct timespec st_mtimespec;  /* time of last data modification */
143    struct timespec st_ctimespec;  /* time of last file status change */
144#else
145    time_t    st_atime;             /* time of last access */
146    long      st_atimensec;         /* nsec of last access */
147    time_t    st_mtime;             /* time of last data modification */
148    long      st_mtimensec;         /* nsec of last data modification */
149    time_t    st_ctime;             /* time of last file status change */
150    long      st_ctimensec;         /* nsec of last file status change */
151#endif
152    off_t     st_size;              /* file size, in bytes */
153    int64_t   st_blocks;            /* blocks allocated for file */
154    u_int32_t st_blksize;           /* optimal blocksize for I/O */
155    u_int32_t st_flags;             /* user defined flags for file */
156    u_int32_t st_gen;               /* file generation number */
157};
158.Ed
159.Pp
160The time-related fields of
161.Vt struct stat
162are as follows:
163.Bl -tag -width ".Fa st_mtime"
164.It Fa st_atime
165Time when file data last accessed.
166Changed by the
167.Xr execve 2 ,
168.Xr mknod 2 ,
169.Xr mmap 2 ,
170.Xr read 2
171and
172.Xr utimes 2
173system calls.
174.It Fa st_mtime
175Time when file data last modified.
176Changed by the
177.Xr mknod 2 ,
178.Xr utimes 2
179and
180.Xr write 2
181system calls.
182.It Fa st_ctime
183Time when file status was last changed (inode data modification).
184Changed by the
185.Xr chmod 2 ,
186.Xr chown 2 ,
187.Xr link 2 ,
188.Xr mknod 2 ,
189.Xr rename 2 ,
190.Xr unlink 2 ,
191.Xr utimes 2
192and
193.Xr write 2
194system calls.
195.El
196.Pp
197If
198.Dv _POSIX_SOURCE
199is not defined, the time-related fields are defined as:
200.Bd -literal
201#ifndef _POSIX_SOURCE
202#define st_atime st_atimespec.tv_sec
203#define st_mtime st_mtimespec.tv_sec
204#define st_ctime st_ctimespec.tv_sec
205#endif
206.Ed
207.Pp
208The size-related fields of the
209.Vt struct stat
210are as follows:
211.Bl -tag -width ".Fa st_blksize"
212.It Fa st_blksize
213The optimal I/O block size for the file.
214.It Fa st_blocks
215The actual number of blocks allocated for the file in 512-byte units.
216As short symbolic links are stored in the inode, this number may
217be zero.
218.El
219.Pp
220The status information word
221.Fa st_mode
222has the following bits:
223.Bd -literal
224#define S_IFMT 0170000           /* type of file */
225#define        S_IFIFO  0010000  /* named pipe (fifo) */
226#define        S_IFCHR  0020000  /* character special */
227#define        S_IFDIR  0040000  /* directory */
228#define        S_IFBLK  0060000  /* block special */
229#define        S_IFREG  0100000  /* regular */
230#define        S_IFLNK  0120000  /* symbolic link */
231#define        S_IFSOCK 0140000  /* socket */
232#define        S_IFWHT  0160000  /* whiteout */
233#define S_ISUID 0004000  /* set user id on execution */
234#define S_ISGID 0002000  /* set group id on execution */
235#define S_ISVTX 0001000  /* save swapped text even after use */
236#define S_IRUSR 0000400  /* read permission, owner */
237#define S_IWUSR 0000200  /* write permission, owner */
238#define S_IXUSR 0000100  /* execute/search permission, owner */
239.Ed
240.Pp
241For a list of access modes, see
242.In sys/stat.h ,
243.Xr access 2
244and
245.Xr chmod 2 .
246.Sh RETURN VALUES
247.Rv -std
248.Sh COMPATIBILITY
249Previous versions of the system used different types for the
250.Fa st_dev ,
251.Fa st_uid ,
252.Fa st_gid ,
253.Fa st_rdev ,
254.Fa st_size ,
255.Fa st_blksize
256and
257.Fa st_blocks
258fields.
259.Sh ERRORS
260.Fn Stat
261and
262.Fn lstat
263will fail if:
264.Bl -tag -width Er
265.It Bq Er EACCES
266Search permission is denied for a component of the path prefix.
267.It Bq Er EIO
268An I/O error occurred while reading from or writing to the file system.
269.It Bq Er ELOOP
270Too many symbolic links were encountered in translating the pathname.
271.It Bq Er ENAMETOOLONG
272A component of a pathname exceeded 255 characters,
273or an entire path name exceeded 1023 characters.
274.It Bq Er ENOENT
275The named file does not exist.
276.It Bq Er ENOTDIR
277A component of the path prefix is not a directory.
278.It Bq Er EFAULT
279.Fa sb
280or
281.Em name
282points to an invalid address.
283.El
284.Pp
285.Fn Fstat
286will fail if:
287.Bl -tag -width Er
288.It Bq Er EBADF
289.Fa fd
290is not a valid open file descriptor.
291.It Bq Er EFAULT
292.Fa sb
293points to an invalid address.
294.It Bq Er EIO
295An I/O error occurred while reading from or writing to the file system.
296.El
297.Sh SEE ALSO
298.Xr access 2 ,
299.Xr chmod 2 ,
300.Xr chown 2 ,
301.Xr statfs 2 ,
302.Xr statvfs 2 ,
303.Xr utimes 2 ,
304.Xr symlink 7
305.Sh STANDARDS
306The
307.Fn stat
308and
309.Fn fstat
310system calls are expected to conform to
311.St -p1003.1-90 .
312.Sh HISTORY
313A
314.Fn stat
315and a
316.Fn fstat
317system call appeared in
318.At v7 .
319A
320.Fn lstat
321system call appeared in
322.Bx 4.2 .
323The
324.Fn fstatat
325system call appeared in
326.Dx 2.3 .
327.Sh BUGS
328Applying
329.Fn fstat
330to a socket (and thus to a pipe)
331returns a zeroed buffer,
332except for the blocksize field,
333and a unique device and inode number.
334