990 lines
24 KiB
HTML
990 lines
24 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of STAT</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>STAT</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
stat, fstat, lstat, fstatat - get file status
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></B>
|
|
<B>#include <<A HREF="file:///usr/include/sys/stat.h">sys/stat.h</A>></B>
|
|
<B>#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>></B>
|
|
|
|
<B>int stat(const char *</B><I>pathname</I><B>, struct stat *</B><I>statbuf</I><B>);</B>
|
|
<B>int fstat(int </B><I>fd</I><B>, struct stat *</B><I>statbuf</I><B>);</B>
|
|
<B>int lstat(const char *</B><I>pathname</I><B>, struct stat *</B><I>statbuf</I><B>);</B>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>> </B>/* Definition of AT_* constants */
|
|
<B>#include <<A HREF="file:///usr/include/sys/stat.h">sys/stat.h</A>></B>
|
|
|
|
<B>int fstatat(int </B><I>dirfd</I><B>, const char *</B><I>pathname</I><B>, struct stat *</B><I>statbuf</I><B>,</B>
|
|
<B> int </B><I>flags</I><B>);</B>
|
|
</PRE>
|
|
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
<P>
|
|
|
|
|
|
<B>lstat</B>():
|
|
|
|
<DL COMPACT><DT id="1"><DD>
|
|
/* glibc 2.19 and earlier */ _BSD_SOURCE
|
|
<BR>
|
|
|
|
<BR> || /* Since glibc 2.20 */ _DEFAULT_SOURCE
|
|
<BR>
|
|
|
|
<BR> || _XOPEN_SOURCE >= 500
|
|
|
|
<BR>
|
|
|
|
<BR> || /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200112L
|
|
</DL>
|
|
|
|
<P>
|
|
|
|
<B>fstatat</B>():
|
|
|
|
|
|
<DL COMPACT><DT id="2"><DD>
|
|
|
|
<DL COMPACT>
|
|
<DT id="3">Since glibc 2.10:<DD>
|
|
_POSIX_C_SOURCE >= 200809L
|
|
<DT id="4">Before glibc 2.10:<DD>
|
|
_ATFILE_SOURCE
|
|
</DL>
|
|
</DL>
|
|
|
|
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
<P>
|
|
|
|
These functions return information about a file, in the buffer pointed to by
|
|
<I>statbuf</I>.
|
|
|
|
No permissions are required on the file itself, but---in the case of
|
|
<B>stat</B>(),
|
|
|
|
<B>fstatat</B>(),
|
|
|
|
and
|
|
<B>lstat</B>()---execute
|
|
|
|
(search) permission is required on all of the directories in
|
|
<I>pathname</I>
|
|
|
|
that lead to the file.
|
|
<P>
|
|
|
|
<B>stat</B>()
|
|
|
|
and
|
|
<B>fstatat</B>()
|
|
|
|
retrieve information about the file pointed to by
|
|
<I>pathname</I>;
|
|
|
|
the differences for
|
|
<B>fstatat</B>()
|
|
|
|
are described below.
|
|
<P>
|
|
|
|
<B>lstat</B>()
|
|
|
|
is identical to
|
|
<B>stat</B>(),
|
|
|
|
except that if
|
|
<I>pathname</I>
|
|
|
|
is a symbolic link, then it returns information about the link itself,
|
|
not the file that it refers to.
|
|
<P>
|
|
|
|
<B>fstat</B>()
|
|
|
|
is identical to
|
|
<B>stat</B>(),
|
|
|
|
except that the file about which information is to be retrieved
|
|
is specified by the file descriptor
|
|
<I>fd</I>.
|
|
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H3>The stat structure</H3>
|
|
|
|
All of these system calls return a
|
|
<I>stat</I>
|
|
|
|
structure, which contains the following fields:
|
|
<P>
|
|
|
|
|
|
|
|
struct stat {
|
|
<BR> dev_t st_dev; /* ID of device containing file */
|
|
<BR> ino_t st_ino; /* Inode number */
|
|
<BR> mode_t st_mode; /* File type and mode */
|
|
<BR> nlink_t st_nlink; /* Number of hard links */
|
|
<BR> uid_t st_uid; /* User ID of owner */
|
|
<BR> gid_t st_gid; /* Group ID of owner */
|
|
<BR> dev_t st_rdev; /* Device ID (if special file) */
|
|
<BR> off_t st_size; /* Total size, in bytes */
|
|
<BR> blksize_t st_blksize; /* Block size for filesystem I/O */
|
|
<BR> blkcnt_t st_blocks; /* Number of 512B blocks allocated */
|
|
<P>
|
|
<BR> /* Since Linux 2.6, the kernel supports nanosecond
|
|
<BR> precision for the following timestamp fields.
|
|
<BR> For the details before Linux 2.6, see NOTES. */
|
|
<P>
|
|
<BR> struct timespec st_atim; /* Time of last access */
|
|
<BR> struct timespec st_mtim; /* Time of last modification */
|
|
<BR> struct timespec st_ctim; /* Time of last status change */
|
|
<P>
|
|
#define st_atime st_atim.tv_sec /* Backward compatibility */
|
|
#define st_mtime st_mtim.tv_sec
|
|
#define st_ctime st_ctim.tv_sec
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
<I>Note</I>:
|
|
|
|
the order of fields in the
|
|
<I>stat</I>
|
|
|
|
structure varies somewhat
|
|
across architectures.
|
|
In addition,
|
|
the definition above does not show the padding bytes
|
|
that may be present between some fields on various architectures.
|
|
Consult the glibc and kernel source code
|
|
if you need to know the details.
|
|
<P>
|
|
|
|
|
|
|
|
<I>Note</I>:
|
|
|
|
for performance and simplicity reasons, different fields in the
|
|
<I>stat</I>
|
|
|
|
structure may contain state information from different moments
|
|
during the execution of the system call.
|
|
For example, if
|
|
<I>st_mode</I>
|
|
|
|
or
|
|
<I>st_uid</I>
|
|
|
|
is changed by another process by calling
|
|
<B><A HREF="/cgi-bin/man/man2html?2+chmod">chmod</A></B>(2)
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?2+chown">chown</A></B>(2),
|
|
|
|
<B>stat</B>()
|
|
|
|
might return the old
|
|
<I>st_mode</I>
|
|
|
|
together with the new
|
|
<I>st_uid</I>,
|
|
|
|
or the old
|
|
<I>st_uid</I>
|
|
|
|
together with the new
|
|
<I>st_mode</I>.
|
|
|
|
<P>
|
|
|
|
The fields in the
|
|
<I>stat</I>
|
|
|
|
structure are as follows:
|
|
<DL COMPACT>
|
|
<DT id="5"><I>st_dev</I>
|
|
|
|
<DD>
|
|
This field describes the device on which this file resides.
|
|
(The
|
|
<B><A HREF="/cgi-bin/man/man2html?3+major">major</A></B>(3)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+minor">minor</A></B>(3)
|
|
|
|
macros may be useful to decompose the device ID in this field.)
|
|
<DT id="6"><I>st_ino</I>
|
|
|
|
<DD>
|
|
This field contains the file's inode number.
|
|
<DT id="7"><I>st_mode</I>
|
|
|
|
<DD>
|
|
This field contains the file type and mode.
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?7+inode">inode</A></B>(7)
|
|
|
|
for further information.
|
|
<DT id="8"><I>st_nlink</I>
|
|
|
|
<DD>
|
|
This field contains the number of hard links to the file.
|
|
<DT id="9"><I>st_uid</I>
|
|
|
|
<DD>
|
|
This field contains the user ID of the owner of the file.
|
|
<DT id="10"><I>st_gid</I>
|
|
|
|
<DD>
|
|
This field contains the ID of the group owner of the file.
|
|
<DT id="11"><I>st_rdev</I>
|
|
|
|
<DD>
|
|
This field describes the device that this file (inode) represents.
|
|
<DT id="12"><I>st_size</I>
|
|
|
|
<DD>
|
|
This field gives the size of the file (if it is a regular
|
|
file or a symbolic link) in bytes.
|
|
The size of a symbolic link is the length of the pathname
|
|
it contains, without a terminating null byte.
|
|
<DT id="13"><I>st_blksize</I>
|
|
|
|
<DD>
|
|
This field gives the "preferred" block size for efficient filesystem I/O.
|
|
<DT id="14"><I>st_blocks</I>
|
|
|
|
<DD>
|
|
This field indicates the number of blocks allocated to the file,
|
|
in 512-byte units.
|
|
(This may be smaller than
|
|
<I>st_size</I>/512
|
|
|
|
when the file has holes.)
|
|
<DT id="15"><I>st_atime</I>
|
|
|
|
<DD>
|
|
This is the file's last access timestamp.
|
|
<DT id="16"><I>st_mtime</I>
|
|
|
|
<DD>
|
|
This is the file's last modification timestamp.
|
|
<DT id="17"><I>st_ctime</I>
|
|
|
|
<DD>
|
|
This is the file's last status change timestamp.
|
|
</DL>
|
|
<P>
|
|
|
|
For further information on the above fields, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+inode">inode</A></B>(7).
|
|
|
|
|
|
<A NAME="lbAF"> </A>
|
|
<H3>fstatat()</H3>
|
|
|
|
The
|
|
<B>fstatat</B>()
|
|
|
|
system call is a more general interface for accessing file information
|
|
which can still provide exactly the behavior of each of
|
|
<B>stat</B>(),
|
|
|
|
<B>lstat</B>(),
|
|
|
|
and
|
|
<B>fstat</B>().
|
|
|
|
<P>
|
|
|
|
If the pathname given in
|
|
<I>pathname</I>
|
|
|
|
is relative, then it is interpreted relative to the directory
|
|
referred to by the file descriptor
|
|
<I>dirfd</I>
|
|
|
|
(rather than relative to the current working directory of
|
|
the calling process, as is done by
|
|
<B>stat</B>()
|
|
|
|
and
|
|
<B>lstat</B>()
|
|
|
|
for a relative pathname).
|
|
<P>
|
|
|
|
If
|
|
<I>pathname</I>
|
|
|
|
is relative and
|
|
<I>dirfd</I>
|
|
|
|
is the special value
|
|
<B>AT_FDCWD</B>,
|
|
|
|
then
|
|
<I>pathname</I>
|
|
|
|
is interpreted relative to the current working
|
|
directory of the calling process (like
|
|
<B>stat</B>()
|
|
|
|
and
|
|
<B>lstat</B>()).
|
|
|
|
<P>
|
|
|
|
If
|
|
<I>pathname</I>
|
|
|
|
is absolute, then
|
|
<I>dirfd</I>
|
|
|
|
is ignored.
|
|
<P>
|
|
|
|
<I>flags</I>
|
|
|
|
can either be 0, or include one or more of the following flags ORed:
|
|
<DL COMPACT>
|
|
<DT id="18"><B>AT_EMPTY_PATH</B> (since Linux 2.6.39)
|
|
|
|
<DD>
|
|
|
|
If
|
|
<I>pathname</I>
|
|
|
|
is an empty string, operate on the file referred to by
|
|
<I>dirfd</I>
|
|
|
|
(which may have been obtained using the
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
|
|
|
|
<B>O_PATH</B>
|
|
|
|
flag).
|
|
In this case,
|
|
<I>dirfd</I>
|
|
|
|
can refer to any type of file, not just a directory, and
|
|
the behavior of
|
|
<B>fstatat</B>()
|
|
|
|
is similar to that of
|
|
<B>fstat</B>().
|
|
|
|
If
|
|
<I>dirfd</I>
|
|
|
|
is
|
|
<B>AT_FDCWD</B>,
|
|
|
|
the call operates on the current working directory.
|
|
This flag is Linux-specific; define
|
|
<B>_GNU_SOURCE</B>
|
|
|
|
|
|
to obtain its definition.
|
|
<DT id="19"><B>AT_NO_AUTOMOUNT</B> (since Linux 2.6.38)
|
|
|
|
<DD>
|
|
Don't automount the terminal ("basename") component of
|
|
<I>pathname</I>
|
|
|
|
if it is a directory that is an automount point.
|
|
This allows the caller to gather attributes of an automount point
|
|
(rather than the location it would mount).
|
|
Since Linux 4.14,
|
|
|
|
also don't instantiate a nonexistent name in an
|
|
on-demand directory such as used for automounter indirect maps.
|
|
This flag can be used in tools that scan directories
|
|
to prevent mass-automounting of a directory of automount points.
|
|
The
|
|
<B>AT_NO_AUTOMOUNT</B>
|
|
|
|
flag has no effect if the mount point has already been mounted over.
|
|
This flag is Linux-specific; define
|
|
<B>_GNU_SOURCE</B>
|
|
|
|
|
|
to obtain its definition.
|
|
Both
|
|
<B>stat</B>()
|
|
|
|
and
|
|
<B>lstat</B>()
|
|
|
|
act as though
|
|
<B>AT_NO_AUTOMOUNT</B>
|
|
|
|
was set.
|
|
<DT id="20"><B>AT_SYMLINK_NOFOLLOW</B>
|
|
|
|
<DD>
|
|
If
|
|
<I>pathname</I>
|
|
|
|
is a symbolic link, do not dereference it:
|
|
instead return information about the link itself, like
|
|
<B>lstat</B>().
|
|
|
|
(By default,
|
|
<B>fstatat</B>()
|
|
|
|
dereferences symbolic links, like
|
|
<B>stat</B>().)
|
|
|
|
</DL>
|
|
<P>
|
|
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?2+openat">openat</A></B>(2)
|
|
|
|
for an explanation of the need for
|
|
<B>fstatat</B>().
|
|
|
|
<A NAME="lbAG"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success, zero is returned.
|
|
On error, -1 is returned, and
|
|
<I>errno</I>
|
|
|
|
is set appropriately.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="21"><B>EACCES</B>
|
|
|
|
<DD>
|
|
Search permission is denied for one of the directories
|
|
in the path prefix of
|
|
<I>pathname</I>.
|
|
|
|
(See also
|
|
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7).)
|
|
|
|
<DT id="22"><B>EBADF</B>
|
|
|
|
<DD>
|
|
<I>fd</I>
|
|
|
|
is not a valid open file descriptor.
|
|
<DT id="23"><B>EFAULT</B>
|
|
|
|
<DD>
|
|
Bad address.
|
|
<DT id="24"><B>ELOOP</B>
|
|
|
|
<DD>
|
|
Too many symbolic links encountered while traversing the path.
|
|
<DT id="25"><B>ENAMETOOLONG</B>
|
|
|
|
<DD>
|
|
<I>pathname</I>
|
|
|
|
is too long.
|
|
<DT id="26"><B>ENOENT</B>
|
|
|
|
<DD>
|
|
A component of
|
|
<I>pathname</I>
|
|
|
|
does not exist or is a dangling symbolic link.
|
|
<DT id="27"><B>ENOENT</B>
|
|
|
|
<DD>
|
|
<I>pathname</I>
|
|
|
|
is an empty string and
|
|
<B>AT_EMPTY_PATH</B>
|
|
|
|
was not specified in
|
|
<I>flags</I>.
|
|
|
|
<DT id="28"><B>ENOMEM</B>
|
|
|
|
<DD>
|
|
Out of memory (i.e., kernel memory).
|
|
<DT id="29"><B>ENOTDIR</B>
|
|
|
|
<DD>
|
|
A component of the path prefix of
|
|
<I>pathname</I>
|
|
|
|
is not a directory.
|
|
<DT id="30"><B>EOVERFLOW</B>
|
|
|
|
<DD>
|
|
<I>pathname</I>
|
|
|
|
or
|
|
<I>fd</I>
|
|
|
|
refers to a file whose size, inode number,
|
|
or number of blocks cannot be represented in, respectively, the types
|
|
<I>off_t</I>,
|
|
|
|
<I>ino_t</I>,
|
|
|
|
or
|
|
<I>blkcnt_t</I>.
|
|
|
|
This error can occur when, for example,
|
|
an application compiled on a 32-bit platform without
|
|
<I>-D_FILE_OFFSET_BITS=64</I>
|
|
|
|
calls
|
|
<B>stat</B>()
|
|
|
|
on a file whose size exceeds
|
|
<I>(1<<31)-1</I>
|
|
|
|
bytes.
|
|
</DL>
|
|
<P>
|
|
|
|
The following additional errors can occur for
|
|
<B>fstatat</B>():
|
|
|
|
<DL COMPACT>
|
|
<DT id="31"><B>EBADF</B>
|
|
|
|
<DD>
|
|
<I>dirfd</I>
|
|
|
|
is not a valid file descriptor.
|
|
<DT id="32"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
Invalid flag specified in
|
|
<I>flags</I>.
|
|
|
|
<DT id="33"><B>ENOTDIR</B>
|
|
|
|
<DD>
|
|
<I>pathname</I>
|
|
|
|
is relative and
|
|
<I>dirfd</I>
|
|
|
|
is a file descriptor referring to a file other than a directory.
|
|
</DL>
|
|
<A NAME="lbAI"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
<B>fstatat</B>()
|
|
|
|
was added to Linux in kernel 2.6.16;
|
|
library support was added to glibc in version 2.4.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
<B>stat</B>(),
|
|
|
|
<B>fstat</B>(),
|
|
|
|
<B>lstat</B>():
|
|
|
|
SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<P>
|
|
|
|
<B>fstatat</B>():
|
|
|
|
POSIX.1-2008.
|
|
<P>
|
|
|
|
According to POSIX.1-2001,
|
|
<B>lstat</B>()
|
|
|
|
on a symbolic link need return valid information only in the
|
|
<I>st_size</I>
|
|
|
|
field and the file type of the
|
|
<I>st_mode</I>
|
|
|
|
field of the
|
|
<I>stat</I>
|
|
|
|
structure.
|
|
POSIX.1-2008 tightens the specification, requiring
|
|
<B>lstat</B>()
|
|
|
|
to return valid information in all fields except the mode bits in
|
|
<I>st_mode</I>.
|
|
|
|
<P>
|
|
|
|
Use of the
|
|
<I>st_blocks</I>
|
|
|
|
and
|
|
<I>st_blksize</I>
|
|
|
|
fields may be less portable.
|
|
(They were introduced in BSD.
|
|
The interpretation differs between systems,
|
|
and possibly on a single system when NFS mounts are involved.)
|
|
<A NAME="lbAK"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H3>Timestamp fields</H3>
|
|
|
|
Older kernels and older standards did not support nanosecond timestamp
|
|
fields.
|
|
Instead, there were three timestamp
|
|
fields---<I>st_atime</I>,
|
|
|
|
<I>st_mtime</I>,
|
|
|
|
and
|
|
<I>st_ctime</I>---typed
|
|
|
|
as
|
|
<I>time_t</I>
|
|
|
|
that recorded timestamps with one-second precision.
|
|
<P>
|
|
|
|
Since kernel 2.5.48, the
|
|
<I>stat</I>
|
|
|
|
structure supports nanosecond resolution for the three file timestamp fields.
|
|
The nanosecond components of each timestamp are available
|
|
via names of the form
|
|
<I>st_atim.tv_nsec</I>,
|
|
|
|
if suitable feature test macros are defined.
|
|
Nanosecond timestamps were standardized in POSIX.1-2008,
|
|
and, starting with version 2.12,
|
|
glibc exposes the nanosecond component names if
|
|
<B>_POSIX_C_SOURCE</B>
|
|
|
|
is defined with the value 200809L or greater, or
|
|
<B>_XOPEN_SOURCE</B>
|
|
|
|
is defined with the value 700 or greater.
|
|
Up to and including glibc 2.19,
|
|
the definitions of the nanoseconds components are also defined if
|
|
<B>_BSD_SOURCE</B>
|
|
|
|
or
|
|
<B>_SVID_SOURCE</B>
|
|
|
|
is defined.
|
|
If none of the aforementioned macros are defined,
|
|
then the nanosecond values are exposed with names of the form
|
|
<I>st_atimensec</I>.
|
|
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H3>C library/kernel differences</H3>
|
|
|
|
Over time, increases in the size of the
|
|
<I>stat</I>
|
|
|
|
structure have led to three successive versions of
|
|
<B>stat</B>():
|
|
|
|
<I>sys_stat</I>()
|
|
|
|
(slot
|
|
<I>__NR_oldstat</I>),
|
|
|
|
<I>sys_newstat</I>()
|
|
|
|
(slot
|
|
<I>__NR_stat</I>),
|
|
|
|
and
|
|
<I>sys_stat64()</I>
|
|
|
|
(slot
|
|
<I>__NR_stat64</I>)
|
|
|
|
on 32-bit platforms such as i386.
|
|
The first two versions were already present in Linux 1.0
|
|
(albeit with different names);
|
|
|
|
|
|
the last was added in Linux 2.4.
|
|
Similar remarks apply for
|
|
<B>fstat</B>()
|
|
|
|
and
|
|
<B>lstat</B>().
|
|
|
|
<P>
|
|
|
|
The kernel-internal versions of the
|
|
<I>stat</I>
|
|
|
|
structure dealt with by the different versions are, respectively:
|
|
<DL COMPACT>
|
|
<DT id="34"><I>__old_kernel_stat</I>
|
|
|
|
<DD>
|
|
The original structure, with rather narrow fields, and no padding.
|
|
<DT id="35"><I>stat</I>
|
|
|
|
<DD>
|
|
Larger
|
|
<I>st_ino</I>
|
|
|
|
field and padding added to various parts of the structure to
|
|
allow for future expansion.
|
|
<DT id="36"><I>stat64</I>
|
|
|
|
<DD>
|
|
Even larger
|
|
<I>st_ino</I>
|
|
|
|
field,
|
|
larger
|
|
<I>st_uid</I>
|
|
|
|
and
|
|
<I>st_gid</I>
|
|
|
|
fields to accommodate the Linux-2.4 expansion of UIDs and GIDs to 32 bits,
|
|
and various other enlarged fields and further padding in the structure.
|
|
(Various padding bytes were eventually consumed in Linux 2.6,
|
|
with the advent of 32-bit device IDs and nanosecond components
|
|
for the timestamp fields.)
|
|
</DL>
|
|
<P>
|
|
|
|
The glibc
|
|
<B>stat</B>()
|
|
|
|
wrapper function hides these details from applications,
|
|
invoking the most recent version of the system call provided by the kernel,
|
|
and repacking the returned information if required for old binaries.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<P>
|
|
|
|
On modern 64-bit systems, life is simpler: there is a single
|
|
<B>stat</B>()
|
|
|
|
system call and the kernel deals with a
|
|
<I>stat</I>
|
|
|
|
structure that contains fields of a sufficient size.
|
|
<P>
|
|
|
|
The underlying system call employed by the glibc
|
|
<B>fstatat</B>()
|
|
|
|
wrapper function is actually called
|
|
<B>fstatat64</B>()
|
|
|
|
or, on some architectures,
|
|
|
|
<B>newfstatat</B>().
|
|
|
|
<A NAME="lbAN"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The following program calls
|
|
<B>lstat</B>()
|
|
|
|
and displays selected fields in the returned
|
|
<I>stat</I>
|
|
|
|
structure.
|
|
<P>
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/stat.h">sys/stat.h</A>>
|
|
#include <<A HREF="file:///usr/include/time.h">time.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/sysmacros.h">sys/sysmacros.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> struct stat sb;
|
|
<P>
|
|
<BR> if (argc != 2) {
|
|
<BR> fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> if (lstat(argv[1], &sb) == -1) {
|
|
<BR> perror("lstat");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> printf("ID of containing device: [%lx,%lx]\n",
|
|
<TT> </TT> (long) major(sb.st_dev), (long) minor(sb.st_dev));<BR>
|
|
<P>
|
|
<BR> printf("File type: ");
|
|
<P>
|
|
<BR> switch (sb.st_mode & S_IFMT) {
|
|
<BR> case S_IFBLK: printf("block device\n"); break;
|
|
<BR> case S_IFCHR: printf("character device\n"); break;
|
|
<BR> case S_IFDIR: printf("directory\n"); break;
|
|
<BR> case S_IFIFO: printf("FIFO/pipe\n"); break;
|
|
<BR> case S_IFLNK: printf("symlink\n"); break;
|
|
<BR> case S_IFREG: printf("regular file\n"); break;
|
|
<BR> case S_IFSOCK: printf("socket\n"); break;
|
|
<BR> default: printf("unknown?\n"); break;
|
|
<BR> }
|
|
<P>
|
|
<BR> printf("I-node number: %ld\n", (long) sb.st_ino);
|
|
<P>
|
|
<BR> printf("Mode: %lo (octal)\n",
|
|
<BR> (unsigned long) sb.st_mode);
|
|
<P>
|
|
<BR> printf("Link count: %ld\n", (long) sb.st_nlink);
|
|
<BR> printf("Ownership: UID=%ld GID=%ld\n",
|
|
<BR> (long) sb.st_uid, (long) sb.st_gid);
|
|
<P>
|
|
<BR> printf("Preferred I/O block size: %ld bytes\n",
|
|
<BR> (long) sb.st_blksize);
|
|
<BR> printf("File size: %lld bytes\n",
|
|
<BR> (long long) sb.st_size);
|
|
<BR> printf("Blocks allocated: %lld\n",
|
|
<BR> (long long) sb.st_blocks);
|
|
<P>
|
|
<BR> printf("Last status change: %s", ctime(&sb.st_ctime));
|
|
<BR> printf("Last file access: %s", ctime(&sb.st_atime));
|
|
<BR> printf("Last file modification: %s", ctime(&sb.st_mtime));
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAO"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+ls">ls</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+stat">stat</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+access">access</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+chmod">chmod</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+chown">chown</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+readlink">readlink</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+statx">statx</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+utime">utime</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+capabilities">capabilities</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+inode">inode</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+symlink">symlink</A></B>(7)
|
|
|
|
<A NAME="lbAP"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="37"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="38"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="39"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="40"><A HREF="#lbAE">The stat structure</A><DD>
|
|
<DT id="41"><A HREF="#lbAF">fstatat()</A><DD>
|
|
</DL>
|
|
<DT id="42"><A HREF="#lbAG">RETURN VALUE</A><DD>
|
|
<DT id="43"><A HREF="#lbAH">ERRORS</A><DD>
|
|
<DT id="44"><A HREF="#lbAI">VERSIONS</A><DD>
|
|
<DT id="45"><A HREF="#lbAJ">CONFORMING TO</A><DD>
|
|
<DT id="46"><A HREF="#lbAK">NOTES</A><DD>
|
|
<DL>
|
|
<DT id="47"><A HREF="#lbAL">Timestamp fields</A><DD>
|
|
<DT id="48"><A HREF="#lbAM">C library/kernel differences</A><DD>
|
|
</DL>
|
|
<DT id="49"><A HREF="#lbAN">EXAMPLE</A><DD>
|
|
<DT id="50"><A HREF="#lbAO">SEE ALSO</A><DD>
|
|
<DT id="51"><A HREF="#lbAP">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:34 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|