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