1stat(2)                          System Calls                          stat(2)
2
3
4

NAME

6       stat, lstat, fstat, fstatat - get file status
7

SYNOPSIS

9       #include <fcntl.h>
10       #include <sys/types.h>
11       #include <sys/stat.h>
12
13       int stat(const char *restrict path, struct stat *restrict buf);
14
15
16       int lstat(const char *restrict path, struct stat *restrict buf);
17
18
19       int fstat(int fildes, struct stat *buf);
20
21
22       int fstatat(int fildes, const char *path, struct stat *buf,
23            int flag);
24
25

DESCRIPTION

27       The  stat()  function  obtains information about the file pointed to by
28       path. Read, write, or execute permission  of  the  named  file  is  not
29       required,  but  all  directories listed in the path name leading to the
30       file must be searchable.
31
32
33       The lstat() function obtains file attributes similar to stat(),  except
34       when  the  named  file is a symbolic link; in that case lstat() returns
35       information about the link, while stat() returns information about  the
36       file the link references.
37
38
39       The  fstat()  function  obtains information about an open file known by
40       the  file  descriptor  fildes,  obtained  from  a  successful  open(2),
41       creat(2), dup(2), fcntl(2), or pipe(2) function. If fildes references a
42       shared memory object, the system updates in the stat structure  pointed
43       to  by  the  buf argument only the st_uid, st_gid, st_size, and st_mode
44       fields, and only the S_IRUSR, S_IWUSR, S_IRGRP, S_IWGRP,  S_IROTH,  and
45       S_IWOTH file permission bits need be valid. The system can update other
46       fields and flags. The fstat() function updates any pending time-related
47       fields before writing to the stat structure.
48
49
50       The  fstatat()  function obtains file attributes similar to the stat(),
51       lstat(), and fstat() functions.  If the path  argument  is  a  relative
52       path,  it  is  resolved relative to the fildes argument rather than the
53       current working directory.  If path is absolute, the fildes argument is
54       unused.   If  the fildes argument has the special value AT_FDCWD, rela‐
55       tive paths are resolved from the current working directory. If  AT_SYM‐
56       LINK_NOFOLLOW  is  set  in the flag argument, the function behaves like
57       lstat()  and  does  not  automatically  follow  symbolic   links.   See
58       fsattr(5).  If _ATTR_TRIGGER is set in the  flag argument and the vnode
59       is a trigger mount point, the  mount  is  performed  and  the  function
60       returns the attributes of the root of the mounted filesystem.
61
62
63       The  buf  argument is a pointer to a stat structure into which informa‐
64       tion is placed concerning the file. A stat structure includes the  fol‐
65       lowing members:
66
67         mode_t   st_mode;          /* File mode (see mknod(2)) */
68         ino_t    st_ino;           /* Inode number */
69         dev_t    st_dev;           /* ID of device containing */
70                                    /* a directory entry for this file */
71         dev_t    st_rdev;          /* ID of device */
72                                    /* This entry is defined only for */
73                                    /* char special or block special files */
74         nlink_t  st_nlink;         /* Number of links */
75         uid_t    st_uid;           /* User ID of the file's owner */
76         gid_t    st_gid;           /* Group ID of the file's group */
77         off_t    st_size;          /* File size in bytes */
78         time_t   st_atime;         /* Time of last access */
79         time_t   st_mtime;         /* Time of last data modification */
80         time_t   st_ctime;         /* Time of last file status change */
81                                    /* Times measured in seconds since */
82                                    /* 00:00:00 UTC, Jan. 1, 1970 */
83         long     st_blksize;       /* Preferred I/O block size */
84         blkcnt_t st_blocks;        /* Number of 512 byte blocks allocated*/
85         char     st_fstype[_ST_FSTYPSZ];
86                                    /* Null-terminated type of filesystem */
87
88
89
90       Descriptions of structure members are as follows:
91
92       st_mode       The  mode  of the file as described for the mknod() func‐
93                     tion. In addition to the modes described on the  mknod(2)
94                     manual  page,  the mode of a file can also be S_IFSOCK if
95                     the file is a socket, S_IFDOOR if the  file  is  a  door,
96                     S_IFPORT  if the file is an event port, or S_IFLNK if the
97                     file is a symbolic link. S_IFLNK can be  returned  either
98                     by  lstat()  or  by  fstat() when the AT_SYMLINK_NOFOLLOW
99                     flag is set.
100
101
102       st_ino        This field uniquely identifies the file in a  given  file
103                     system.  The pair  st_ino and  st_dev uniquely identifies
104                     regular files.
105
106
107       st_dev        This field uniquely identifies the file system that  con‐
108                     tains  the  file.  Its  value may be used as input to the
109                     ustat() function to determine more information about this
110                     file  system.  No  other  meaning is associated with this
111                     value.
112
113
114       st_rdev       This field should be used  only  by  administrative  com‐
115                     mands.  It  is  valid only for block special or character
116                     special files and only has meaning on  the  system  where
117                     the file was configured.
118
119
120       st_nlink      This  field  should  be  used only by administrative com‐
121                     mands.
122
123
124       st_uid        The user ID of the file's owner.
125
126
127       st_gid        The group ID of the file's group.
128
129
130       st_size       For regular files, this is the address of the end of  the
131                     file. For block special or character special, this is not
132                     defined. See also pipe(2).
133
134
135       st_atime      Time when file data was last accessed. Some of the  func‐
136                     tions  that  change  this  member  are: creat(), mknod(),
137                     pipe(), utime(2), and read(2).
138
139
140       st_mtime      Time when data was last modified. Some of  the  functions
141                     that  change  this  member are: creat(), mknod(), pipe(),
142                     utime(), and write(2).
143
144
145       st_ctime      Time when file status was last changed. Some of the func‐
146                     tions  that  change  this member are: chmod(2), chown(2),
147                     creat(2),   link(2),   mknod(2),   pipe(2),    rename(2),
148                     unlink(2), utime(2), and write(2).
149
150
151       st_blksize    A  hint  as  to  the "best" unit size for I/O operations.
152                     This field is not defined for block special or  character
153                     special files.
154
155
156       st_blocks     The  total  number  of  physical blocks of size 512 bytes
157                     actually allocated on disk. This field is not defined for
158                     block special or character special files.
159
160
161       st_fstype     A null-teminated string that uniquely identifies the type
162                     of the filesystem that contains the file.
163
164

