]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/stat.2
f05121253e85ecccb407416705952be8871f82cb
[thirdparty/man-pages.git] / man2 / stat.2
1 '\" t
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
4 .\" and Copyright (c) 2006, 2007, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\"
6 .\" %%%LICENSE_START(VERBATIM)
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.
15 .\"
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.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" %%%LICENSE_END
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
36 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" 2007-06-08 mtk: Added example program
38 .\" 2007-07-05 mtk: Added details on underlying system call interfaces
39 .\"
40 .TH STAT 2 2015-07-23 "Linux" "Linux Programmer's Manual"
41 .SH NAME
42 stat, fstat, lstat, fstatat \- get file status
43 .SH SYNOPSIS
44 .nf
45 .B #include <sys/types.h>
46 .br
47 .B #include <sys/stat.h>
48 .br
49 .B #include <unistd.h>
50 .sp
51 .BI "int stat(const char *" pathname ", struct stat *" buf );
52 .br
53 .BI "int fstat(int " fd ", struct stat *" buf );
54 .br
55 .BI "int lstat(const char *" pathname ", struct stat *" buf );
56 .sp
57 .BR "#include <fcntl.h> " "/* Definition of AT_* constants */"
58 .B #include <sys/stat.h>
59 .sp
60 .BI "int fstatat(int " dirfd ", const char *" pathname ", struct stat *" \
61 buf ,
62 .BI " int " flags );
63 .fi
64 .sp
65 .in -4n
66 Feature Test Macro Requirements for glibc (see
67 .BR feature_test_macros (7)):
68 .in
69 .ad l
70 .PD 0
71 .sp
72 .BR lstat ():
73 .RS 4
74 /* glibc 2.19 and earlier */ _BSD_SOURCE
75 .br
76 || /* Since glibc 2.20 */ _DEFAULT_SOURCE
77 .br
78 || _XOPEN_SOURCE\ >=\ 500
79 .\" _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
80 .br
81 || /* Since glibc 2.10: */ _POSIX_C_SOURCE\ >=\ 200112L
82 .RE
83 .sp
84 .BR fstatat ():
85 .PD 0
86 .ad l
87 .RS 4
88 .TP 4
89 Since glibc 2.10:
90 _POSIX_C_SOURCE\ >=\ 200809L
91 .TP
92 Before glibc 2.10:
93 _ATFILE_SOURCE
94 .RE
95 .PD
96 .ad
97 .SH DESCRIPTION
98 .PP
99 These functions return information about a file, in the buffer pointed to by
100 .IR buf .
101 No permissions are required on the file itself, but\(emin the case of
102 .BR stat (),
103 .BR fstatat (),
104 and
105 .BR lstat ()\(emexecute
106 (search) permission is required on all of the directories in
107 .I pathname
108 that lead to the file.
109 .PP
110 .BR stat ()
111 and
112 .BR fstatat ()
113 retrieve information about the file pointed to by
114 .IR pathname ;
115 the differences for
116 .BR fstatat ()
117 are described below.
118
119 .BR lstat ()
120 is identical to
121 .BR stat (),
122 except that if
123 .I pathname
124 is a symbolic link, then it returns information about the link itself,
125 not the file that it refers to.
126
127 .BR fstat ()
128 is identical to
129 .BR stat (),
130 except that the file about which information is to be retrieved
131 is specified by the file descriptor
132 .IR fd .
133 .PP
134 All of these system calls return a
135 .I stat
136 structure, which contains the following fields:
137 .PP
138 .in +4n
139 .nf
140 struct stat {
141 dev_t st_dev; /* ID of device containing file */
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; /* blocksize for filesystem I/O */
150 blkcnt_t st_blocks; /* number of 512B blocks allocated */
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
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 */
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
163 };
164 .fi
165 .in
166
167 .I Note:
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.
175 Consult the glibc and kernel source code
176 if you need to know the details.
177
178 .\" Background: inode attributes are modified with i_mutex held, but
179 .\" read by stat() without taking the mutex.
180 .I Note:
181 For performance and simplicity reasons, different fields in the
182 .I stat
183 structure may contain state information from different moments
184 during the execution of the system call.
185 For example, if
186 .IR st_mode
187 or
188 .IR st_uid
189 is changed by another process by calling
190 .BR chmod (2)
191 or
192 .BR chown (2),
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
203 The
204 .I st_dev
205 field describes the device on which this file resides.
206 (The
207 .BR major (3)
208 and
209 .BR minor (3)
210 macros may be useful to decompose the device ID in this field.)
211
212 The
213 .I st_rdev
214 field describes the device that this file (inode) represents.
215
216 The
217 .I st_size
218 field gives the size of the file (if it is a regular
219 file or a symbolic link) in bytes.
220 The size of a symbolic link is the length of the pathname
221 it contains, without a terminating null byte.
222
223 The
224 .I st_blocks
225 field indicates the number of blocks allocated to the file, 512-byte units.
226 (This may be smaller than
227 .IR st_size /512
228 when the file has holes.)
229
230 The
231 .I st_blksize
232 field gives the "preferred" blocksize for efficient filesystem I/O.
233 (Writing to a file in smaller chunks may cause
234 an inefficient read-modify-rewrite.)
235 .PP
236 Not all of the Linux filesystems implement all of the time fields.
237 Some filesystem types allow mounting in such a way that file
238 and/or directory accesses do not cause an update of the
239 .I st_atime
240 field.
241 (See
242 .IR noatime ,
243 .IR nodiratime ,
244 and
245 .I relatime
246 in
247 .BR mount (8),
248 and related information in
249 .BR mount (2).)
250 In addition,
251 .I st_atime
252 is not updated if a file is opened with the
253 .BR O_NOATIME ;
254 see
255 .BR open (2).
256
257 The field
258 .I st_atime
259 is changed by file accesses, for example, by
260 .BR execve (2),
261 .BR mknod (2),
262 .BR pipe (2),
263 .BR utime (2),
264 and
265 .BR read (2)
266 (of more than zero bytes).
267 Other routines, like
268 .BR mmap (2),
269 may or may not update
270 .IR st_atime .
271
272 The field
273 .I st_mtime
274 is changed by file modifications, for example, by
275 .BR mknod (2),
276 .BR truncate (2),
277 .BR utime (2),
278 and
279 .BR write (2)
280 (of more than zero bytes).
281 Moreover,
282 .I st_mtime
283 of a directory is changed by the creation or deletion of files
284 in that directory.
285 The
286 .I st_mtime
287 field is
288 .I not
289 changed for changes in owner, group, hard link count, or mode.
290
291 The field
292 .I st_ctime
293 is changed by writing or by setting inode information
294 (i.e., owner, group, link count, mode, etc.).
295 .PP
296 POSIX refers to the
297 .I st_mode
298 bits corresponding to the mask
299 .B S_IFMT
300 (see below) as the
301 .IR "file type" ,
302 the 12 bits corresponding to the mask 07777 as the
303 .IR "file mode bits"
304 and the least significant 9 bits (0777) as the
305 .IR "file permission bits" .
306 .PP
307 The following mask values are defined for the file type of the
308 .I st_mode
309 field:
310 .in +4n
311 .TS
312 lB l l.
313 S_IFMT 0170000 bit mask for the file type bit field
314
315 S_IFSOCK 0140000 socket
316 S_IFLNK 0120000 symbolic link
317 S_IFREG 0100000 regular file
318 S_IFBLK 0060000 block device
319 S_IFDIR 0040000 directory
320 S_IFCHR 0020000 character device
321 S_IFIFO 0010000 FIFO
322 .TE
323 .in
324 .PP
325 Thus, to test for a regular file (for example), one could write:
326
327 .nf
328 .in +4n
329 stat(pathname, &sb);
330 if ((sb.st_mode & S_IFMT) == S_IFREG) {
331 /* Handle regular file */
332 }
333 .in
334 .fi
335 .PP
336 Because tests of the above form are common, additional
337 macros are defined by POSIX to allow the test of the file type in
338 .I st_mode
339 to be written more concisely:
340 .RS 4
341 .TP 1.2i
342 .BR S_ISREG (m)
343 is it a regular file?
344 .TP
345 .BR S_ISDIR (m)
346 directory?
347 .TP
348 .BR S_ISCHR (m)
349 character device?
350 .TP
351 .BR S_ISBLK (m)
352 block device?
353 .TP
354 .BR S_ISFIFO (m)
355 FIFO (named pipe)?
356 .TP
357 .BR S_ISLNK (m)
358 symbolic link? (Not in POSIX.1-1996.)
359 .TP
360 .BR S_ISSOCK (m)
361 socket? (Not in POSIX.1-1996.)
362 .RE
363 .PP
364 The preceding code snippet could thus be rewritten as:
365
366 .nf
367 .in +4n
368 stat(pathname, &sb);
369 if (S_ISREG(sb.st_mode)) {
370 /* Handle regular file */
371 }
372 .in
373 .fi
374 .PP
375 The definitions of most of the above file type test macros
376 are provided if any of the following feature test macros is defined:
377 .BR _BSD_SOURCE
378 (in glibc 2.19 and earlier),
379 .BR _SVID_SOURCE
380 (in glibc 2.19 and earlier),
381 or
382 .BR _DEFAULT_SOURCE
383 (in glibc 2.20 and later).
384 In addition, definitions of all of the above macros except
385 .BR S_IFSOCK
386 and
387 .BR S_ISSOCK ()
388 are provided if
389 .BR _XOPEN_SOURCE
390 is defined.
391 The definition of
392 .BR S_IFSOCK
393 can also be exposed by defining
394 .BR _XOPEN_SOURCE
395 with a value of 500 or greater.
396
397 The definition of
398 .BR S_ISSOCK ()
399 is exposed if any of the following feature test macros is defined:
400 .BR _BSD_SOURCE
401 (in glibc 2.19 and earlier),
402 .BR _DEFAULT_SOURCE
403 (in glibc 2.20 and later),
404 .BR _XOPEN_SOURCE
405 with a value of 500 or greater, or
406 .BR _POSIX_C_SOURCE
407 with a value of 200112L or greater.
408 .PP
409 The following mask values are defined for
410 the file mode component of the
411 .I st_mode
412 field:
413 .in +4n
414 .TS
415 lB l l.
416 S_ISUID 04000 set-user-ID bit
417 S_ISGID 02000 set-group-ID bit (see below)
418 S_ISVTX 01000 sticky bit (see below)
419
420 S_IRWXU 00700 owner has read, write, and execute permission
421 S_IRUSR 00400 owner has read permission
422 S_IWUSR 00200 owner has write permission
423 S_IXUSR 00100 owner has execute permission
424
425 S_IRWXG 00070 group has read, write, and execute permission
426 S_IRGRP 00040 group has read permission
427 S_IWGRP 00020 group has write permission
428 S_IXGRP 00010 group has execute permission
429
430 S_IRWXO 00007 T{
431 others (not in group) have read, write, and execute permission
432 T}
433 S_IROTH 00004 others have read permission
434 S_IWOTH 00002 others have write permission
435 S_IXOTH 00001 others have execute permission
436 .TE
437 .in
438 .P
439 The set-group-ID bit
440 .RB ( S_ISGID )
441 has several special uses.
442 For a directory, it indicates that BSD semantics is to be used
443 for that directory: files created there inherit their group ID from
444 the directory, not from the effective group ID of the creating process,
445 and directories created there will also get the
446 .B S_ISGID
447 bit set.
448 For a file that does not have the group execution bit
449 .RB ( S_IXGRP )
450 set,
451 the set-group-ID bit indicates mandatory file/record locking.
452 .P
453 The sticky bit
454 .RB ( S_ISVTX )
455 on a directory means that a file
456 in that directory can be renamed or deleted only by the owner
457 of the file, by the owner of the directory, and by a privileged
458 process.
459 .\"
460 .\"
461 .SS fstatat()
462 The
463 .BR fstatat ()
464 system call operates in exactly the same way as
465 .BR stat (),
466 except for the differences described here.
467
468 If the pathname given in
469 .I pathname
470 is relative, then it is interpreted relative to the directory
471 referred to by the file descriptor
472 .I dirfd
473 (rather than relative to the current working directory of
474 the calling process, as is done by
475 .BR stat ()
476 for a relative pathname).
477
478 If
479 .I pathname
480 is relative and
481 .I dirfd
482 is the special value
483 .BR AT_FDCWD ,
484 then
485 .I pathname
486 is interpreted relative to the current working
487 directory of the calling process (like
488 .BR stat ()).
489
490 If
491 .I pathname
492 is absolute, then
493 .I dirfd
494 is ignored.
495
496 .I flags
497 can either be 0, or include one or more of the following flags ORed:
498 .TP
499 .BR AT_EMPTY_PATH " (since Linux 2.6.39)"
500 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
501 If
502 .I pathname
503 is an empty string, operate on the file referred to by
504 .IR dirfd
505 (which may have been obtained using the
506 .BR open (2)
507 .B O_PATH
508 flag).
509 If
510 .I dirfd
511 is
512 .BR AT_FDCWD ,
513 the call operates on the current working directory.
514 In this case,
515 .I dirfd
516 can refer to any type of file, not just a directory.
517 This flag is Linux-specific; define
518 .B _GNU_SOURCE
519 .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
520 to obtain its definition.
521 .TP
522 .BR AT_NO_AUTOMOUNT " (since Linux 2.6.38)"
523 Don't automount the terminal ("basename") component of
524 .I pathname
525 if it is a directory that is an automount point.
526 This allows the caller to gather attributes of an automount point
527 (rather than the location it would mount).
528 This flag can be used in tools that scan directories
529 to prevent mass-automounting of a directory of automount points.
530 The
531 .B AT_NO_AUTOMOUNT
532 flag has no effect if the mount point has already been mounted over.
533 This flag is Linux-specific; define
534 .B _GNU_SOURCE
535 .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
536 to obtain its definition.
537 .TP
538 .B AT_SYMLINK_NOFOLLOW
539 If
540 .I pathname
541 is a symbolic link, do not dereference it:
542 instead return information about the link itself, like
543 .BR lstat ().
544 (By default,
545 .BR fstatat ()
546 dereferences symbolic links, like
547 .BR stat ().)
548 .PP
549 See
550 .BR openat (2)
551 for an explanation of the need for
552 .BR fstatat ().
553 .SH RETURN VALUE
554 On success, zero is returned.
555 On error, \-1 is returned, and
556 .I errno
557 is set appropriately.
558 .SH ERRORS
559 .TP
560 .B EACCES
561 Search permission is denied for one of the directories
562 in the path prefix of
563 .IR pathname .
564 (See also
565 .BR path_resolution (7).)
566 .TP
567 .B EBADF
568 .I fd
569 is not a valid open file descriptor.
570 .TP
571 .B EFAULT
572 Bad address.
573 .TP
574 .B ELOOP
575 Too many symbolic links encountered while traversing the path.
576 .TP
577 .B ENAMETOOLONG
578 .I pathname
579 is too long.
580 .TP
581 .B ENOENT
582 A component of
583 .I pathname
584 does not exist, or
585 .I pathname
586 is an empty string.
587 .TP
588 .B ENOMEM
589 Out of memory (i.e., kernel memory).
590 .TP
591 .B ENOTDIR
592 A component of the path prefix of
593 .I pathname
594 is not a directory.
595 .TP
596 .B EOVERFLOW
597 .I pathname
598 or
599 .I fd
600 refers to a file whose size, inode number,
601 or number of blocks cannot be represented in, respectively, the types
602 .IR off_t ,
603 .IR ino_t ,
604 or
605 .IR blkcnt_t .
606 This error can occur when, for example,
607 an application compiled on a 32-bit platform without
608 .I -D_FILE_OFFSET_BITS=64
609 calls
610 .BR stat ()
611 on a file whose size exceeds
612 .I (1<<31)-1
613 bytes.
614 .PP
615 The following additional errors can occur for
616 .BR fstatat ():
617 .TP
618 .B EBADF
619 .I dirfd
620 is not a valid file descriptor.
621 .TP
622 .B EINVAL
623 Invalid flag specified in
624 .IR flags .
625 .TP
626 .B ENOTDIR
627 .I pathname
628 is relative and
629 .I dirfd
630 is a file descriptor referring to a file other than a directory.
631 .SH VERSIONS
632 .BR fstatat ()
633 was added to Linux in kernel 2.6.16;
634 library support was added to glibc in version 2.4.
635 .SH CONFORMING TO
636 .BR stat (),
637 .BR fstat (),
638 .BR lstat ():
639 SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008.
640 .\" SVr4 documents additional
641 .\" .BR fstat ()
642 .\" error conditions EINTR, ENOLINK, and EOVERFLOW. SVr4
643 .\" documents additional
644 .\" .BR stat ()
645 .\" and
646 .\" .BR lstat ()
647 .\" error conditions EINTR, EMULTIHOP, ENOLINK, and EOVERFLOW.
648
649 .BR fstatat ():
650 POSIX.1-2008.
651
652 According to POSIX.1-2001,
653 .BR lstat ()
654 on a symbolic link need return valid information only in the
655 .I st_size
656 field and the file type of the
657 .IR st_mode
658 field of the
659 .IR stat
660 structure.
661 POSIX.1-2008 tightens the specification, requiring
662 .BR lstat ()
663 to return valid information in all fields except the mode bits in
664 .IR st_mode .
665
666 Use of the
667 .I st_blocks
668 and
669 .I st_blksize
670 fields may be less portable.
671 (They were introduced in BSD.
672 The interpretation differs between systems,
673 and possibly on a single system when NFS mounts are involved.)
674 If you need to obtain the definition of the
675 .IR blkcnt_t
676 or
677 .IR blksize_t
678 types from
679 .IR <sys/stat.h> ,
680 then define
681 .BR _XOPEN_SOURCE
682 with the value 500 or greater (before including
683 .I any
684 header files).
685 .LP
686 POSIX.1-1990 did not describe the
687 .BR S_IFMT ,
688 .BR S_IFSOCK ,
689 .BR S_IFLNK ,
690 .BR S_IFREG ,
691 .BR S_IFBLK ,
692 .BR S_IFDIR ,
693 .BR S_IFCHR ,
694 .BR S_IFIFO ,
695 .B S_ISVTX
696 constants, but instead demanded the use of
697 the macros
698 .BR S_ISDIR (),
699 and so on.
700 The
701 .BR S_IF*
702 constants are present in POSIX.1-2001 and later.
703
704 The
705 .BR S_ISLNK ()
706 and
707 .BR S_ISSOCK ()
708 macros are not in
709 POSIX.1-1996, but both are present in POSIX.1-2001;
710 the former is from SVID 4, the latter from SUSv2.
711 .LP
712 UNIX\ V7 (and later systems) had
713 .BR S_IREAD ,
714 .BR S_IWRITE ,
715 .BR S_IEXEC ,
716 where POSIX
717 prescribes the synonyms
718 .BR S_IRUSR ,
719 .BR S_IWUSR ,
720 .BR S_IXUSR .
721 .SS Other systems
722 Values that have been (or are) in use on various systems:
723 .ad l
724 .TS
725 l l l l l.
726 hex name ls octal description
727 f000 S_IFMT 170000 mask for file type
728 0000 000000 T{
729 SCO out-of-service inode; BSD unknown type; SVID-v2 and XPG2
730 have both 0 and 0100000 for ordinary file
731 T}
732 1000 S_IFIFO p| 010000 FIFO (named pipe)
733 2000 S_IFCHR c 020000 character special (V7)
734 3000 S_IFMPC 030000 multiplexed character special (V7)
735 4000 S_IFDIR d/ 040000 directory (V7)
736 5000 S_IFNAM 050000 T{
737 XENIX named special file with two subtypes, distinguished by
738 \fIst_rdev\fP values 1, 2
739 T}
740 0001 S_INSEM s 000001 XENIX semaphore subtype of IFNAM
741 0002 S_INSHD m 000002 XENIX shared data subtype of IFNAM
742 6000 S_IFBLK b 060000 block special (V7)
743 7000 S_IFMPB 070000 multiplexed block special (V7)
744 8000 S_IFREG - 100000 regular (V7)
745 9000 S_IFCMP 110000 VxFS compressed
746 9000 S_IFNWK n 110000 network special (HP-UX)
747 a000 S_IFLNK l@ 120000 symbolic link (BSD)
748 b000 S_IFSHAD 130000 T{
749 Solaris shadow inode for ACL (not seen by user space)
750 T}
751 c000 S_IFSOCK s= 140000 socket (BSD; also "S_IFSOC" on VxFS)
752 d000 S_IFDOOR D> 150000 Solaris door
753 e000 S_IFWHT w% 160000 BSD whiteout (not used for inode)
754 0200 S_ISVTX 001000 T{
755 sticky bit: save swapped text even after use (V7)
756 .br
757 reserved (SVID-v2)
758 .br
759 On nondirectories: don't cache this file (SunOS)
760 .br
761 On directories: restricted deletion flag (SVID-v4.2)
762 T}
763 0400 S_ISGID 002000 T{
764 set-group-ID on execution (V7)
765 .br
766 for directories: use BSD semantics for propagation of GID
767 T}
768 0400 S_ENFMT 002000 T{
769 System V file locking enforcement (shared with S_ISGID)
770 T}
771 0800 S_ISUID 004000 set-user-ID on execution (V7)
772 0800 S_CDF 004000 T{
773 directory is a context dependent file (HP-UX)
774 T}
775 .TE
776 .ad
777
778 A sticky command appeared in Version 32V AT&T UNIX.
779 .SH NOTES
780 On Linux,
781 .BR lstat ()
782 will generally not trigger automounter action, whereas
783 .BR stat ()
784 will (but see
785 .BR fstatat (2)).
786
787 For most files under the
788 .I /proc
789 directory,
790 .BR stat ()
791 does not return the file size in the
792 .I st_size
793 field; instead the field is returned with the value 0.
794 .SS Timestamp fields
795 Older kernels and older standards did not support nanosecond timestamp
796 fields.
797 Instead, there were three timestamp
798 .RI fields\(em st_atime ,
799 .IR st_mtime ,
800 and
801 .IR st_ctime \(emtyped
802 as
803 .IR time_t
804 that recorded timestamps with one-second precision.
805
806 Since kernel 2.5.48, the
807 .I stat
808 structure supports nanosecond resolution for the three file timestamp fields.
809 The nanosecond components of each timestamp are available
810 via names of the form
811 .IR st_atim.tv_nsec
812 if the
813 .B _BSD_SOURCE
814 or
815 .B _SVID_SOURCE
816 feature test macro is defined.
817 Nanosecond timestamps are nowadays standardized,
818 starting with POSIX.1-2008, and, starting with version 2.12,
819 glibc also exposes the nanosecond component names if
820 .BR _POSIX_C_SOURCE
821 is defined with the value 200809L or greater, or
822 .BR _XOPEN_SOURCE
823 is defined with the value 700 or greater.
824 If none of the aforementioned macros are defined,
825 then the nanosecond values are exposed with names of the form
826 .IR st_atimensec .
827
828 Nanosecond timestamps are supported on XFS, JFS, Btrfs, and
829 ext4 (since Linux 2.6.23).
830 .\" commit ef7f38359ea8b3e9c7f2cae9a4d4935f55ca9e80
831 Nanosecond timestamps are not supported in ext2, ext3, and Reiserfs.
832 On filesystems that do not support subsecond timestamps,
833 the nanosecond fields are returned with the value 0.
834 .SS C library/kernel differences
835 Over time, increases in the size of the
836 .I stat
837 structure have led to three successive versions of
838 .BR stat ():
839 .IR sys_stat ()
840 (slot
841 .IR __NR_oldstat ),
842 .IR sys_newstat ()
843 (slot
844 .IR __NR_stat ),
845 and
846 .I sys_stat64()
847 (slot
848 .IR __NR_stat64 )
849 on 32-bit platforms such as i386.
850 The first two versions were already present in Linux 1.0
851 (albeit with different names);
852 .\" See include/asm-i386/stat.h in the Linux 2.4 source code for the
853 .\" various versions of the structure definitions
854 the last was added in Linux 2.4.
855 Similar remarks apply for
856 .BR fstat ()
857 and
858 .BR lstat ().
859
860 The kernel-internal versions of the
861 .I stat
862 structure dealt with by the different versions are, respectively:
863 .RS
864 .TP
865 .IR __old_kernel_stat
866 The original structure, with rather narrow fields, and no padding.
867 .TP
868 .IR stat
869 Larger
870 .I st_ino
871 field and padding added to various parts of the structure to
872 allow for future expansion.
873 .TP
874 .IR stat64
875 Even larger
876 .I st_ino
877 field,
878 larger
879 .I st_uid
880 and
881 .I st_gid
882 fields to accommodate the Linux-2.4 expansion of UIDs and GIDs to 32 bits,
883 and various other enlarged fields and further padding in the structure.
884 (Various padding bytes were eventually consumed in Linux 2.6,
885 with the advent of 32-bit device IDs and nanosecond components
886 for the timestamp fields.)
887 .RE
888 .PP
889 The glibc
890 .BR stat ()
891 wrapper function hides these details from applications,
892 invoking the most recent version of the system call provided by the kernel,
893 and repacking the returned information if required for old binaries.
894 .\"
895 .\" A note from Andries Brouwer, July 2007
896 .\"
897 .\" > Is the story not rather more complicated for some calls like
898 .\" > stat(2)?
899 .\"
900 .\" Yes and no, mostly no. See /usr/include/sys/stat.h .
901 .\"
902 .\" The idea is here not so much that syscalls change, but that
903 .\" the definitions of struct stat and of the types dev_t and mode_t change.
904 .\" This means that libc (even if it does not call the kernel
905 .\" but only calls some internal function) must know what the
906 .\" format of dev_t or of struct stat is.
907 .\" The communication between the application and libc goes via
908 .\" the include file <sys/stat.h> that defines a _STAT_VER and
909 .\" _MKNOD_VER describing the layout of the data that user space
910 .\" uses. Each (almost each) occurrence of stat() is replaced by
911 .\" an occurrence of xstat() where the first parameter of xstat()
912 .\" is this version number _STAT_VER.
913 .\"
914 .\" Now, also the definitions used by the kernel change.
915 .\" But glibc copes with this in the standard way, and the
916 .\" struct stat as returned by the kernel is repacked into
917 .\" the struct stat as expected by the application.
918 .\" Thus, _STAT_VER and this setup cater for the application-libc
919 .\" interface, rather than the libc-kernel interface.
920 .\"
921 .\" (Note that the details depend on gcc being used as c compiler.)
922
923 On modern 64-bit systems, life is simpler: there is a single
924 .BR stat ()
925 system call and the kernel deals with a
926 .I stat
927 structure that contains fields of a sufficient size.
928
929 The underlying system call employed by the glibc
930 .BR fstatat ()
931 wrapper function is actually called
932 .BR fstatat64 ()
933 or, on some architectures,
934 .\" strace(1) shows the name "newfstatat" on x86-64
935 .BR newfstatat ().
936 .SH EXAMPLE
937 The following program calls
938 .BR stat ()
939 and displays selected fields in the returned
940 .I stat
941 structure.
942 .nf
943
944 #include <sys/types.h>
945 #include <sys/stat.h>
946 #include <time.h>
947 #include <stdio.h>
948 #include <stdlib.h>
949
950 int
951 main(int argc, char *argv[])
952 {
953 struct stat sb;
954
955 if (argc != 2) {
956 fprintf(stderr, "Usage: %s <pathname>\\n", argv[0]);
957 exit(EXIT_FAILURE);
958 }
959
960 if (stat(argv[1], &sb) == \-1) {
961 perror("stat");
962 exit(EXIT_FAILURE);
963 }
964
965 printf("File type: ");
966
967 switch (sb.st_mode & S_IFMT) {
968 case S_IFBLK: printf("block device\\n"); break;
969 case S_IFCHR: printf("character device\\n"); break;
970 case S_IFDIR: printf("directory\\n"); break;
971 case S_IFIFO: printf("FIFO/pipe\\n"); break;
972 case S_IFLNK: printf("symlink\\n"); break;
973 case S_IFREG: printf("regular file\\n"); break;
974 case S_IFSOCK: printf("socket\\n"); break;
975 default: printf("unknown?\\n"); break;
976 }
977
978 printf("I\-node number: %ld\\n", (long) sb.st_ino);
979
980 printf("Mode: %lo (octal)\\n",
981 (unsigned long) sb.st_mode);
982
983 printf("Link count: %ld\\n", (long) sb.st_nlink);
984 printf("Ownership: UID=%ld GID=%ld\\n",
985 (long) sb.st_uid, (long) sb.st_gid);
986
987 printf("Preferred I/O block size: %ld bytes\\n",
988 (long) sb.st_blksize);
989 printf("File size: %lld bytes\\n",
990 (long long) sb.st_size);
991 printf("Blocks allocated: %lld\\n",
992 (long long) sb.st_blocks);
993
994 printf("Last status change: %s", ctime(&sb.st_ctime));
995 printf("Last file access: %s", ctime(&sb.st_atime));
996 printf("Last file modification: %s", ctime(&sb.st_mtime));
997
998 exit(EXIT_SUCCESS);
999 }
1000 .fi
1001 .SH SEE ALSO
1002 .BR ls (1),
1003 .BR stat (1),
1004 .BR access (2),
1005 .BR chmod (2),
1006 .BR chown (2),
1007 .BR readlink (2),
1008 .BR utime (2),
1009 .BR capabilities (7),
1010 .BR symlink (7)