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