RETURN VALUES

166       Upon successful completion, 0 is returned. Otherwise,  −1  is  returned
167       and errno is set to indicate the error.
168

ERRORS

170       The stat(), fstat(), lstat(), and fstatat() functions will fail if:
171
172       EIO          An error occurred while reading from the file system.
173
174
175       EOVERFLOW    The  file  size in bytes or the number of blocks allocated
176                    to the file or the file serial  number  cannot  be  repre‐
177                    sented correctly in the structure pointed to by buf.
178
179
180
181       The stat(), lstat(), and fstatat() functions will fail if:
182
183       EACCES          Search permission is denied for a component of the path
184                       prefix.
185
186
187       EFAULT          The  buf or path argument points to an illegal address.
188
189
190       EINTR           A signal was caught during the execution of the  stat()
191                       or lstat() function.
192
193
194       ELOOP           A  loop exists in symbolic links encountered during the
195                       resolution of the path argument.
196
197
198       ENAMETOOLONG    The length of the path argument exceeds {PATH_MAX},  or
199                       the length of a path component exceeds {NAME_MAX} while
200                       _POSIX_NO_TRUNC is in effect.
201
202
203       ENOENT          A component of path does not name an existing  file  or
204                       path is an empty string.
205
206
207       ENOLINK         The  path  argument  points to a remote machine and the
208                       link to that machine is no longer active.
209
210
211       ENOTDIR         A component of the path prefix is not a  directory,  or
212                       the fildes argument does not refer to a valid directory
213                       when given a non-null relative path.
214
215
216
217       The fstat() and fstatat() functions will fail if:
218
219       EBADF      The fildes argument is not a valid open file descriptor. The
220                  fildes  argument  to fstatat() can also have the valid value
221                  of AT_FDCWD.
222
223
224       EFAULT     The buf argument points to an illegal address.
225
226
227       EINTR      A signal was caught during  the  execution  of  the  fstat()
228                  function.
229
230
231       ENOLINK    The  fildes argument points to a remote machine and the link
232                  to that machine is no longer active.
233
234
235
236       The stat(), fstat(), and lstat() functions may fail if:
237
238       EOVERFLOW    One of the members is too  large  to  store  in  the  stat
239                    structure pointed to by buf.
240
241
242
243       The stat() and lstat() functions may fail if:
244
245       ELOOP           More than {SYMLOOP_MAX} symbolic links were encountered
246                       during the resolution of the path argument.
247
248
249       ENAMETOOLONG    As a result of encountering a symbolic link in  resolu‐
250                       tion of thepath argument, the length of the substituted
251                       pathname strings exceeds {PATH_MAX}.
252
253
254
255       The stat() and fstatat() functions may fail if:
256
257       ENXIO    The path argument names a character or  block  device  special
258                file  and the corresponding I/O device has been retired by the
259                fault management framework.
260
261

EXAMPLES

