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