OSDN Git Service

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