OSDN Git Service

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