xref: /netbsd-src/lib/libc/sys/stat.2 (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1.\"	$NetBSD: stat.2,v 1.11 1997/05/16 15:31:05 kleink 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.
72Unlike other filesystem objects,
73symbolic links do not have an owner, group, access mode, times, etc.
74Instead, these attributes are taken from the directory that
75contains the link.
76The only attributes returned from an
77.Fn lstat
78that refer to the symbolic link itself are the file type (S_IFLNK),
79size, blocks, and link count (always 1).
80.Pp
81The
82.Fn fstat
83function obtains the same information about an open file
84known by the file descriptor
85.Fa fd .
86.Pp
87The
88.Fa sb
89argument is a pointer to a
90.Fn stat
91structure
92as defined by
93.Aq Pa sys/stat.h
94(shown below)
95and into which information is placed concerning the file.
96.Bd -literal
97struct stat {
98    dev_t     st_dev;     /* inode's device */
99    ino_t     st_ino;     /* inode's number */
100    mode_t    st_mode;    /* inode protection mode */
101    nlink_t   st_nlink;   /* number or hard links to the file */
102    uid_t     st_uid;     /* user-id of owner */
103    gid_t     st_gid;     /* group-id of owner */
104    dev_t     st_rdev;    /* device type, for special file inode */
105    struct timespec st_atimespec;  /* time of last access */
106    struct timespec st_mtimespec;  /* time of last data modification */
107    struct timespec st_ctimespec;  /* time of last file status change */
108    off_t     st_size;    /* file size, in bytes */
109    int64_t   st_blocks;  /* blocks allocated for file */
110    u_int32_t st_blksize; /* optimal file sys I/O ops blocksize */
111    u_int32_t st_flags;   /* user defined flags for file */
112    u_int32_t st_gen;     /* file generation number */
113};
114.Ed
115.Pp
116The time-related fields of
117.Fa struct stat
118are as follows:
119.Bl -tag -width XXXst_mtime
120.It st_atime
121Time when file data was last accessed.
122Changed by the
123.Xr mknod 2 ,
124.Xr utimes 2
125and
126.Xr read 2
127system calls.
128.It st_mtime
129Time when file data was last modified.
130Changed by the
131.Xr mknod 2 ,
132.Xr utimes 2
133and
134.Xr write 2
135system calls.
136.It st_ctime
137Time when file status was last changed (inode data modification).
138Changed by the
139.Xr chflags 2 ,
140.Xr chmod 2 ,
141.Xr chown 2 ,
142.Xr link 2 ,
143.Xr mknod 2 ,
144.Xr rename 2 ,
145.Xr unlink 2 ,
146.Xr utimes 2
147and
148.Xr write 2
149system calls.
150.El
151.Pp
152The size-related fields of the
153.Fa struct stat
154are as follows:
155.Bl -tag -width XXXst_blksize
156.It st_blksize
157The optimal I/O block size for the file.
158.It st_blocks
159The actual number of blocks allocated for the file in 512-byte units.
160As short symbolic links are stored in the inode, this number may
161be zero.
162.El
163.Pp
164The status information word
165.Fa st_mode
166has the following bits:
167.Bd -literal
168#define S_IFMT 0170000           /* type of file */
169#define        S_IFIFO  0010000  /* named pipe (fifo) */
170#define        S_IFCHR  0020000  /* character special */
171#define        S_IFDIR  0040000  /* directory */
172#define        S_IFBLK  0060000  /* block special */
173#define        S_IFREG  0100000  /* regular */
174#define        S_IFLNK  0120000  /* symbolic link */
175#define        S_IFSOCK 0140000  /* socket */
176#define        S_IFWHT  0160000  /* whiteout */
177#define S_ISUID 0004000  /* set user id on execution */
178#define S_ISGID 0002000  /* set group id on execution */
179#define S_ISVTX 0001000  /* save swapped text even after use */
180#define S_IRUSR 0000400  /* read permission, owner */
181#define S_IWUSR 0000200  /* write permission, owner */
182#define S_IXUSR 0000100  /* execute/search permission, owner */
183#define S_IRGRP 0000040  /* read permission, group */
184#define S_IWGRP 0000020  /* write permission, group */
185#define S_IXGRP 0000010  /* execute/search permission, group */
186#define S_IROTH 0000004  /* read permission, other */
187#define S_IWOTH 0000002  /* write permission, other */
188#define S_IXOTH 0000001  /* execute/search permission, other */
189.Ed
190.Pp
191For a list of access modes, see
192.Aq Pa sys/stat.h ,
193.Xr access 2
194and
195.Xr chmod 2 .
196.Pp
197The status information word
198.Fa st_flags
199has the following bits:
200.Bd -literal
201#define UF_NODUMP	0x00000001 /* do not dump file */
202#define UF_IMMUTABLE	0x00000002 /* file may not be changed */
203#define UF_APPEND	0x00000004 /* writes to file may only append */
204#define UF_OPAQUE	0x00000008 /* directory is opaque wrt. union */
205#define SF_ARCHIVED	0x00010000 /* file is archived */
206#define SF_IMMUTABLE	0x00020000 /* file may not be changed */
207#define SF_APPEND	0x00040000 /* writes to file may only append */
208.Ed
209.Pp
210For a description of the flags, see
211.Xr chflags 2 .
212.Sh RETURN VALUES
213Upon successful completion a value of 0 is returned.
214Otherwise, a value of -1 is returned and
215.Va errno
216is set to indicate the error.
217.Sh COMPATIBILITY
218Previous versions of the system used different types for the
219.Li st_dev ,
220.Li st_uid ,
221.Li st_gid ,
222.Li st_rdev ,
223.Li st_size ,
224.Li st_blksize
225and
226.Li st_blocks
227fields.
228.Sh ERRORS
229.Fn Stat
230and
231.Fn lstat
232will fail if:
233.Bl -tag -width Er
234.It Bq Er ENOTDIR
235A component of the path prefix is not a directory.
236.It Bq Er ENAMETOOLONG
237A component of a pathname exceeded
238.Dv {NAME_MAX}
239characters, or an entire path name exceeded
240.Dv {PATH_MAX}
241characters.
242.It Bq Er ENOENT
243The named file does not exist.
244.It Bq Er EACCES
245Search permission is denied for a component of the path prefix.
246.It Bq Er ELOOP
247Too many symbolic links were encountered in translating the pathname.
248.It Bq Er EFAULT
249.Fa Sb
250or
251.Em name
252points to an invalid address.
253.It Bq Er EIO
254An I/O error occurred while reading from or writing to the file system.
255.El
256.Pp
257.Bl -tag -width Er
258.Fn Fstat
259will fail if:
260.It Bq Er EBADF
261.Fa fd
262is not a valid open file descriptor.
263.It Bq Er EFAULT
264.Fa Sb
265points to an invalid address.
266.It Bq Er EIO
267An I/O error occurred while reading from or writing to the file system.
268.El
269.Sh SEE ALSO
270.Xr chflags 2 ,
271.Xr chmod 2 ,
272.Xr chown 2 ,
273.Xr utimes 2 ,
274.Xr symlink 7
275.Sh BUGS
276Applying
277.Fn fstat
278to a socket (and thus to a pipe)
279returns a zero'd buffer,
280except for the blocksize field,
281and a unique device and inode number.
282.Sh STANDARDS
283The
284.Fn stat
285and
286.Fn fstat
287functions conform to
288.St -p1003.1-88 .
289.Sh HISTORY
290A
291.Fn lstat
292function call appeared in
293.Bx 4.2 .
294