]> 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
c13182ef 38.\"
bc7ff20e 39.TH STAT 2 2007-06-08 "Linux" "Linux Programmer's Manual"
fea681da
MK
40.SH NAME
41stat, fstat, lstat \- get file status
42.SH SYNOPSIS
43.B #include <sys/types.h>
44.br
45.B #include <sys/stat.h>
46.br
47.B #include <unistd.h>
48.sp
da2d9dad 49.BI "int stat(const char *" path ", struct stat *" buf );
fea681da
MK
50.br
51.BI "int fstat(int " filedes ", struct stat *" buf );
52.br
da2d9dad 53.BI "int lstat(const char *" path ", struct stat *" buf );
fea681da
MK
54.SH DESCRIPTION
55.PP
da2d9dad
MK
56These functions return information about a file.
57No permissions are required on the file itself, but \(em in the case of
58.BR stat ()
c13182ef 59and
da2d9dad
MK
60.BR lstat ()
61\(em
62execute (search) permission is required on all of the directories in
63.I path
64that lead to the file.
fea681da 65.PP
da2d9dad 66.BR stat ()
c13182ef 67stats the file pointed to by
da2d9dad 68.I path
fea681da
MK
69and fills in
70.IR buf .
71
da2d9dad 72.BR lstat ()
fea681da 73is identical to
da2d9dad
MK
74.BR stat (),
75except that if
76.I path
77is a symbolic link, then the link itself is stat-ed,
fea681da
MK
78not the file that it refers to.
79
da2d9dad 80.BR fstat ()
fea681da 81is identical to
da2d9dad
MK
82.BR stat (),
83except that the file to be stat-ed is specified by the file descriptor
84.IR filedes .
fea681da 85.PP
da2d9dad 86All of these system calls return a
fea681da
MK
87.I stat
88structure, which contains the following fields:
89.PP
5ae873ff 90.RS 0.25i
fea681da
MK
91.nf
92struct stat {
da2d9dad
MK
93 dev_t st_dev; /* ID of device containing file */
94 ino_t st_ino; /* inode number */
5ae873ff
MK
95 mode_t st_mode; /* protection */
96 nlink_t st_nlink; /* number of hard links */
97 uid_t st_uid; /* user ID of owner */
98 gid_t st_gid; /* group ID of owner */
da2d9dad 99 dev_t st_rdev; /* device ID (if special file) */
5ae873ff
MK
100 off_t st_size; /* total size, in bytes */
101 blksize_t st_blksize; /* blocksize for filesystem I/O */
102 blkcnt_t st_blocks; /* number of blocks allocated */
103 time_t st_atime; /* time of last access */
104 time_t st_mtime; /* time of last modification */
105 time_t st_ctime; /* time of last status change */
fea681da
MK
106};
107.fi
108.RE
109.PP
da2d9dad 110The
29de83af 111.I st_dev
da2d9dad
MK
112field describes the device on which this file resides.
113
114The
115.I st_rdev
116field describes the device that this file (inode) represents.
117
118The
fea681da 119.I st_size
c13182ef
MK
120field gives the size of the file (if it is a regular
121file or a symbolic link) in bytes.
da2d9dad 122The size of a symlink is the length of the pathname
28d88c17 123it contains, without a trailing null byte.
fea681da 124
da2d9dad 125The
fea681da 126.I st_blocks
32f30015 127field indicates the number of blocks allocated to the file, 512-byte units.
fea681da 128(This may be smaller than
da2d9dad
MK
129.IR st_size /512,
130for example, when the file has holes.)
131
132The
fea681da 133.IR st_blksize
da2d9dad 134field gives the "preferred" blocksize for efficient file system I/O.
fea681da
MK
135(Writing to a file in smaller chunks may cause
136an inefficient read-modify-rewrite.)
137.PP
138Not all of the Linux filesystems implement all of the time fields.
139Some file system types allow mounting in such a way that file
140accesses do not cause an update of the
141.I st_atime
c13182ef
MK
142field.
143(See `noatime' in
fea681da
MK
144.BR mount (8).)
145
146The field
147.I st_atime
75b94dc3 148is changed by file accesses, for example, by
fea681da
MK
149.BR execve (2),
150.BR mknod (2),
151.BR pipe (2),
152.BR utime (2)
153and
154.BR read (2)
7c93fec0
MK
155(of more than zero bytes).
156Other routines, like
fea681da
MK
157.BR mmap (2),
158may or may not update
159.IR st_atime .
160
161The field
162.I st_mtime
75b94dc3 163is changed by file modifications, for example, by
fea681da
MK
164.BR mknod (2),
165.BR truncate (2),
166.BR utime (2)
167and
168.BR write (2)
169(of more than zero bytes).
170Moreover,
171.I st_mtime
172of a directory is changed by the creation or deletion of files
173in that directory.
174The
175.I st_mtime
176field is
177.I not
178changed for changes in owner, group, hard link count, or mode.
179
180The field
181.I st_ctime
182is changed by writing or by setting inode information
183(i.e., owner, group, link count, mode, etc.).
184.PP
168df940
MK
185The following POSIX macros are defined to check the file type using the
186.I st_mode
187field:
fea681da
MK
188.RS
189.TP 1.2i
190S_ISREG(m)
191is it a regular file?
192.TP
193S_ISDIR(m)
194directory?
195.TP
196S_ISCHR(m)
197character device?
198.TP
199S_ISBLK(m)
200block device?
201.TP
202S_ISFIFO(m)
da2d9dad 203FIFO (named pipe)?
fea681da
MK
204.TP
205S_ISLNK(m)
206symbolic link? (Not in POSIX.1-1996.)
207.TP
208S_ISSOCK(m)
209socket? (Not in POSIX.1-1996.)
210.RE
211.PP
212The following flags are defined for the
213.I st_mode
214field:
fea681da
MK
215.TS
216l l l.
217S_IFMT 0170000 bitmask for the file type bitfields
218S_IFSOCK 0140000 socket
219S_IFLNK 0120000 symbolic link
220S_IFREG 0100000 regular file
221S_IFBLK 0060000 block device
222S_IFDIR 0040000 directory
223S_IFCHR 0020000 character device
da2d9dad 224S_IFIFO 0010000 FIFO
fea681da 225S_ISUID 0004000 set UID bit
da2d9dad 226S_ISGID 0002000 set-group-ID bit (see below)
fea681da
MK
227S_ISVTX 0001000 sticky bit (see below)
228S_IRWXU 00700 mask for file owner permissions
229S_IRUSR 00400 owner has read permission
230S_IWUSR 00200 owner has write permission
231S_IXUSR 00100 owner has execute permission
232S_IRWXG 00070 mask for group permissions
233S_IRGRP 00040 group has read permission
234S_IWGRP 00020 group has write permission
235S_IXGRP 00010 group has execute permission
236S_IRWXO 00007 mask for permissions for others (not in group)
237S_IROTH 00004 others have read permission
704a18f0 238S_IWOTH 00002 others have write permission
fea681da
MK
239S_IXOTH 00001 others have execute permission
240.TE
241.P
2f0af33b
MK
242The set-group-ID bit
243.RB ( S_ISGID )
244has several special uses.
fea681da
MK
245For a directory it indicates that BSD semantics is to be used
246for that directory: files created there inherit their group ID from
247the directory, not from the effective group ID of the creating process,
2f0af33b
MK
248and directories created there will also get the
249.B S_ISGID
250bit set.
251For a file that does not have the group execution bit
252.RB ( S_IXGRP )
253set,
da2d9dad 254the set-group-ID bit indicates mandatory file/record locking.
fea681da 255.P
2f0af33b
MK
256The `sticky' bit
257.RB ( S_ISVTX )
258on a directory means that a file
fea681da
MK
259in that directory can be renamed or deleted only by the owner
260of the file, by the owner of the directory, and by a privileged
261process.
262.SH "RETURN VALUE"
c13182ef
MK
263On success, zero is returned.
264On error, \-1 is returned, and
fea681da
MK
265.I errno
266is set appropriately.
267.SH ERRORS
268.TP
269.B EACCES
270Search permission is denied for one of the directories
271in the path prefix of
da2d9dad 272.IR path .
fea681da 273(See also
ad7cc990 274.BR path_resolution (7).)
fea681da
MK
275.TP
276.B EBADF
277.I filedes
278is bad.
279.TP
280.B EFAULT
281Bad address.
282.TP
283.B ELOOP
284Too many symbolic links encountered while traversing the path.
285.TP
286.B ENAMETOOLONG
287File name too long.
288.TP
289.B ENOENT
290A component of the path
da2d9dad 291.I path
fea681da
MK
292does not exist, or the path is an empty string.
293.TP
294.B ENOMEM
75b94dc3 295Out of memory (i.e., kernel memory).
fea681da
MK
296.TP
297.B ENOTDIR
298A component of the path is not a directory.
299.SH "CONFORMING TO"
a7fadb55 300These system calls conform to SVr4, 4.3BSD, POSIX.1-2001.
97c1eac8
MK
301.\" SVr4 documents additional
302.\" .BR fstat ()
303.\" error conditions EINTR, ENOLINK, and EOVERFLOW. SVr4
304.\" documents additional
305.\" .BR stat ()
306.\" and
307.\" .BR lstat ()
308.\" error conditions EINTR, EMULTIHOP, ENOLINK, and EOVERFLOW.
309
fea681da
MK
310Use of the
311.I st_blocks
312and
313.I st_blksize
c13182ef
MK
314fields may be less portable.
315(They were introduced in BSD.
316The interpretation differs between systems,
317and possibly on a single system when NFS mounts are involved.)
fea681da
MK
318.LP
319POSIX does not describe the S_IFMT, S_IFSOCK, S_IFLNK, S_IFREG, S_IFBLK,
320S_IFDIR, S_IFCHR, S_IFIFO, S_ISVTX bits, but instead demands the use of
c13182ef 321the macros S_ISDIR(), etc.
1274071a
MK
322The
323.B S_ISLNK
324and
325.B S_ISSOCK
326macros are not in
97c1eac8
MK
327POSIX.1-1996, but both are present in POSIX.1-2001;
328the former is from SVID 4, the latter from SUSv2.
fea681da
MK
329.LP
330Unix V7 (and later systems) had S_IREAD, S_IWRITE, S_IEXEC, where POSIX
331prescribes the synonyms S_IRUSR, S_IWUSR, S_IXUSR.
042bae96 332.SS "Other Systems"
fea681da 333Values that have been (or are) in use on various systems:
fea681da
MK
334.TS
335l l l l l.
336hex name ls octal description
337f000 S_IFMT 170000 mask for file type
3380000 000000 SCO out-of-service inode, BSD unknown type
339 SVID-v2 and XPG2 have both 0 and 0100000 for ordinary file
da2d9dad 3401000 S_IFIFO p| 010000 FIFO (named pipe)
fea681da
MK
3412000 S_IFCHR c 020000 character special (V7)
3423000 S_IFMPC 030000 multiplexed character special (V7)
3434000 S_IFDIR d/ 040000 directory (V7)
3445000 S_IFNAM 050000 XENIX named special file
345 with two subtypes, distinguished by st_rdev values 1, 2:
3460001 S_INSEM s 000001 XENIX semaphore subtype of IFNAM
3470002 S_INSHD m 000002 XENIX shared data subtype of IFNAM
3486000 S_IFBLK b 060000 block special (V7)
3497000 S_IFMPB 070000 multiplexed block special (V7)
3508000 S_IFREG - 100000 regular (V7)
3519000 S_IFCMP 110000 VxFS compressed
3529000 S_IFNWK n 110000 network special (HP-UX)
353a000 S_IFLNK l@ 120000 symbolic link (BSD)
354b000 S_IFSHAD 130000 Solaris shadow inode for ACL (not seen by userspace)
355c000 S_IFSOCK s= 140000 socket (BSD; also "S_IFSOC" on VxFS)
356d000 S_IFDOOR D> 150000 Solaris door
357e000 S_IFWHT w% 160000 BSD whiteout (not used for inode)
358
3590200 S_ISVTX 001000 `sticky bit': save swapped text even after use (V7)
360 reserved (SVID-v2)
361 On non-directories: don't cache this file (SunOS)
362 On directories: restricted deletion flag (SVID-v4.2)
c7400a2c 3630400 S_ISGID 002000 set-group-ID on execution (V7)
e75a4542 364 for directories: use BSD semantics for propagation of GID
da2d9dad
MK
3650400 S_ENFMT 002000 SysV file locking enforcement (shared with S_ISGID)
3660800 S_ISUID 004000 set-user-ID on execution (V7)
fea681da
MK
3670800 S_CDF 004000 directory is a context dependent file (HP-UX)
368.TE
369
370A sticky command appeared in Version 32V AT&T UNIX.
2b2581ee
MK
371.SH NOTES
372.SS Linux Notes
373Since kernel 2.5.48, the
374.I stat
375structure supports nanosecond resolution for the three
376file timestamp fields.
377Glibc exposes the nanosecond component of each field using names either
378of the form
379.IR st_atim.tv_nsec ,
db4e96b7
MK
380if the
381.B _BSD_SOURCE
382or
383.B _SVID_SOURCE
384feature test macro is defined,
2b2581ee
MK
385or of the form
386.IR st_atimensec ,
387if neither of these macros is defined.
388On file systems that do not support sub-second timestamps,
389these nanosecond fields are returned with the value 0.
390
391For most files under the
392.I /proc
393directory,
394.BR stat ()
395does not return the file size in the
396.I st_size
397field; instead the field is returned with the value 0.
bc7ff20e 398.SH EXAMPLE
988db661 399The following program calls
bc7ff20e
MK
400.BR stat (2)
401and displays selected fields in the returned
402.I stat
403structure.
404.nf
405
406#include <sys/types.h>
407#include <sys/stat.h>
408#include <time.h>
409#include <stdio.h>
988db661 410#include <stdlib.h>
bc7ff20e
MK
411
412int
413main(int argc, char *argv[])
414{
415 struct stat sb;
416
417 if (argc != 2) {
418 fprintf(stderr, "Usage: %s <pathname>\\n", argv[0]);
419 exit(EXIT_FAILURE);
420 }
421
29059a65 422 if (stat(argv[1], &sb) == \-1) {
bc7ff20e
MK
423 perror("stat");
424 exit(EXIT_SUCCESS);
425 }
426
427 printf("File type: ");
428 switch (sb.st_mode & S_IFMT) {
429 case S_IFBLK: printf("block device\\n"); break;
430 case S_IFCHR: printf("character device\\n"); break;
431 case S_IFDIR: printf("directory\\n"); break;
432 case S_IFIFO: printf("FIFO/pipe\\n"); break;
433 case S_IFLNK: printf("symlink\\n"); break;
434 case S_IFREG: printf("regular file\\n"); break;
435 case S_IFSOCK: printf("socket\\n"); break;
436 default: printf("unknown?\\n"); break;
437 }
438
29059a65 439 printf("I\-node number: %ld\\n", (long) sb.st_ino);
bc7ff20e 440
988db661 441 printf("Mode: %lo (octal)\\n",
bc7ff20e
MK
442 (unsigned long) sb.st_mode);
443
444 printf("Link count: %ld\\n", (long) sb.st_nlink);
445 printf("Ownership: UID=%ld GID=%ld\\n",
446 (long) sb.st_uid, (long) sb.st_gid);
447
988db661 448 printf("Preferred I/O block size: %ld bytes\\n",
bc7ff20e 449 (long) sb.st_blksize);
988db661 450 printf("File size: %lld bytes\\n",
bc7ff20e 451 (long long) sb.st_size);
988db661 452 printf("Blocks allocated: %lld\\n",
bc7ff20e
MK
453 (long long) sb.st_blocks);
454
460e6363 455 printf("Last inode change: %s", ctime(&sb.st_ctime));
bc7ff20e
MK
456 printf("Last file access: %s", ctime(&sb.st_atime));
457 printf("Last file modification: %s", ctime(&sb.st_mtime));
458
459 exit(EXIT_SUCCESS);
460}
461.fi
fea681da 462.SH "SEE ALSO"
7a5b4ffb 463.BR access (2),
fea681da
MK
464.BR chmod (2),
465.BR chown (2),
956a6446 466.BR fstatat (2),
fea681da
MK
467.BR readlink (2),
468.BR utime (2),
469.BR capabilities (7)