Initial import from FreeBSD RELENG_4:
[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 .\"
35 .Dd May 1, 1995
36 .Dt STAT 2
37 .Os
38 .Sh NAME
39 .Nm stat ,
40 .Nm lstat ,
41 .Nm fstat
42 .Nd get file status
43 .Sh LIBRARY
44 .Lb libc
45 .Sh SYNOPSIS
46 .In sys/types.h
47 .In sys/stat.h
48 .Ft int
49 .Fn stat "const char *path" "struct stat *sb"
50 .Ft int
51 .Fn lstat "const char *path" "struct stat *sb"
52 .Ft int
53 .Fn fstat "int fd" "struct stat *sb"
54 .Sh DESCRIPTION
55 The
56 .Fn stat
57 function obtains information about the file pointed to by
58 .Fa path .
59 Read, write or execute
60 permission of the named file is not required, but all directories
61 listed in the path name leading to the file must be searchable.
62 .Pp
63 .Fn Lstat
64 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 obtains the same information about an open file
77 known by the file descriptor
78 .Fa fd .
79 .Pp
80 The
81 .Fa sb
82 argument is a pointer to a
83 .Fn stat
84 structure
85 as defined by
86 .Aq Pa sys/stat.h
87 (shown below)
88 and into which information is placed concerning the file.
89 .Bd -literal
90 struct stat {
91     dev_t     st_dev;               /* inode's device */
92     ino_t     st_ino;               /* inode's number */
93     mode_t    st_mode;              /* inode protection mode */
94     nlink_t   st_nlink;             /* number of hard links */
95     uid_t     st_uid;               /* user ID of the file's owner */
96     gid_t     st_gid;               /* group ID of the file's group */
97     dev_t     st_rdev;              /* device type */
98 #ifndef _POSIX_SOURCE
99     struct timespec st_atimespec;  /* time of last access */
100     struct timespec st_mtimespec;  /* time of last data modification */
101     struct timespec st_ctimespec;  /* time of last file status change */
102 #else
103     time_t    st_atime;             /* time of last access */
104     long      st_atimensec;         /* nsec of last access */
105     time_t    st_mtime;             /* time of last data modification */
106     long      st_mtimensec;         /* nsec of last data modification */
107     time_t    st_ctime;             /* time of last file status change */
108     long      st_ctimensec;         /* nsec of last file status change */
109 #endif
110     off_t     st_size;              /* file size, in bytes */
111     int64_t   st_blocks;            /* blocks allocated for file */
112     u_int32_t st_blksize;           /* optimal blocksize for I/O */
113     u_int32_t st_flags;             /* user defined flags for file */
114     u_int32_t st_gen;               /* file generation number */
115 };
116 .Ed
117 .Pp
118 The time-related fields of
119 .Fa struct stat
120 are as follows:
121 .Bl -tag -width XXXst_mtime
122 .It st_atime
123 Time when file data last accessed.
124 Changed by the
125 .Xr mknod 2 ,
126 .Xr utimes 2
127 and
128 .Xr read 2
129 system calls.
130 .It st_mtime
131 Time when file data last modified.
132 Changed by the
133 .Xr mknod 2 ,
134 .Xr utimes 2
135 and
136 .Xr write 2
137 system calls.
138 .It st_ctime
139 Time when file status was last changed (inode data modification).
140 Changed by the
141 .Xr chmod 2 ,
142 .Xr chown 2 ,
143 .Xr link 2 ,
144 .Xr mknod 2 ,
145 .Xr rename 2 ,
146 .Xr unlink 2 ,
147 .Xr utimes 2
148 and
149 .Xr write 2
150 system calls.
151 .El
152 .Pp
153 If
154 .Dv _POSIX_SOURCE
155 is not defined, the time-related fields are defined as:
156 .Bd -literal
157 #ifndef _POSIX_SOURCE
158 #define st_atime st_atimespec.tv_sec
159 #define st_mtime st_mtimespec.tv_sec
160 #define st_ctime st_ctimespec.tv_sec
161 #endif
162 .Ed
163 .Pp
164 The size-related fields of the
165 .Fa struct stat
166 are as follows:
167 .Bl -tag -width XXXst_blksize
168 .It st_blksize
169 The optimal I/O block size for the file.
170 .It st_blocks
171 The actual number of blocks allocated for the file in 512-byte units.
172 As short symbolic links are stored in the inode, this number may
173 be zero.
174 .El
175 .Pp
176 The status information word
177 .Fa st_mode
178 has the following bits:
179 .Bd -literal
180 #define S_IFMT 0170000           /* type of file */
181 #define        S_IFIFO  0010000  /* named pipe (fifo) */
182 #define        S_IFCHR  0020000  /* character special */
183 #define        S_IFDIR  0040000  /* directory */
184 #define        S_IFBLK  0060000  /* block special */
185 #define        S_IFREG  0100000  /* regular */
186 #define        S_IFLNK  0120000  /* symbolic link */
187 #define        S_IFSOCK 0140000  /* socket */
188 #define        S_IFWHT  0160000  /* whiteout */
189 #define S_ISUID 0004000  /* set user id on execution */
190 #define S_ISGID 0002000  /* set group id on execution */
191 #define S_ISVTX 0001000  /* save swapped text even after use */
192 #define S_IRUSR 0000400  /* read permission, owner */
193 #define S_IWUSR 0000200  /* write permission, owner */
194 #define S_IXUSR 0000100  /* execute/search permission, owner */
195 .Ed
196 .Pp
197 For a list of access modes, see
198 .Aq Pa sys/stat.h ,
199 .Xr access 2
200 and
201 .Xr chmod 2 .
202 .Sh RETURN VALUES
203 .Rv -std
204 .Sh COMPATIBILITY
205 Previous versions of the system used different types for the
206 .Li st_dev ,
207 .Li st_uid ,
208 .Li st_gid ,
209 .Li st_rdev ,
210 .Li st_size ,
211 .Li st_blksize
212 and
213 .Li st_blocks
214 fields.
215 .Sh ERRORS
216 .Fn Stat
217 and
218 .Fn lstat
219 will fail if:
220 .Bl -tag -width Er
221 .It Bq Er ENOTDIR
222 A component of the path prefix is not a directory.
223 .It Bq Er ENAMETOOLONG
224 A component of a pathname exceeded 255 characters,
225 or an entire path name exceeded 1023 characters.
226 .It Bq Er ENOENT
227 The named file does not exist.
228 .It Bq Er EACCES
229 Search permission is denied for a component of the path prefix.
230 .It Bq Er ELOOP
231 Too many symbolic links were encountered in translating the pathname.
232 .It Bq Er EFAULT
233 .Fa sb
234 or
235 .Em name
236 points to an invalid address.
237 .It Bq Er EIO
238 An I/O error occurred while reading from or writing to the file system.
239 .El
240 .Pp
241 .Bl -tag -width Er
242 .Fn Fstat
243 will fail if:
244 .It Bq Er EBADF
245 .Fa fd
246 is not a valid open file descriptor.
247 .It Bq Er EFAULT
248 .Fa sb
249 points to an invalid address.
250 .It Bq Er EIO
251 An I/O error occurred while reading from or writing to the file system.
252 .El
253 .Sh SEE ALSO
254 .Xr access 2 ,
255 .Xr chmod 2 ,
256 .Xr chown 2 ,
257 .Xr utimes 2 ,
258 .Xr symlink 7
259 .Sh BUGS
260 Applying
261 .Fn fstat
262 to a socket (and thus to a pipe)
263 returns a zeroed buffer,
264 except for the blocksize field,
265 and a unique device and inode number.
266 .Sh STANDARDS
267 The
268 .Fn stat
269 and
270 .Fn fstat
271 function calls are expected to conform to
272 .St -p1003.1-90 .
273 .Sh HISTORY
274 A
275 .Fn stat
276 and a
277 .Fn fstat
278 function call appeared in
279 .At v7 .
280 A
281 .Fn lstat
282 function call appeared in
283 .Bx 4.2 .