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