]>
Commit | Line | Data |
---|---|---|
fea681da | 1 | '\" t |
fea681da MK |
2 | .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992 |
3 | .\" Parts Copyright (c) 1995 Nicolai Langfeldt (janl@ifi.uio.no), 1/1/95 | |
40084043 | 4 | .\" and Copyright (c) 2006, 2007, 2014 Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da | 5 | .\" |
93015253 | 6 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
7 | .\" Permission is granted to make and distribute verbatim copies of this |
8 | .\" manual provided the copyright notice and this permission notice are | |
9 | .\" preserved on all copies. | |
10 | .\" | |
11 | .\" Permission is granted to copy and distribute modified versions of this | |
12 | .\" manual under the conditions for verbatim copying, provided that the | |
13 | .\" entire resulting derived work is distributed under the terms of a | |
14 | .\" permission notice identical to this one. | |
c13182ef | 15 | .\" |
fea681da MK |
16 | .\" Since the Linux kernel and libraries are constantly changing, this |
17 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
18 | .\" responsibility for errors or omissions, or for damages resulting from | |
19 | .\" the use of the information contained herein. The author(s) may not | |
20 | .\" have taken the same level of care in the production of this manual, | |
21 | .\" which is licensed free of charge, as they might when working | |
22 | .\" professionally. | |
c13182ef | 23 | .\" |
fea681da MK |
24 | .\" Formatted or processed versions of this manual, if unaccompanied by |
25 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 26 | .\" %%%LICENSE_END |
fea681da MK |
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 | .\" |
734882f4 | 40 | .TH STAT 2 2017-05-03 "Linux" "Linux Programmer's Manual" |
fea681da | 41 | .SH NAME |
40084043 | 42 | stat, fstat, lstat, fstatat \- get file status |
fea681da | 43 | .SH SYNOPSIS |
40084043 | 44 | .nf |
fea681da MK |
45 | .B #include <sys/types.h> |
46 | .br | |
47 | .B #include <sys/stat.h> | |
48 | .br | |
49 | .B #include <unistd.h> | |
68e4db0a | 50 | .PP |
9a38b2ce | 51 | .BI "int stat(const char *" pathname ", struct stat *" statbuf ); |
fea681da | 52 | .br |
9a38b2ce | 53 | .BI "int fstat(int " fd ", struct stat *" statbuf ); |
fea681da | 54 | .br |
9a38b2ce | 55 | .BI "int lstat(const char *" pathname ", struct stat *" statbuf ); |
68e4db0a | 56 | .PP |
40084043 MK |
57 | .BR "#include <fcntl.h> " "/* Definition of AT_* constants */" |
58 | .B #include <sys/stat.h> | |
68e4db0a | 59 | .PP |
40084043 | 60 | .BI "int fstatat(int " dirfd ", const char *" pathname ", struct stat *" \ |
9a38b2ce | 61 | statbuf , |
40084043 MK |
62 | .BI " int " flags ); |
63 | .fi | |
68e4db0a | 64 | .PP |
8179def1 MK |
65 | .in -4n |
66 | Feature Test Macro Requirements for glibc (see | |
67 | .BR feature_test_macros (7)): | |
68 | .in | |
68e4db0a | 69 | .PP |
43e0fe7d | 70 | .ad l |
8179def1 | 71 | .BR lstat (): |
43e0fe7d | 72 | .RS 4 |
cf7fa0a1 | 73 | /* glibc 2.19 and earlier */ _BSD_SOURCE |
afb9bd5d | 74 | .br |
cf7fa0a1 | 75 | || /* Since glibc 2.20 */ _DEFAULT_SOURCE |
afb9bd5d | 76 | .br |
cf7fa0a1 MK |
77 | || _XOPEN_SOURCE\ >=\ 500 |
78 | .\" _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED | |
3ba63d80 | 79 | .br |
cf7fa0a1 | 80 | || /* Since glibc 2.10: */ _POSIX_C_SOURCE\ >=\ 200112L |
43e0fe7d | 81 | .RE |
68e4db0a | 82 | .PP |
40084043 | 83 | .BR fstatat (): |
40084043 MK |
84 | .ad l |
85 | .RS 4 | |
68e4db0a | 86 | .PD 0 |
40084043 MK |
87 | .TP 4 |
88 | Since glibc 2.10: | |
b0da7b8b | 89 | _POSIX_C_SOURCE\ >=\ 200809L |
40084043 MK |
90 | .TP |
91 | Before glibc 2.10: | |
92 | _ATFILE_SOURCE | |
93 | .RE | |
43e0fe7d MK |
94 | .PD |
95 | .ad | |
fea681da MK |
96 | .SH DESCRIPTION |
97 | .PP | |
e096bd70 | 98 | These functions return information about a file, in the buffer pointed to by |
9a38b2ce | 99 | .IR statbuf . |
5503c85e | 100 | No permissions are required on the file itself, but\(emin the case of |
071d1c4f MK |
101 | .BR stat (), |
102 | .BR fstatat (), | |
c13182ef | 103 | and |
827edbd2 MK |
104 | .BR lstat ()\(emexecute |
105 | (search) permission is required on all of the directories in | |
66cbeaf4 | 106 | .I pathname |
da2d9dad | 107 | that lead to the file. |
fea681da | 108 | .PP |
da2d9dad | 109 | .BR stat () |
071d1c4f MK |
110 | and |
111 | .BR fstatat () | |
e096bd70 MK |
112 | retrieve information about the file pointed to by |
113 | .IR pathname ; | |
071d1c4f MK |
114 | the differences for |
115 | .BR fstatat () | |
116 | are described below. | |
fea681da | 117 | |
da2d9dad | 118 | .BR lstat () |
fea681da | 119 | is identical to |
da2d9dad MK |
120 | .BR stat (), |
121 | except that if | |
66cbeaf4 | 122 | .I pathname |
e096bd70 | 123 | is a symbolic link, then it returns information about the link itself, |
fea681da MK |
124 | not the file that it refers to. |
125 | ||
da2d9dad | 126 | .BR fstat () |
fea681da | 127 | is identical to |
da2d9dad | 128 | .BR stat (), |
5201bb40 | 129 | except that the file about which information is to be retrieved |
e096bd70 | 130 | is specified by the file descriptor |
d3b03141 | 131 | .IR fd . |
9152b8c9 MK |
132 | .\" |
133 | .SS The stat structure | |
da2d9dad | 134 | All of these system calls return a |
fea681da MK |
135 | .I stat |
136 | structure, which contains the following fields: | |
137 | .PP | |
bd191423 | 138 | .in +4n |
fea681da MK |
139 | .nf |
140 | struct stat { | |
e9e9e87e | 141 | dev_t st_dev; /* ID of device containing file */ |
96dea201 MK |
142 | ino_t st_ino; /* Inode number */ |
143 | mode_t st_mode; /* File type and mode */ | |
144 | nlink_t st_nlink; /* Number of hard links */ | |
145 | uid_t st_uid; /* User ID of owner */ | |
146 | gid_t st_gid; /* Group ID of owner */ | |
147 | dev_t st_rdev; /* Device ID (if special file) */ | |
148 | off_t st_size; /* Total size, in bytes */ | |
149 | blksize_t st_blksize; /* Block size for filesystem I/O */ | |
150 | blkcnt_t st_blocks; /* Number of 512B blocks allocated */ | |
e9e9e87e MK |
151 | |
152 | /* Since Linux 2.6, the kernel supports nanosecond | |
153 | precision for the following timestamp fields. | |
154 | For the details before Linux 2.6, see NOTES. */ | |
155 | ||
96dea201 MK |
156 | struct timespec st_atim; /* Time of last access */ |
157 | struct timespec st_mtim; /* Time of last modification */ | |
158 | struct timespec st_ctim; /* Time of last status change */ | |
e9e9e87e MK |
159 | |
160 | #define st_atime st_atim.tv_sec /* Backward compatibility */ | |
161 | #define st_mtime st_mtim.tv_sec | |
162 | #define st_ctime st_ctim.tv_sec | |
fea681da MK |
163 | }; |
164 | .fi | |
bd191423 | 165 | .in |
496bdb56 | 166 | |
b3deaf0f | 167 | .IR Note : |
496bdb56 MK |
168 | the order of fields in the |
169 | .I stat | |
170 | structure varies somewhat | |
171 | across architectures. | |
172 | In addition, | |
173 | the definition above does not show the padding bytes | |
174 | that may be present between some fields on various architectures. | |
409b0278 | 175 | Consult the glibc and kernel source code |
496bdb56 MK |
176 | if you need to know the details. |
177 | ||
64639e9a MK |
178 | .\" Background: inode attributes are modified with i_mutex held, but |
179 | .\" read by stat() without taking the mutex. | |
b3deaf0f | 180 | .IR Note : |
7ad5fb5d | 181 | for performance and simplicity reasons, different fields in the |
f240296d JH |
182 | .I stat |
183 | structure may contain state information from different moments | |
64639e9a MK |
184 | during the execution of the system call. |
185 | For example, if | |
addfd6c3 MK |
186 | .IR st_mode |
187 | or | |
f240296d | 188 | .IR st_uid |
addfd6c3 MK |
189 | is changed by another process by calling |
190 | .BR chmod (2) | |
191 | or | |
64639e9a | 192 | .BR chown (2), |
f240296d JH |
193 | .BR stat () |
194 | might return the old | |
195 | .I st_mode | |
196 | together with the new | |
197 | .IR st_uid , | |
198 | or the old | |
199 | .I st_uid | |
200 | together with the new | |
201 | .IR st_mode . | |
202 | ||
53cb52e5 MK |
203 | The fields in the |
204 | .I stat | |
205 | structure are as follows: | |
206 | .TP | |
29de83af | 207 | .I st_dev |
53cb52e5 | 208 | This field describes the device on which this file resides. |
e9ef777f MK |
209 | (The |
210 | .BR major (3) | |
211 | and | |
212 | .BR minor (3) | |
213 | macros may be useful to decompose the device ID in this field.) | |
53cb52e5 | 214 | .TP |
c61617be MK |
215 | .I st_ino |
216 | This field contains the file's inode number. | |
217 | .TP | |
cafa9752 | 218 | .I st_mode |
e8ff4f53 MK |
219 | This field contains the file type and mode. |
220 | See | |
221 | .BR inode (7) | |
222 | for further information. | |
cafa9752 | 223 | .TP |
d41bfb20 MK |
224 | .I st_nlink |
225 | This field contains the number of hard links to the file. | |
226 | .TP | |
78909f3e MK |
227 | .I st_uid |
228 | This field contains the user ID of the owner of the file. | |
229 | .TP | |
230 | .I st_gid | |
231 | This field contains the ID of the group owner of the file. | |
232 | .TP | |
da2d9dad | 233 | .I st_rdev |
53cb52e5 MK |
234 | This field describes the device that this file (inode) represents. |
235 | .TP | |
fea681da | 236 | .I st_size |
53cb52e5 | 237 | This field gives the size of the file (if it is a regular |
c13182ef | 238 | file or a symbolic link) in bytes. |
280ff209 MK |
239 | The size of a symbolic link is the length of the pathname |
240 | it contains, without a terminating null byte. | |
53cb52e5 | 241 | .TP |
28f042cc | 242 | .I st_blksize |
96dea201 | 243 | This field gives the "preferred" block size for efficient filesystem I/O. |
28f042cc | 244 | .TP |
fea681da | 245 | .I st_blocks |
53cb52e5 | 246 | This field indicates the number of blocks allocated to the file, |
e8ff4f53 | 247 | in 512-byte units. |
fea681da | 248 | (This may be smaller than |
eee0a2ec MK |
249 | .IR st_size /512 |
250 | when the file has holes.) | |
53cb52e5 | 251 | .TP |
fea681da | 252 | .I st_atime |
53cb52e5 | 253 | This is the file's last access timestamp. |
53cb52e5 | 254 | .TP |
fea681da | 255 | .I st_mtime |
53cb52e5 | 256 | This is the file's last modification timestamp. |
53cb52e5 | 257 | .TP |
fea681da | 258 | .I st_ctime |
53cb52e5 | 259 | This is the file's last status change timestamp. |
fea681da | 260 | .PP |
e8ff4f53 MK |
261 | For further information on the above fields, see |
262 | .BR inode (7). | |
40084043 | 263 | .\" |
1fef0fa3 | 264 | .SS fstatat() |
40084043 MK |
265 | The |
266 | .BR fstatat () | |
267 | system call operates in exactly the same way as | |
cadd38ba | 268 | .BR stat (), |
40084043 MK |
269 | except for the differences described here. |
270 | ||
271 | If the pathname given in | |
272 | .I pathname | |
273 | is relative, then it is interpreted relative to the directory | |
274 | referred to by the file descriptor | |
275 | .I dirfd | |
276 | (rather than relative to the current working directory of | |
277 | the calling process, as is done by | |
cadd38ba | 278 | .BR stat () |
40084043 MK |
279 | for a relative pathname). |
280 | ||
281 | If | |
282 | .I pathname | |
283 | is relative and | |
284 | .I dirfd | |
285 | is the special value | |
286 | .BR AT_FDCWD , | |
287 | then | |
288 | .I pathname | |
289 | is interpreted relative to the current working | |
290 | directory of the calling process (like | |
cadd38ba | 291 | .BR stat ()). |
40084043 MK |
292 | |
293 | If | |
294 | .I pathname | |
295 | is absolute, then | |
296 | .I dirfd | |
297 | is ignored. | |
298 | ||
299 | .I flags | |
300 | can either be 0, or include one or more of the following flags ORed: | |
301 | .TP | |
302 | .BR AT_EMPTY_PATH " (since Linux 2.6.39)" | |
303 | .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d | |
304 | If | |
305 | .I pathname | |
306 | is an empty string, operate on the file referred to by | |
307 | .IR dirfd | |
308 | (which may have been obtained using the | |
309 | .BR open (2) | |
310 | .B O_PATH | |
311 | flag). | |
b78969ac MÅš |
312 | In this case, |
313 | .I dirfd | |
314 | can refer to any type of file, not just a directory. | |
a6fcbdf8 MK |
315 | If |
316 | .I dirfd | |
317 | is | |
318 | .BR AT_FDCWD , | |
319 | the call operates on the current working directory. | |
40084043 MK |
320 | This flag is Linux-specific; define |
321 | .B _GNU_SOURCE | |
322 | .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed | |
323 | to obtain its definition. | |
324 | .TP | |
325 | .BR AT_NO_AUTOMOUNT " (since Linux 2.6.38)" | |
326 | Don't automount the terminal ("basename") component of | |
327 | .I pathname | |
328 | if it is a directory that is an automount point. | |
329 | This allows the caller to gather attributes of an automount point | |
330 | (rather than the location it would mount). | |
331 | This flag can be used in tools that scan directories | |
332 | to prevent mass-automounting of a directory of automount points. | |
333 | The | |
334 | .B AT_NO_AUTOMOUNT | |
335 | flag has no effect if the mount point has already been mounted over. | |
336 | This flag is Linux-specific; define | |
337 | .B _GNU_SOURCE | |
338 | .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed | |
339 | to obtain its definition. | |
340 | .TP | |
341 | .B AT_SYMLINK_NOFOLLOW | |
342 | If | |
343 | .I pathname | |
344 | is a symbolic link, do not dereference it: | |
345 | instead return information about the link itself, like | |
cadd38ba | 346 | .BR lstat (). |
40084043 MK |
347 | (By default, |
348 | .BR fstatat () | |
349 | dereferences symbolic links, like | |
cadd38ba | 350 | .BR stat ().) |
40084043 MK |
351 | .PP |
352 | See | |
353 | .BR openat (2) | |
354 | for an explanation of the need for | |
355 | .BR fstatat (). | |
47297adb | 356 | .SH RETURN VALUE |
c13182ef MK |
357 | On success, zero is returned. |
358 | On error, \-1 is returned, and | |
fea681da MK |
359 | .I errno |
360 | is set appropriately. | |
361 | .SH ERRORS | |
362 | .TP | |
363 | .B EACCES | |
364 | Search permission is denied for one of the directories | |
365 | in the path prefix of | |
66cbeaf4 | 366 | .IR pathname . |
fea681da | 367 | (See also |
ad7cc990 | 368 | .BR path_resolution (7).) |
fea681da MK |
369 | .TP |
370 | .B EBADF | |
d3b03141 | 371 | .I fd |
0fa890e0 | 372 | is not a valid open file descriptor. |
fea681da MK |
373 | .TP |
374 | .B EFAULT | |
375 | Bad address. | |
376 | .TP | |
377 | .B ELOOP | |
378 | Too many symbolic links encountered while traversing the path. | |
379 | .TP | |
380 | .B ENAMETOOLONG | |
66cbeaf4 | 381 | .I pathname |
c066b169 | 382 | is too long. |
fea681da MK |
383 | .TP |
384 | .B ENOENT | |
5cf0b3b4 | 385 | A component of |
66cbeaf4 | 386 | .I pathname |
5cf0b3b4 | 387 | does not exist, or |
66cbeaf4 | 388 | .I pathname |
96dea201 MK |
389 | is an empty string and |
390 | .B AT_EMPTY_PATH | |
391 | was not specified in | |
392 | .IR flags . | |
fea681da MK |
393 | .TP |
394 | .B ENOMEM | |
75b94dc3 | 395 | Out of memory (i.e., kernel memory). |
fea681da MK |
396 | .TP |
397 | .B ENOTDIR | |
94ea1e9d | 398 | A component of the path prefix of |
66cbeaf4 | 399 | .I pathname |
5cf0b3b4 | 400 | is not a directory. |
9dcc4605 MK |
401 | .TP |
402 | .B EOVERFLOW | |
66cbeaf4 | 403 | .I pathname |
fec75159 SP |
404 | or |
405 | .I fd | |
406 | refers to a file whose size, inode number, | |
407 | or number of blocks cannot be represented in, respectively, the types | |
408 | .IR off_t , | |
409 | .IR ino_t , | |
410 | or | |
411 | .IR blkcnt_t . | |
412 | This error can occur when, for example, | |
413 | an application compiled on a 32-bit platform without | |
5e4dc269 | 414 | .I -D_FILE_OFFSET_BITS=64 |
9dcc4605 MK |
415 | calls |
416 | .BR stat () | |
417 | on a file whose size exceeds | |
4d69ed2e | 418 | .I (1<<31)-1 |
fec75159 | 419 | bytes. |
40084043 | 420 | .PP |
40084043 MK |
421 | The following additional errors can occur for |
422 | .BR fstatat (): | |
423 | .TP | |
424 | .B EBADF | |
425 | .I dirfd | |
426 | is not a valid file descriptor. | |
427 | .TP | |
428 | .B EINVAL | |
429 | Invalid flag specified in | |
430 | .IR flags . | |
431 | .TP | |
432 | .B ENOTDIR | |
433 | .I pathname | |
434 | is relative and | |
435 | .I dirfd | |
436 | is a file descriptor referring to a file other than a directory. | |
437 | .SH VERSIONS | |
438 | .BR fstatat () | |
439 | was added to Linux in kernel 2.6.16; | |
440 | library support was added to glibc in version 2.4. | |
47297adb | 441 | .SH CONFORMING TO |
40084043 MK |
442 | .BR stat (), |
443 | .BR fstat (), | |
444 | .BR lstat (): | |
282daba9 | 445 | SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008. |
97c1eac8 MK |
446 | .\" SVr4 documents additional |
447 | .\" .BR fstat () | |
448 | .\" error conditions EINTR, ENOLINK, and EOVERFLOW. SVr4 | |
449 | .\" documents additional | |
450 | .\" .BR stat () | |
451 | .\" and | |
452 | .\" .BR lstat () | |
453 | .\" error conditions EINTR, EMULTIHOP, ENOLINK, and EOVERFLOW. | |
454 | ||
40084043 MK |
455 | .BR fstatat (): |
456 | POSIX.1-2008. | |
40084043 | 457 | |
d3e8b141 MK |
458 | According to POSIX.1-2001, |
459 | .BR lstat () | |
460 | on a symbolic link need return valid information only in the | |
461 | .I st_size | |
44b1f5a0 | 462 | field and the file type of the |
d3e8b141 MK |
463 | .IR st_mode |
464 | field of the | |
465 | .IR stat | |
466 | structure. | |
a03c016c | 467 | POSIX.1-2008 tightens the specification, requiring |
d3e8b141 | 468 | .BR lstat () |
7901c9c3 | 469 | to return valid information in all fields except the mode bits in |
d3e8b141 MK |
470 | .IR st_mode . |
471 | ||
fea681da MK |
472 | Use of the |
473 | .I st_blocks | |
474 | and | |
475 | .I st_blksize | |
c13182ef MK |
476 | fields may be less portable. |
477 | (They were introduced in BSD. | |
478 | The interpretation differs between systems, | |
479 | and possibly on a single system when NFS mounts are involved.) | |
2b2581ee | 480 | .SH NOTES |
1ef5b280 MK |
481 | On Linux, |
482 | .BR lstat () | |
483 | will generally not trigger automounter action, whereas | |
484 | .BR stat () | |
5d5985b8 MK |
485 | will (but see the description of |
486 | .BR fstatat () | |
487 | .B AT_NO_AUTOMOUNT | |
488 | fag, above). | |
9dbc4adb | 489 | .\" |
1ef5b280 | 490 | .SS Timestamp fields |
e9e9e87e MK |
491 | Older kernels and older standards did not support nanosecond timestamp |
492 | fields. | |
b8efc3ed | 493 | Instead, there were three timestamp |
e9e9e87e MK |
494 | .RI fields\(em st_atime , |
495 | .IR st_mtime , | |
496 | and | |
497 | .IR st_ctime \(emtyped | |
b8efc3ed | 498 | as |
e9e9e87e MK |
499 | .IR time_t |
500 | that recorded timestamps with one-second precision. | |
1ef5b280 | 501 | |
2b2581ee MK |
502 | Since kernel 2.5.48, the |
503 | .I stat | |
f5935752 | 504 | structure supports nanosecond resolution for the three file timestamp fields. |
e9e9e87e MK |
505 | The nanosecond components of each timestamp are available |
506 | via names of the form | |
2a0bd971 MK |
507 | .IR st_atim.tv_nsec , |
508 | if suitable feature test macros are defined. | |
509 | Nanosecond timestamps were standardized in POSIX.1-2008, | |
510 | and, starting with version 2.12, | |
511 | glibc exposes the nanosecond component names if | |
f5935752 MK |
512 | .BR _POSIX_C_SOURCE |
513 | is defined with the value 200809L or greater, or | |
514 | .BR _XOPEN_SOURCE | |
515 | is defined with the value 700 or greater. | |
2a0bd971 MK |
516 | Up to and including glibc 2.19, |
517 | the definitions of the nanoseconds components are also defined if | |
518 | .B _BSD_SOURCE | |
519 | or | |
520 | .B _SVID_SOURCE | |
521 | is defined. | |
f5935752 MK |
522 | If none of the aforementioned macros are defined, |
523 | then the nanosecond values are exposed with names of the form | |
524 | .IR st_atimensec . | |
e8ff4f53 | 525 | .\" |
0722a578 | 526 | .SS C library/kernel differences |
b8f9d9dd MK |
527 | Over time, increases in the size of the |
528 | .I stat | |
0c8dd254 | 529 | structure have led to three successive versions of |
b8f9d9dd MK |
530 | .BR stat (): |
531 | .IR sys_stat () | |
532 | (slot | |
533 | .IR __NR_oldstat ), | |
534 | .IR sys_newstat () | |
535 | (slot | |
536 | .IR __NR_stat ), | |
537 | and | |
0daa9e92 | 538 | .I sys_stat64() |
a39f2f4d | 539 | (slot |
94971420 MK |
540 | .IR __NR_stat64 ) |
541 | on 32-bit platforms such as i386. | |
542 | The first two versions were already present in Linux 1.0 | |
543 | (albeit with different names); | |
cd358be3 | 544 | .\" See include/asm-i386/stat.h in the Linux 2.4 source code for the |
94971420 MK |
545 | .\" various versions of the structure definitions |
546 | the last was added in Linux 2.4. | |
547 | Similar remarks apply for | |
548 | .BR fstat () | |
549 | and | |
550 | .BR lstat (). | |
551 | ||
552 | The kernel-internal versions of the | |
553 | .I stat | |
554 | structure dealt with by the different versions are, respectively: | |
94971420 MK |
555 | .TP |
556 | .IR __old_kernel_stat | |
557 | The original structure, with rather narrow fields, and no padding. | |
558 | .TP | |
559 | .IR stat | |
560 | Larger | |
561 | .I st_ino | |
562 | field and padding added to various parts of the structure to | |
563 | allow for future expansion. | |
564 | .TP | |
565 | .IR stat64 | |
566 | Even larger | |
567 | .I st_ino | |
568 | field, | |
569 | larger | |
570 | .I st_uid | |
571 | and | |
572 | .I st_gid | |
573 | fields to accommodate the Linux-2.4 expansion of UIDs and GIDs to 32 bits, | |
574 | and various other enlarged fields and further padding in the structure. | |
575 | (Various padding bytes were eventually consumed in Linux 2.6, | |
576 | with the advent of 32-bit device IDs and nanosecond components | |
577 | for the timestamp fields.) | |
94971420 | 578 | .PP |
d28a0b77 | 579 | The glibc |
b8f9d9dd | 580 | .BR stat () |
d28a0b77 | 581 | wrapper function hides these details from applications, |
6f1a1e61 MK |
582 | invoking the most recent version of the system call provided by the kernel, |
583 | and repacking the returned information if required for old binaries. | |
8179def1 MK |
584 | .\" |
585 | .\" A note from Andries Brouwer, July 2007 | |
e0bf9127 MK |
586 | .\" |
587 | .\" > Is the story not rather more complicated for some calls like | |
8179def1 | 588 | .\" > stat(2)? |
e0bf9127 | 589 | .\" |
8179def1 | 590 | .\" Yes and no, mostly no. See /usr/include/sys/stat.h . |
e0bf9127 | 591 | .\" |
8179def1 MK |
592 | .\" The idea is here not so much that syscalls change, but that |
593 | .\" the definitions of struct stat and of the types dev_t and mode_t change. | |
594 | .\" This means that libc (even if it does not call the kernel | |
595 | .\" but only calls some internal function) must know what the | |
596 | .\" format of dev_t or of struct stat is. | |
597 | .\" The communication between the application and libc goes via | |
598 | .\" the include file <sys/stat.h> that defines a _STAT_VER and | |
599 | .\" _MKNOD_VER describing the layout of the data that user space | |
600 | .\" uses. Each (almost each) occurrence of stat() is replaced by | |
601 | .\" an occurrence of xstat() where the first parameter of xstat() | |
602 | .\" is this version number _STAT_VER. | |
e0bf9127 | 603 | .\" |
8179def1 MK |
604 | .\" Now, also the definitions used by the kernel change. |
605 | .\" But glibc copes with this in the standard way, and the | |
606 | .\" struct stat as returned by the kernel is repacked into | |
607 | .\" the struct stat as expected by the application. | |
608 | .\" Thus, _STAT_VER and this setup cater for the application-libc | |
609 | .\" interface, rather than the libc-kernel interface. | |
e0bf9127 | 610 | .\" |
8179def1 | 611 | .\" (Note that the details depend on gcc being used as c compiler.) |
40084043 | 612 | |
94971420 MK |
613 | On modern 64-bit systems, life is simpler: there is a single |
614 | .BR stat () | |
615 | system call and the kernel deals with a | |
616 | .I stat | |
617 | structure that contains fields of a sufficient size. | |
618 | ||
40084043 MK |
619 | The underlying system call employed by the glibc |
620 | .BR fstatat () | |
621 | wrapper function is actually called | |
cad6df32 MK |
622 | .BR fstatat64 () |
623 | or, on some architectures, | |
624 | .\" strace(1) shows the name "newfstatat" on x86-64 | |
625 | .BR newfstatat (). | |
bc7ff20e | 626 | .SH EXAMPLE |
988db661 | 627 | The following program calls |
2777b1ca | 628 | .BR stat () |
bc7ff20e MK |
629 | and displays selected fields in the returned |
630 | .I stat | |
631 | structure. | |
632 | .nf | |
633 | ||
634 | #include <sys/types.h> | |
635 | #include <sys/stat.h> | |
636 | #include <time.h> | |
637 | #include <stdio.h> | |
988db661 | 638 | #include <stdlib.h> |
51dbffc5 | 639 | #include <sys/sysmacros.h> |
bc7ff20e MK |
640 | |
641 | int | |
642 | main(int argc, char *argv[]) | |
643 | { | |
644 | struct stat sb; | |
645 | ||
646 | if (argc != 2) { | |
647 | fprintf(stderr, "Usage: %s <pathname>\\n", argv[0]); | |
648 | exit(EXIT_FAILURE); | |
649 | } | |
650 | ||
29059a65 | 651 | if (stat(argv[1], &sb) == \-1) { |
bc7ff20e | 652 | perror("stat"); |