]> git.saurik.com Git - apple/xnu.git/blob - bsd/man/man2/stat.2
xnu-1228.7.58.tar.gz
[apple/xnu.git] / bsd / man / man2 / stat.2
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
40 .Nm fstat ,
41 .Nm fstat64 ,
42 .Nm lstat ,
43 .Nm lstat64 ,
44 .Nm stat ,
45 .Nm stat64
46 .Nd get file status
47 .Sh SYNOPSIS
48 .Fd #include <sys/stat.h>
49 .Ft int
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
59 .Ft int
60 .Fo lstat
61 .Fa "const char *restrict path"
62 .Fa "struct stat *restrict buf"
63 .Fc
64 .Ft int
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
79 .Sh DESCRIPTION
80 The
81 .Fn stat
82 family of functions and their 64 bit variants obtain information about a file. The
83 .Fn stat
84 function obtains information about the file pointed to by
85 .Fa path .
86 Read, write or execute
87 permission of the named file is not required, but all directories
88 listed in the path name leading to the file must be searchable.
89 .Pp
90 .Fn Lstat
91 is like
92 .Fn stat
93 except in the case where the named file is a symbolic link,
94 in which case
95 .Fn lstat
96 returns information about the link,
97 while
98 .Fn stat
99 returns information about the file the link references.
100 Unlike other filesystem objects,
101 symbolic links do not have an owner, group, access mode, times, etc.
102 Instead, these attributes are taken from the directory that
103 contains the link.
104 The only attributes returned from an
105 .Fn lstat
106 that refer to the symbolic link itself are the file type (S_IFLNK),
107 size, blocks, and link count (always 1).
108 .Pp
109 The
110 .Fn fstat
111 obtains the same information about an open file
112 known by the file descriptor
113 .Fa fildes .
114 .Pp
115 The
116 .Fa buf
117 argument is a pointer to a
118 .Fa stat
119 or
120 .Fa stat64
121 structure
122 as defined by
123 .Aq Pa sys/stat.h
124 (both shown below)
125 and into which information is placed concerning the file.
126 .Bd -literal
127 struct 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 };
144
145
146 struct 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
168 .Ed
169 .Pp
170 The time-related fields of
171 .Fa struct stat
172 and
173 .Fa struct stat64
174 are as follows:
175 .Bl -tag -width XXXst_birthtime
176 .It st_atime
177 Time when file data last accessed.
178 Changed by the
179 .Xr mknod 2 ,
180 .Xr utimes 2
181 and
182 .Xr read 2
183 system calls.
184 .It st_mtime
185 Time when file data last modified.
186 Changed by the
187 .Xr mknod 2 ,
188 .Xr utimes 2
189 and
190 .Xr write 2
191 system calls.
192 .It st_ctime
193 Time when file status was last changed (inode data modification).
194 Changed 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
202 and
203 .Xr write 2
204 system calls.
205 .It st_birthtime
206 Time of file creation. Only set once when the file is created. This field is
207 only available in the 64 bit variants. On filesystems where birthtime is
208 not available, this field holds the
209 .Fa ctime
210 instead.
211 .El
212 .Pp
213 The size-related fields of the structures are as follows:
214 .Bl -tag -width XXXst_blksize
215 .It st_blksize
216 The optimal I/O block size for the file.
217 .It st_blocks
218 The actual number of blocks allocated for the file in 512-byte units.
219 As short symbolic links are stored in the inode, this number may
220 be zero.
221 .El
222 .Pp
223 The status information word
224 .Fa st_mode
225 has 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
244 For a list of access modes, see
245 .Aq Pa sys/stat.h ,
246 .Xr access 2
247 and
248 .Xr chmod 2 .
249 .Pp
250 For a list of the file flags in the
251 .Fa st_flags
252 field, see
253 .Aq Pa sys/stat.h
254 and
255 .Xr chflags 2 .
256 .Sh RETURN VALUES
257 Upon successful completion a value of 0 is returned.
258 Otherwise, a value of -1 is returned and
259 .Va errno
260 is set to indicate the error.
261 .Sh COMPATIBILITY
262 Previous 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
269 and
270 .Li st_blocks
271 fields.
272 .Sh ERRORS
273 .Bl -tag -width Er
274 The
275 .Fn fstat
276 system call will fail if:
277 .\" ===========
278 .It Bq Er EBADF
279 .Fa fildes
280 is not a valid open file descriptor.
281 .\" ===========
282 .It Bq Er EFAULT
283 .Fa Sb
284 points to an invalid address.
285 .\" ===========
286 .It Bq Er EIO
287 An I/O error occurs while reading from or writing to the file system.
288 .El
289 .Pp
290 The
291 .Fn lstat
292 and
293 .Fn stat
294 system calls will fail if:
295 .Bl -tag -width Er
296 .\" ===========
297 .It Bq Er EACCES
298 Search permission is denied for a component of the path prefix.
299 .\" ===========
300 .It Bq Er EFAULT
301 .Fa Sb
302 or
303 .Em name
304 points to an invalid address.
305 .\" ===========
306 .It Bq Er EIO
307 An I/O error occurs while reading from or writing to the file system.
308 .\" ===========
309 .It Bq Er ELOOP
310 Too many symbolic links are encountered in translating the pathname.
311 This is taken to be indicative of a looping symbolic link.
312 .\" ===========
313 .It Bq Er ENAMETOOLONG
314 A component of a pathname exceeds
315 .Dv {NAME_MAX}
316 characters, or an entire path name exceeds
317 .Dv {PATH_MAX}
318 characters.
319 .\" ===========
320 .It Bq Er ENOENT
321 The named file does not exist.
322 .\" ===========
323 .It Bq Er ENOTDIR
324 A component of the path prefix is not a directory.
325 .El
326 .Pp
327 The
328 .Fn fstat ,
329 .Fn lstat ,
330 and
331 .Fn stat
332 system calls will fail if:
333 .Bl -tag -width Er
334 .\" ===========
335 .It Bq Er EOVERFLOW
336 The file size in bytes
337 or the number of blocks allocated to the file
338 or the file serial number cannot be represented correctly
339 in the structure pointed to by
340 .Fa buf .
341 .El
342 .Sh CAVEATS
343 The file generation number,
344 .Fa st_gen ,
345 is only available to the super-user.
346 .br
347 The fields in the stat structure currently marked
348 .Fa st_spare1 ,
349 .Fa st_spare2 ,
350 and
351 .Fa st_spare3
352 are present in preparation for inode time stamps expanding
353 to 64 bits. This, however, can break certain programs that
354 depend on the time stamps being contiguous (in calls to
355 .Xr utimes 2 ) .
356 .Sh LEGACY SYNOPSIS
357 .Fd #include <sys/types.h>
358 .Fd #include <sys/stat.h>
359 .Pp
360 The include file
361 .In sys/types.h
362 is necessary.
363 .Sh SEE ALSO
364 .Xr chflags 2 ,
365 .Xr chmod 2 ,
366 .Xr chown 2 ,
367 .Xr utimes 2 ,
368 .Xr compat 5 ,
369 .Xr symlink 7
370 .Sh BUGS
371 Applying
372 .Xr fstat
373 to a socket (and thus to a pipe)
374 returns a zero'd buffer,
375 except for the blocksize field,
376 and a unique device and inode number.
377 .Sh STANDARDS
378 The
379 .Fn stat
380 and
381 .Fn fstat
382 function calls are expected to conform to
383 .St -p1003.1-88 .
384 .Sh HISTORY
385 An
386 .Fn lstat
387 function call appeared in
388 .Bx 4.2 .
389 The
390 .Fn stat64 ,
391 .Fn fstat64 ,
392 and
393 .Fn lstat64
394 system calls first appeared in Mac OS X 10.5 (Leopard).