]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | '\" t |
2 | .\" Hey Emacs! This file is -*- nroff -*- source. | |
3 | .\" | |
4 | .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992 | |
5 | .\" Parts Copyright (c) 1995 Nicolai Langfeldt (janl@ifi.uio.no), 1/1/95 | |
c11b1abf | 6 | .\" and Copyright (c) 2007 Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da MK |
7 | .\" |
8 | .\" Permission is granted to make and distribute verbatim copies of this | |
9 | .\" manual provided the copyright notice and this permission notice are | |
10 | .\" preserved on all copies. | |
11 | .\" | |
12 | .\" Permission is granted to copy and distribute modified versions of this | |
13 | .\" manual under the conditions for verbatim copying, provided that the | |
14 | .\" entire resulting derived work is distributed under the terms of a | |
15 | .\" permission notice identical to this one. | |
c13182ef | 16 | .\" |
fea681da MK |
17 | .\" Since the Linux kernel and libraries are constantly changing, this |
18 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
19 | .\" responsibility for errors or omissions, or for damages resulting from | |
20 | .\" the use of the information contained herein. The author(s) may not | |
21 | .\" have taken the same level of care in the production of this manual, | |
22 | .\" which is licensed free of charge, as they might when working | |
23 | .\" professionally. | |
c13182ef | 24 | .\" |
fea681da MK |
25 | .\" Formatted or processed versions of this manual, if unaccompanied by |
26 | .\" the source, must acknowledge the copyright and authors of this work. | |
27 | .\" | |
28 | .\" Modified by Michael Haardt <michael@moria.de> | |
29 | .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu> | |
30 | .\" Modified 1995-05-18 by Todd Larason <jtl@molehill.org> | |
31 | .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com> | |
32 | .\" Modified 1995-01-09 by Richard Kettlewell <richard@greenend.org.uk> | |
33 | .\" Modified 1998-05-13 by Michael Haardt <michael@cantor.informatik.rwth-aachen.de> | |
34 | .\" Modified 1999-07-06 by aeb & Albert Cahalan | |
35 | .\" Modified 2000-01-07 by aeb | |
c11b1abf | 36 | .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com> |
bc7ff20e | 37 | .\" 2007-06-08 mtk: Added example program |
cd667553 | 38 | .\" 2007-07-05 mtk: Added details on underlying system call interfaces |
c13182ef | 39 | .\" |
2843ba93 | 40 | .TH STAT 2 2009-09-30 "Linux" "Linux Programmer's Manual" |
fea681da MK |
41 | .SH NAME |
42 | stat, fstat, lstat \- get file status | |
43 | .SH SYNOPSIS | |
44 | .B #include <sys/types.h> | |
45 | .br | |
46 | .B #include <sys/stat.h> | |
47 | .br | |
48 | .B #include <unistd.h> | |
49 | .sp | |
da2d9dad | 50 | .BI "int stat(const char *" path ", struct stat *" buf ); |
fea681da | 51 | .br |
d3b03141 | 52 | .BI "int fstat(int " fd ", struct stat *" buf ); |
fea681da | 53 | .br |
da2d9dad | 54 | .BI "int lstat(const char *" path ", struct stat *" buf ); |
8179def1 MK |
55 | .sp |
56 | .in -4n | |
57 | Feature Test Macro Requirements for glibc (see | |
58 | .BR feature_test_macros (7)): | |
59 | .in | |
60 | .sp | |
61 | .BR lstat (): | |
cc4615cc | 62 | _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 |
fea681da MK |
63 | .SH DESCRIPTION |
64 | .PP | |
da2d9dad MK |
65 | These functions return information about a file. |
66 | No permissions are required on the file itself, but \(em in the case of | |
67 | .BR stat () | |
c13182ef | 68 | and |
da2d9dad MK |
69 | .BR lstat () |
70 | \(em | |
71 | execute (search) permission is required on all of the directories in | |
72 | .I path | |
73 | that lead to the file. | |
fea681da | 74 | .PP |
da2d9dad | 75 | .BR stat () |
c13182ef | 76 | stats the file pointed to by |
da2d9dad | 77 | .I path |
fea681da MK |
78 | and fills in |
79 | .IR buf . | |
80 | ||
da2d9dad | 81 | .BR lstat () |
fea681da | 82 | is identical to |
da2d9dad MK |
83 | .BR stat (), |
84 | except that if | |
85 | .I path | |
86 | is a symbolic link, then the link itself is stat-ed, | |
fea681da MK |
87 | not the file that it refers to. |
88 | ||
da2d9dad | 89 | .BR fstat () |
fea681da | 90 | is identical to |
da2d9dad MK |
91 | .BR stat (), |
92 | except that the file to be stat-ed is specified by the file descriptor | |
d3b03141 | 93 | .IR fd . |
fea681da | 94 | .PP |
da2d9dad | 95 | All of these system calls return a |
fea681da MK |
96 | .I stat |
97 | structure, which contains the following fields: | |
98 | .PP | |
bd191423 | 99 | .in +4n |
fea681da MK |
100 | .nf |
101 | struct stat { | |
da2d9dad MK |
102 | dev_t st_dev; /* ID of device containing file */ |
103 | ino_t st_ino; /* inode number */ | |
5ae873ff MK |
104 | mode_t st_mode; /* protection */ |
105 | nlink_t st_nlink; /* number of hard links */ | |
106 | uid_t st_uid; /* user ID of owner */ | |
107 | gid_t st_gid; /* group ID of owner */ | |
da2d9dad | 108 | dev_t st_rdev; /* device ID (if special file) */ |
5ae873ff | 109 | off_t st_size; /* total size, in bytes */ |
24d01c53 | 110 | blksize_t st_blksize; /* blocksize for file system I/O */ |
614aae41 | 111 | blkcnt_t st_blocks; /* number of 512B blocks allocated */ |
5ae873ff MK |
112 | time_t st_atime; /* time of last access */ |
113 | time_t st_mtime; /* time of last modification */ | |
114 | time_t st_ctime; /* time of last status change */ | |
fea681da MK |
115 | }; |
116 | .fi | |
bd191423 | 117 | .in |
fea681da | 118 | .PP |
da2d9dad | 119 | The |
29de83af | 120 | .I st_dev |
da2d9dad | 121 | field describes the device on which this file resides. |
e9ef777f MK |
122 | (The |
123 | .BR major (3) | |
124 | and | |
125 | .BR minor (3) | |
126 | macros may be useful to decompose the device ID in this field.) | |
da2d9dad MK |
127 | |
128 | The | |
129 | .I st_rdev | |
130 | field describes the device that this file (inode) represents. | |
131 | ||
132 | The | |
fea681da | 133 | .I st_size |
c13182ef MK |
134 | field gives the size of the file (if it is a regular |
135 | file or a symbolic link) in bytes. | |
da2d9dad | 136 | The size of a symlink is the length of the pathname |
28d88c17 | 137 | it contains, without a trailing null byte. |
fea681da | 138 | |
da2d9dad | 139 | The |
fea681da | 140 | .I st_blocks |
32f30015 | 141 | field indicates the number of blocks allocated to the file, 512-byte units. |
fea681da | 142 | (This may be smaller than |
eee0a2ec MK |
143 | .IR st_size /512 |
144 | when the file has holes.) | |
da2d9dad MK |
145 | |
146 | The | |
0daa9e92 | 147 | .I st_blksize |
da2d9dad | 148 | field gives the "preferred" blocksize for efficient file system I/O. |
fea681da MK |
149 | (Writing to a file in smaller chunks may cause |
150 | an inefficient read-modify-rewrite.) | |
151 | .PP | |
24d01c53 | 152 | Not all of the Linux file systems implement all of the time fields. |
fea681da | 153 | Some file system types allow mounting in such a way that file |
86d89e4c | 154 | and/or directory accesses do not cause an update of the |
fea681da | 155 | .I st_atime |
c13182ef | 156 | field. |
86d89e4c MK |
157 | (See |
158 | .IR noatime , | |
159 | .IR nodiratime , | |
160 | and | |
161 | .I relatime | |
162 | in | |
163 | .BR mount (8), | |
164 | and related information in | |
165 | .BR mount (2).) | |
14adeb6d MK |
166 | In addition, |
167 | .I st_atime | |
168 | is not updated if a file is opened with the | |
169 | .BR O_NOATIME ; | |
170 | see | |
171 | .BR open (2). | |
fea681da MK |
172 | |
173 | The field | |
174 | .I st_atime | |
75b94dc3 | 175 | is changed by file accesses, for example, by |
fea681da MK |
176 | .BR execve (2), |
177 | .BR mknod (2), | |
178 | .BR pipe (2), | |
179 | .BR utime (2) | |
180 | and | |
181 | .BR read (2) | |
7c93fec0 MK |
182 | (of more than zero bytes). |
183 | Other routines, like | |
fea681da MK |
184 | .BR mmap (2), |
185 | may or may not update | |
186 | .IR st_atime . | |
187 | ||
188 | The field | |
189 | .I st_mtime | |
75b94dc3 | 190 | is changed by file modifications, for example, by |
fea681da MK |
191 | .BR mknod (2), |
192 | .BR truncate (2), | |
193 | .BR utime (2) | |
194 | and | |
195 | .BR write (2) | |
196 | (of more than zero bytes). | |
197 | Moreover, | |
198 | .I st_mtime | |
199 | of a directory is changed by the creation or deletion of files | |
200 | in that directory. | |
201 | The | |
202 | .I st_mtime | |
203 | field is | |
204 | .I not | |
205 | changed for changes in owner, group, hard link count, or mode. | |
206 | ||
207 | The field | |
208 | .I st_ctime | |
209 | is changed by writing or by setting inode information | |
210 | (i.e., owner, group, link count, mode, etc.). | |
211 | .PP | |
168df940 MK |
212 | The following POSIX macros are defined to check the file type using the |
213 | .I st_mode | |
214 | field: | |
bd191423 | 215 | .RS 4 |
fea681da | 216 | .TP 1.2i |
c91e381d | 217 | .BR S_ISREG (m) |
fea681da MK |
218 | is it a regular file? |
219 | .TP | |
c91e381d | 220 | .BR S_ISDIR (m) |
fea681da MK |
221 | directory? |
222 | .TP | |
c91e381d | 223 | .BR S_ISCHR (m) |
fea681da MK |
224 | character device? |
225 | .TP | |
c91e381d | 226 | .BR S_ISBLK (m) |
fea681da MK |
227 | block device? |
228 | .TP | |
c91e381d | 229 | .BR S_ISFIFO (m) |
da2d9dad | 230 | FIFO (named pipe)? |
fea681da | 231 | .TP |
c91e381d | 232 | .BR S_ISLNK (m) |
fea681da MK |
233 | symbolic link? (Not in POSIX.1-1996.) |
234 | .TP | |
c91e381d | 235 | .BR S_ISSOCK (m) |
fea681da MK |
236 | socket? (Not in POSIX.1-1996.) |
237 | .RE | |
238 | .PP | |
239 | The following flags are defined for the | |
240 | .I st_mode | |
241 | field: | |
bd191423 | 242 | .in +4n |
fea681da | 243 | .TS |
c91e381d | 244 | lB l l. |
10f5f294 | 245 | S_IFMT 0170000 bit mask for the file type bit fields |
fea681da MK |
246 | S_IFSOCK 0140000 socket |
247 | S_IFLNK 0120000 symbolic link | |
248 | S_IFREG 0100000 regular file | |
249 | S_IFBLK 0060000 block device | |
250 | S_IFDIR 0040000 directory | |
251 | S_IFCHR 0020000 character device | |
da2d9dad | 252 | S_IFIFO 0010000 FIFO |
fea681da | 253 | S_ISUID 0004000 set UID bit |
da2d9dad | 254 | S_ISGID 0002000 set-group-ID bit (see below) |
fea681da MK |
255 | S_ISVTX 0001000 sticky bit (see below) |
256 | S_IRWXU 00700 mask for file owner permissions | |
257 | S_IRUSR 00400 owner has read permission | |
258 | S_IWUSR 00200 owner has write permission | |
259 | S_IXUSR 00100 owner has execute permission | |
260 | S_IRWXG 00070 mask for group permissions | |
261 | S_IRGRP 00040 group has read permission | |
262 | S_IWGRP 00020 group has write permission | |
263 | S_IXGRP 00010 group has execute permission | |
264 | S_IRWXO 00007 mask for permissions for others (not in group) | |
265 | S_IROTH 00004 others have read permission | |
704a18f0 | 266 | S_IWOTH 00002 others have write permission |
fea681da MK |
267 | S_IXOTH 00001 others have execute permission |
268 | .TE | |
bd191423 | 269 | .in |
fea681da | 270 | .P |
2f0af33b MK |
271 | The set-group-ID bit |
272 | .RB ( S_ISGID ) | |
273 | has several special uses. | |
fea681da MK |
274 | For a directory it indicates that BSD semantics is to be used |
275 | for that directory: files created there inherit their group ID from | |
276 | the directory, not from the effective group ID of the creating process, | |
2f0af33b MK |
277 | and directories created there will also get the |
278 | .B S_ISGID | |
279 | bit set. | |
66ee0c7e | 280 | For a file that does not have the group execution bit |
2f0af33b MK |
281 | .RB ( S_IXGRP ) |
282 | set, | |
da2d9dad | 283 | the set-group-ID bit indicates mandatory file/record locking. |
fea681da | 284 | .P |
2d986c92 | 285 | The sticky bit |
2f0af33b MK |
286 | .RB ( S_ISVTX ) |
287 | on a directory means that a file | |
fea681da MK |
288 | in that directory can be renamed or deleted only by the owner |
289 | of the file, by the owner of the directory, and by a privileged | |
290 | process. | |
291 | .SH "RETURN VALUE" | |
c13182ef MK |
292 | On success, zero is returned. |
293 | On error, \-1 is returned, and | |
fea681da MK |
294 | .I errno |
295 | is set appropriately. | |
296 | .SH ERRORS | |
297 | .TP | |
298 | .B EACCES | |
299 | Search permission is denied for one of the directories | |
300 | in the path prefix of | |
da2d9dad | 301 | .IR path . |
fea681da | 302 | (See also |
ad7cc990 | 303 | .BR path_resolution (7).) |
fea681da MK |
304 | .TP |
305 | .B EBADF | |
d3b03141 | 306 | .I fd |
fea681da MK |
307 | is bad. |
308 | .TP | |
309 | .B EFAULT | |
310 | Bad address. | |
311 | .TP | |
312 | .B ELOOP | |
313 | Too many symbolic links encountered while traversing the path. | |
314 | .TP | |
315 | .B ENAMETOOLONG | |
316 | File name too long. | |
317 | .TP | |
318 | .B ENOENT | |
5cf0b3b4 | 319 | A component of |
da2d9dad | 320 | .I path |
5cf0b3b4 MK |
321 | does not exist, or |
322 | .I path | |
323 | is an empty string. | |
fea681da MK |
324 | .TP |
325 | .B ENOMEM | |
75b94dc3 | 326 | Out of memory (i.e., kernel memory). |
fea681da MK |
327 | .TP |
328 | .B ENOTDIR | |
94ea1e9d | 329 | A component of the path prefix of |
5cf0b3b4 MK |
330 | .I path |
331 | is not a directory. | |
9dcc4605 MK |
332 | .TP |
333 | .B EOVERFLOW | |
334 | .RB ( stat ()) | |
335 | .I path | |