]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/stat.2
MFV r299425:
[FreeBSD/FreeBSD.git] / lib / libc / sys / stat.2
1 .\" Copyright (c) 1980, 1991, 1993, 1994
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 4. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)stat.2      8.4 (Berkeley) 5/1/95
29 .\" $FreeBSD$
30 .\"
31 .Dd January 14, 2016
32 .Dt STAT 2
33 .Os
34 .Sh NAME
35 .Nm stat ,
36 .Nm lstat ,
37 .Nm fstat ,
38 .Nm fstatat
39 .Nd get file status
40 .Sh LIBRARY
41 .Lb libc
42 .Sh SYNOPSIS
43 .In sys/stat.h
44 .Ft int
45 .Fn stat "const char * restrict path" "struct stat * restrict sb"
46 .Ft int
47 .Fn lstat "const char * restrict path" "struct stat * restrict sb"
48 .Ft int
49 .Fn fstat "int fd" "struct stat *sb"
50 .Ft int
51 .Fn fstatat "int fd" "const char *path" "struct stat *buf" "int flag"
52 .Sh DESCRIPTION
53 The
54 .Fn stat
55 system call obtains information about the file pointed to by
56 .Fa path .
57 Read, write or execute
58 permission of the named file is not required, but all directories
59 listed in the path name leading to the file must be searchable.
60 .Pp
61 The
62 .Fn lstat
63 system call is like
64 .Fn stat
65 except in the case where the named file is a symbolic link,
66 in which case
67 .Fn lstat
68 returns information about the link,
69 while
70 .Fn stat
71 returns information about the file the link references.
72 .Pp
73 The
74 .Fn fstat
75 system call obtains the same information about an open file
76 known by the file descriptor
77 .Fa fd .
78 .Pp
79 The
80 .Fn fstatat
81 system call is equivalent to
82 .Fn stat
83 and
84 .Fn lstat
85 except in the case where the
86 .Fa path
87 specifies a relative path.
88 In this case the status is retrieved from a file relative to
89 the directory associated with the file descriptor
90 .Fa fd
91 instead of the current working directory.
92 .Pp
93 The values for the
94 .Fa flag
95 are constructed by a bitwise-inclusive OR of flags from the following list,
96 defined in
97 .In fcntl.h :
98 .Bl -tag -width indent
99 .It Dv AT_SYMLINK_NOFOLLOW
100 If
101 .Fa path
102 names a symbolic link, the status of the symbolic link is returned.
103 .El
104 .Pp
105 If
106 .Fn fstatat
107 is passed the special value
108 .Dv AT_FDCWD
109 in the
110 .Fa fd
111 parameter, the current working directory is used and the behavior is
112 identical to a call to
113 .Fn stat
114 or
115 .Fn lstat
116 respectively, depending on whether or not the
117 .Dv AT_SYMLINK_NOFOLLOW
118 bit is set in
119 .Fa flag .
120 .Pp
121 The
122 .Fa sb
123 argument is a pointer to a
124 .Vt stat
125 structure
126 as defined by
127 .In sys/stat.h
128 and into which information is placed concerning the file.
129 .Pp
130 The fields of
131 .Vt "struct stat"
132 related to the file system are as follows:
133 .Bl -tag -width ".Va st_nlink"
134 .It Va st_dev
135 The numeric ID of the device containing the file.
136 .It Va st_ino
137 The file's inode number.
138 .It Va st_nlink
139 The number of hard links to the file.
140 .El
141 .Pp
142 The
143 .Va st_dev
144 and
145 .Va st_ino
146 fields together identify the file uniquely within the system.
147 .Pp
148 The time-related fields of
149 .Vt "struct stat"
150 are as follows:
151 .Bl -tag -width ".Va st_birthtim"
152 .It Va st_atim
153 Time when file data last accessed.
154 Changed by the
155 .Xr mknod 2 ,
156 .Xr utimes 2 ,
157 .Xr read 2
158 and
159 .Xr readv 2
160 system calls.
161 .It Va st_mtim
162 Time when file data last modified.
163 Changed by the
164 .Xr mkdir 2 ,
165 .Xr mkfifo 2 ,
166 .Xr mknod 2 ,
167 .Xr utimes 2 ,
168 .Xr write 2
169 and
170 .Xr writev 2
171 system calls.
172 .It Va st_ctim
173 Time when file status was last changed (inode data modification).
174 Changed by the
175 .Xr chflags 2 ,
176 .Xr chmod 2 ,
177 .Xr chown 2 ,
178 .Xr creat 2 ,
179 .Xr link 2 ,
180 .Xr mkdir 2 ,
181 .Xr mkfifo 2 ,
182 .Xr mknod 2 ,
183 .Xr rename 2 ,
184 .Xr rmdir 2 ,
185 .Xr symlink 2 ,
186 .Xr truncate 2 ,
187 .Xr unlink 2 ,
188 .Xr utimes 2 ,
189 .Xr write 2
190 and
191 .Xr writev 2
192 system calls.
193 .It Va st_birthtim
194 Time when the inode was created.
195 .El
196 .Pp
197 The following time-related macros are defined for compatibility:
198 .Bd -literal
199 #define st_atime                st_atim.tv_sec
200 #define st_mtime                st_mtim.tv_sec
201 #define st_ctime                st_ctim.tv_sec
202 #ifndef _POSIX_SOURCE
203 #define st_birthtime            st_birthtim.tv_sec
204 #endif
205
206 #ifndef _POSIX_SOURCE
207 #define st_atimespec            st_atim
208 #define st_mtimespec            st_mtim
209 #define st_ctimespec            st_ctim
210 #define st_birthtimespec        st_birthtim
211 #endif
212 .Ed
213 .Pp
214 The size-related fields of the
215 .Vt "struct stat"
216 are as follows:
217 .Bl -tag -width ".Va st_blksize"
218 .It Va st_size
219 The file size in bytes.
220 .It Va st_blksize
221 The optimal I/O block size for the file.
222 .It Va st_blocks
223 The actual number of blocks allocated for the file in 512-byte units.
224 As short symbolic links are stored in the inode, this number may
225 be zero.
226 .El
227 .Pp
228 The access-related fields of
229 .Vt "struct stat"
230 are as follows:
231 .Bl -tag -width ".Va st_mode"
232 .It Va st_uid
233 The user ID of the file's owner.
234 .It Va st_gid
235 The group ID of the file.
236 .It Va st_mode
237 Status of the file (see below).
238 .El
239 .Pp
240 The status information word
241 .Fa st_mode
242 has the following bits:
243 .Bd -literal
244 #define S_IFMT   0170000  /* type of file mask */
245 #define S_IFIFO  0010000  /* named pipe (fifo) */
246 #define S_IFCHR  0020000  /* character special */
247 #define S_IFDIR  0040000  /* directory */
248 #define S_IFBLK  0060000  /* block special */
249 #define S_IFREG  0100000  /* regular */
250 #define S_IFLNK  0120000  /* symbolic link */
251 #define S_IFSOCK 0140000  /* socket */
252 #define S_IFWHT  0160000  /* whiteout */
253 #define S_ISUID  0004000  /* set user id on execution */
254 #define S_ISGID  0002000  /* set group id on execution */
255 #define S_ISVTX  0001000  /* save swapped text even after use */
256 #define S_IRWXU  0000700  /* RWX mask for owner */
257 #define S_IRUSR  0000400  /* read permission, owner */
258 #define S_IWUSR  0000200  /* write permission, owner */
259 #define S_IXUSR  0000100  /* execute/search permission, owner */
260 #define S_IRWXG  0000070  /* RWX mask for group */
261 #define S_IRGRP  0000040  /* read permission, group */
262 #define S_IWGRP  0000020  /* write permission, group */
263 #define S_IXGRP  0000010  /* execute/search permission, group */
264 #define S_IRWXO  0000007  /* RWX mask for other */
265 #define S_IROTH  0000004  /* read permission, other */
266 #define S_IWOTH  0000002  /* write permission, other */
267 #define S_IXOTH  0000001  /* execute/search permission, other */
268 .Ed
269 .Pp
270 For a list of access modes, see
271 .In sys/stat.h ,
272 .Xr access 2
273 and
274 .Xr chmod 2 .
275 The following macros are available to test whether a
276 .Va st_mode
277 value passed in the
278 .Fa m
279 argument corresponds to a file of the specified type:
280 .Bl -tag -width ".Fn S_ISFIFO m"
281 .It Fn S_ISBLK m
282 Test for a block special file.
283 .It Fn S_ISCHR m
284 Test for a character special file.
285 .It Fn S_ISDIR m
286 Test for a directory.
287 .It Fn S_ISFIFO m
288 Test for a pipe or FIFO special file.
289 .It Fn S_ISLNK m
290 Test for a symbolic link.
291 .It Fn S_ISREG m
292 Test for a regular file.
293 .It Fn S_ISSOCK m
294 Test for a socket.
295 .It Fn S_ISWHT m
296 Test for a whiteout.
297 .El
298 .Pp
299 The macros evaluate to a non-zero value if the test is true
300 or to the value 0 if the test is false.
301 .Sh RETURN VALUES
302 .Rv -std
303 .Sh COMPATIBILITY
304 Previous versions of the system used different types for the
305 .Va st_dev ,
306 .Va st_uid ,
307 .Va st_gid ,
308 .Va st_rdev ,
309 .Va st_size ,
310 .Va st_blksize
311 and
312 .Va st_blocks
313 fields.
314 .Sh ERRORS
315 The
316 .Fn stat
317 and
318 .Fn lstat
319 system calls will fail if:
320 .Bl -tag -width Er
321 .It Bq Er EACCES
322 Search permission is denied for a component of the path prefix.
323 .It Bq Er EFAULT
324 The
325 .Fa sb
326 or
327 .Fa path
328 argument
329 points to an invalid address.
330 .It Bq Er EIO
331 An I/O error occurred while reading from or writing to the file system.
332 .It Bq Er ELOOP
333 Too many symbolic links were encountered in translating the pathname.
334 .It Bq Er ENAMETOOLONG
335 A component of a pathname exceeded 255 characters,
336 or an entire path name exceeded 1023 characters.
337 .It Bq Er ENOENT
338 The named file does not exist.
339 .It Bq Er ENOTDIR
340 A component of the path prefix is not a directory.
341 .It Bq Er EOVERFLOW
342 The file size in bytes cannot be
343 represented correctly in the structure pointed to by
344 .Fa sb .
345 .El
346 .Pp
347 The
348 .Fn fstat
349 system call will fail if:
350 .Bl -tag -width Er
351 .It Bq Er EBADF
352 The
353 .Fa fd
354 argument
355 is not a valid open file descriptor.
356 .It Bq Er EFAULT
357 The
358 .Fa sb
359 argument
360 points to an invalid address.
361 .It Bq Er EIO
362 An I/O error occurred while reading from or writing to the file system.
363 .It Bq Er EOVERFLOW
364 The file size in bytes cannot be
365 represented correctly in the structure pointed to by
366 .Fa sb .
367 .El
368 .Pp
369 In addition to the errors returned by the
370 .Fn lstat ,
371 the
372 .Fn fstatat
373 may fail if:
374 .Bl -tag -width Er
375 .It Bq Er EBADF
376 The
377 .Fa path
378 argument does not specify an absolute path and the
379 .Fa fd
380 argument is neither
381 .Dv AT_FDCWD
382 nor a valid file descriptor open for searching.
383 .It Bq Er EINVAL
384 The value of the
385 .Fa flag
386 argument is not valid.
387 .It Bq Er ENOTDIR
388 The
389 .Fa path
390 argument is not an absolute path and
391 .Fa fd
392 is neither
393 .Dv AT_FDCWD
394 nor a file descriptor associated with a directory.
395 .El
396 .Sh SEE ALSO
397 .Xr access 2 ,
398 .Xr chmod 2 ,
399 .Xr chown 2 ,
400 .Xr fhstat 2 ,
401 .Xr statfs 2 ,
402 .Xr utimes 2 ,
403 .Xr sticky 7 ,
404 .Xr symlink 7
405 .Sh STANDARDS
406 The
407 .Fn stat
408 and
409 .Fn fstat
410 system calls are expected to conform to
411 .St -p1003.1-90 .
412 The
413 .Fn fstatat
414 system call follows The Open Group Extended API Set 2 specification.
415 .Sh HISTORY
416 The
417 .Fn stat
418 and
419 .Fn fstat
420 system calls appeared in
421 .At v7 .
422 The
423 .Fn lstat
424 system call appeared in
425 .Bx 4.2 .
426 The
427 .Fn fstatat
428 system call appeared in
429 .Fx 8.0 .
430 .Sh BUGS
431 Applying
432 .Fn fstat
433 to a socket
434 returns a zeroed buffer,
435 except for the blocksize field,
436 and a unique device and inode number.