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