Merge branch 'vendor/GCC47'
[dragonfly.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 .\" 3. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     @(#)stat.2      8.4 (Berkeley) 5/1/95
33 .\" $FreeBSD: src/lib/libc/sys/stat.2,v 1.16.2.7 2001/12/14 18:34:01 ru Exp $
34 .\" $DragonFly: src/lib/libc/sys/stat.2,v 1.6 2008/09/28 16:33:35 swildner Exp $
35 .\"
36 .Dd August 1, 2009
37 .Dt STAT 2
38 .Os
39 .Sh NAME
40 .Nm stat ,
41 .Nm lstat ,
42 .Nm fstat ,
43 .Nm fstatat
44 .Nd get file status
45 .Sh LIBRARY
46 .Lb libc
47 .Sh SYNOPSIS
48 .In sys/types.h
49 .In sys/stat.h
50 .Ft int
51 .Fn stat "const char *path" "struct stat *sb"
52 .Ft int
53 .Fn lstat "const char *path" "struct stat *sb"
54 .Ft int
55 .Fn fstat "int fd" "struct stat *sb"
56 .Ft int
57 .Fn fstatat "int fd" "const char *path" "struct stat *buf" "int flag"
58 .Sh DESCRIPTION
59 The
60 .Fn stat
61 system call obtains information about the file pointed to by
62 .Fa path .
63 Read, write or execute
64 permission of the named file is not required, but all directories
65 listed in the path name leading to the file must be searchable.
66 .Pp
67 .Fn Lstat
68 is like
69 .Fn stat
70 except in the case where the named file is a symbolic link,
71 in which case
72 .Fn lstat
73 returns information about the link,
74 while
75 .Fn stat
76 returns information about the file the link references.
77 .Pp
78 The
79 .Fn fstat
80 system call obtains the same information about an open file
81 known by the file descriptor
82 .Fa fd .
83 .Pp
84 The
85 .Fn fstatat
86 system call is equivalent to
87 .Fn stat
88 and
89 .Fn lstat
90 except in the case where the
91 .Fa path
92 specifies a relative path.
93 In this case the status is retrieved from a file relative to
94 the directory associated with the file descriptor
95 .Fa fd
96 instead of the current working directory.
97 .Pp
98 The values for the
99 .Fa flag
100 are constructed by a bitwise-inclusive OR of flags from the following list,
101 defined in
102 .In fcntl.h :
103 .Bl -tag -width indent
104 .It Dv AT_SYMLINK_NOFOLLOW
105 If
106 .Fa path
107 names a symbolic link, the status of the symbolic link is returned.
108 .El
109 .Pp
110 If
111 .Fn fstatat
112 is passed the special value
113 .Dv AT_FDCWD
114 in the
115 .Fa fd
116 parameter, the current working directory is used and the behavior is
117 identical to a call to
118 .Fn stat
119 or
120 .Fn lstat
121 respectively, depending on whether or not the
122 .Dv AT_SYMLINK_NOFOLLOW
123 bit is set in
124 .Fa flag .
125 .Pp
126 The
127 .Fa sb
128 argument is a pointer to a
129 .Vt stat
130 structure
131 as defined by
132 .In sys/stat.h
133 (shown below)
134 and into which information is placed concerning the file.
135 .Bd -literal
136 struct stat {
137     dev_t     st_dev;               /* inode's device */
138     ino_t     st_ino;               /* inode's number */
139     mode_t    st_mode;              /* inode protection mode */
140     nlink_t   st_nlink;             /* number of hard links */
141     uid_t     st_uid;               /* user ID of the file's owner */
142     gid_t     st_gid;               /* group ID of the file's group */
143     dev_t     st_rdev;              /* device type */
144 #ifndef _POSIX_SOURCE
145     struct timespec st_atimespec;  /* time of last access */
146     struct timespec st_mtimespec;  /* time of last data modification */
147     struct timespec st_ctimespec;  /* time of last file status change */
148 #else
149     time_t    st_atime;             /* time of last access */
150     long      st_atimensec;         /* nsec of last access */
151     time_t    st_mtime;             /* time of last data modification */
152     long      st_mtimensec;         /* nsec of last data modification */
153     time_t    st_ctime;             /* time of last file status change */
154     long      st_ctimensec;         /* nsec of last file status change */
155 #endif
156     off_t     st_size;              /* file size, in bytes */
157     int64_t   st_blocks;            /* blocks allocated for file */
158     u_int32_t st_blksize;           /* optimal blocksize for I/O */
159     u_int32_t st_flags;             /* user defined flags for file */
160     u_int32_t st_gen;               /* file generation number */
161 };
162 .Ed
163 .Pp
164 The time-related fields of
165 .Vt struct stat
166 are as follows:
167 .Bl -tag -width ".Fa st_mtime"
168 .It Fa st_atime
169 Time when file data last accessed.
170 Changed by the
171 .Xr execve 2 ,
172 .Xr mknod 2 ,
173 .Xr mmap 2 ,
174 .Xr read 2
175 and
176 .Xr utimes 2
177 system calls.
178 .It Fa st_mtime
179 Time when file data last modified.
180 Changed by the
181 .Xr mknod 2 ,
182 .Xr utimes 2
183 and
184 .Xr write 2
185 system calls.
186 .It Fa st_ctime
187 Time when file status was last changed (inode data modification).
188 Changed by the
189 .Xr chmod 2 ,
190 .Xr chown 2 ,
191 .Xr link 2 ,
192 .Xr mknod 2 ,
193 .Xr rename 2 ,
194 .Xr unlink 2 ,
195 .Xr utimes 2
196 and
197 .Xr write 2
198 system calls.
199 .El
200 .Pp
201 If
202 .Dv _POSIX_SOURCE
203 is not defined, the time-related fields are defined as:
204 .Bd -literal
205 #ifndef _POSIX_SOURCE
206 #define st_atime st_atimespec.tv_sec
207 #define st_mtime st_mtimespec.tv_sec
208 #define st_ctime st_ctimespec.tv_sec
209 #endif
210 .Ed
211 .Pp
212 The size-related fields of the
213 .Vt struct stat
214 are as follows:
215 .Bl -tag -width ".Fa st_blksize"
216 .It Fa st_blksize
217 The optimal I/O block size for the file.
218 .It Fa st_blocks
219 The actual number of blocks allocated for the file in 512-byte units.
220 As short symbolic links are stored in the inode, this number may
221 be zero.
222 .El
223 .Pp
224 The status information word
225 .Fa st_mode
226 has the following bits:
227 .Bd -literal
228 #define S_IFMT 0170000           /* type of file */
229 #define        S_IFIFO  0010000  /* named pipe (fifo) */
230 #define        S_IFCHR  0020000  /* character special */
231 #define        S_IFDIR  0040000  /* directory */
232 #define        S_IFBLK  0060000  /* block special */
233 #define        S_IFREG  0100000  /* regular */
234 #define        S_IFLNK  0120000  /* symbolic link */
235 #define        S_IFSOCK 0140000  /* socket */
236 #define        S_IFWHT  0160000  /* whiteout */
237 #define S_ISUID 0004000  /* set user id on execution */
238 #define S_ISGID 0002000  /* set group id on execution */
239 #define S_ISVTX 0001000  /* save swapped text even after use */
240 #define S_IRUSR 0000400  /* read permission, owner */
241 #define S_IWUSR 0000200  /* write permission, owner */
242 #define S_IXUSR 0000100  /* execute/search permission, owner */
243 .Ed
244 .Pp
245 For a list of access modes, see
246 .In sys/stat.h ,
247 .Xr access 2
248 and
249 .Xr chmod 2 .
250 .Sh RETURN VALUES
251 .Rv -std
252 .Sh COMPATIBILITY
253 Previous versions of the system used different types for the
254 .Fa st_dev ,
255 .Fa st_uid ,
256 .Fa st_gid ,
257 .Fa st_rdev ,
258 .Fa st_size ,
259 .Fa st_blksize
260 and
261 .Fa st_blocks
262 fields.
263 .Sh ERRORS
264 .Fn Stat
265 and
266 .Fn lstat
267 will fail if:
268 .Bl -tag -width Er
269 .It Bq Er EACCES
270 Search permission is denied for a component of the path prefix.
271 .It Bq Er EIO
272 An I/O error occurred while reading from or writing to the file system.
273 .It Bq Er ELOOP
274 Too many symbolic links were encountered in translating the pathname.
275 .It Bq Er ENAMETOOLONG
276 A component of a pathname exceeded 255 characters,
277 or an entire path name exceeded 1023 characters.
278 .It Bq Er ENOENT
279 The named file does not exist.
280 .It Bq Er ENOTDIR
281 A component of the path prefix is not a directory.
282 .It Bq Er EFAULT
283 .Fa sb
284 or
285 .Em name
286 points to an invalid address.
287 .El
288 .Pp
289 .Fn Fstat
290 will fail if:
291 .Bl -tag -width Er
292 .It Bq Er EBADF
293 .Fa fd
294 is not a valid open file descriptor.
295 .It Bq Er EFAULT
296 .Fa sb
297 points to an invalid address.
298 .It Bq Er EIO
299 An I/O error occurred while reading from or writing to the file system.
300 .El
301 .Sh SEE ALSO
302 .Xr access 2 ,
303 .Xr chmod 2 ,
304 .Xr chown 2 ,
305 .Xr statfs 2 ,
306 .Xr statvfs 2 ,
307 .Xr utimes 2 ,
308 .Xr symlink 7
309 .Sh STANDARDS
310 The
311 .Fn stat
312 and
313 .Fn fstat
314 system calls are expected to conform to
315 .St -p1003.1-90 .
316 .Sh HISTORY
317 A
318 .Fn stat
319 and a
320 .Fn fstat
321 system call appeared in
322 .At v7 .
323 A
324 .Fn lstat
325 system call appeared in
326 .Bx 4.2 .
327 The
328 .Fn fstatat
329 system call appeared in
330 .Dx 2.3 .
331 .Sh BUGS
332 Applying
333 .Fn fstat
334 to a socket (and thus to a pipe)
335 returns a zeroed buffer,
336 except for the blocksize field,
337 and a unique device and inode number.