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