]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libc/sys/stat.2
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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 April 10, 2008
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/types.h
44 .In sys/stat.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 fstat "int fd" "struct stat *sb"
51 .Ft int
52 .Fn fstatat "int fd" "const char *path" "struct stat *buf" "int flag"
53 .Sh DESCRIPTION
54 The
55 .Fn stat
56 system call obtains information about the file pointed to by
57 .Fa path .
58 Read, write or execute
59 permission of the named file is not required, but all directories
60 listed in the path name leading to the file must be searchable.
61 .Pp
62 The
63 .Fn lstat
64 system call is like
65 .Fn stat
66 except in the case where the named file is a symbolic link,
67 in which case
68 .Fn lstat
69 returns information about the link,
70 while
71 .Fn stat
72 returns information about the file the link references.
73 .Pp
74 The
75 .Fn fstat
76 system call obtains the same information about an open file
77 known by the file descriptor
78 .Fa fd .
79 .Pp
80 The
81 .Fn fstatat
82 system call is equivalent to
83 .Fn stat
84 and
85 .Fn lstat
86 except in the case where the
87 .Fa path
88 specifies a relative path.
89 In this case the status is retrieved from a file relative to
90 the directory associated with the file descriptor
91 .Fa fd
92 instead of the current working directory.
93 .Pp
94 The values for the
95 .Fa flag
96 are constructed by a bitwise-inclusive OR of flags from the following list,
97 defined in
98 .In fcntl.h :
99 .Bl -tag -width indent
100 .It Dv AT_SYMLINK_NOFOLLOW
101 If
102 .Fa path
103 names a symbolic link, the status of the symbolic link is returned.
104 .El
105 .Pp
106 If
107 .Fn fstatat
108 is passed the special value
109 .Dv AT_FDCWD
110 in the
111 .Fa fd
112 parameter, the current working directory is used and the behavior is
113 identical to a call to
114 .Fn stat
115 or
116 .Fn lstat
117 respectively, depending on whether or not the
118 .Dv AT_SYMLINK_NOFOLLOW
119 bit is set in
120 .Fa flag .
121 .Pp
122 The
123 .Fa sb
124 argument is a pointer to a
125 .Vt stat
126 structure
127 as defined by
128 .In sys/stat.h
129 and into which information is placed concerning the file.
130 .Pp
131 The fields of
132 .Vt "struct stat"
133 related to the file system are as follows:
134 .Bl -tag -width ".Va st_nlink"
135 .It Va st_dev
136 The numeric ID of the device containing the file.
137 .It Va st_ino
138 The file's inode number.
139 .It Va st_nlink
140 The number of hard links to the file.
141 .El
142 .Pp
143 The
144 .Va st_dev
145 and
146 .Va st_ino
147 fields together identify the file uniquely within the system.
148 .Pp
149 The time-related fields of
150 .Vt "struct stat"
151 are as follows:
152 .Bl -tag -width ".Va st_birthtime"
153 .It Va st_atime
154 Time when file data last accessed.
155 Changed by the
156 .Xr mknod 2 ,
157 .Xr utimes 2 ,
158 .Xr read 2
159 and
160 .Xr readv 2
161 system calls.
162 .It Va st_mtime
163 Time when file data last modified.
164 Changed by the
165 .Xr mkdir 2 ,
166 .Xr mkfifo 2 ,
167 .Xr mknod 2 ,
168 .Xr utimes 2 ,
169 .Xr write 2
170 and
171 .Xr writev 2
172 system calls.
173 .It Va st_ctime
174 Time when file status was last changed (inode data modification).
175 Changed by the
176 .Xr chflags 2 ,
177 .Xr chmod 2 ,
178 .Xr chown 2 ,
179 .Xr creat 2 ,
180 .Xr link 2 ,
181 .Xr mkdir 2 ,
182 .Xr mkfifo 2 ,
183 .Xr mknod 2 ,
184 .Xr rename 2 ,
185 .Xr rmdir 2 ,
186 .Xr symlink 2 ,
187 .Xr truncate 2 ,
188 .Xr unlink 2 ,
189 .Xr utimes 2 ,
190 .Xr write 2
191 and
192 .Xr writev 2
193 system calls.
194 .It Va st_birthtime
195 Time when the inode was created.
196 .El
197 .Pp
198 If
199 .Dv _POSIX_SOURCE
200 is not defined, the time-related fields are defined as:
201 .Bd -literal
202 #ifndef _POSIX_SOURCE
203 #define st_atime st_atimespec.tv_sec
204 #define st_mtime st_mtimespec.tv_sec
205 #define st_ctime st_ctimespec.tv_sec
206 #endif
207 .Ed
208 .Pp
209 The size-related fields of the
210 .Vt "struct stat"
211 are as follows:
212 .Bl -tag -width ".Va st_blksize"
213 .It Va st_size
214 The file size in bytes.
215 .It Va st_blksize
216 The optimal I/O block size for the file.
217 .It Va 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 access-related fields of
224 .Vt "struct stat"
225 are as follows:
226 .Bl -tag -width ".Va st_mode"
227 .It Va st_uid
228 The user ID of the file's owner.
229 .It Va st_gid
230 The group ID of the file.
231 .It Va st_mode
232 Status of the file (see below).
233 .El
234 .Pp
235 The status information word
236 .Fa st_mode
237 has the following bits:
238 .Bd -literal
239 #define S_IFMT   0170000  /* type of file mask */
240 #define S_IFIFO  0010000  /* named pipe (fifo) */
241 #define S_IFCHR  0020000  /* character special */
242 #define S_IFDIR  0040000  /* directory */
243 #define S_IFBLK  0060000  /* block special */
244 #define S_IFREG  0100000  /* regular */
245 #define S_IFLNK  0120000  /* symbolic link */
246 #define S_IFSOCK 0140000  /* socket */
247 #define S_IFWHT  0160000  /* whiteout */
248 #define S_ISUID  0004000  /* set user id on execution */
249 #define S_ISGID  0002000  /* set group id on execution */
250 #define S_ISVTX  0001000  /* save swapped text even after use */
251 #define S_IRWXU  0000700  /* RWX mask for owner */
252 #define S_IRUSR  0000400  /* read permission, owner */
253 #define S_IWUSR  0000200  /* write permission, owner */
254 #define S_IXUSR  0000100  /* execute/search permission, owner */
255 #define S_IRWXG  0000070  /* RWX mask for group */
256 #define S_IRGRP  0000040  /* read permission, group */
257 #define S_IWGRP  0000020  /* write permission, group */
258 #define S_IXGRP  0000010  /* execute/search permission, group */
259 #define S_IRWXO  0000007  /* RWX mask for other */
260 #define S_IROTH  0000004  /* read permission, other */
261 #define S_IWOTH  0000002  /* write permission, other */
262 #define S_IXOTH  0000001  /* execute/search permission, other */
263 .Ed
264 .Pp
265 For a list of access modes, see
266 .In sys/stat.h ,
267 .Xr access 2
268 and
269 .Xr chmod 2 .
270 The following macros are available to test whether a
271 .Va st_mode
272 value passed in the
273 .Fa m
274 argument corresponds to a file of the specified type:
275 .Bl -tag -width ".Fn S_ISFIFO m"
276 .It Fn S_ISBLK m
277 Test for a block special file.
278 .It Fn S_ISCHR m
279 Test for a character special file.
280 .It Fn S_ISDIR m
281 Test for a directory.
282 .It Fn S_ISFIFO m
283 Test for a pipe or FIFO special file.
284 .It Fn S_ISLNK m
285 Test for a symbolic link.
286 .It Fn S_ISREG m
287 Test for a regular file.
288 .It Fn S_ISSOCK m
289 Test for a socket.
290 .It Fn S_ISWHT m
291 Test for a whiteout.
292 .El
293 .Pp
294 The macros evaluate to a non-zero value if the test is true
295 or to the value 0 if the test is false.
296 .Sh RETURN VALUES
297 .Rv -std
298 .Sh COMPATIBILITY
299 Previous versions of the system used different types for the
300 .Va st_dev ,
301 .Va st_uid ,
302 .Va st_gid ,
303 .Va st_rdev ,
304 .Va st_size ,
305 .Va st_blksize
306 and
307 .Va st_blocks
308 fields.
309 .Sh ERRORS
310 The
311 .Fn stat
312 and
313 .Fn lstat
314 system calls will fail if:
315 .Bl -tag -width Er
316 .It Bq Er EACCES
317 Search permission is denied for a component of the path prefix.
318 .It Bq Er EFAULT
319 The
320 .Fa sb
321 or
322 .Fa path
323 argument
324 points to an invalid address.
325 .It Bq Er EIO
326 An I/O error occurred while reading from or writing to the file system.
327 .It Bq Er ELOOP
328 Too many symbolic links were encountered in translating the pathname.
329 .It Bq Er ENAMETOOLONG
330 A component of a pathname exceeded 255 characters,
331 or an entire path name exceeded 1023 characters.
332 .It Bq Er ENOENT
333 The named file does not exist.
334 .It Bq Er ENOTDIR
335 A component of the path prefix is not a directory.
336 .It Bq Er EOVERFLOW
337 The file size in bytes cannot be
338 represented correctly in the structure pointed to by
339 .Fa sb .
340 .El
341 .Pp
342 .Bl -tag -width Er
343 The
344 .Fn fstat
345 system call will fail if:
346 .It Bq Er EBADF
347 The
348 .Fa fd
349 argument
350 is not a valid open file descriptor.
351 .It Bq Er EFAULT
352 The
353 .Fa sb
354 argument
355 points to an invalid address.
356 .It Bq Er EIO
357 An I/O error occurred while reading from or writing to the file system.
358 .It Bq Er EOVERFLOW
359 The file size in bytes cannot be
360 represented correctly in the structure pointed to by
361 .Fa sb .
362 .El
363 .Pp
364 In addition to the errors returned by the
365 .Fn lstat ,
366 the
367 .Fn fstatat
368 may fail if:
369 .Bl -tag -width Er
370 .It Bq Er EBADF
371 The
372 .Fa path
373 argument does not specify an absolute path and the
374 .Fa fd
375 argument is neither
376 .Dv AT_FDCWD
377 nor a valid file descriptor open for searching.
378 .It Bq Er EINVAL
379 The value of the
380 .Fa flag
381 argument is not valid.
382 .It Bq Er ENOTDIR
383 The
384 .Fa path
385 argument is not an absolute path and
386 .Fa fd
387 is neither
388 .Dv AT_FDCWD
389 nor a file descriptor associated with a directory.
390 .El
391 .Sh SEE ALSO
392 .Xr access 2 ,
393 .Xr chmod 2 ,
394 .Xr chown 2 ,
395 .Xr fhstat 2 ,
396 .Xr statfs 2 ,
397 .Xr utimes 2 ,
398 .Xr symlink 7 ,
399 .Xr sticky 8
400 .Sh STANDARDS
401 The
402 .Fn stat
403 and
404 .Fn fstat
405 system calls are expected to conform to
406 .St -p1003.1-90 .
407 The
408 .Fn fchownat
409 system call follows The Open Group Extended API Set 2 specification.
410 .Sh HISTORY
411 The
412 .Fn stat
413 and
414 .Fn fstat
415 system calls appeared in
416 .At v7 .
417 The
418 .Fn lstat
419 system call appeared in
420 .Bx 4.2 .
421 The
422 .Fn fstatat
423 system call appeared in
424 .Fx 8.0 .
425 .Sh BUGS
426 Applying
427 .Fn fstat
428 to a socket (and thus to a pipe)
429 returns a zeroed buffer,
430 except for the blocksize field,
431 and a unique device and inode number.