263       Example 1 Use stat() to obtain file status information.
264
265
266       The following example shows how to obtain file status information for a
267       file named /home/cnd/mod1. The structure variable buffer is defined for
268       the stat structure.
269
270
271         #include <sys/types.h>
272         #include <sys/stat.h>
273         #include <fcntl.h>
274         struct stat buffer;
275         int         status;
276         ...
277         status = stat("/home/cnd/mod1", &buffer);
278
279
280       Example 2 Use stat() to get directory information.
281
282
283       The following example fragment gets status information for  each  entry
284       in a directory. The call to the stat() function stores file information
285       in the stat structure pointed to by statbuf. The lines that follow  the
286       stat() call format the fields in the stat structure for presentation to
287       the user of the program.
288
289
290         #include <sys/types.h>
291         #include <sys/stat.h>
292         #include <dirent.h>
293         #include <pwd.h>
294         #include <grp.h>
295         #include <time.h>
296         #include <locale.h>
297         #include <langinfo.h>
298         #include <stdio.h>
299         #include <stdint.h>
300         struct dirent *dp;
301         struct stat   statbuf;
302         struct passwd *pwd;
303         struct group  *grp;
304         struct tm     *tm;
305         char          datestring[256];
306         ...
307         /* Loop through directory entries */
308         while ((dp = readdir(dir)) != NULL) {
309             /* Get entry's information. */
310             if (stat(dp->d_name, &statbuf) == -1)
311             continue;
312
313              /* Print out type, permissions, and number of links. */
314              printf("%10.10s", sperm (statbuf.st_mode));
315              printf("%4d", statbuf.st_nlink);
316
317              /* Print out owners name if it is found using getpwuid(). */
318              if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
319                 printf(" %-8.8s", pwd->pw_name);
320              else
321                 printf(" %-8d", statbuf.st_uid);
322
323              /* Print out group name if it's found using getgrgid(). */
324              if ((grp = getgrgid(statbuf.st_gid)) != NULL)
325                 printf(" %-8.8s", grp->gr_name);
326              else
327                 printf(" %-8d", statbuf.st_gid);
328
329              /* Print size of file. */
330              printf(" %9jd", (intmax_t)statbuf.st_size);
331              tm = localtime(&statbuf.st_mtime);
332
333              /* Get localized date string. */
334              strftime(datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm);
335
336              printf(" %s %s\n", datestring, dp->d_name);
337          }
338
339
340       Example 3 Use fstat() to obtain file status information.
341
342
343       The following example shows how to obtain file status information for a
344       file named /home/cnd/mod1. The structure variable buffer is defined for
345       the stat structure. The /home/cnd/mod1 file is opened  with  read/write
346       privileges and is passed to the open file descriptor fildes.
347
348
349         #include <sys/types.h>
350         #include <sys/stat.h>
351         #include <fcntl.h>
352         struct stat buffer;
353         int         status;
354         ...
355         fildes = open("/home/cnd/mod1", O_RDWR);
356         status = fstat(fildes, &buffer);
357
358
359       Example 4 Use lstat() to obtain symbolic link status information.
360
361
362       The following example shows how to obtain status information for a sym‐
363       bolic link named  /modules/pass1.  The  structure  variable  buffer  is
364       defined  for  the  stat  structure.  If the path argument specified the
365       filename for the file pointed to by the symbolic link (/home/cnd/mod1),
366       the results of calling the function would be the same as those returned
367       by a call to the stat() function.
368
369
370         #include <sys/stat.h>
371         struct stat buffer;
372         int         status;
373         ...
374         status = lstat("/modules/pass1", &buffer);
375
376

USAGE

378       If chmod() or fchmod() is used to change the file group  owner  permis‐
379       sions  on a file with non-trivial ACL entries, only the ACL mask is set
380       to the new permissions and the  group  owner  permission  bits  in  the
381       file's  mode  field (defined in mknod(2)) are unchanged.  A non-trivial
382       ACL entry is one whose meaning cannot be represented in the file's mode
383       field  alone.  The new ACL mask permissions  might change the effective
384       permissions for additional users and groups that have  ACL  entries  on
385       the file.
386
387
388       The stat(), fstat(), and lstat() functions have transitional interfaces
389       for 64-bit file offsets.  See lf64(5).
390

ATTRIBUTES

392       See attributes(5) for descriptions of the following attributes:
393
394
395
396
397       ┌─────────────────────────────┬─────────────────────────────┐
398       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
399       ├─────────────────────────────┼─────────────────────────────┤
400       │Interface Stability          │Committed                    │
401       ├─────────────────────────────┼─────────────────────────────┤
402       │MT-Level                     │Async-Signal-Safe            │
403       ├─────────────────────────────┼─────────────────────────────┤
404       │Standard                     │See below.                   │
405       └─────────────────────────────┴─────────────────────────────┘
406
407
408       For stat(), fstat(), and lstat(), see standards(5).
409

SEE ALSO

411       access(2), chmod(2), chown(2), creat(2),  link(2),  mknod(2),  pipe(2),
412       read(2),   time(2),   unlink(2),   utime(2),   write(2),   fattach(3C),
413       stat.h(3HEAD), attributes(5), fsattr(5), lf64(5), standards(5)
414
415
416
417SunOS 5.11                        10 Oct 2007                          stat(2)
Impressum