1STAT(2)                    Linux Programmer's Manual                   STAT(2)
2
3
4

NAME

6       stat, fstat, lstat - get file status
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/stat.h>
11       #include <unistd.h>
12
13       int stat(const char *path, struct stat *buf);
14       int fstat(int filedes, struct stat *buf);
15       int lstat(const char *path, struct stat *buf);
16

DESCRIPTION

18       These  functions  return  information about a file.  No permissions are
19       required on the file itself, but — in the case of stat() and lstat()  —
20       execute  (search)  permission  is required on all of the directories in
21       path that lead to the file.
22
23       stat() stats the file pointed to by path and fills in buf.
24
25       lstat() is identical to stat(), except that if path is a symbolic link,
26       then the link itself is stat-ed, not the file that it refers to.
27
28       fstat()  is  identical to stat(), except that the file to be stat-ed is
29       specified by the file descriptor filedes.
30
31       All of these system calls return a stat structure, which  contains  the
32       following fields:
33
34         struct stat {
35             dev_t     st_dev;     /* ID of device containing file */
36             ino_t     st_ino;     /* inode number */
37             mode_t    st_mode;    /* protection */
38             nlink_t   st_nlink;   /* number of hard links */
39             uid_t     st_uid;     /* user ID of owner */
40             gid_t     st_gid;     /* group ID of owner */
41             dev_t     st_rdev;    /* device ID (if special file) */
42             off_t     st_size;    /* total size, in bytes */
43             blksize_t st_blksize; /* blocksize for filesystem I/O */
44             blkcnt_t  st_blocks;  /* number of blocks allocated */
45             time_t    st_atime;   /* time of last access */
46             time_t    st_mtime;   /* time of last modification */
47             time_t    st_ctime;   /* time of last status change */
48         };
49
50       The st_dev field describes the device on which this file resides.
51
52       The  st_rdev  field  describes the device that this file (inode) repre‐
53       sents.
54
55       The st_size field gives the size of the file (if it is a  regular  file
56       or  a  symbolic link) in bytes.  The size of a symlink is the length of
57       the pathname it contains, without a trailing null byte.
58
59       The st_blocks field indicates the number of  blocks  allocated  to  the
60       file, 512-byte units.  (This may be smaller than st_size/512, for exam‐
61       ple, when the file has holes.)
62
63       The st_blksize field gives the "preferred" blocksize for efficient file
64       system  I/O.  (Writing to a file in smaller chunks may cause an ineffi‐
65       cient read-modify-rewrite.)
66
67       Not all of the Linux filesystems implement  all  of  the  time  fields.
68       Some  file system types allow mounting in such a way that file accesses
69       do not cause an  update  of  the  st_atime  field.  (See  `noatime'  in
70       mount(8).)
71
72       The  field  st_atime  is  changed  by file accesses, e.g. by execve(2),
73       mknod(2), pipe(2), utime(2) and read(2)  (of  more  than  zero  bytes).
74       Other routines, like mmap(2), may or may not update st_atime.
75
76       The  field st_mtime is changed by file modifications, e.g. by mknod(2),
77       truncate(2), utime(2) and write(2) (of more than  zero  bytes).   More‐
78       over, st_mtime of a directory is changed by the creation or deletion of
79       files in that directory.  The st_mtime field is not changed for changes
80       in owner, group, hard link count, or mode.
81
82       The  field  st_ctime is changed by writing or by setting inode informa‐
83       tion (i.e., owner, group, link count, mode, etc.).
84
85       The following POSIX macros are defined to check the file type using the
86       st_mode field:
87
88              S_ISREG(m)  is it a regular file?
89
90              S_ISDIR(m)  directory?
91
92              S_ISCHR(m)  character device?
93
94              S_ISBLK(m)  block device?
95
96              S_ISFIFO(m) FIFO (named pipe)?
97
98              S_ISLNK(m)  symbolic link? (Not in POSIX.1-1996.)
99
100              S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
101
102       The following flags are defined for the st_mode field:
103
104       S_IFMT     0170000   bitmask for the file type bitfields
105       S_IFSOCK   0140000   socket
106       S_IFLNK    0120000   symbolic link
107       S_IFREG    0100000   regular file
108       S_IFBLK    0060000   block device
109       S_IFDIR    0040000   directory
110       S_IFCHR    0020000   character device
111       S_IFIFO    0010000   FIFO
112       S_ISUID    0004000   set UID bit
113       S_ISGID    0002000   set-group-ID bit (see below)
114       S_ISVTX    0001000   sticky bit (see below)
115       S_IRWXU    00700     mask for file owner permissions
116       S_IRUSR    00400     owner has read permission
117       S_IWUSR    00200     owner has write permission
118       S_IXUSR    00100     owner has execute permission
119       S_IRWXG    00070     mask for group permissions
120       S_IRGRP    00040     group has read permission
121       S_IWGRP    00020     group has write permission
122       S_IXGRP    00010     group has execute permission
123       S_IRWXO    00007     mask for permissions for others (not in group)
124       S_IROTH    00004     others have read permission
125       S_IWOTH    00002     others have write permission
126       S_IXOTH    00001     others have execute permission
127
128       The  set-group-ID bit (S_ISGID) has several special uses.  For a direc‐
129       tory it indicates that BSD semantics is to be used for that  directory:
130       files created there inherit their group ID from the directory, not from
131       the effective group ID of the creating process, and directories created
132       there will also get the S_ISGID bit set.  For a file that does not have
133       the group execution bit (S_IXGRP) set, the set-group-ID  bit  indicates
134       mandatory file/record locking.
135
136       The  `sticky'  bit  (S_ISVTX)  on a directory means that a file in that
137       directory can be renamed or deleted only by the owner of the  file,  by
138       the owner of the directory, and by a privileged process.
139

LINUX NOTES

141       Since  kernel 2.5.48, the stat structure supports nanosecond resolution
142       for the three file timestamp fields.  Glibc exposes the nanosecond com‐
143       ponent of each field using names either of the form st_atim.tv_nsec, if
144       the _BSD_SOURCE or _SVID_SOURCE feature test macro is  defined,  or  of
145       the  form st_atimensec, if neither of these macros is defined.  On file
146       systems that do not support  sub-second  timestamps,  these  nanosecond
147       fields are returned with the value 0.
148
149       For  most  files  under the /proc directory, stat() does not return the
150       file size in the st_size field; instead the field is returned with  the
151       value 0.
152

RETURN VALUE

154       On  success,  zero is returned.  On error, -1 is returned, and errno is
155       set appropriately.
156

ERRORS

158       EACCES Search permission is denied for one of the  directories  in  the
159              path prefix of path.  (See also path_resolution(2).)
160
161       EBADF  filedes is bad.
162
163       EFAULT Bad address.
164
165       ELOOP  Too many symbolic links encountered while traversing the path.
166
167       ENAMETOOLONG
168              File name too long.
169
170       ENOENT A  component  of the path path does not exist, or the path is an
171              empty string.
172
173       ENOMEM Out of memory (i.e. kernel memory).
174
175       ENOTDIR
176              A component of the path is not a directory.
177

CONFORMING TO

179       These system calls conform to SVr4, 4.3BSD, POSIX.1-2001.
180
181       Use of the st_blocks and st_blksize fields may be less portable.  (They
182       were  introduced  in  BSD.  The interpretation differs between systems,
183       and possibly on a single system when NFS mounts are involved.)
184
185       POSIX  does  not  describe  the  S_IFMT,  S_IFSOCK,  S_IFLNK,  S_IFREG,
186       S_IFBLK,  S_IFDIR,  S_IFCHR, S_IFIFO, S_ISVTX bits, but instead demands
187       the use of the macros S_ISDIR(), etc.  The S_ISLNK and S_ISSOCK  macros
188       are not in POSIX.1-1996, but both are present in POSIX.1-2001; the for‐
189       mer is from SVID 4, the latter from SUSv2.
190
191       Unix V7 (and later systems) had S_IREAD, S_IWRITE, S_IEXEC, where POSIX
192       prescribes the synonyms S_IRUSR, S_IWUSR, S_IXUSR.
193

OTHER SYSTEMS

195       Values that have been (or are) in use on various systems:
196
197       hex    name       ls   octal    description
198
199       f000   S_IFMT          170000   mask for file type
200       0000                   000000   SCO out-of-service inode, BSD unknown type
201                                       SVID-v2 and XPG2 have both 0 and 0100000 for ordinary file
202       1000   S_IFIFO    p|   010000   FIFO (named pipe)
203       2000   S_IFCHR    c    020000   character special (V7)
204       3000   S_IFMPC         030000   multiplexed character special (V7)
205       4000   S_IFDIR    d/   040000   directory (V7)
206       5000   S_IFNAM         050000   XENIX named special file
207                                       with two subtypes, distinguished by st_rdev values 1, 2:
208       0001   S_INSEM    s    000001   XENIX semaphore subtype of IFNAM
209       0002   S_INSHD    m    000002   XENIX shared data subtype of IFNAM
210       6000   S_IFBLK    b    060000   block special (V7)
211       7000   S_IFMPB         070000   multiplexed block special (V7)
212       8000   S_IFREG    -    100000   regular (V7)
213       9000   S_IFCMP         110000   VxFS compressed
214       9000   S_IFNWK    n    110000   network special (HP-UX)
215       a000   S_IFLNK    l@   120000   symbolic link (BSD)
216       b000   S_IFSHAD        130000   Solaris shadow inode for ACL (not seen by userspace)
217       c000   S_IFSOCK   s=   140000   socket (BSD; also "S_IFSOC" on VxFS)
218       d000   S_IFDOOR   D>   150000   Solaris door
219       e000   S_IFWHT    w%   160000   BSD whiteout (not used for inode)
220
221       0200   S_ISVTX         001000   `sticky bit': save swapped text even after use (V7)
222                                       reserved (SVID-v2)
223                                       On non-directories: don't cache this file (SunOS)
224                                       On directories: restricted deletion flag (SVID-v4.2)
225       0400   S_ISGID         002000   set-group-ID on execution (V7)
226                                       for directories: use BSD semantics for propagation of GID
227       0400   S_ENFMT         002000   SysV file locking enforcement (shared with S_ISGID)
228       0800   S_ISUID         004000   set-user-ID on execution (V7)
229       0800   S_CDF           004000   directory is a context dependent file (HP-UX)
230
231       A sticky command appeared in Version 32V AT&T UNIX.
232

SEE ALSO

234       access(2), chmod(2), chown(2), fstatat(2), readlink(2), utime(2), capa‐
235       bilities(7)
236
237
238
239Linux 2.6.7                       2004-06-23                           STAT(2)
Impressum