1STAT(P)                    POSIX Programmer's Manual                   STAT(P)
2
3
4

NAME

6       stat - get file status
7

SYNOPSIS

9       #include <sys/stat.h>
10
11       int stat(const char *restrict path, struct stat *restrict buf);
12
13

DESCRIPTION

15       The  stat()  function shall obtain information about the named file and
16       write it to the area pointed to by the buf argument. The path  argument
17       points to a pathname naming a file.  Read, write, or execute permission
18       of the named file is not  required.  An  implementation  that  provides
19       additional  or  alternate  file  access  control  mechanisms may, under
20       implementation-defined conditions, cause stat() to fail. In particular,
21       the system may deny the existence of the file specified by path.
22
23       If  the  named  file is a symbolic link, the stat() function shall con‐
24       tinue pathname resolution using the contents of the symbolic link,  and
25       shall  return  information pertaining to the resulting file if the file
26       exists.
27
28       The buf argument is a pointer to a stat structure, as  defined  in  the
29       <sys/stat.h>  header,  into  which information is placed concerning the
30       file.
31
32       The stat() function shall update any time-related fields (as  described
33       in  the  Base  Definitions volume of IEEE Std 1003.1-2001, Section 4.7,
34       File Times Update), before writing into the stat structure.
35
36       Unless otherwise specified,  the  structure  members  st_mode,  st_ino,
37       st_dev,  st_uid,  st_gid,  st_atime,  st_ctime, and st_mtime shall have
38       meaningful values  for  all  file  types  defined  in  this  volume  of
39       IEEE Std 1003.1-2001.  The value of the member st_nlink shall be set to
40       the number of links to the file.
41

RETURN VALUE

43       Upon successful completion, 0 shall be returned. Otherwise, -1 shall be
44       returned and errno set to indicate the error.
45

ERRORS

47       The stat() function shall fail if:
48
49       EACCES Search permission is denied for a component of the path prefix.
50
51       EIO    An error occurred while reading from the file system.
52
53       ELOOP  A loop exists in symbolic links encountered during resolution of
54              the path argument.
55
56       ENAMETOOLONG
57              The length of the path argument exceeds {PATH_MAX} or a pathname
58              component is longer than {NAME_MAX}.
59
60       ENOENT A component of path does not name an existing file or path is an
61              empty string.
62
63       ENOTDIR
64              A component of the path prefix is not a directory.
65
66       EOVERFLOW
67              The file size in bytes or the number of blocks allocated to  the
68              file  or  the file serial number cannot be represented correctly
69              in the structure pointed to by buf.
70
71
72       The stat() function may fail if:
73
74       ELOOP  More than {SYMLOOP_MAX} symbolic links were  encountered  during
75              resolution of the path argument.
76
77       ENAMETOOLONG
78              As a result of encountering a symbolic link in resolution of the
79              path argument, the length of  the  substituted  pathname  string
80              exceeded {PATH_MAX}.
81
82       EOVERFLOW
83              A  value  to  be stored would overflow one of the members of the
84              stat structure.
85
86
87       The following sections are informative.
88

EXAMPLES

90   Obtaining File Status Information
91       The following example shows how to obtain file status information for a
92       file named /home/cnd/mod1. The structure variable buffer is defined for
93       the stat structure.
94
95
96              #include <sys/types.h>
97              #include <sys/stat.h>
98              #include <fcntl.h>
99
100
101              struct stat buffer;
102              int         status;
103              ...
104              status = stat("/home/cnd/mod1", &buffer);
105
106   Getting Directory Information
107       The following example fragment gets status information for  each  entry
108       in a directory. The call to the stat() function stores file information
109       in the stat structure pointed to by statbuf. The lines that follow  the
110       stat() call format the fields in the stat structure for presentation to
111       the user of the program.
112
113
114              #include <sys/types.h>
115              #include <sys/stat.h>
116              #include <dirent.h>
117              #include <pwd.h>
118              #include <grp.h>
119              #include <time.h>
120              #include <locale.h>
121              #include <langinfo.h>
122              #include <stdio.h>
123              #include <stdint.h>
124
125
126              struct dirent  *dp;
127              struct stat     statbuf;
128              struct passwd  *pwd;
129              struct group   *grp;
130              struct tm      *tm;
131              char            datestring[256];
132              ...
133              /* Loop through directory entries. */
134              while ((dp = readdir(dir)) != NULL) {
135
136
137                  /* Get entry's information. */
138                  if (stat(dp->d_name, &statbuf) == -1)
139                      continue;
140
141
142                  /* Print out type, permissions, and number of links. */
143                  printf("%10.10s", sperm (statbuf.st_mode));
144                  printf("%4d", statbuf.st_nlink);
145
146
147                  /* Print out owner's name if it is found using getpwuid(). */
148                  if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
149                      printf(" %-8.8s", pwd->pw_name);
150                  else
151                      printf(" %-8d", statbuf.st_uid);
152
153
154                  /* Print out group name if it is found using getgrgid(). */
155                  if ((grp = getgrgid(statbuf.st_gid)) != NULL)
156                      printf(" %-8.8s", grp->gr_name);
157                  else
158                      printf(" %-8d", statbuf.st_gid);
159
160
161                  /* Print size of file. */
162                  printf(" %9jd", (intmax_t)statbuf.st_size);
163
164
165                  tm = localtime(&statbuf.st_mtime);
166
167
168                  /* Get localized date string. */
169                  strftime(datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm);
170
171
172                  printf(" %s %s\n", datestring, dp->d_name);
173              }
174

APPLICATION USAGE

176       None.
177

RATIONALE

179       The intent of the paragraph describing "additional  or  alternate  file
180       access  control mechanisms" is to allow a secure implementation where a
181       process with a label that does not dominate  the  file's  label  cannot
182       perform  a  stat()  function. This is not related to read permission; a
183       process with a label that dominates the file's label does not need read
184       permission.  An  implementation that supports write-up operations could
185       fail fstat() function calls even though it has a valid file  descriptor
186       open for writing.
187

FUTURE DIRECTIONS

189       None.
190

SEE ALSO

192       fstat()  , lstat() , readlink() , symlink() , the Base Definitions vol‐
193       ume of IEEE Std 1003.1-2001, <sys/stat.h>, <sys/types.h>
194
196       Portions of this text are reprinted and reproduced in  electronic  form
197       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
198       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
199       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
200       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
201       event of any discrepancy between this version and the original IEEE and
202       The Open Group Standard, the original IEEE and The Open Group  Standard
203       is  the  referee document. The original Standard can be obtained online
204       at http://www.opengroup.org/unix/online.html .
205
206
207
208IEEE/The Open Group                  2003                              STAT(P)
Impressum