]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/stat.2
ffix
[thirdparty/man-pages.git] / man2 / stat.2
CommitLineData
fea681da
MK
1'\" t
2.\" Hey Emacs! This file is -*- nroff -*- source.
3.\"
4.\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
5.\" Parts Copyright (c) 1995 Nicolai Langfeldt (janl@ifi.uio.no), 1/1/95
bc7ff20e 6.\" and Copyright (c) 2007 Michael Kerrisk <mtk-manpages@gmx.net>
fea681da
MK
7.\"
8.\" Permission is granted to make and distribute verbatim copies of this
9.\" manual provided the copyright notice and this permission notice are
10.\" preserved on all copies.
11.\"
12.\" Permission is granted to copy and distribute modified versions of this
13.\" manual under the conditions for verbatim copying, provided that the
14.\" entire resulting derived work is distributed under the terms of a
15.\" permission notice identical to this one.
c13182ef 16.\"
fea681da
MK
17.\" Since the Linux kernel and libraries are constantly changing, this
18.\" manual page may be incorrect or out-of-date. The author(s) assume no
19.\" responsibility for errors or omissions, or for damages resulting from
20.\" the use of the information contained herein. The author(s) may not
21.\" have taken the same level of care in the production of this manual,
22.\" which is licensed free of charge, as they might when working
23.\" professionally.
c13182ef 24.\"
fea681da
MK
25.\" Formatted or processed versions of this manual, if unaccompanied by
26.\" the source, must acknowledge the copyright and authors of this work.
27.\"
28.\" Modified by Michael Haardt <michael@moria.de>
29.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
30.\" Modified 1995-05-18 by Todd Larason <jtl@molehill.org>
31.\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
32.\" Modified 1995-01-09 by Richard Kettlewell <richard@greenend.org.uk>
33.\" Modified 1998-05-13 by Michael Haardt <michael@cantor.informatik.rwth-aachen.de>
34.\" Modified 1999-07-06 by aeb & Albert Cahalan
35.\" Modified 2000-01-07 by aeb
305a0578 36.\" Modified 2004-06-23 by Michael Kerrisk <mtk-manpages@gmx.net>
bc7ff20e 37.\" 2007-06-08 mtk: Added example program
cd667553 38.\" 2007-07-05 mtk: Added details on underlying system call interfaces
c13182ef 39.\"
cc4615cc 40.TH STAT 2 2007-07-26 "Linux" "Linux Programmer's Manual"
fea681da
MK
41.SH NAME
42stat, fstat, lstat \- get file status
43.SH SYNOPSIS
44.B #include <sys/types.h>
45.br
46.B #include <sys/stat.h>
47.br
48.B #include <unistd.h>
49.sp
da2d9dad 50.BI "int stat(const char *" path ", struct stat *" buf );
fea681da
MK
51.br
52.BI "int fstat(int " filedes ", struct stat *" buf );
53.br
da2d9dad 54.BI "int lstat(const char *" path ", struct stat *" buf );
8179def1
MK
55.sp
56.in -4n
57Feature Test Macro Requirements for glibc (see
58.BR feature_test_macros (7)):
59.in
60.sp
61.BR lstat ():
cc4615cc 62_BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
fea681da
MK
63.SH DESCRIPTION
64.PP
da2d9dad
MK
65These functions return information about a file.
66No permissions are required on the file itself, but \(em in the case of
67.BR stat ()
c13182ef 68and
da2d9dad
MK
69.BR lstat ()
70\(em
71execute (search) permission is required on all of the directories in
72.I path
73that lead to the file.
fea681da 74.PP
da2d9dad 75.BR stat ()
c13182ef 76stats the file pointed to by
da2d9dad 77.I path
fea681da
MK
78and fills in
79.IR buf .
80
da2d9dad 81.BR lstat ()
fea681da 82is identical to
da2d9dad
MK
83.BR stat (),
84except that if
85.I path
86is a symbolic link, then the link itself is stat-ed,
fea681da
MK
87not the file that it refers to.
88
da2d9dad 89.BR fstat ()
fea681da 90is identical to
da2d9dad
MK
91.BR stat (),
92except that the file to be stat-ed is specified by the file descriptor
93.IR filedes .
fea681da 94.PP
da2d9dad 95All of these system calls return a
fea681da
MK
96.I stat
97structure, which contains the following fields:
98.PP
5ae873ff 99.RS 0.25i
fea681da
MK
100.nf
101struct stat {
da2d9dad
MK
102 dev_t st_dev; /* ID of device containing file */
103 ino_t st_ino; /* inode number */
5ae873ff
MK
104 mode_t st_mode; /* protection */
105 nlink_t st_nlink; /* number of hard links */
106 uid_t st_uid; /* user ID of owner */
107 gid_t st_gid; /* group ID of owner */
da2d9dad 108 dev_t st_rdev; /* device ID (if special file) */
5ae873ff
MK
109 off_t st_size; /* total size, in bytes */
110 blksize_t st_blksize; /* blocksize for filesystem I/O */
111 blkcnt_t st_blocks; /* number of blocks allocated */
112 time_t st_atime; /* time of last access */
113 time_t st_mtime; /* time of last modification */
114 time_t st_ctime; /* time of last status change */
fea681da
MK
115};
116.fi
117.RE
118.PP
da2d9dad 119The
29de83af 120.I st_dev
da2d9dad
MK
121field describes the device on which this file resides.
122
123The
124.I st_rdev
125field describes the device that this file (inode) represents.
126
127The
fea681da 128.I st_size
c13182ef
MK
129field gives the size of the file (if it is a regular
130file or a symbolic link) in bytes.
da2d9dad 131The size of a symlink is the length of the pathname
28d88c17 132it contains, without a trailing null byte.
fea681da 133
da2d9dad 134The
fea681da 135.I st_blocks
32f30015 136field indicates the number of blocks allocated to the file, 512-byte units.
fea681da 137(This may be smaller than
eee0a2ec
MK
138.IR st_size /512
139when the file has holes.)
da2d9dad
MK
140
141The
fea681da 142.IR st_blksize
da2d9dad 143field gives the "preferred" blocksize for efficient file system I/O.
fea681da
MK
144(Writing to a file in smaller chunks may cause
145an inefficient read-modify-rewrite.)
146.PP
147Not all of the Linux filesystems implement all of the time fields.
148Some file system types allow mounting in such a way that file
149accesses do not cause an update of the
150.I st_atime
c13182ef
MK
151field.
152(See `noatime' in
fea681da
MK
153.BR mount (8).)
154
155The field
156.I st_atime
75b94dc3 157is changed by file accesses, for example, by
fea681da
MK
158.BR execve (2),
159.BR mknod (2),
160.BR pipe (2),
161.BR utime (2)
162and
163.BR read (2)
7c93fec0
MK
164(of more than zero bytes).
165Other routines, like
fea681da
MK
166.BR mmap (2),
167may or may not update
168.IR st_atime .
169
170The field
171.I st_mtime
75b94dc3 172is changed by file modifications, for example, by
fea681da
MK
173.BR mknod (2),
174.BR truncate (2),
175.BR utime (2)
176and
177.BR write (2)
178(of more than zero bytes).
179Moreover,
180.I st_mtime
181of a directory is changed by the creation or deletion of files
182in that directory.
183The
184.I st_mtime
185field is
186.I not
187changed for changes in owner, group, hard link count, or mode.
188
189The field
190.I st_ctime
191is changed by writing or by setting inode information
192(i.e., owner, group, link count, mode, etc.).
193.PP
168df940
MK
194The following POSIX macros are defined to check the file type using the
195.I st_mode
196field:
fea681da
MK
197.RS
198.TP 1.2i
c91e381d 199.BR S_ISREG (m)
fea681da
MK
200is it a regular file?
201.TP
c91e381d 202.BR S_ISDIR (m)
fea681da
MK
203directory?
204.TP
c91e381d 205.BR S_ISCHR (m)
fea681da
MK
206character device?
207.TP
c91e381d 208.BR S_ISBLK (m)
fea681da
MK
209block device?
210.TP
c91e381d 211.BR S_ISFIFO (m)
da2d9dad 212FIFO (named pipe)?
fea681da 213.TP
c91e381d 214.BR S_ISLNK (m)
fea681da
MK
215symbolic link? (Not in POSIX.1-1996.)
216.TP
c91e381d 217.BR S_ISSOCK (m)
fea681da
MK
218socket? (Not in POSIX.1-1996.)
219.RE
220.PP
221The following flags are defined for the
222.I st_mode
223field:
c91e381d 224.RS
fea681da 225.TS
c91e381d 226lB l l.
10f5f294 227S_IFMT 0170000 bit mask for the file type bit fields
fea681da
MK
228S_IFSOCK 0140000 socket
229S_IFLNK 0120000 symbolic link
230S_IFREG 0100000 regular file
231S_IFBLK 0060000 block device
232S_IFDIR 0040000 directory
233S_IFCHR 0020000 character device
da2d9dad 234S_IFIFO 0010000 FIFO
fea681da 235S_ISUID 0004000 set UID bit
da2d9dad 236S_ISGID 0002000 set-group-ID bit (see below)
fea681da
MK
237S_ISVTX 0001000 sticky bit (see below)
238S_IRWXU 00700 mask for file owner permissions
239S_IRUSR 00400 owner has read permission
240S_IWUSR 00200 owner has write permission
241S_IXUSR 00100 owner has execute permission
242S_IRWXG 00070 mask for group permissions
243S_IRGRP 00040 group has read permission
244S_IWGRP 00020 group has write permission
245S_IXGRP 00010 group has execute permission
246S_IRWXO 00007 mask for permissions for others (not in group)
247S_IROTH 00004 others have read permission
704a18f0 248S_IWOTH 00002 others have write permission
fea681da
MK
249S_IXOTH 00001 others have execute permission
250.TE
c91e381d 251.RE
fea681da 252.P
2f0af33b
MK
253The set-group-ID bit
254.RB ( S_ISGID )
255has several special uses.
fea681da
MK
256For a directory it indicates that BSD semantics is to be used
257for that directory: files created there inherit their group ID from
258the directory, not from the effective group ID of the creating process,
2f0af33b
MK
259and directories created there will also get the
260.B S_ISGID
261bit set.
66ee0c7e 262For a file that does not have the group execution bit
2f0af33b
MK
263.RB ( S_IXGRP )
264set,
da2d9dad 265the set-group-ID bit indicates mandatory file/record locking.
fea681da 266.P
2f0af33b
MK
267The `sticky' bit
268.RB ( S_ISVTX )
269on a directory means that a file
fea681da
MK
270in that directory can be renamed or deleted only by the owner
271of the file, by the owner of the directory, and by a privileged
272process.
273.SH "RETURN VALUE"
c13182ef
MK
274On success, zero is returned.
275On error, \-1 is returned, and
fea681da
MK
276.I errno
277is set appropriately.
278.SH ERRORS
279.TP
280.B EACCES
281Search permission is denied for one of the directories
282in the path prefix of
da2d9dad 283.IR path .
fea681da 284(See also
ad7cc990 285.BR path_resolution (7).)
fea681da
MK
286.TP
287.B EBADF
288.I filedes
289is bad.
290.TP
291.B EFAULT
292Bad address.
293.TP
294.B ELOOP
295Too many symbolic links encountered while traversing the path.
296.TP
297.B ENAMETOOLONG
298File name too long.
299.TP
300.B ENOENT
301A component of the path
da2d9dad 302.I path
fea681da
MK
303does not exist, or the path is an empty string.
304.TP
305.B ENOMEM
75b94dc3 306Out of memory (i.e., kernel memory).
fea681da
MK
307.TP
308.B ENOTDIR
309A component of the path is not a directory.
310.SH "CONFORMING TO"
a7fadb55 311These system calls conform to SVr4, 4.3BSD, POSIX.1-2001.
97c1eac8
MK
312.\" SVr4 documents additional
313.\" .BR fstat ()
314.\" error conditions EINTR, ENOLINK, and EOVERFLOW. SVr4
315.\" documents additional
316.\" .BR stat ()
317.\" and
318.\" .BR lstat ()
319.\" error conditions EINTR, EMULTIHOP, ENOLINK, and EOVERFLOW.
320
fea681da
MK
321Use of the
322.I st_blocks
323and
324.I st_blksize
c13182ef
MK
325fields may be less portable.
326(They were introduced in BSD.
327The interpretation differs between systems,
328and possibly on a single system when NFS mounts are involved.)
fea681da 329.LP
c91e381d 330POSIX does not describe the
66ee0c7e
MK
331.BR S_IFMT ,
332.BR S_IFSOCK ,
333.BR S_IFLNK ,
334.BR S_IFREG ,
c91e381d 335.BR S_IFBLK ,
66ee0c7e
MK
336.BR S_IFDIR ,
337.BR S_IFCHR ,
338.BR S_IFIFO ,
c91e381d
MK
339.BR S_ISVTX
340bits, but instead demands the use of
341the macros
342.BR S_ISDIR (),
343etc.
1274071a 344The
c91e381d 345.BR S_ISLNK ()
1274071a 346and
c91e381d 347.BR S_ISSOCK ()
1274071a 348macros are not in
97c1eac8
MK
349POSIX.1-1996, but both are present in POSIX.1-2001;
350the former is from SVID 4, the latter from SUSv2.
fea681da 351.LP
66ee0c7e
MK
352Unix V7 (and later systems) had
353.BR S_IREAD ,
354.BR S_IWRITE ,
c91e381d
MK
355.BR S_IEXEC ,
356where POSIX
66ee0c7e
MK
357prescribes the synonyms
358.BR S_IRUSR ,
359.BR S_IWUSR ,
c91e381d 360.BR S_IXUSR .
042bae96 361.SS "Other Systems"
fea681da 362Values that have been (or are) in use on various systems:
fea681da
MK
363.TS
364l l l l l.
365hex name ls octal description
366f000 S_IFMT 170000 mask for file type
3670000 000000 SCO out-of-service inode, BSD unknown type
c91e381d
MK
368 SVID-v2 and XPG2 have both 0 and 0100000
369 for ordinary file
da2d9dad 3701000 S_IFIFO p| 010000 FIFO (named pipe)
fea681da
MK
3712000 S_IFCHR c 020000 character special (V7)
3723000 S_IFMPC 030000 multiplexed character special (V7)
3734000 S_IFDIR d/ 040000 directory (V7)
3745000 S_IFNAM 050000 XENIX named special file
c91e381d 375 with two subtypes, distinguished by \fIst_rdev\fP values 1, 2
fea681da
MK
3760001 S_INSEM s 000001 XENIX semaphore subtype of IFNAM
3770002 S_INSHD m 000002 XENIX shared data subtype of IFNAM
3786000 S_IFBLK b 060000 block special (V7)
3797000 S_IFMPB 070000 multiplexed block special (V7)
3808000 S_IFREG - 100000 regular (V7)
3819000 S_IFCMP 110000 VxFS compressed
3829000 S_IFNWK n 110000 network special (HP-UX)
383a000 S_IFLNK l@ 120000 symbolic link (BSD)
384b000 S_IFSHAD 130000 Solaris shadow inode for ACL (not seen by userspace)
385c000 S_IFSOCK s= 140000 socket (BSD; also "S_IFSOC" on VxFS)
386d000 S_IFDOOR D> 150000 Solaris door
387e000 S_IFWHT w% 160000 BSD whiteout (not used for inode)
fea681da
MK
3880200 S_ISVTX 001000 `sticky bit': save swapped text even after use (V7)
389 reserved (SVID-v2)
390 On non-directories: don't cache this file (SunOS)
391 On directories: restricted deletion flag (SVID-v4.2)
c7400a2c 3920400 S_ISGID 002000 set-group-ID on execution (V7)
c91e381d
MK
393 for directories: use BSD semantics for
394 propagation of GID
da2d9dad
MK
3950400 S_ENFMT 002000 SysV file locking enforcement (shared with S_ISGID)
3960800 S_ISUID 004000 set-user-ID on execution (V7)
fea681da
MK
3970800 S_CDF 004000 directory is a context dependent file (HP-UX)
398.TE
399
400A sticky command appeared in Version 32V AT&T UNIX.
2b2581ee
MK
401.SH NOTES
402.SS Linux Notes
403Since kernel 2.5.48, the
404.I stat
405structure supports nanosecond resolution for the three
406file timestamp fields.
407Glibc exposes the nanosecond component of each field using names either
408of the form
409.IR st_atim.tv_nsec ,
db4e96b7
MK
410if the
411.B _BSD_SOURCE
412or
413.B _SVID_SOURCE
414feature test macro is defined,
2b2581ee
MK
415or of the form
416.IR st_atimensec ,
417if neither of these macros is defined.
418On file systems that do not support sub-second timestamps,
419these nanosecond fields are returned with the value 0.
420
421For most files under the
422.I /proc
423directory,
424.BR stat ()
425does not return the file size in the
426.I st_size
427field; instead the field is returned with the value 0.
b8f9d9dd
MK
428.SS Underlying kernel interface
429Over time, increases in the size of the
430.I stat
0c8dd254 431structure have led to three successive versions of
b8f9d9dd
MK
432.BR stat ():
433.IR sys_stat ()
434(slot
435.IR __NR_oldstat ),
436.IR sys_newstat ()
437(slot
438.IR __NR_stat ),
439and
440.IR sys_stat64()
441(new in kernel 2.4; slot
d28a0b77 442.IR __NR_stat64 ).
d28a0b77 443The glibc
b8f9d9dd 444.BR stat ()
d28a0b77 445wrapper function hides these details from applications,
6f1a1e61
MK
446invoking the most recent version of the system call provided by the kernel,
447and repacking the returned information if required for old binaries.
b8f9d9dd
MK
448Similar remarks apply for
449.BR fstat (2)
450and
451.BR lstat (2).
8179def1
MK
452.\"
453.\" A note from Andries Brouwer, July 2007
e0bf9127
MK
454.\"
455.\" > Is the story not rather more complicated for some calls like
8179def1 456.\" > stat(2)?
e0bf9127 457.\"
8179def1 458.\" Yes and no, mostly no. See /usr/include/sys/stat.h .
e0bf9127 459.\"
8179def1
MK
460.\" The idea is here not so much that syscalls change, but that
461.\" the definitions of struct stat and of the types dev_t and mode_t change.
462.\" This means that libc (even if it does not call the kernel
463.\" but only calls some internal function) must know what the
464.\" format of dev_t or of struct stat is.
465.\" The communication between the application and libc goes via
466.\" the include file <sys/stat.h> that defines a _STAT_VER and
467.\" _MKNOD_VER describing the layout of the data that user space
468.\" uses. Each (almost each) occurrence of stat() is replaced by
469.\" an occurrence of xstat() where the first parameter of xstat()
470.\" is this version number _STAT_VER.
e0bf9127 471.\"
8179def1
MK
472.\" Now, also the definitions used by the kernel change.
473.\" But glibc copes with this in the standard way, and the
474.\" struct stat as returned by the kernel is repacked into
475.\" the struct stat as expected by the application.
476.\" Thus, _STAT_VER and this setup cater for the application-libc
477.\" interface, rather than the libc-kernel interface.
e0bf9127 478.\"
8179def1 479.\" (Note that the details depend on gcc being used as c compiler.)
bc7ff20e 480.SH EXAMPLE
988db661 481The following program calls
bc7ff20e
MK
482.BR stat (2)
483and displays selected fields in the returned
484.I stat
485structure.
486.nf
487
488#include <sys/types.h>
489#include <sys/stat.h>
490#include <time.h>
491#include <stdio.h>
988db661 492#include <stdlib.h>
bc7ff20e
MK
493
494int
495main(int argc, char *argv[])
496{
497 struct stat sb;
498
499 if (argc != 2) {
500 fprintf(stderr, "Usage: %s <pathname>\\n", argv[0]);
501 exit(EXIT_FAILURE);
502 }
503
29059a65 504 if (stat(argv[1], &sb) == \-1) {
bc7ff20e
MK
505 perror("stat");
506 exit(EXIT_SUCCESS);
507 }
508
509 printf("File type: ");
510 switch (sb.st_mode & S_IFMT) {
511 case S_IFBLK: printf("block device\\n"); break;
512 case S_IFCHR: printf("character device\\n"); break;
513 case S_IFDIR: printf("directory\\n"); break;
514 case S_IFIFO: printf("FIFO/pipe\\n"); break;
515 case S_IFLNK: printf("symlink\\n"); break;
516 case S_IFREG: printf("regular file\\n"); break;
517 case S_IFSOCK: printf("socket\\n"); break;
518 default: printf("unknown?\\n"); break;
519 }
520
29059a65 521 printf("I\-node number: %ld\\n", (long) sb.st_ino);
bc7ff20e 522
988db661 523 printf("Mode: %lo (octal)\\n",
bc7ff20e
MK
524 (unsigned long) sb.st_mode);
525
526 printf("Link count: %ld\\n", (long) sb.st_nlink);
527 printf("Ownership: UID=%ld GID=%ld\\n",
528 (long) sb.st_uid, (long) sb.st_gid);
529
988db661 530 printf("Preferred I/O block size: %ld bytes\\n",
bc7ff20e 531 (long) sb.st_blksize);
988db661 532 printf("File size: %lld bytes\\n",
bc7ff20e 533 (long long) sb.st_size);
988db661 534 printf("Blocks allocated: %lld\\n",
bc7ff20e
MK
535 (long long) sb.st_blocks);
536
460e6363 537 printf("Last inode change: %s", ctime(&sb.st_ctime));
bc7ff20e
MK
538 printf("Last file access: %s", ctime(&sb.st_atime));
539 printf("Last file modification: %s", ctime(&sb.st_mtime));
540
541 exit(EXIT_SUCCESS);
542}
543.fi
fea681da 544.SH "SEE ALSO"
7a5b4ffb 545.BR access (2),
fea681da
MK
546.BR chmod (2),
547.BR chown (2),
956a6446 548.BR fstatat (2),
fea681da
MK
549.BR readlink (2),
550.BR utime (2),
551.BR capabilities (7)