]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/stat.2
xnu-1228.7.58.tar.gz
[apple/xnu.git] / bsd / man / man2 / stat.2
CommitLineData
9bccf70c
A
1.\" $OpenBSD: stat.2,v 1.3 1997/02/13 05:20:55 millert 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 BSD 4
39.Sh NAME
2d21ac55
A
40.Nm fstat ,
41.Nm fstat64 ,
9bccf70c 42.Nm lstat ,
2d21ac55
A
43.Nm lstat64 ,
44.Nm stat ,
45.Nm stat64
9bccf70c
A
46.Nd get file status
47.Sh SYNOPSIS
9bccf70c
A
48.Fd #include <sys/stat.h>
49.Ft int
2d21ac55
A
50.Fo fstat
51.Fa "int fildes"
52.Fa "struct stat *buf"
53.Fc
54.Ft int
55.Fo fstat64
56.Fa "int fildes"
57.Fa "struct stat64 *buf"
58.Fc
9bccf70c 59.Ft int
2d21ac55
A
60.Fo lstat
61.Fa "const char *restrict path"
62.Fa "struct stat *restrict buf"
63.Fc
9bccf70c 64.Ft int
2d21ac55
A
65.Fo lstat64
66.Fa "const char *restrict path"
67.Fa "struct stat64 *restrict buf"
68.Fc
69.Ft int
70.Fo stat
71.Fa "const char *restrict path"
72.Fa "struct stat *restrict buf"
73.Fc
74.Ft int
75.Fo stat64
76.Fa "const char *restrict path"
77.Fa "struct stat64 *restrict buf"
78.Fc
9bccf70c
A
79.Sh DESCRIPTION
80The
81.Fn stat
2d21ac55
A
82family of functions and their 64 bit variants obtain information about a file. The
83.Fn stat
9bccf70c
A
84function obtains information about the file pointed to by
85.Fa path .
86Read, write or execute
87permission of the named file is not required, but all directories
88listed in the path name leading to the file must be searchable.
89.Pp
90.Fn Lstat
91is like
92.Fn stat
93except in the case where the named file is a symbolic link,
94in which case
95.Fn lstat
96returns information about the link,
97while
98.Fn stat
99returns information about the file the link references.
100Unlike other filesystem objects,
101symbolic links do not have an owner, group, access mode, times, etc.
102Instead, these attributes are taken from the directory that
103contains the link.
104The only attributes returned from an
105.Fn lstat
106that refer to the symbolic link itself are the file type (S_IFLNK),
107size, blocks, and link count (always 1).
108.Pp
109The
110.Fn fstat
111obtains the same information about an open file
112known by the file descriptor
2d21ac55 113.Fa fildes .
9bccf70c
A
114.Pp
115The
2d21ac55 116.Fa buf
9bccf70c 117argument is a pointer to a
2d21ac55
A
118.Fa stat
119or
120.Fa stat64
121structure
9bccf70c
A
122as defined by
123.Aq Pa sys/stat.h
2d21ac55 124(both shown below)
9bccf70c
A
125and into which information is placed concerning the file.
126.Bd -literal
127struct stat {
128 dev_t st_dev; /* device inode resides on */
129 ino_t st_ino; /* inode's number */
130 mode_t st_mode; /* inode protection mode */
131 nlink_t st_nlink; /* number or hard links to the file */
132 uid_t st_uid; /* user-id of owner */
133 gid_t st_gid; /* group-id of owner */
134 dev_t st_rdev; /* device type, for special file inode */
135 struct timespec st_atimespec; /* time of last access */
136 struct timespec st_mtimespec; /* time of last data modification */
137 struct timespec st_ctimespec; /* time of last file status change */
138 off_t st_size; /* file size, in bytes */
139 quad_t st_blocks; /* blocks allocated for file */
140 u_long st_blksize;/* optimal file sys I/O ops blocksize */
141 u_long st_flags; /* user defined flags for file */
142 u_long st_gen; /* file generation number */
143};
2d21ac55
A
144
145
146struct stat64 {
147 dev_t st_dev; /* ID of device containing file */
148 mode_t st_mode; /* Mode of file (see below) */
149 nlink_t st_nlink; /* Number of hard links */
150 ino64_t st_ino; /* File serial number */
151 uid_t st_uid; /* User ID of the file */
152 gid_t st_gid; /* Group ID of the file */
153 dev_t st_rdev; /* Device ID */
154 struct timespec st_atimespec; /* time of last access */
155 struct timespec st_mtimespec; /* time of last data modification */
156 struct timespec st_ctimespec; /* time of last status change */
157 struct timespec st_birthtimespec; /* time of file creation(birth) */
158 off_t st_size; /* file size, in bytes */
159 blkcnt_t st_blocks; /* blocks allocated for file */
160 blksize_t st_blksize; /* optimal blocksize for I/O */
161 uint32_t st_flags; /* user defined flags for file */
162 uint32_t st_gen; /* file generation number */
163 int32_t st_lspare; /* RESERVED: DO NOT USE! */
164 int64_t st_qspare[2]; /* RESERVED: DO NOT USE! */
165};
166
167
9bccf70c
A
168.Ed
169.Pp
170The time-related fields of
171.Fa struct stat
2d21ac55
A
172and
173.Fa struct stat64
9bccf70c 174are as follows:
2d21ac55 175.Bl -tag -width XXXst_birthtime
9bccf70c
A
176.It st_atime
177Time when file data last accessed.
178Changed by the
179.Xr mknod 2 ,
180.Xr utimes 2
181and
182.Xr read 2
183system calls.
184.It st_mtime
185Time when file data last modified.
186Changed by the
187.Xr mknod 2 ,
188.Xr utimes 2
189and
190.Xr write 2
191system calls.
192.It st_ctime
193Time when file status was last changed (inode data modification).
194Changed by the
195.Xr chmod 2 ,
196.Xr chown 2 ,
197.Xr link 2 ,
198.Xr mknod 2 ,
199.Xr rename 2 ,
200.Xr unlink 2 ,
201.Xr utimes 2
202and
203.Xr write 2
204system calls.
2d21ac55
A
205.It st_birthtime
206Time of file creation. Only set once when the file is created. This field is
207only available in the 64 bit variants. On filesystems where birthtime is
208not available, this field holds the
209.Fa ctime
210instead.
9bccf70c
A
211.El
212.Pp
2d21ac55 213The size-related fields of the structures are as follows:
9bccf70c
A
214.Bl -tag -width XXXst_blksize
215.It st_blksize
216The optimal I/O block size for the file.
217.It st_blocks
218The actual number of blocks allocated for the file in 512-byte units.
219As short symbolic links are stored in the inode, this number may
220be zero.
221.El
222.Pp
223The status information word
224.Fa st_mode
225has the following bits:
226.Bd -literal
227#define S_IFMT 0170000 /* type of file */
228#define S_IFIFO 0010000 /* named pipe (fifo) */
229#define S_IFCHR 0020000 /* character special */
230#define S_IFDIR 0040000 /* directory */
231#define S_IFBLK 0060000 /* block special */
232#define S_IFREG 0100000 /* regular */
233#define S_IFLNK 0120000 /* symbolic link */
234#define S_IFSOCK 0140000 /* socket */
235#define S_IFWHT 0160000 /* whiteout */
236#define S_ISUID 0004000 /* set user id on execution */
237#define S_ISGID 0002000 /* set group id on execution */
238#define S_ISVTX 0001000 /* save swapped text even after use */
239#define S_IRUSR 0000400 /* read permission, owner */
240#define S_IWUSR 0000200 /* write permission, owner */
241#define S_IXUSR 0000100 /* execute/search permission, owner */
242.Ed
243.Pp
244For a list of access modes, see
245.Aq Pa sys/stat.h ,
246.Xr access 2
247and
248.Xr chmod 2 .
2d21ac55
A
249.Pp
250For a list of the file flags in the
251.Fa st_flags
252field, see
253.Aq Pa sys/stat.h
254and
255.Xr chflags 2 .
9bccf70c
A
256.Sh RETURN VALUES
257Upon successful completion a value of 0 is returned.
258Otherwise, a value of -1 is returned and
259.Va errno
260is set to indicate the error.
261.Sh COMPATIBILITY
262Previous versions of the system used different types for the
263.Li st_dev ,
264.Li st_uid ,
265.Li st_gid ,
266.Li st_rdev ,
267.Li st_size ,
268.Li st_blksize
269and
270.Li st_blocks
271fields.
272.Sh ERRORS
2d21ac55
A
273.Bl -tag -width Er
274The
275.Fn fstat
276system call will fail if:
277.\" ===========
278.It Bq Er EBADF
279.Fa fildes
280is not a valid open file descriptor.
281.\" ===========
282.It Bq Er EFAULT
283.Fa Sb
284points to an invalid address.
285.\" ===========
286.It Bq Er EIO
287An I/O error occurs while reading from or writing to the file system.
288.El
289.Pp
290The
9bccf70c 291.Fn lstat
2d21ac55
A
292and
293.Fn stat
294system calls will fail if:
9bccf70c 295.Bl -tag -width Er
2d21ac55 296.\" ===========
9bccf70c
A
297.It Bq Er EACCES
298Search permission is denied for a component of the path prefix.
2d21ac55 299.\" ===========
9bccf70c
A
300.It Bq Er EFAULT
301.Fa Sb
302or
303.Em name
304points to an invalid address.
2d21ac55 305.\" ===========
9bccf70c 306.It Bq Er EIO
2d21ac55
A
307An I/O error occurs while reading from or writing to the file system.
308.\" ===========
309.It Bq Er ELOOP
310Too many symbolic links are encountered in translating the pathname.
311This is taken to be indicative of a looping symbolic link.
312.\" ===========
313.It Bq Er ENAMETOOLONG
314A component of a pathname exceeds
315.Dv {NAME_MAX}
316characters, or an entire path name exceeds
317.Dv {PATH_MAX}
318characters.
319.\" ===========
320.It Bq Er ENOENT
321The named file does not exist.
322.\" ===========
323.It Bq Er ENOTDIR
324A component of the path prefix is not a directory.
9bccf70c
A
325.El
326.Pp
2d21ac55
A
327The
328.Fn fstat ,
329.Fn lstat ,
330and
331.Fn stat
332system calls will fail if:
9bccf70c 333.Bl -tag -width Er
2d21ac55
A
334.\" ===========
335.It Bq Er EOVERFLOW
336The file size in bytes
337or the number of blocks allocated to the file
338or the file serial number cannot be represented correctly
339in the structure pointed to by
340.Fa buf .
9bccf70c
A
341.El
342.Sh CAVEATS
343The file generation number,
344.Fa st_gen ,
345is only available to the super-user.
346.br
347The fields in the stat structure currently marked
348.Fa st_spare1 ,
349.Fa st_spare2 ,
350and
351.Fa st_spare3
352are present in preparation for inode time stamps expanding
353to 64 bits. This, however, can break certain programs that
354depend on the time stamps being contiguous (in calls to
355.Xr utimes 2 ) .
2d21ac55
A
356.Sh LEGACY SYNOPSIS
357.Fd #include <sys/types.h>
358.Fd #include <sys/stat.h>
359.Pp
360The include file
361.In sys/types.h
362is necessary.
9bccf70c 363.Sh SEE ALSO
2d21ac55 364.Xr chflags 2 ,
9bccf70c
A
365.Xr chmod 2 ,
366.Xr chown 2 ,
2d21ac55
A
367.Xr utimes 2 ,
368.Xr compat 5 ,
9bccf70c
A
369.Xr symlink 7
370.Sh BUGS
371Applying
372.Xr fstat
373to a socket (and thus to a pipe)
374returns a zero'd buffer,
375except for the blocksize field,
376and a unique device and inode number.
377.Sh STANDARDS
378The
379.Fn stat
380and
381.Fn fstat
382function calls are expected to conform to
383.St -p1003.1-88 .
384.Sh HISTORY
385An
386.Fn lstat
387function call appeared in
388.Bx 4.2 .
2d21ac55
A
389The
390.Fn stat64 ,
391.Fn fstat64 ,
392and
393.Fn lstat64
394system calls first appeared in Mac OS X 10.5 (Leopard).