xref: /openbsd-src/lib/libc/sys/stat.2 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\"	$OpenBSD: stat.2,v 1.13 2000/06/29 00:57:02 aaron 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. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)stat.2	8.3 (Berkeley) 4/19/94
35.\"
36.Dd April 19, 1994
37.Dt STAT 2
38.Os
39.Sh NAME
40.Nm stat ,
41.Nm lstat ,
42.Nm fstat
43.Nd get file status
44.Sh SYNOPSIS
45.Fd #include <sys/types.h>
46.Fd #include <sys/stat.h>
47.Ft int
48.Fn stat "const char *path" "struct stat *sb"
49.Ft int
50.Fn lstat "const char *path" "struct stat *sb"
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 (always 1).
79.Pp
80The
81.Fn fstat
82function obtains the same information about an open file
83known by the file descriptor
84.Fa fd .
85.Pp
86The
87.Fa sb
88argument is a pointer to a
89.Fn stat
90structure
91as defined by
92.Aq Pa sys/stat.h
93(shown below)
94and into which information is placed concerning the file.
95.Bd -literal
96struct stat {
97    dev_t      st_dev;    /* device inode resides on */
98    ino_t      st_ino;    /* inode's number */
99    mode_t     st_mode;   /* inode's mode */
100    nlink_t    st_nlink;  /* number of hard links to the file */
101    uid_t      st_uid;    /* user ID of owner */
102    gid_t      st_gid;    /* group ID of owner */
103    dev_t      st_rdev;   /* device type, for special file inode */
104    struct timespec st_atimespec;  /* time of last access */
105    struct timespec st_mtimespec;  /* time of last data modification */
106    struct timespec st_ctimespec;  /* time of last file status change */
107    off_t      st_size;   /* file size, in bytes */
108    int64_t    st_blocks; /* blocks allocated for file */
109    u_int32_t  st_blksize;/* optimal file sys I/O ops blocksize */
110    u_int32_t  st_flags;  /* user defined flags for file */
111    u_int32_t  st_gen;    /* file generation number */
112};
113.Ed
114.Pp
115The time-related fields of
116.Li struct stat
117are as follows:
118.Bl -tag -width XXXst_mtime
119.It Fa st_atime
120Time when file data was last accessed.
121Changed by the
122.Xr mknod 2 ,
123.Xr utimes 2 ,
124and
125.Xr read 2
126system calls.
127.It Fa st_mtime
128Time when file data was last modified.
129Changed by the
130.Xr mknod 2 ,
131.Xr utimes 2 ,
132and
133.Xr write 2
134system calls.
135.It Fa st_ctime
136Time when file status was last changed (inode data modification).
137Changed by the
138.Xr chmod 2 ,
139.Xr chown 2 ,
140.Xr link 2 ,
141.Xr mknod 2 ,
142.Xr rename 2 ,
143.Xr unlink 2 ,
144.Xr utimes 2 ,
145and
146.Xr write 2
147system calls.
148.El
149.Pp
150The size-related fields of the
151.Li struct stat
152are as follows:
153.Bl -tag -width XXXst_blksize
154.It Fa st_blksize
155The optimal I/O block size for the file.
156.It Fa st_blocks
157The actual number of blocks allocated for the file in 512-byte units.
158As short symbolic links are stored in the inode, this number may
159be zero.
160.El
161.Pp
162The status information word
163.Fa st_mode
164has the following bits:
165.Bd -literal
166#define S_IFMT   0170000  /* type of file */
167#define S_IFIFO  0010000  /* named pipe (fifo) */
168#define S_IFCHR  0020000  /* character special */
169#define S_IFDIR  0040000  /* directory */
170#define S_IFBLK  0060000  /* block special */
171#define S_IFREG  0100000  /* regular */
172#define S_IFLNK  0120000  /* symbolic link */
173#define S_IFSOCK 0140000  /* socket */
174#define S_IFWHT  0160000  /* whiteout */
175#define S_ISUID  0004000  /* set-user-ID on execution */
176#define S_ISGID  0002000  /* set-group-ID on execution */
177#define S_ISVTX  0001000  /* save swapped text even after use */
178#define S_IRUSR  0000400  /* read permission, owner */
179#define S_IWUSR  0000200  /* write permission, owner */
180#define S_IXUSR  0000100  /* execute/search permission, owner */
181.Ed
182.Pp
183For a list of access modes, see
184.Aq Pa sys/stat.h ,
185.Xr access 2 ,
186and
187.Xr chmod 2 .
188.Sh RETURN VALUES
189Upon successful completion a value of 0 is returned.
190Otherwise, a value of \-1 is returned and
191.Va errno
192is set to indicate the error.
193.Sh COMPATIBILITY
194Previous versions of the system used different types for the
195.Fa st_dev ,
196.Fa st_uid ,
197.Fa st_gid ,
198.Fa st_rdev ,
199.Fa st_size ,
200.Fa st_blksize ,
201and
202.Fa st_blocks
203fields.
204.Sh ERRORS
205.Fn stat
206and
207.Fn lstat
208will fail if:
209.Bl -tag -width Er
210.It Bq Er ENOTDIR
211A component of the path prefix is not a directory.
212.It Bq Er ENAMETOOLONG
213A component of a pathname exceeded
214.Dv NAME_MAX
215characters, or an entire path name exceeded
216.Dv PATH_MAX
217characters.
218.It Bq Er ENOENT
219The named file does not exist.
220.It Bq Er EACCES
221Search permission is denied for a component of the path prefix.
222.It Bq Er ELOOP
223Too many symbolic links were encountered in translating the pathname.
224.It Bq Er EFAULT
225.Fa sb
226or
227.Em name
228points to an invalid address.
229.It Bq Er EIO
230An I/O error occurred while reading from or writing to the file system.
231.El
232.Pp
233.Bl -tag -width Er
234.Fn fstat
235will fail if:
236.It Bq Er EBADF
237.Fa fd
238is not a valid open file descriptor.
239.It Bq Er EFAULT
240.Fa sb
241points to an invalid address.
242.It Bq Er EIO
243An I/O error occurred while reading from or writing to the file system.
244.El
245.Sh CAVEATS
246The file generation number,
247.Fa st_gen ,
248is only available to the superuser.
249.Pp
250The fields in the stat structure currently marked
251.Fa st_spare1 ,
252.Fa st_spare2 ,
253and
254.Fa st_spare3
255are present in preparation for inode time stamps expanding to 64 bits.
256This, however, can break certain programs that
257depend on the time stamps being contiguous (in calls to
258.Xr utimes 2 ) .
259.Sh SEE ALSO
260.Xr chmod 2 ,
261.Xr chown 2 ,
262.Xr utimes 2 ,
263.Xr symlink 7
264.Sh BUGS
265Applying
266.Fn fstat
267to a socket (and thus to a pipe)
268returns a zero'd buffer,
269except for the blocksize field,
270and a unique device and inode number.
271.Sh STANDARDS
272The
273.Fn stat
274and
275.Fn fstat
276function calls are expected to conform to
277.St -p1003.1-88 .
278.Sh HISTORY
279An
280.Fn lstat
281function call appeared in
282.Bx 4.2 .
283