Commit | Line | Data |
---|---|---|
931b8415 CL |
1 | .\" Copyright (c) 1980, 1991 Regents of the University of California. |
2 | .\" All rights reserved. | |
924d83cf | 3 | .\" |
931b8415 | 4 | .\" %sccs.include.redist.man% |
924d83cf | 5 | .\" |
931b8415 CL |
6 | .\" @(#)stat.2 6.9 (Berkeley) %G% |
7 | .\" | |
8 | .Dd | |
9 | .Dt STAT 2 | |
10 | .Os BSD 4 | |
11 | .Sh NAME | |
12 | .Nm stat , | |
13 | .Nm lstat , | |
14 | .Nm fstat | |
15 | .Nd get file status | |
16 | .Sh SYNOPSIS | |
17 | .Fd #include <sys/types.h> | |
18 | .Fd #include <sys/stat.h> | |
19 | .Ft int | |
20 | .Fn stat "const char *path" "struct stat *buf" | |
21 | .Ft int | |
22 | .Fn lstat "const char *path" "struct stat *buf" | |
23 | .Ft int | |
24 | .Fn fstat "int fd" "struct stat *buf" | |
25 | .Sh DESCRIPTION | |
26 | The | |
27 | .Fn stat | |
28 | function obtains information about the file pointed to by | |
29 | .Fa path . | |
7b1f0e33 KM |
30 | Read, write or execute |
31 | permission of the named file is not required, but all directories | |
931b8415 CL |
32 | listed in the path name leading to the file must be seachable. |
33 | .Pp | |
34 | .Fn Lstat | |
35 | is like | |
36 | .Fn stat | |
37 | except in the case where the named file is a symbolic link, | |
7b1f0e33 | 38 | in which case |
931b8415 | 39 | .Fn lstat |
7b1f0e33 KM |
40 | returns information about the link, |
41 | while | |
931b8415 | 42 | .Fn stat |
7b1f0e33 | 43 | returns information about the file the link references. |
931b8415 CL |
44 | .Pp |
45 | The | |
46 | .Fn fstat | |
924d83cf | 47 | obtains the same information about an open file |
931b8415 CL |
48 | known by the file descriptor |
49 | .Fa fd , | |
50 | such as would | |
d642d017 | 51 | be obtained by an |
931b8415 | 52 | .Xr open |
d642d017 | 53 | call. |
931b8415 CL |
54 | .Pp |
55 | .Fa Buf | |
7b1f0e33 | 56 | is a pointer to a |
931b8415 CL |
57 | .Fn stat |
58 | structure | |
59 | as defined by | |
60 | .Aq Pa sys/stat.h | |
61 | (shown below) | |
62 | and into which information is placed concerning the file. | |
63 | .Bd -literal | |
64 | struct stat { | |
65 | dev_t st_dev; /* device inode resides on */ | |
66 | ino_t st_ino; /* inode's number */ | |
67 | mode_t st_mode; /* inode protection mode */ | |
68 | nlink_t st_nlink; /* number or hard links to the file */ | |
69 | uid_t st_uid; /* user-id of owner */ | |
70 | gid_t st_gid; /* group-id of owner */ | |
71 | dev_t st_rdev; /* device type, for special file inode */ | |
72 | off_t st_size; /* file size, in bytes */ | |
73 | time_t st_atime; /* time of last access */ | |
74 | long st_spare1; | |
75 | time_t st_mtime; /* time of last data modification */ | |
76 | long st_spare2; | |
77 | time_t st_ctime; /* time of last file status change */ | |
78 | long st_spare3; | |
79 | long st_blksize;/* optimal file sys I/O ops blocksize */ | |
80 | long st_blocks; /* blocks allocated for file */ | |
81 | u_long st_flags; /* user defined flags for file */ | |
82 | u_long st_gen; /* file generation number */ | |
83 | }; | |
84 | .Ed | |
85 | .Pp | |
86 | The time-related fields of | |
87 | .Fa struct stat | |
88 | are as follows: | |
89 | .Bl -tag -width st_blocks | |
90 | .It st_atime | |
91 | Time when file data last accessed. Changed by the following system | |
7b1f0e33 | 92 | calls: |
931b8415 CL |
93 | .Xr mknod 2 , |
94 | .Xr utimes 2 , | |
7b1f0e33 | 95 | and |
931b8415 CL |
96 | .Xr read 2 . |
97 | .It st_mtime | |
98 | Time when file data last modified. | |
7b1f0e33 | 99 | Changed by the following system calls: |
931b8415 CL |
100 | .Xr mknod 2 , |
101 | .Xr utimes 2 , | |
102 | .Xr write 2 . | |
103 | .It st_ctime | |
104 | Time when file status was last changed (inode data modification). | |
7b1f0e33 | 105 | Changed by the following system calls: |
931b8415 CL |
106 | .Xr chmod 2 |
107 | .Xr chown 2 , | |
108 | .Xr link 2 , | |
109 | .Xr mknod 2 , | |
110 | .Xr rename 2 , | |
111 | .Xr unlink 2 , | |
112 | .Xr utimes 2 , | |
113 | .Xr write 2 . | |
114 | .It st_blocks | |
d642d017 | 115 | The actual number of blocks allocated for the file in 512-byte units. |
931b8415 CL |
116 | .El |
117 | .Pp | |
118 | The status information word | |
119 | .Fa st_mode | |
120 | has bits: | |
121 | .Bd -literal | |
122 | #define S_IFMT 0170000 /* type of file */ | |
123 | #define S_IFIFO 0010000 /* named pipe (fifo) */ | |
124 | #define S_IFCHR 0020000 /* character special */ | |
125 | #define S_IFDIR 0040000 /* directory */ | |
126 | #define S_IFBLK 0060000 /* block special */ | |
127 | #define S_IFREG 0100000 /* regular */ | |
128 | #define S_IFLNK 0120000 /* symbolic link */ | |
129 | #define S_IFSOCK 0140000 /* socket */ | |
130 | #define S_ISUID 0004000 /* set user id on execution */ | |
131 | #define S_ISGID 0002000 /* set group id on execution */ | |
132 | #define S_ISVTX 0001000 /* save swapped text even after use */ | |
133 | #define S_IRUSR 0000400 /* read permission, owner */ | |
134 | #define S_IWUSR 0000200 /* write permission, owner */ | |
135 | #define S_IXUSR 0000100 /* execute/search permission, owner */ | |
136 | .Ed | |
137 | .Pp | |
138 | For a list of access modes, see | |
139 | .Aq Pa sys/stat.h , | |
140 | .Xr access 2 | |
141 | and | |
142 | .Xr chmod 2 . | |
143 | .Sh RETURN VALUES | |
7b1f0e33 | 144 | Upon successful completion a value of 0 is returned. |
931b8415 CL |
145 | Otherwise, a value of -1 is returned and |
146 | .Va errno | |
7b1f0e33 | 147 | is set to indicate the error. |
931b8415 CL |
148 | .Sh ERRORS |
149 | .Fn Stat | |
7b1f0e33 | 150 | and |
931b8415 CL |
151 | .Fn lstat |
152 | will fail if: | |
153 | .Bl -tag -width ENAMETOOLONGAA | |
154 | .It Bq Er ENOTDIR | |
7b1f0e33 | 155 | A component of the path prefix is not a directory. |
931b8415 | 156 | .It Bq Er EINVAL |
7b1f0e33 | 157 | The pathname contains a character with the high-order bit set. |
931b8415 | 158 | .It Bq Er ENAMETOOLONG |
b5984ffe KM |
159 | A component of a pathname exceeded 255 characters, |
160 | or an entire path name exceeded 1023 characters. | |
931b8415 | 161 | .It Bq Er ENOENT |
7b1f0e33 | 162 | The named file does not exist. |
931b8415 | 163 | .It Bq Er EACCES |
7b1f0e33 | 164 | Search permission is denied for a component of the path prefix. |
931b8415 | 165 | .It Bq Er ELOOP |
b5984ffe | 166 | Too many symbolic links were encountered in translating the pathname. |
931b8415 CL |
167 | .It Bq Er EFAULT |
168 | .Fa Buf | |
7b1f0e33 | 169 | or |
931b8415 | 170 | .Em name |
7b1f0e33 | 171 | points to an invalid address. |
931b8415 | 172 | .It Bq Er EIO |
fd690c8b | 173 | An I/O error occurred while reading from or writing to the file system. |
931b8415 CL |
174 | .El |
175 | .Pp | |
176 | .Bl -tag -width [EFAULT] | |
177 | .Fn Fstat | |
178 | will fail if: | |
179 | .It Bq Er EBADF | |
180 | .Fa fd | |
7b1f0e33 | 181 | is not a valid open file descriptor. |
931b8415 CL |
182 | .It Bq Er EFAULT |
183 | .Fa Buf | |
7b1f0e33 | 184 | points to an invalid address. |
931b8415 | 185 | .It Bq Er EIO |
fd690c8b | 186 | An I/O error occurred while reading from or writing to the file system. |
931b8415 CL |
187 | .El |
188 | .Sh CAVEAT | |
7b1f0e33 | 189 | The fields in the stat structure currently marked |
931b8415 CL |
190 | .Fa st_spare1 , |
191 | .Fa st_spare2 , | |
7b1f0e33 | 192 | and |
931b8415 | 193 | .Fa st_spare3 |
7b1f0e33 | 194 | are present in preparation for inode time stamps expanding |
eff6446c | 195 | to 64 bits. This, however, can break certain programs that |
7b1f0e33 | 196 | depend on the time stamps being contiguous (in calls to |
931b8415 CL |
197 | .Xr utimes 2 ) . |
198 | .Sh SEE ALSO | |
199 | .Xr chmod 2 , | |
200 | .Xr chown 2 , | |
201 | .Xr utimes 2 | |
202 | .Sh BUGS | |
7b1f0e33 | 203 | Applying |
931b8415 | 204 | .Xr fstat |
9d2e549b | 205 | to a socket (and thus to a pipe) |
460177a7 KM |
206 | returns a zero'd buffer, |
207 | except for the blocksize field, | |
208 | and a unique device and inode number. | |
931b8415 CL |
209 | .Sh STANDARDS |
210 | The | |
211 | .Fn stat | |
212 | and | |
213 | .Fn fstat | |
214 | function calls are expected to | |
215 | conform to IEEE Std 1003.1-1988 | |
216 | .Pq Dq Tn POSIX . | |
217 | .Sh HISTORY | |
218 | A | |
219 | .Nm lstat | |
220 | function call appeared in | |
221 | .Bx 4.2 . |