stat64 (2) - Linux Manuals
stat64: get file status
NAME
stat, fstat, lstat, fstatat - get file status
SYNOPSIS
#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int stat(const char *pathname, struct stat *statbuf); int fstat(int fd, struct stat *statbuf); int lstat(const char *pathname, struct stat *statbuf); #include <fcntl.h> /* Definition of AT_* constants */ #include <sys/stat.h> int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
lstat():
-
/* glibc 2.19 and earlier */ _BSD_SOURCE
|| /* Since glibc 2.20 */ _DEFAULT_SOURCE
|| _XOPEN_SOURCE >= 500
|| /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200112L
fstatat():
-
- Since glibc 2.10:
- _POSIX_C_SOURCE >= 200809L
- Before glibc 2.10:
- _ATFILE_SOURCE
DESCRIPTION
These functions return information about a file, in the buffer pointed to by statbuf. No permissions are required on the file itself, but---in the case of stat(), fstatat(), and lstat()---execute (search) permission is required on all of the directories in pathname that lead to the file.stat() and fstatat() retrieve information about the file pointed to by pathname; the differences for fstatat() are described below.
lstat() is identical to stat(), except that if pathname is a symbolic link, then it returns information about the link itself, not the file that the link refers to.
fstat() is identical to stat(), except that the file about which information is to be retrieved is specified by the file descriptor fd.
The stat structure
All of these system calls return a stat structure, which contains the following fields:
struct stat {
#define st_atime st_atim.tv_sec /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};
Note:
the order of fields in the
stat
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.
Note:
for performance and simplicity reasons, different fields in the
stat
structure may contain state information from different moments
during the execution of the system call.
For example, if
st_mode
or
st_uid
is changed by another process by calling
chmod(2)
or
chown(2),
stat()
might return the old
st_mode
together with the new
st_uid,
or the old
st_uid
together with the new
st_mode.
The fields in the
stat
structure are as follows:
For further information on the above fields, see
inode(7).
If the pathname given in
pathname
is relative, then it is interpreted relative to the directory
referred to by the file descriptor
dirfd
(rather than relative to the current working directory of
the calling process, as is done by
stat()
and
lstat()
for a relative pathname).
If
pathname
is relative and
dirfd
is the special value
AT_FDCWD,
then
pathname
is interpreted relative to the current working
directory of the calling process (like
stat()
and
lstat()).
If
pathname
is absolute, then
dirfd
is ignored.
flags
can either be 0, or include one or more of the following flags ORed:
See
openat(2)
for an explanation of the need for
fstatat().
The following additional errors can occur for
fstatat():
fstatat():
POSIX.1-2008.
According to POSIX.1-2001,
lstat()
on a symbolic link need return valid information only in the
st_size
field and the file type of the
st_mode
field of the
stat
structure.
POSIX.1-2008 tightens the specification, requiring
lstat()
to return valid information in all fields except the mode bits in
st_mode.
Use of the
st_blocks
and
st_blksize
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.)
Since kernel 2.5.48, the
stat
structure supports nanosecond resolution for the three file timestamp fields.
The nanosecond components of each timestamp are available
via names of the form
st_atim.tv_nsec,
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
_POSIX_C_SOURCE
is defined with the value 200809L or greater, or
_XOPEN_SOURCE
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
_BSD_SOURCE
or
_SVID_SOURCE
is defined.
If none of the aforementioned macros are defined,
then the nanosecond values are exposed with names of the form
st_atimensec.
The kernel-internal versions of the
stat
structure dealt with by the different versions are, respectively:
The glibc
stat()
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.
On modern 64-bit systems, life is simpler: there is a single
stat()
system call and the kernel deals with a
stat
structure that contains fields of a sufficient size.
The underlying system call employed by the glibc
fstatat()
wrapper function is actually called
fstatat64()
or, on some architectures,
newfstatat().
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysmacros.h>
int
main(int argc, char *argv[])
{
fstatat()
The
fstatat()
system call is a more general interface for accessing file information
which can still provide exactly the behavior of each of
stat(),
lstat(),
and
fstat().
RETURN VALUE
On success, zero is returned.
On error, -1 is returned, and
errno
is set appropriately.
ERRORS
VERSIONS
fstatat()
was added to Linux in kernel 2.6.16;
library support was added to glibc in version 2.4.
CONFORMING TO
stat(),
fstat(),
lstat():
SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008.
NOTES
Timestamp fields
Older kernels and older standards did not support nanosecond timestamp
fields.
Instead, there were three timestamp
fields---st_atime,
st_mtime,
and
st_ctime---typed
as
time_t
that recorded timestamps with one-second precision.
C library/kernel differences
Over time, increases in the size of the
stat
structure have led to three successive versions of
stat():
sys_stat()
(slot
__NR_oldstat),
sys_newstat()
(slot
__NR_stat),
and
sys_stat64()
(slot
__NR_stat64)
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
fstat()
and
lstat().
EXAMPLES
The following program calls
lstat()
and displays selected fields in the returned
stat
structure.