xref: /386bsd/usr/src/lib/libc/sys/stat.2 (revision a2142627)
1.\" Copyright (c) 1980, 1991 Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)stat.2	6.9 (Berkeley) 3/10/91
33.\"
34.Dd March 10, 1991
35.Dt STAT 2
36.Os BSD 4
37.Sh NAME
38.Nm stat ,
39.Nm lstat ,
40.Nm fstat
41.Nd get file status
42.Sh SYNOPSIS
43.Fd #include <sys/types.h>
44.Fd #include <sys/stat.h>
45.Ft int
46.Fn stat "const char *path" "struct stat *buf"
47.Ft int
48.Fn lstat "const char *path" "struct stat *buf"
49.Ft int
50.Fn fstat "int fd" "struct stat *buf"
51.Sh DESCRIPTION
52The
53.Fn stat
54function obtains information about the file pointed to by
55.Fa path .
56Read, write or execute
57permission of the named file is not required, but all directories
58listed in the path name leading to the file must be seachable.
59.Pp
60.Fn Lstat
61is like
62.Fn stat
63except in the case where the named file is a symbolic link,
64in which case
65.Fn lstat
66returns information about the link,
67while
68.Fn stat
69returns information about the file the link references.
70.Pp
71The
72.Fn fstat
73obtains the same information about an open file
74known by the file descriptor
75.Fa fd ,
76such as would
77be obtained by an
78.Xr open
79call.
80.Pp
81.Fa Buf
82is a pointer to a
83.Fn stat
84structure
85as defined by
86.Aq Pa sys/stat.h
87(shown below)
88and into which information is placed concerning the file.
89.Bd -literal
90struct stat {
91    dev_t    st_dev;    /* device inode resides on */
92    ino_t    st_ino;    /* inode's number */
93    mode_t   st_mode;   /* inode protection mode */
94    nlink_t  st_nlink;  /* number or hard links to the file */
95    uid_t    st_uid;    /* user-id of owner */
96    gid_t    st_gid;    /* group-id of owner */
97    dev_t    st_rdev;   /* device type, for special file inode */
98    off_t    st_size;   /* file size, in bytes */
99    time_t   st_atime;  /* time of last access */
100    long     st_spare1;
101    time_t   st_mtime;  /* time of last data modification */
102    long     st_spare2;
103    time_t   st_ctime;  /* time of last file status change */
104    long     st_spare3;
105    long     st_blksize;/* optimal file sys I/O ops blocksize */
106    long     st_blocks; /* blocks allocated for file */
107    u_long   st_flags;  /* user defined flags for file */
108    u_long   st_gen;    /* file generation number */
109};
110.Ed
111.Pp
112The time-related fields of
113.Fa struct stat
114are as follows:
115.Bl -tag -width st_blocks
116.It st_atime
117Time when file data last accessed.  Changed by the following system
118calls:
119.Xr mknod 2 ,
120.Xr utimes 2 ,
121and
122.Xr read 2 .
123.It st_mtime
124Time when file data last modified.
125Changed by the following system calls:
126.Xr mknod 2 ,
127.Xr utimes 2 ,
128.Xr write 2 .
129.It st_ctime
130Time when file status was last changed (inode data modification).
131Changed by the following system calls:
132.Xr chmod 2
133.Xr chown 2 ,
134.Xr link 2 ,
135.Xr mknod 2 ,
136.Xr rename 2 ,
137.Xr unlink 2 ,
138.Xr utimes 2 ,
139.Xr write 2 .
140.It st_blocks
141The actual number of blocks allocated for the file in 512-byte units.
142.El
143.Pp
144The status information word
145.Fa st_mode
146has bits:
147.Bd -literal
148#define S_IFMT 0170000           /* type of file */
149#define        S_IFIFO  0010000  /* named pipe (fifo) */
150#define        S_IFCHR  0020000  /* character special */
151#define        S_IFDIR  0040000  /* directory */
152#define        S_IFBLK  0060000  /* block special */
153#define        S_IFREG  0100000  /* regular */
154#define        S_IFLNK  0120000  /* symbolic link */
155#define        S_IFSOCK 0140000  /* socket */
156#define S_ISUID 0004000  /* set user id on execution */
157#define S_ISGID 0002000  /* set group id on execution */
158#define S_ISVTX 0001000  /* save swapped text even after use */
159#define S_IRUSR 0000400  /* read permission, owner */
160#define S_IWUSR 0000200  /* write permission, owner */
161#define S_IXUSR 0000100  /* execute/search permission, owner */
162.Ed
163.Pp
164For a list of access modes, see
165.Aq Pa sys/stat.h ,
166.Xr access 2
167and
168.Xr chmod 2 .
169.Sh RETURN VALUES
170Upon successful completion a value of 0 is returned.
171Otherwise, a value of -1 is returned and
172.Va errno
173is set to indicate the error.
174.Sh ERRORS
175.Fn Stat
176and
177.Fn lstat
178will fail if:
179.Bl -tag -width ENAMETOOLONGAA
180.It Bq Er ENOTDIR
181A component of the path prefix is not a directory.
182.It Bq Er EINVAL
183The pathname contains a character with the high-order bit set.
184.It Bq Er ENAMETOOLONG
185A component of a pathname exceeded 255 characters,
186or an entire path name exceeded 1023 characters.
187.It Bq Er ENOENT
188The named file does not exist.
189.It Bq Er EACCES
190Search permission is denied for a component of the path prefix.
191.It Bq Er ELOOP
192Too many symbolic links were encountered in translating the pathname.
193.It Bq Er EFAULT
194.Fa Buf
195or
196.Em name
197points to an invalid address.
198.It Bq Er EIO
199An I/O error occurred while reading from or writing to the file system.
200.El
201.Pp
202.Bl -tag -width [EFAULT]
203.Fn Fstat
204will fail if:
205.It Bq Er EBADF
206.Fa fd
207is not a valid open file descriptor.
208.It Bq Er EFAULT
209.Fa Buf
210points to an invalid address.
211.It Bq Er EIO
212An I/O error occurred while reading from or writing to the file system.
213.El
214.Sh CAVEAT
215The fields in the stat structure currently marked
216.Fa st_spare1 ,
217.Fa st_spare2 ,
218and
219.Fa st_spare3
220are present in preparation for inode time stamps expanding
221to 64 bits.  This, however, can break certain programs that
222depend on the time stamps being contiguous (in calls to
223.Xr utimes 2 ) .
224.Sh SEE ALSO
225.Xr chmod 2 ,
226.Xr chown 2 ,
227.Xr utimes 2
228.Sh BUGS
229Applying
230.Xr fstat
231to a socket (and thus to a pipe)
232returns a zero'd buffer,
233except for the blocksize field,
234and a unique device and inode number.
235.Sh STANDARDS
236The
237.Fn stat
238and
239.Fn fstat
240function calls are expected to
241conform to IEEE Std 1003.1-1988
242.Pq Dq Tn POSIX .
243.Sh HISTORY
244A
245.Nm lstat
246function call appeared in
247.Bx 4.2 .
248