xref: /openbsd/lib/libc/sys/stat.2 (revision 404b540a)
1.\"	$OpenBSD: stat.2,v 1.27 2009/01/30 18:43:21 guenther 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 $Mdocdate: January 30 2009 $
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;    /* inode's device */
94    ino_t      st_ino;    /* inode's number */
95    mode_t     st_mode;   /* inode protection mode */
96    nlink_t    st_nlink;  /* number of hard links */
97    uid_t      st_uid;    /* user ID of the file's owner */
98    gid_t      st_gid;    /* group ID of the file's group */
99    dev_t      st_rdev;   /* device type */
100    struct timespec st_atim;  /* time of last access */
101    struct timespec st_mtim;  /* time of last data modification */
102    struct timespec st_ctim;  /* 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 blocksize for I/O */
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 represented in
114.Li struct timespec
115format, which has nanosecond precision.
116However, the actual precision is generally limited by the file
117system holding the file.
118The fields are as follows:
119.Bl -tag -width XXXst_mtim
120.It Fa st_atim
121Time when file data was last accessed.
122Set when the file system object was created and updated by the
123.Xr utimes 2
124and
125.Xr read 2
126system calls.
127.It Fa st_mtim
128Time when file data was last modified.
129Changed by the
130.Xr truncate 2 ,
131.Xr utimes 2 ,
132and
133.Xr write 2
134system calls.
135For directories, changed by any system call that alters which files are
136in the directory, such as the
137.Xr unlink 2 ,
138.Xr rename 2 ,
139.Xr mkdir 2 ,
140and
141.Xr symlink 2
142system calls.
143.It Fa st_ctime
144Time when file status was last changed (inode data modification).
145Changed by the
146.Xr chmod 2 ,
147.Xr chown 2 ,
148.Xr link 2 ,
149.Xr rename 2 ,
150.Xr unlink 2 ,
151.Xr utimes 2 ,
152and
153.Xr write 2
154system calls.
155.El
156.Pp
157In addition, all the time fields are set to the current time when
158a file system object is first created by the
159.Xr mkdir 2 ,
160.Xr mkfifo 2 ,
161.Xr mknod 2 ,
162.Xr open 2 ,
163and
164.Xr symlink 2
165system calls.
166.Pp
167For compatibility with previous standards,
168.Fa st_atime ,
169.Fa st_mtime ,
170and
171.Fa st_ctime
172macros are provided that expand to the
173.Fa tv_secs
174member of their respective
175.Li struct timespec
176member.
177Deprecated macros are also provided for some transitional names:
178.Fa st_atimensec ,
179.Fa st_mtimensec ,
180.Fa st_ctimensec ,
181.Fa st_atimespec ,
182.Fa st_mtimespec ,
183and
184.Fa st_ctimespec
185.Pp
186The size-related fields of the
187.Li struct stat
188are as follows:
189.Bl -tag -width XXXst_blksize
190.It Fa st_blksize
191The optimal I/O block size for the file.
192.It Fa st_blocks
193The actual number of blocks allocated for the file in 512-byte units.
194As short symbolic links are stored in the inode, this number may
195be zero.
196.El
197.Pp
198The status information word
199.Fa st_mode
200has the following bits:
201.Bd -literal -offset indent
202#define S_IFMT   0170000  /* type of file mask */
203#define S_IFIFO  0010000  /* named pipe (fifo) */
204#define S_IFCHR  0020000  /* character special */
205#define S_IFDIR  0040000  /* directory */
206#define S_IFBLK  0060000  /* block special */
207#define S_IFREG  0100000  /* regular */
208#define S_IFLNK  0120000  /* symbolic link */
209#define S_IFSOCK 0140000  /* socket */
210#define S_ISUID  0004000  /* set-user-ID on execution */
211#define S_ISGID  0002000  /* set-group-ID on execution */
212#define S_ISVTX  0001000  /* save swapped text even after use */
213#define S_IRWXU  0000700  /* RWX mask for owner */
214#define S_IRUSR  0000400  /* R for owner */
215#define S_IWUSR  0000200  /* W for owner */
216#define S_IXUSR  0000100  /* X for owner */
217#define S_IRWXG  0000070  /* RWX mask for group */
218#define S_IRGRP  0000040  /* R for group */
219#define S_IWGRP  0000020  /* W for group */
220#define S_IXGRP  0000010  /* X for group */
221#define S_IRWXO  0000007  /* RWX mask for other */
222#define S_IROTH  0000004  /* R for other */
223#define S_IWOTH  0000002  /* W for other */
224#define S_IXOTH  0000001  /* X for other */
225.Ed
226.Pp
227The following macros test a file's type.
228If the file is of that type, a non-zero value is returned;
229otherwise, 0 is returned.
230.Bd -literal -offset indent
231S_ISBLK(st_mode m)  /* block special */
232S_ISCHR(st_mode m)  /* char special */
233S_ISDIR(st_mode m)  /* directory */
234S_ISFIFO(st_mode m) /* fifo */
235S_ISLNK(st_mode m)  /* symbolic link */
236S_ISREG(st_mode m)  /* regular file */
237S_ISSOCK(st_mode m) /* socket */
238.Ed
239.Pp
240For a list of access modes, see
241.Aq Pa sys/stat.h ,
242.Xr access 2 ,
243and
244.Xr chmod 2 .
245.Sh RETURN VALUES
246Upon successful completion a value of 0 is returned.
247Otherwise, a value of \-1 is returned and
248.Va errno
249is set to indicate the error.
250.Sh ERRORS
251.Fn stat
252and
253.Fn lstat
254will fail if:
255.Bl -tag -width Er
256.It Bq Er ENOTDIR
257A component of the path prefix is not a directory.
258.It Bq Er ENAMETOOLONG
259A component of a pathname exceeded
260.Dv NAME_MAX
261characters, or an entire path name exceeded
262.Dv PATH_MAX
263characters.
264.It Bq Er ENOENT
265The named file does not exist.
266.It Bq Er EACCES
267Search permission is denied for a component of the path prefix.
268.It Bq Er ELOOP
269Too many symbolic links were encountered in translating the pathname.
270.It Bq Er EFAULT
271.Fa sb
272or
273.Em name
274points to an invalid address.
275.It Bq Er EIO
276An I/O error occurred while reading from or writing to the file system.
277.El
278.Pp
279.Fn fstat
280will fail if:
281.Bl -tag -width Er
282.It Bq Er EBADF
283.Fa fd
284is not a valid open file descriptor.
285.It Bq Er EFAULT
286.Fa sb
287points to an invalid address.
288.It Bq Er EIO
289An I/O error occurred while reading from or writing to the file system.
290.El
291.Sh SEE ALSO
292.Xr chmod 2 ,
293.Xr chown 2 ,
294.Xr utimes 2 ,
295.Xr symlink 7
296.Sh STANDARDS
297Previous versions of the system used different types for the
298.Fa st_dev ,
299.Fa st_uid ,
300.Fa st_gid ,
301.Fa st_rdev ,
302.Fa st_size ,
303.Fa st_blksize ,
304and
305.Fa st_blocks
306fields.
307.Pp
308The
309.Fn stat
310and
311.Fn fstat
312function calls are expected to conform to
313.St -p1003.1-88 .
314.Sh HISTORY
315A
316.Fn stat
317function appeared in
318.At v2 .
319An
320.Fn lstat
321function call appeared in
322.Bx 4.2 .
323.Sh CAVEATS
324The file generation number,
325.Fa st_gen ,
326is only available to the superuser.
327.Pp
328Certain programs written when the timestamps were just of type
329.Li time_t
330assumed that the members were consecutive (and could therefore
331be placed directly to
332.Xr utimes 2 ) .
333The transition to timestamps of type
334.Li struct timespec
335broke them irrevocably.
336.Sh BUGS
337Applying
338.Fn fstat
339to a socket (and thus to a pipe)
340returns a zeroed buffer,
341except for the blocksize field,
342and a unique device and inode number.
343