]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/stat.2
stat.2: NOTES: add "C library/kernel ABI differences" subheading
[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.\"
8980a500 40.TH STAT 2 2014-08-19 "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
afb9bd5d
MK
74/* glibc 2.19 and earlier */ _BSD_SOURCE ||
75.br
76/* Since glibc 2.20 */_DEFAULT_SOURCE ||
77.br
78_XOPEN_SOURCE\ >=\ 500 ||
43e0fe7d 79_XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
3ba63d80
MK
80.br
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:
90_XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
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
MK
99These functions return information about a file, in the buffer pointed to by
100.IR stat .
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 */
143 mode_t st_mode; /* protection */
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.
175Consult the the glibc and kernel source code
176if you need to know the details.
177
da2d9dad 178The
29de83af 179.I st_dev
da2d9dad 180field describes the device on which this file resides.
e9ef777f
MK
181(The
182.BR major (3)
183and
184.BR minor (3)
185macros may be useful to decompose the device ID in this field.)
da2d9dad
MK
186
187The
188.I st_rdev
189field describes the device that this file (inode) represents.
190
191The
fea681da 192.I st_size
c13182ef
MK
193field gives the size of the file (if it is a regular
194file or a symbolic link) in bytes.
280ff209
MK
195The size of a symbolic link is the length of the pathname
196it contains, without a terminating null byte.
fea681da 197
da2d9dad 198The
fea681da 199.I st_blocks
32f30015 200field indicates the number of blocks allocated to the file, 512-byte units.
fea681da 201(This may be smaller than
eee0a2ec
MK
202.IR st_size /512
203when the file has holes.)
da2d9dad
MK
204
205The
0daa9e92 206.I st_blksize
9ee4a2b6 207field gives the "preferred" blocksize for efficient filesystem I/O.
fea681da
MK
208(Writing to a file in smaller chunks may cause
209an inefficient read-modify-rewrite.)
210.PP
9ee4a2b6
MK
211Not all of the Linux filesystems implement all of the time fields.
212Some filesystem types allow mounting in such a way that file
86d89e4c 213and/or directory accesses do not cause an update of the
fea681da 214.I st_atime
c13182ef 215field.
86d89e4c
MK
216(See
217.IR noatime ,
218.IR nodiratime ,
219and
220.I relatime
221in
222.BR mount (8),
223and related information in
224.BR mount (2).)
14adeb6d
MK
225In addition,
226.I st_atime
227is not updated if a file is opened with the
228.BR O_NOATIME ;
229see
230.BR open (2).
fea681da
MK
231
232The field
233.I st_atime
75b94dc3 234is changed by file accesses, for example, by
fea681da
MK
235.BR execve (2),
236.BR mknod (2),
237.BR pipe (2),
9af134cd 238.BR utime (2),
fea681da
MK
239and
240.BR read (2)
7c93fec0
MK
241(of more than zero bytes).
242Other routines, like
fea681da
MK
243.BR mmap (2),
244may or may not update
245.IR st_atime .
246
247The field
248.I st_mtime
75b94dc3 249is changed by file modifications, for example, by
fea681da
MK
250.BR mknod (2),
251.BR truncate (2),
9af134cd 252.BR utime (2),
fea681da
MK
253and
254.BR write (2)
255(of more than zero bytes).
256Moreover,
257.I st_mtime
258of a directory is changed by the creation or deletion of files
259in that directory.
260The
261.I st_mtime
262field is
263.I not
264changed for changes in owner, group, hard link count, or mode.
265
266The field
267.I st_ctime
268is changed by writing or by setting inode information
269(i.e., owner, group, link count, mode, etc.).
270.PP
ff3976ce
MK
271The following mask values are defined for the file type component of the
272.I st_mode
273field:
274.in +4n
275.TS
276lB l l.
277S_IFMT 0170000 bit mask for the file type bit fields
278
279S_IFSOCK 0140000 socket
280S_IFLNK 0120000 symbolic link
281S_IFREG 0100000 regular file
282S_IFBLK 0060000 block device
283S_IFDIR 0040000 directory
284S_IFCHR 0020000 character device
285S_IFIFO 0010000 FIFO
286.TE
287.in
288.PP
3779bbef
MK
289Thus, to test for a regular file (for example), one could write:
290
291.nf
292.in +4n
293stat(pathname, &sb);
294if ((sb.st_mode & S_IFMT) == S_IFREG) {
295 /* Handle regular file */
296}
297.in
298.fi
299.PP
300Because tests of the above form are common, additional
301macros are defined by POSIX to allow the test of the file type in
168df940 302.I st_mode
3779bbef 303to be written more concisely:
bd191423 304.RS 4
fea681da 305.TP 1.2i
c91e381d 306.BR S_ISREG (m)
fea681da
MK
307is it a regular file?
308.TP
c91e381d 309.BR S_ISDIR (m)
fea681da
MK
310directory?
311.TP
c91e381d 312.BR S_ISCHR (m)
fea681da
MK
313character device?
314.TP
c91e381d 315.BR S_ISBLK (m)
fea681da
MK
316block device?
317.TP
c91e381d 318.BR S_ISFIFO (m)
da2d9dad 319FIFO (named pipe)?
fea681da 320.TP
c91e381d 321.BR S_ISLNK (m)
77b99918 322symbolic link? (Not in POSIX.1-1996.)
fea681da 323.TP
c91e381d 324.BR S_ISSOCK (m)
77b99918 325socket? (Not in POSIX.1-1996.)
fea681da
MK
326.RE
327.PP
3779bbef
MK
328The preceding code snippet could thus be rewritten as:
329
330.nf
331.in +4n
332stat(pathname, &sb);
333if (S_ISREG(sb.st_mode)) {
334 /* Handle regular file */
335}
336.in
337.fi
338.PP
339The definitions of most of the above file type test macros
cc2b44eb 340are provided if any of the following feature test macros is defined:
3779bbef
MK
341.BR _BSD_SOURCE
342(in glibc 2.19 and earlier),
343.BR _SVID_SOURCE
344(in glibc 2.19 and earlier),
345or
346.BR _DEFAULT_SOURCE
347(in glibc 2.20 and later).
348In addition, definitions of all of the above macros except
349.BR S_IFSOCK
350and
351.BR S_ISSOCK ()
352are provided if
353.BR _XOPEN_SOURCE
354is defined.
355The definition of
356.BR S_IFSOCK
357can also be exposed by defining
358.BR _XOPEN_SOURCE
359with a value of 500 or greater.
360
361The definition of
362.BR S_ISSOCK ()
363is exposed if any of the following feature test macros is defined:
364.BR _BSD_SOURCE
365(in glibc 2.19 and earlier),
366.BR _DEFAULT_SOURCE
367(in glibc 2.20 and later),
368.BR _XOPEN_SOURCE
369with a value of 500 or greater, or
370.BR _POSIX_C_SOURCE
371with a value of 200112L or greater.
372.PP
ff0afc3f
MK
373The following mask values are defined for
374the file permissions component of the
375.I st_mode
376field:
377.in +4n
378.TS
379lB l l.
0d24f47f 380S_ISUID 0004000 set-user-ID bit
da2d9dad 381S_ISGID 0002000 set-group-ID bit (see below)
fea681da 382S_ISVTX 0001000 sticky bit (see below)
ff0afc3f 383
51c6aa6e
MK
384S_IRWXU 00700 mask for file owner permissions
385S_IRUSR 00400 owner has read permission
386S_IWUSR 00200 owner has write permission
387S_IXUSR 00100 owner has execute permission
ff0afc3f 388
51c6aa6e
MK
389S_IRWXG 00070 mask for group permissions
390S_IRGRP 00040 group has read permission
391S_IWGRP 00020 group has write permission
392S_IXGRP 00010 group has execute permission
ff0afc3f 393
51c6aa6e 394S_IRWXO 00007 T{
a211773a
MK
395mask for permissions for others (not in group)
396T}
51c6aa6e
MK
397S_IROTH 00004 others have read permission
398S_IWOTH 00002 others have write permission
399S_IXOTH 00001 others have execute permission
fea681da 400.TE
bd191423 401.in
fea681da 402.P
2f0af33b
MK
403The set-group-ID bit
404.RB ( S_ISGID )
405has several special uses.
5c3122b1 406For a directory, it indicates that BSD semantics is to be used
fea681da
MK
407for that directory: files created there inherit their group ID from
408the directory, not from the effective group ID of the creating process,
2f0af33b
MK
409and directories created there will also get the
410.B S_ISGID
411bit set.
66ee0c7e 412For a file that does not have the group execution bit
2f0af33b
MK
413.RB ( S_IXGRP )
414set,
da2d9dad 415the set-group-ID bit indicates mandatory file/record locking.
fea681da 416.P
2d986c92 417The sticky bit
2f0af33b
MK
418.RB ( S_ISVTX )
419on a directory means that a file
fea681da
MK
420in that directory can be renamed or deleted only by the owner
421of the file, by the owner of the directory, and by a privileged
422process.
40084043
MK
423.\"
424.\"
1fef0fa3 425.SS fstatat()
40084043
MK
426The
427.BR fstatat ()
428system call operates in exactly the same way as
cadd38ba 429.BR stat (),
40084043
MK
430except for the differences described here.
431
432If the pathname given in
433.I pathname
434is relative, then it is interpreted relative to the directory
435referred to by the file descriptor
436.I dirfd
437(rather than relative to the current working directory of
438the calling process, as is done by
cadd38ba 439.BR stat ()
40084043
MK
440for a relative pathname).
441
442If
443.I pathname
444is relative and
445.I dirfd
446is the special value
447.BR AT_FDCWD ,
448then
449.I pathname
450is interpreted relative to the current working
451directory of the calling process (like
cadd38ba 452.BR stat ()).
40084043
MK
453
454If
455.I pathname
456is absolute, then
457.I dirfd
458is ignored.
459
460.I flags
461can either be 0, or include one or more of the following flags ORed:
462.TP
463.BR AT_EMPTY_PATH " (since Linux 2.6.39)"
464.\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
465If
466.I pathname
467is an empty string, operate on the file referred to by
468.IR dirfd
469(which may have been obtained using the
470.BR open (2)
471.B O_PATH
472flag).
a6fcbdf8
MK
473If
474.I dirfd
475is
476.BR AT_FDCWD ,
477the call operates on the current working directory.
40084043
MK
478In this case,
479.I dirfd
480can refer to any type of file, not just a directory.
481This flag is Linux-specific; define
482.B _GNU_SOURCE
483.\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
484to obtain its definition.
485.TP
486.BR AT_NO_AUTOMOUNT " (since Linux 2.6.38)"
487Don't automount the terminal ("basename") component of
488.I pathname
489if it is a directory that is an automount point.
490This allows the caller to gather attributes of an automount point
491(rather than the location it would mount).
492This flag can be used in tools that scan directories
493to prevent mass-automounting of a directory of automount points.
494The
495.B AT_NO_AUTOMOUNT
496flag has no effect if the mount point has already been mounted over.
497This flag is Linux-specific; define
498.B _GNU_SOURCE
499.\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
500to obtain its definition.
501.TP
502.B AT_SYMLINK_NOFOLLOW
503If
504.I pathname
505is a symbolic link, do not dereference it:
506instead return information about the link itself, like
cadd38ba 507.BR lstat ().
40084043
MK
508(By default,
509.BR fstatat ()
510dereferences symbolic links, like
cadd38ba 511.BR stat ().)
40084043
MK
512.PP
513See
514.BR openat (2)
515for an explanation of the need for
516.BR fstatat ().
47297adb 517.SH RETURN VALUE
c13182ef
MK
518On success, zero is returned.
519On error, \-1 is returned, and
fea681da
MK
520.I errno
521is set appropriately.
522.SH ERRORS
523.TP
524.B EACCES
525Search permission is denied for one of the directories
526in the path prefix of
66cbeaf4 527.IR pathname .
fea681da 528(See also
ad7cc990 529.BR path_resolution (7).)
fea681da
MK
530.TP
531.B EBADF
d3b03141 532.I fd
fea681da
MK
533is bad.
534.TP
535.B EFAULT
536Bad address.
537.TP
538.B ELOOP
539Too many symbolic links encountered while traversing the path.
540.TP
541.B ENAMETOOLONG
66cbeaf4 542.I pathname
c066b169 543is too long.
fea681da
MK
544.TP
545.B ENOENT
5cf0b3b4 546A component of
66cbeaf4 547.I pathname
5cf0b3b4 548does not exist, or
66cbeaf4 549.I pathname
5cf0b3b4 550is an empty string.
fea681da
MK
551.TP
552.B ENOMEM
75b94dc3 553Out of memory (i.e., kernel memory).
fea681da
MK
554.TP
555.B ENOTDIR
94ea1e9d 556A component of the path prefix of
66cbeaf4 557.I pathname
5cf0b3b4 558is not a directory.
9dcc4605
MK
559.TP
560.B EOVERFLOW
66cbeaf4 561.I pathname
fec75159
SP
562or
563.I fd
564refers to a file whose size, inode number,
565or number of blocks cannot be represented in, respectively, the types
566.IR off_t ,
567.IR ino_t ,
568or
569.IR blkcnt_t .
570This error can occur when, for example,
571an application compiled on a 32-bit platform without
5e4dc269 572.I -D_FILE_OFFSET_BITS=64
9dcc4605
MK
573calls
574.BR stat ()
575on a file whose size exceeds
4d69ed2e 576.I (1<<31)-1
fec75159 577bytes.
40084043 578.PP
40084043
MK
579The following additional errors can occur for
580.BR fstatat ():
581.TP
582.B EBADF
583.I dirfd
584is not a valid file descriptor.
585.TP
586.B EINVAL
587Invalid flag specified in
588.IR flags .
589.TP
590.B ENOTDIR
591.I pathname
592is relative and
593.I dirfd
594is a file descriptor referring to a file other than a directory.
595.SH VERSIONS
596.BR fstatat ()
597was added to Linux in kernel 2.6.16;
598library support was added to glibc in version 2.4.
47297adb 599.SH CONFORMING TO
40084043
MK
600.BR stat (),
601.BR fstat (),
602.BR lstat ():
282daba9 603SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008.
97c1eac8
MK
604.\" SVr4 documents additional
605.\" .BR fstat ()
606.\" error conditions EINTR, ENOLINK, and EOVERFLOW. SVr4
607.\" documents additional
608.\" .BR stat ()
609.\" and
610.\" .BR lstat ()
611.\" error conditions EINTR, EMULTIHOP, ENOLINK, and EOVERFLOW.
612
40084043
MK
613.BR fstatat ():
614POSIX.1-2008.
40084043 615
d3e8b141
MK
616According to POSIX.1-2001,
617.BR lstat ()
618on a symbolic link need return valid information only in the
619.I st_size
620field and the file-type component of the
621.IR st_mode
622field of the
623.IR stat
624structure.
a03c016c 625POSIX.1-2008 tightens the specification, requiring
d3e8b141
MK
626.BR lstat ()
627to return valid information in all fields except the permission bits in
628.IR st_mode .
629
fea681da
MK
630Use of the
631.I st_blocks
632and
633.I st_blksize
c13182ef
MK
634fields may be less portable.
635(They were introduced in BSD.
636The interpretation differs between systems,
637and possibly on a single system when NFS mounts are involved.)
34c909ac
MK
638If you need to obtain the definition of the
639.IR blkcnt_t
640or
641.IR blksize_t
642types from
643.IR <sys/stat.h> ,
644then define
645.BR _XOPEN_SOURCE
646with the value 500 or greater (before including
647.I any
648header files).
fea681da 649.LP
beb0ebc8 650POSIX.1-1990 did not describe the
66ee0c7e
MK
651.BR S_IFMT ,
652.BR S_IFSOCK ,
653.BR S_IFLNK ,
654.BR S_IFREG ,
c91e381d 655.BR S_IFBLK ,
66ee0c7e
MK
656.BR S_IFDIR ,
657.BR S_IFCHR ,
658.BR S_IFIFO ,
0daa9e92 659.B S_ISVTX
beb0ebc8 660constants, but instead demanded the use of
c91e381d
MK
661the macros
662.BR S_ISDIR (),
f78ed33a 663and so on.
beb0ebc8
MK
664The
665.BR S_IF*
318c8142 666constants are present in POSIX.1-2001 and later.
beb0ebc8 667
1274071a 668The
c91e381d 669.BR S_ISLNK ()
1274071a 670and
c91e381d 671.BR S_ISSOCK ()
1274071a 672macros are not in
97c1eac8
MK
673POSIX.1-1996, but both are present in POSIX.1-2001;
674the former is from SVID 4, the latter from SUSv2.
fea681da 675.LP
b4112efb 676UNIX\ V7 (and later systems) had
66ee0c7e
MK
677.BR S_IREAD ,
678.BR S_IWRITE ,
c91e381d
MK
679.BR S_IEXEC ,
680where POSIX
66ee0c7e
MK
681prescribes the synonyms
682.BR S_IRUSR ,
683.BR S_IWUSR ,
c91e381d 684.BR S_IXUSR .
73d8cece 685.SS Other systems
fea681da 686Values that have been (or are) in use on various systems:
77b99918 687.ad l
fea681da
MK
688.TS
689l l l l l.
690hex name ls octal description
691f000 S_IFMT 170000 mask for file type
77b99918
BIG
6920000 000000 T{
693SCO out-of-service inode; BSD unknown type; SVID-v2 and XPG2
694have both 0 and 0100000 for ordinary file
695T}
da2d9dad 6961000 S_IFIFO p| 010000 FIFO (named pipe)
fea681da
MK
6972000 S_IFCHR c 020000 character special (V7)
6983000 S_IFMPC 030000 multiplexed character special (V7)
6994000 S_IFDIR d/ 040000 directory (V7)
77b99918
BIG
7005000 S_IFNAM 050000 T{
701XENIX named special file with two subtypes, distinguished by
702\fIst_rdev\fP values 1, 2
703T}
fea681da
MK
7040001 S_INSEM s 000001 XENIX semaphore subtype of IFNAM
7050002 S_INSHD m 000002 XENIX shared data subtype of IFNAM
7066000 S_IFBLK b 060000 block special (V7)
7077000 S_IFMPB 070000 multiplexed block special (V7)
7088000 S_IFREG - 100000 regular (V7)
7099000 S_IFCMP 110000 VxFS compressed
7109000 S_IFNWK n 110000 network special (HP-UX)
711a000 S_IFLNK l@ 120000 symbolic link (BSD)
77b99918 712b000 S_IFSHAD 130000 T{
7fac88a9 713Solaris shadow inode for ACL (not seen by user space)
77b99918 714T}
fea681da
MK
715c000 S_IFSOCK s= 140000 socket (BSD; also "S_IFSOC" on VxFS)
716d000 S_IFDOOR D> 150000 Solaris door
717e000 S_IFWHT w% 160000 BSD whiteout (not used for inode)
77b99918
BIG
7180200 S_ISVTX 001000 T{
719sticky bit: save swapped text even after use (V7)
720.br
721reserved (SVID-v2)
722.br
723On nondirectories: don't cache this file (SunOS)
724.br
725On directories: restricted deletion flag (SVID-v4.2)
726T}
7270400 S_ISGID 002000 T{
728set-group-ID on execution (V7)
729.br
730for directories: use BSD semantics for propagation of GID
731T}
7320400 S_ENFMT 002000 T{
733System V file locking enforcement (shared with S_ISGID)
734T}
da2d9dad 7350800 S_ISUID 004000 set-user-ID on execution (V7)
77b99918
BIG
7360800 S_CDF 004000 T{
737directory is a context dependent file (HP-UX)
738T}
fea681da 739.TE
77b99918 740.ad
fea681da
MK
741
742A sticky command appeared in Version 32V AT&T UNIX.
2b2581ee 743.SH NOTES
1ef5b280
MK
744On Linux,
745.BR lstat ()
746will generally not trigger automounter action, whereas
747.BR stat ()
748will (but see
749.BR fstatat (2)).
750
751For most files under the
752.I /proc
753directory,
754.BR stat ()
755does not return the file size in the
756.I st_size
757field; instead the field is returned with the value 0.
758.SS Timestamp fields
e9e9e87e
MK
759Older kernels and older standards did not support nanosecond timestamp
760fields.
b8efc3ed 761Instead, there were three timestamp
e9e9e87e
MK
762.RI fields\(em st_atime ,
763.IR st_mtime ,
764and
765.IR st_ctime \(emtyped
b8efc3ed 766as
e9e9e87e
MK
767.IR time_t
768that recorded timestamps with one-second precision.
1ef5b280 769
2b2581ee
MK
770Since kernel 2.5.48, the
771.I stat
f5935752 772structure supports nanosecond resolution for the three file timestamp fields.
e9e9e87e
MK
773The nanosecond components of each timestamp are available
774via names of the form
f5935752 775.IR st_atim.tv_nsec
db4e96b7
MK
776if the
777.B _BSD_SOURCE
778or
779.B _SVID_SOURCE
f5935752 780feature test macro is defined.
e9e9e87e
MK
781Nanosecond timestamps are nowadays standardized,
782starting with POSIX.1-2008, and, starting with version 2.12,
783glibc also exposes the nanosecond component names if
f5935752
MK
784.BR _POSIX_C_SOURCE
785is defined with the value 200809L or greater, or
786.BR _XOPEN_SOURCE
787is defined with the value 700 or greater.
788If none of the aforementioned macros are defined,
789then the nanosecond values are exposed with names of the form
790.IR st_atimensec .
1ef5b280 791
96f92a7c
MK
792Nanosecond timestamps are supported on XFS, JFS, Btrfs, and
793ext4 (since Linux 2.6.23).
794.\" commit ef7f38359ea8b3e9c7f2cae9a4d4935f55ca9e80
c2033cfe 795Nanosecond timestamps are not supported in ext2, ext3, and Reiserfs.
1ef5b280
MK
796On filesystems that do not support subsecond timestamps,
797the nanosecond fields are returned with the value 0.
b8f9d9dd 798.SS Underlying kernel interface
1fbe05e0 799.SS C library/kernel ABI differences
b8f9d9dd
MK
800Over time, increases in the size of the
801.I stat
0c8dd254 802structure have led to three successive versions of
b8f9d9dd
MK
803.BR stat ():
804.IR sys_stat ()
805(slot
806.IR __NR_oldstat ),
807.IR sys_newstat ()
808(slot
809.IR __NR_stat ),
810and
0daa9e92 811.I sys_stat64()
b8f9d9dd 812(new in kernel 2.4; slot
d28a0b77 813.IR __NR_stat64 ).
d28a0b77 814The glibc
b8f9d9dd 815.BR stat ()
d28a0b77 816wrapper function hides these details from applications,
6f1a1e61
MK
817invoking the most recent version of the system call provided by the kernel,
818and repacking the returned information if required for old binaries.
b8f9d9dd 819Similar remarks apply for
2777b1ca 820.BR fstat ()
b8f9d9dd 821and
2777b1ca 822.BR lstat ().
8179def1
MK
823.\"
824.\" A note from Andries Brouwer, July 2007
e0bf9127
MK
825.\"
826.\" > Is the story not rather more complicated for some calls like
8179def1 827.\" > stat(2)?
e0bf9127 828.\"
8179def1 829.\" Yes and no, mostly no. See /usr/include/sys/stat.h .
e0bf9127 830.\"
8179def1
MK
831.\" The idea is here not so much that syscalls change, but that
832.\" the definitions of struct stat and of the types dev_t and mode_t change.
833.\" This means that libc (even if it does not call the kernel
834.\" but only calls some internal function) must know what the
835.\" format of dev_t or of struct stat is.
836.\" The communication between the application and libc goes via
837.\" the include file <sys/stat.h> that defines a _STAT_VER and
838.\" _MKNOD_VER describing the layout of the data that user space
839.\" uses. Each (almost each) occurrence of stat() is replaced by
840.\" an occurrence of xstat() where the first parameter of xstat()
841.\" is this version number _STAT_VER.
e0bf9127 842.\"
8179def1
MK
843.\" Now, also the definitions used by the kernel change.
844.\" But glibc copes with this in the standard way, and the
845.\" struct stat as returned by the kernel is repacked into
846.\" the struct stat as expected by the application.
847.\" Thus, _STAT_VER and this setup cater for the application-libc
848.\" interface, rather than the libc-kernel interface.
e0bf9127 849.\"
8179def1 850.\" (Note that the details depend on gcc being used as c compiler.)
40084043
MK
851
852The underlying system call employed by the glibc
853.BR fstatat ()
854wrapper function is actually called
855.BR fstatat64 ().
bc7ff20e 856.SH EXAMPLE
988db661 857The following program calls
2777b1ca 858.BR stat ()
bc7ff20e
MK
859and displays selected fields in the returned
860.I stat
861structure.
862.nf
863
864#include <sys/types.h>
865#include <sys/stat.h>
866#include <time.h>
867#include <stdio.h>
988db661 868#include <stdlib.h>
bc7ff20e
MK
869
870int
871main(int argc, char *argv[])
872{
873 struct stat sb;
874
875 if (argc != 2) {
876 fprintf(stderr, "Usage: %s <pathname>\\n", argv[0]);
877 exit(EXIT_FAILURE);
878 }
879
29059a65 880 if (stat(argv[1], &sb) == \-1) {
bc7ff20e 881 perror("stat");