xref: /openbsd-src/lib/libc/sys/stat.2 (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1.\"	$OpenBSD: stat.2,v 1.46 2015/11/07 22:57:52 jmc 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: November 7 2015 $
33.Dt STAT 2
34.Os
35.Sh NAME
36.Nm stat ,
37.Nm lstat ,
38.Nm fstatat ,
39.Nm fstat ,
40.Nm S_ISBLK ,
41.Nm S_ISCHR ,
42.Nm S_ISDIR ,
43.Nm S_ISFIFO ,
44.Nm S_ISLNK ,
45.Nm S_ISREG ,
46.Nm S_ISSOCK
47.Nd get file status
48.Sh SYNOPSIS
49.In sys/stat.h
50.Ft int
51.Fn stat "const char *path" "struct stat *sb"
52.Ft int
53.Fn lstat "const char *path" "struct stat *sb"
54.Ft int
55.Fn fstat "int fd" "struct stat *sb"
56.In sys/stat.h
57.In fcntl.h
58.Ft int
59.Fn fstatat "int fd" "const char *path" "struct stat *sb" "int flag"
60.Sh DESCRIPTION
61The
62.Fn stat
63function obtains information about the file pointed to by
64.Fa path .
65Read, write, or execute
66permission of the named file is not required, but all directories
67listed in the path name leading to the file must be searchable.
68.Pp
69The
70.Fn lstat
71function is identical to
72.Fn stat
73except when the named file is a symbolic link,
74in which case
75.Fn lstat
76returns information about the link itself, not the file the link references.
77.Pp
78The
79.Fn fstatat
80function is equivalent to either the
81.Fn stat
82or
83.Fn lstat
84function depending on the value of
85.Fa flag
86(see below), except that where
87.Fa path
88specifies a relative path,
89the file whose information is returned is determined relative to
90the directory associated with file descriptor
91.Fa fd
92instead of the current working directory.
93.Pp
94If
95.Fn fstatat
96is passed the special value
97.Dv AT_FDCWD
98(defined in
99.In fcntl.h )
100in the
101.Fa fd
102parameter, the current working directory is used
103and the behavior is identical to a call to
104.Fn stat
105or
106.Fn lstat ,
107depending on whether or not the
108.Dv AT_SYMLINK_NOFOLLOW
109bit is set in
110.Fa flag .
111.Pp
112The
113.Fa flag
114argument is the bitwise OR of zero or more of the following values:
115.Pp
116.Bl -tag -width AT_SYMLINK_NOFOLLOW -offset indent -compact
117.It Dv AT_SYMLINK_NOFOLLOW
118If
119.Fa path
120names a symbolic link, then the status of the symbolic link is returned.
121.El
122.Pp
123The
124.Fn fstat
125function obtains the same information about an open file
126known by the file descriptor
127.Fa fd .
128.Pp
129The
130.Fa sb
131argument is a pointer to a
132.Fn stat
133structure
134as defined by
135.In sys/stat.h
136(shown below)
137and into which information is placed concerning the file.
138.Bd -literal
139struct stat {
140    dev_t      st_dev;    /* inode's device */
141    ino_t      st_ino;    /* inode's number */
142    mode_t     st_mode;   /* inode protection mode */
143    nlink_t    st_nlink;  /* number of hard links */
144    uid_t      st_uid;    /* user ID of the file's owner */
145    gid_t      st_gid;    /* group ID of the file's group */
146    dev_t      st_rdev;   /* device type */
147    struct timespec st_atim;  /* time of last access */
148    struct timespec st_mtim;  /* time of last data modification */
149    struct timespec st_ctim;  /* time of last file status change */
150    off_t      st_size;   /* file size, in bytes */
151    blkcnt_t   st_blocks; /* blocks allocated for file */
152    blksize_t  st_blksize;/* optimal blocksize for I/O */
153    u_int32_t  st_flags;  /* user defined flags for file */
154    u_int32_t  st_gen;    /* file generation number */
155};
156.Ed
157.Pp
158The time-related fields of
159.Li struct stat
160are represented in
161.Li struct timespec
162format, which has nanosecond precision.
163However, the actual precision is generally limited by the file
164system holding the file.
165The fields are as follows:
166.Bl -tag -width XXXst_mtim
167.It Fa st_atim
168Time when file data was last accessed.
169Set when the file system object was created and updated by the
170.Xr utimes 2
171and
172.Xr read 2
173system calls.
174.It Fa st_mtim
175Time when file data was last modified.
176Changed by the
177.Xr truncate 2 ,
178.Xr utimes 2 ,
179and
180.Xr write 2
181system calls.
182For directories, changed by any system call that alters which files are
183in the directory, such as the
184.Xr unlink 2 ,
185.Xr rename 2 ,
186.Xr mkdir 2 ,
187and
188.Xr symlink 2
189system calls.
190.It Fa st_ctim
191Time when file status was last changed (inode data modification).
192Changed by the
193.Xr chmod 2 ,
194.Xr chown 2 ,
195.Xr link 2 ,
196.Xr rename 2 ,
197.Xr unlink 2 ,
198.Xr utimes 2 ,
199and
200.Xr write 2
201system calls.
202.El
203.Pp
204In addition, all the time fields are set to the current time when
205a file system object is first created by the
206.Xr mkdir 2 ,
207.Xr mkfifo 2 ,
208.Xr mknod 2 ,
209.Xr open 2 ,
210and
211.Xr symlink 2
212system calls.
213.Pp
214For compatibility with previous standards,
215.Fa st_atime ,
216.Fa st_mtime ,
217and
218.Fa st_ctime
219macros are provided that expand to the
220.Fa tv_secs
221member of their respective
222.Li struct timespec
223member.
224Deprecated macros are also provided for some transitional names:
225.Fa st_atimensec ,
226.Fa st_mtimensec ,
227.Fa st_ctimensec ,
228.Fa st_atimespec ,
229.Fa st_mtimespec ,
230and
231.Fa st_ctimespec
232.Pp
233The size-related fields of the
234.Li struct stat
235are as follows:
236.Bl -tag -width XXXst_blksize
237.It Fa st_blksize
238The optimal I/O block size for the file.
239.It Fa st_blocks
240The actual number of blocks allocated for the file in 512-byte units.
241As short symbolic links are stored in the inode, this number may
242be zero.
243.El
244.Pp
245The status information word
246.Fa st_mode
247has the following bits:
248.Bd -literal -offset indent
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_ISUID  0004000  /* set-user-ID on execution */
258#define S_ISGID  0002000  /* set-group-ID on execution */
259#define S_ISVTX  0001000  /* save swapped text even after use */
260#define S_IRWXU  0000700  /* RWX mask for owner */
261#define S_IRUSR  0000400  /* R for owner */
262#define S_IWUSR  0000200  /* W for owner */
263#define S_IXUSR  0000100  /* X for owner */
264#define S_IRWXG  0000070  /* RWX mask for group */
265#define S_IRGRP  0000040  /* R for group */
266#define S_IWGRP  0000020  /* W for group */
267#define S_IXGRP  0000010  /* X for group */
268#define S_IRWXO  0000007  /* RWX mask for other */
269#define S_IROTH  0000004  /* R for other */
270#define S_IWOTH  0000002  /* W for other */
271#define S_IXOTH  0000001  /* X for other */
272.Ed
273.Pp
274The following macros test a file's type.
275If the file is of that type, a non-zero value is returned;
276otherwise, 0 is returned.
277.Bd -literal -offset indent
278S_ISBLK(st_mode m)  /* block special */
279S_ISCHR(st_mode m)  /* char special */
280S_ISDIR(st_mode m)  /* directory */
281S_ISFIFO(st_mode m) /* fifo */
282S_ISLNK(st_mode m)  /* symbolic link */
283S_ISREG(st_mode m)  /* regular file */
284S_ISSOCK(st_mode m) /* socket */
285.Ed
286.Pp
287For a list of access modes, see
288.In sys/stat.h ,
289.Xr access 2 ,
290and
291.Xr chmod 2 .
292.Sh RETURN VALUES
293.Rv -std
294.Sh ERRORS
295.Fn stat ,
296.Fn lstat ,
297and
298.Fn fstatat
299will fail if:
300.Bl -tag -width Er
301.It Bq Er ENOTDIR
302A component of the path prefix is not a directory.
303.It Bq Er ENAMETOOLONG
304A component of a pathname exceeded
305.Dv NAME_MAX
306characters, or an entire pathname (including the terminating NUL)
307exceeded
308.Dv PATH_MAX
309bytes.
310.It Bq Er ENOENT
311A component of
312.Em name
313does not exist or
314.Em name
315is the empty path.
316.It Bq Er EACCES
317Search permission is denied for a component of the path prefix.
318.It Bq Er ELOOP
319Too many symbolic links were encountered in translating the pathname.
320.It Bq Er EFAULT
321.Fa sb
322or
323.Em name
324points to an invalid address.
325.It Bq Er EIO
326An I/O error occurred while reading from or writing to the file system.
327.El
328.Pp
329Additionally,
330.Fn fstatat
331will fail if:
332.Bl -tag -width Er
333.It Bq Er EINVAL
334The value of the
335.Fa flag
336argument was neither zero nor
337.Dv AT_SYMLINK_NOFOLLOW .
338.It Bq Er EBADF
339The
340.Fa path
341argument specifies a relative path and the
342.Fa fd
343argument is neither
344.Dv AT_FDCWD
345nor a valid file descriptor.
346.It Bq Er ENOTDIR
347The
348.Fa path
349argument specifies a relative path and the
350.Fa fd
351argument is a valid file descriptor but it does not reference a directory.
352.It Bq Er EACCES
353The
354.Fa path
355argument specifies a relative path but search permission is denied
356for the directory which the
357.Fa fd
358file descriptor references.
359.El
360.Pp
361.Fn fstat
362will fail if:
363.Bl -tag -width Er
364.It Bq Er EBADF
365.Fa fd
366is not a valid open file descriptor.
367.It Bq Er EFAULT
368.Fa sb
369points to an invalid address.
370.It Bq Er EIO
371An I/O error occurred while reading from the file system.
372.El
373.Sh SEE ALSO
374.Xr chmod 2 ,
375.Xr chown 2 ,
376.Xr clock_gettime 2 ,
377.Xr utimes 2 ,
378.Xr symlink 7
379.Sh STANDARDS
380Previous versions of the system used different types for the
381.Fa st_dev ,
382.Fa st_uid ,
383.Fa st_gid ,
384.Fa st_rdev ,
385.Fa st_size ,
386.Fa st_blksize ,
387and
388.Fa st_blocks
389fields.
390.Pp
391The
392.Fn fstat ,
393.Fn fstatat ,
394.Fn lstat ,
395and
396.Fn stat
397functions are intended to conform to
398.St -p1003.1-2008 .
399.Sh HISTORY
400The
401.Fn stat
402and
403.Fn fstat
404system calls first appeared in
405.At v1 .
406The
407.In sys/stat.h
408header file and the
409.Vt "struct stat"
410were introduced in
411.At v7 .
412.Pp
413An
414.Fn lstat
415function call appeared in
416.Bx 4.2 .
417The
418.Fn fstatat
419function appeared in
420.Ox 5.0 .
421.Sh CAVEATS
422The file generation number,
423.Fa st_gen ,
424is only available to the superuser.
425.Pp
426Certain programs written when the timestamps were just of type
427.Li time_t
428assumed that the members were consecutive (and could therefore
429be treated as an array and have their address passed directly to
430.Xr utime 3 ) .
431The transition to timestamps of type
432.Li struct timespec
433broke them irrevocably.
434.Sh BUGS
435Applying
436.Fn fstat
437to a pipe or socket
438fails to fill in a unique device and inode number pair.
439Applying
440.Fn fstat
441to a socket
442also fails to fill in the time fields.
443