xref: /netbsd-src/lib/libc/sys/stat.2 (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1.\"	$NetBSD: stat.2,v 1.14 1997/10/11 03:19:39 enami Exp $
2.\"
3.\" Copyright (c) 1980, 1991, 1993, 1994
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)stat.2	8.3 (Berkeley) 4/19/94
35.\"
36.Dd April 19, 1994
37.Dt STAT 2
38.Os BSD 4
39.Sh NAME
40.Nm stat ,
41.Nm lstat ,
42.Nm fstat
43.Nd get file status
44.Sh SYNOPSIS
45.Fd #include <sys/types.h>
46.Fd #include <sys/stat.h>
47.Ft int
48.Fn stat "const char *path" "struct stat *sb"
49.Ft int
50.Fn lstat "const char *path" "struct stat *sb"
51.Ft int
52.Fn fstat "int fd" "struct stat *sb"
53.Sh DESCRIPTION
54The
55.Fn stat
56function obtains information about the file pointed to by
57.Fa path .
58Read, write or execute
59permission of the named file is not required, but all directories
60listed in the path name leading to the file must be searchable.
61.Pp
62.Fn lstat
63is like
64.Fn stat
65except in the case where the named file is a symbolic link,
66in which case
67.Fn lstat
68returns information about the link,
69while
70.Fn stat
71returns information about the file the link references.
72.Pp
73The
74.Fn fstat
75function obtains the same information about an open file
76known by the file descriptor
77.Fa fd .
78.Pp
79The
80.Fa sb
81argument is a pointer to a
82.Fn stat
83structure
84as defined by
85.Aq Pa sys/stat.h
86(shown below)
87and into which information is placed concerning the file.
88.Bd -literal
89struct stat {
90    dev_t     st_dev;     /* inode's device */
91    ino_t     st_ino;     /* inode's number */
92    mode_t    st_mode;    /* inode protection mode */
93    nlink_t   st_nlink;   /* number or hard links to the file */
94    uid_t     st_uid;     /* user-id of owner */
95    gid_t     st_gid;     /* group-id of owner */
96    dev_t     st_rdev;    /* device type, for special file inode */
97    struct timespec st_atimespec;  /* time of last access */
98    struct timespec st_mtimespec;  /* time of last data modification */
99    struct timespec st_ctimespec;  /* time of last file status change */
100    off_t     st_size;    /* file size, in bytes */
101    int64_t   st_blocks;  /* blocks allocated for file */
102    u_int32_t st_blksize; /* optimal file sys I/O ops blocksize */
103    u_int32_t st_flags;   /* user defined flags for file */
104    u_int32_t st_gen;     /* file generation number */
105};
106.Ed
107.Pp
108The time-related fields of
109.Fa struct stat
110are as follows:
111.Bl -tag -width XXXst_mtime
112.It st_atime
113Time when file data was last accessed.
114Changed by the
115.Xr mknod 2 ,
116.Xr utimes 2
117and
118.Xr read 2
119system calls.
120.It st_mtime
121Time when file data was last modified.
122Changed by the
123.Xr mknod 2 ,
124.Xr utimes 2
125and
126.Xr write 2
127system calls.
128.It st_ctime
129Time when file status was last changed (inode data modification).
130Changed by the
131.Xr chflags 2 ,
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
139and
140.Xr write 2
141system calls.
142.El
143.Pp
144The size-related fields of the
145.Fa struct stat
146are as follows:
147.Bl -tag -width XXXst_blksize
148.It st_blksize
149The optimal I/O block size for the file.
150.It st_blocks
151The actual number of blocks allocated for the file in 512-byte units.
152As short symbolic links are stored in the inode, this number may
153be zero.
154.El
155.Pp
156The status information word
157.Fa st_mode
158has the following bits:
159.Bd -literal
160#define S_IFMT 0170000           /* type of file */
161#define        S_IFIFO  0010000  /* named pipe (fifo) */
162#define        S_IFCHR  0020000  /* character special */
163#define        S_IFDIR  0040000  /* directory */
164#define        S_IFBLK  0060000  /* block special */
165#define        S_IFREG  0100000  /* regular */
166#define        S_IFLNK  0120000  /* symbolic link */
167#define        S_IFSOCK 0140000  /* socket */
168#define        S_IFWHT  0160000  /* whiteout */
169#define S_ISUID 0004000  /* set user id on execution */
170#define S_ISGID 0002000  /* set group id on execution */
171#define S_ISVTX 0001000  /* save swapped text even after use */
172#define S_IRUSR 0000400  /* read permission, owner */
173#define S_IWUSR 0000200  /* write permission, owner */
174#define S_IXUSR 0000100  /* execute/search permission, owner */
175#define S_IRGRP 0000040  /* read permission, group */
176#define S_IWGRP 0000020  /* write permission, group */
177#define S_IXGRP 0000010  /* execute/search permission, group */
178#define S_IROTH 0000004  /* read permission, other */
179#define S_IWOTH 0000002  /* write permission, other */
180#define S_IXOTH 0000001  /* execute/search permission, other */
181.Ed
182.Pp
183For a list of access modes, see
184.Aq Pa sys/stat.h ,
185.Xr access 2
186and
187.Xr chmod 2 .
188.Pp
189The status information word
190.Fa st_flags
191has the following bits:
192.Bd -literal
193#define UF_NODUMP	0x00000001 /* do not dump file */
194#define UF_IMMUTABLE	0x00000002 /* file may not be changed */
195#define UF_APPEND	0x00000004 /* writes to file may only append */
196#define UF_OPAQUE	0x00000008 /* directory is opaque wrt. union */
197#define SF_ARCHIVED	0x00010000 /* file is archived */
198#define SF_IMMUTABLE	0x00020000 /* file may not be changed */
199#define SF_APPEND	0x00040000 /* writes to file may only append */
200.Ed
201.Pp
202For a description of the flags, see
203.Xr chflags 2 .
204.Sh RETURN VALUES
205Upon successful completion a value of 0 is returned.
206Otherwise, a value of -1 is returned and
207.Va errno
208is set to indicate the error.
209.Sh COMPATIBILITY
210Previous versions of the system used different types for the
211.Li st_dev ,
212.Li st_uid ,
213.Li st_gid ,
214.Li st_rdev ,
215.Li st_size ,
216.Li st_blksize
217and
218.Li st_blocks
219fields.
220.Sh ERRORS
221.Fn stat
222and
223.Fn lstat
224will fail if:
225.Bl -tag -width Er
226.It Bq Er ENOTDIR
227A component of the path prefix is not a directory.
228.It Bq Er ENAMETOOLONG
229A component of a pathname exceeded
230.Dv {NAME_MAX}
231characters, or an entire path name exceeded
232.Dv {PATH_MAX}
233characters.
234.It Bq Er ENOENT
235The named file does not exist.
236.It Bq Er EACCES
237Search permission is denied for a component of the path prefix.
238.It Bq Er ELOOP
239Too many symbolic links were encountered in translating the pathname.
240.It Bq Er EFAULT
241.Fa Sb
242or
243.Em name
244points to an invalid address.
245.It Bq Er EIO
246An I/O error occurred while reading from or writing to the file system.
247.El
248.Pp
249.Bl -tag -width Er
250.Fn fstat
251will fail if:
252.It Bq Er EBADF
253.Fa fd
254is not a valid open file descriptor.
255.It Bq Er EFAULT
256.Fa Sb
257points to an invalid address.
258.It Bq Er EIO
259An I/O error occurred while reading from or writing to the file system.
260.El
261.Sh SEE ALSO
262.Xr chflags 2 ,
263.Xr chmod 2 ,
264.Xr chown 2 ,
265.Xr utimes 2 ,
266.Xr symlink 7
267.Sh BUGS
268Applying
269.Fn fstat
270to a socket (and thus to a pipe)
271returns a zero'd buffer,
272except for the blocksize field,
273and a unique device and inode number.
274.Sh STANDARDS
275The
276.Fn stat
277and
278.Fn fstat
279functions conform to
280.St -p1003.1-90 .
281.Sh HISTORY
282A
283.Fn lstat
284function call appeared in
285.Bx 4.2 .
286