1STAT(3P) POSIX Programmer's Manual STAT(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 stat - get file status
13
15 #include <sys/stat.h>
16
17 int stat(const char *restrict path, struct stat *restrict buf);
18
19
21 The stat() function shall obtain information about the named file and
22 write it to the area pointed to by the buf argument. The path argument
23 points to a pathname naming a file. Read, write, or execute permission
24 of the named file is not required. An implementation that provides
25 additional or alternate file access control mechanisms may, under
26 implementation-defined conditions, cause stat() to fail. In particular,
27 the system may deny the existence of the file specified by path.
28
29 If the named file is a symbolic link, the stat() function shall con‐
30 tinue pathname resolution using the contents of the symbolic link, and
31 shall return information pertaining to the resulting file if the file
32 exists.
33
34 The buf argument is a pointer to a stat structure, as defined in the
35 <sys/stat.h> header, into which information is placed concerning the
36 file.
37
38 The stat() function shall update any time-related fields (as described
39 in the Base Definitions volume of IEEE Std 1003.1-2001, Section 4.7,
40 File Times Update), before writing into the stat structure.
41
42 Unless otherwise specified, the structure members st_mode, st_ino,
43 st_dev, st_uid, st_gid, st_atime, st_ctime, and st_mtime shall have
44 meaningful values for all file types defined in this volume of
45 IEEE Std 1003.1-2001. The value of the member st_nlink shall be set to
46 the number of links to the file.
47
49 Upon successful completion, 0 shall be returned. Otherwise, -1 shall be
50 returned and errno set to indicate the error.
51
53 The stat() function shall fail if:
54
55 EACCES Search permission is denied for a component of the path prefix.
56
57 EIO An error occurred while reading from the file system.
58
59 ELOOP A loop exists in symbolic links encountered during resolution of
60 the path argument.
61
62 ENAMETOOLONG
63 The length of the path argument exceeds {PATH_MAX} or a pathname
64 component is longer than {NAME_MAX}.
65
66 ENOENT A component of path does not name an existing file or path is an
67 empty string.
68
69 ENOTDIR
70 A component of the path prefix is not a directory.
71
72 EOVERFLOW
73 The file size in bytes or the number of blocks allocated to the
74 file or the file serial number cannot be represented correctly
75 in the structure pointed to by buf.
76
77
78 The stat() function may fail if:
79
80 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
81 resolution of the path argument.
82
83 ENAMETOOLONG
84 As a result of encountering a symbolic link in resolution of the
85 path argument, the length of the substituted pathname string
86 exceeded {PATH_MAX}.
87
88 EOVERFLOW
89 A value to be stored would overflow one of the members of the
90 stat structure.
91
92
93 The following sections are informative.
94
96 Obtaining File Status Information
97 The following example shows how to obtain file status information for a
98 file named /home/cnd/mod1. The structure variable buffer is defined for
99 the stat structure.
100
101
102 #include <sys/types.h>
103 #include <sys/stat.h>
104 #include <fcntl.h>
105
106
107 struct stat buffer;
108 int status;
109 ...
110 status = stat("/home/cnd/mod1", &buffer);
111
112 Getting Directory Information
113 The following example fragment gets status information for each entry
114 in a directory. The call to the stat() function stores file information
115 in the stat structure pointed to by statbuf. The lines that follow the
116 stat() call format the fields in the stat structure for presentation to
117 the user of the program.
118
119
120 #include <sys/types.h>
121 #include <sys/stat.h>
122 #include <dirent.h>
123 #include <pwd.h>
124 #include <grp.h>
125 #include <time.h>
126 #include <locale.h>
127 #include <langinfo.h>
128 #include <stdio.h>
129 #include <stdint.h>
130
131
132 struct dirent *dp;
133 struct stat statbuf;
134 struct passwd *pwd;
135 struct group *grp;
136 struct tm *tm;
137 char datestring[256];
138 ...
139 /* Loop through directory entries. */
140 while ((dp = readdir(dir)) != NULL) {
141
142
143 /* Get entry's information. */
144 if (stat(dp->d_name, &statbuf) == -1)
145 continue;
146
147
148 /* Print out type, permissions, and number of links. */
149 printf("%10.10s", sperm (statbuf.st_mode));
150 printf("%4d", statbuf.st_nlink);
151
152
153 /* Print out owner's name if it is found using getpwuid(). */
154 if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
155 printf(" %-8.8s", pwd->pw_name);
156 else
157 printf(" %-8d", statbuf.st_uid);
158
159
160 /* Print out group name if it is found using getgrgid(). */
161 if ((grp = getgrgid(statbuf.st_gid)) != NULL)
162 printf(" %-8.8s", grp->gr_name);
163 else
164 printf(" %-8d", statbuf.st_gid);
165
166
167 /* Print size of file. */
168 printf(" %9jd", (intmax_t)statbuf.st_size);
169
170
171 tm = localtime(&statbuf.st_mtime);
172
173
174 /* Get localized date string. */
175 strftime(datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm);
176
177
178 printf(" %s %s\n", datestring, dp->d_name);
179 }
180
182 None.
183
185 The intent of the paragraph describing "additional or alternate file
186 access control mechanisms" is to allow a secure implementation where a
187 process with a label that does not dominate the file's label cannot
188 perform a stat() function. This is not related to read permission; a
189 process with a label that dominates the file's label does not need read
190 permission. An implementation that supports write-up operations could
191 fail fstat() function calls even though it has a valid file descriptor
192 open for writing.
193
195 None.
196
198 fstat(), lstat(), readlink(), symlink(), the Base Definitions volume of
199 IEEE Std 1003.1-2001, <sys/stat.h>, <sys/types.h>
200
202 Portions of this text are reprinted and reproduced in electronic form
203 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
204 -- Portable Operating System Interface (POSIX), The Open Group Base
205 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
206 Electrical and Electronics Engineers, Inc and The Open Group. In the
207 event of any discrepancy between this version and the original IEEE and
208 The Open Group Standard, the original IEEE and The Open Group Standard
209 is the referee document. The original Standard can be obtained online
210 at http://www.opengroup.org/unix/online.html .
211
212
213
214IEEE/The Open Group 2003 STAT(3P)