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