System Calls stat(2)
NAME
stat, lstat, fstat, fstatat - get file status
SYNOPSIS
#include
#include
#include
int stat(const char *restrict path, struct stat *restrict buf);
int lstat(const char *restrict path, struct stat *restrict buf);
int fstat(int fildes, struct stat *buf);
int fstatat(int fildes, const char *path, struct stat *buf,
int flag);
DESCRIPTION
The stat() function obtains information about the file
pointed to by path. Read, write, or execute permission of
the named file is not required, but all directories listed
in the path name leading to the file must be searchable.
The lstat() function obtains file attributes similar to
stat(), except when the named file is a symbolic link; in
that case lstat() returns information about the link, while
stat() returns information about the file the link refer-
ences.
The fstat() function obtains information about an open file
known by the file descriptor fildes, obtained from a suc-
cessful open(2), creat(2), dup(2), fcntl(2), or pipe(2)
function. If fildes references a shared memory object, the
system updates in the stat structure pointed to by the buf
argument only the stuid, stgid, stsize, and stmode
fields, and only the SIRUSR, SIWUSR, SIRGRP, SIWGRP,
SIROTH, and SIWOTH file permission bits need be valid. The
system can update other fields and flags. The fstat() func-
tion updates any pending time-related fields before writing
to the stat structure.
The fstatat() function obtains file attributes similar to
the stat(), lstat(), and fstat() functions. If the path
argument is a relative path, it is resolved relative to the
fildes argument rather than the current working directory.
SunOS 5.11 Last change: 10 Oct 2007 1
System Calls stat(2)
If path is absolute, the fildes argument is unused. If the
fildes argument has the special value ATFDCWD, relative
paths are resolved from the current working directory. If
ATSYMLINKNOFOLOW is set in the flag argument, the func-
tion behaves like lstat() and does not automatically follow
symbolic links. See fsattr(5). If ATRTRIGER is set in
the flag argument and the vnode is a trigger mount point,
the mount is performed and the function returns the attri-
butes of the root of the mounted filesystem.
The buf argument is a pointer to a stat structure into which
information is placed concerning the file. A stat structure
includes the following members:
modet stmode; /* File mode (see mknod(2)) */
inot stino; /* Inode number */
devt stdev; /* ID of device containing */
/* a directory entry for this file */
devt strdev; /* ID of device */
/* This entry is defined only for */
/* char special or block special files */
nlinkt stnlink; /* Number of links */
uidt stuid; /* User ID of the file's owner */
gidt stgid; /* Group ID of the file's group */
offt stsize; /* File size in bytes */
timet statime; /* Time of last access */
timet stmtime; /* Time of last data modification */
timet stctime; /* Time of last file status change */
/* Times measured in seconds since */
/* 00:00:00 UTC, Jan. 1, 1970 */
long stblksize; /* Preferred I/O block size */
blkcntt stblocks; /* Number of 512 byte blocks allocated*/
char stfstype[STFSTYPSZ];
/* Null-terminated type of filesystem */
Descriptions of structure members are as follows:
stmode The mode of the file as described for the
mknod() function. In addition to the modes
described on the mknod(2) manual page, the
mode of a file can also be SIFSOCK if the
file is a socket, SIFDOR if the file is a
door, SIFPORT if the file is an event port,
or SIFLNK if the file is a symbolic link.
SIFLNK can be returned either by lstat() or
by fstat() when the ATSYMLINKNOFOLOW flag
is set.
SunOS 5.11 Last change: 10 Oct 2007 2
System Calls stat(2)
stino This field uniquely identifies the file in a
given file system. The pair stino and
stdev uniquely identifies regular files.
stdev This field uniquely identifies the file system
that contains the file. Its value may be used
as input to the ustat() function to determine
more information about this file system. No
other meaning is associated with this value.
strdev This field should be used only by administra-
tive commands. It is valid only for block spe-
cial or character special files and only has
meaning on the system where the file was con-
figured.
stnlink This field should be used only by administra-
tive commands.
stuid The user ID of the file's owner.
stgid The group ID of the file's group.
stsize For regular files, this is the address of the
end of the file. For block special or charac-
ter special, this is not defined. See also
pipe(2).
statime Time when file data was last accessed. Some of
the functions that change this member are:
creat(), mknod(), pipe(), utime(2), and
read(2).
stmtime Time when data was last modified. Some of the
functions that change this member are:
creat(), mknod(), pipe(), utime(), and
write(2).
stctime Time when file status was last changed. Some
of the functions that change this member are:
chmod(2), chown(2), creat(2), link(2),
mknod(2), pipe(2), rename(2), unlink(2),
utime(2), and write(2).
SunOS 5.11 Last change: 10 Oct 2007 3
System Calls stat(2)
stblksize A hint as to the "best" unit size for I/O
operations. This field is not defined for
block special or character special files.
stblocks The total number of physical blocks of size
512 bytes actually allocated on disk. This
field is not defined for block special or
character special files.
stfstype A null-teminated string that uniquely identi-
fies the type of the filesystem that contains
the file.
RETURN VALUES
Upon successful completion, 0 is returned. Otherwise, -1 is
returned and errno is set to indicate the error.
ERORS
The stat(), fstat(), lstat(), and fstatat() functions will
fail if:
EIO An error occurred while reading from the file
system.
EOVERFLOW The file size in bytes or the number of blocks
allocated to the file or the file serial number
cannot be represented correctly in the struc-
ture pointed to by buf.
The stat(), lstat(), and fstatat() functions will fail if:
EACES Search permission is denied for a component
of the path prefix.
EFAULT The buf or path argument points to an ille-
gal address.
EINTR A signal was caught during the execution of
the stat() or lstat() function.
ELOP A loop exists in symbolic links encountered
during the resolution of the path argument.
SunOS 5.11 Last change: 10 Oct 2007 4
System Calls stat(2)
ENAMETOLONG The length of the path argument exceeds
{PATHMAX}, or the length of a path com-
ponent exceeds {NAMEMAX} while
POSIXNOTRUNC is in effect.
ENOENT A component of path does not name an exist-
ing file or path is an empty string.
ENOLINK The path argument points to a remote machine
and the link to that machine is no longer
active.
ENOTDIR A component of the path prefix is not a
directory, or the fildes argument does not
refer to a valid directory when given a
non-null relative path.
The fstat() and fstatat() functions will fail if:
EBADF The fildes argument is not a valid open file
descriptor. The fildes argument to fstatat() can
also have the valid value of ATFDCWD.
EFAULT The buf argument points to an illegal address.
EINTR A signal was caught during the execution of the
fstat() function.
ENOLINK The fildes argument points to a remote machine
and the link to that machine is no longer active.
The stat(), fstat(), and lstat() functions may fail if:
EOVERFLOW One of the members is too large to store in the
stat structure pointed to by buf.
The stat() and lstat() functions may fail if:
ELOP More than {SYMLOPMAX} symbolic links were
encountered during the resolution of the
SunOS 5.11 Last change: 10 Oct 2007 5
System Calls stat(2)
path argument.
ENAMETOLONG As a result of encountering a symbolic link
in resolution of thepath argument, the
length of the substituted pathname strings
exceeds {PATHMAX}.
The stat() and fstatat() functions may fail if:
ENXIO The path argument names a character or block device
special file and the corresponding I/O device has
been retired by the fault management framework.
EXAMPLES
Example 1 Use stat() to obtain file status information.
The following example shows how to obtain file status infor-
mation for a file named /home/cnd/mod1. The structure vari-
able buffer is defined for the stat structure.
#include
#include
#include
struct stat buffer;
int status;
...
status = stat("/home/cnd/mod1", &buffer);
Example 2 Use stat() to get directory information.
The following example fragment gets status information for
each entry in a directory. The call to the stat() function
stores file information in the stat structure pointed to by
statbuf. The lines that follow the stat() call format the
fields in the stat structure for presentation to the user of
the program.
#include
#include
#include
#include
#include
#include
SunOS 5.11 Last change: 10 Oct 2007 6
System Calls stat(2)
#include
#include
#include
#include
struct dirent *dp;
struct stat statbuf;
struct passwd *pwd;
struct group *grp;
struct tm *tm;
char datestring[256];
...
/* Loop through directory entries */
while ((dp = readdir(dir)) != NUL) {
/* Get entry's information. */
if (stat(dp->dname, &statbuf) == -1)
continue;
/* Print out type, permissions, and number of links. */
printf("%10.10s", sperm (statbuf.stmode));
printf("%4d", statbuf.stnlink);
/* Print out owners name if it is found using getpwuid(). */
if ((pwd = getpwuid(statbuf.stuid)) != NUL)
printf(" %-8.8s", pwd->pwname);
else
printf(" %-8d", statbuf.stuid);
/* Print out group name if it's found using getgrgid(). */
if ((grp = getgrgid(statbuf.stgid)) != NUL)
printf(" %-8.8s", grp->grname);
else
printf(" %-8d", statbuf.stgid);
/* Print size of file. */
printf(" %9jd", (intmaxt)statbuf.stsize);
tm = localtime(&statbuf.stmtime);
/* Get localized date string. */
strftime(datestring, sizeof(datestring), nllanginfo(DTFMT), tm);
printf(" %s %s\n", datestring, dp->dname);
}
Example 3 Use fstat() to obtain file status information.
The following example shows how to obtain file status infor-
mation for a file named /home/cnd/mod1. The structure vari-
able buffer is defined for the stat structure. The
/home/cnd/mod1 file is opened with read/write privileges and
is passed to the open file descriptor fildes.
SunOS 5.11 Last change: 10 Oct 2007 7
System Calls stat(2)
#include
#include
#include
struct stat buffer;
int status;
...
fildes = open("/home/cnd/mod1", ORDWR);
status = fstat(fildes, &buffer);
Example 4 Use lstat() to obtain symbolic link status infor-
mation.
The following example shows how to obtain status information
for a symbolic link named /modules/pass1. The structure
variable buffer is defined for the stat structure. If the
path argument specified the filename for the file pointed to
by the symbolic link (/home/cnd/mod1), the results of cal-
ling the function would be the same as those returned by a
call to the stat() function.
#include
struct stat buffer;
int status;
...
status = lstat("/modules/pass1", &buffer);
USAGE
If chmod() or fchmod() is used to change the file group
owner permissions on a file with non-trivial ACL entries,
only the ACL mask is set to the new permissions and the
group owner permission bits in the file's mode field
(defined in mknod(2)) are unchanged. A non-trivial ACL
entry is one whose meaning cannot be represented in the
file's mode field alone. The new ACL mask permissions might
change the effective permissions for additional users and
groups that have ACL entries on the file.
The stat(), fstat(), and lstat() functions have transitional
interfaces for 64-bit file offsets. See lf64(5).
ATRIBUTES
See attributes(5) for descriptions of the following attri-
butes:
SunOS 5.11 Last change: 10 Oct 2007 8
System Calls stat(2)
ATRIBUTE TYPE ATRIBUTE VALUE
Interface Stability Committed
MT-Level Async-Signal-Safe
Standard See below.
For stat(), fstat(), and lstat(), see standards(5).
SEE ALSO
access(2), chmod(2), chown(2), creat(2), link(2), mknod(2),
pipe(2), read(2), time(2), unlink(2), utime(2), write(2),
fattach(3C), stat.h(3HEAD), attributes(5), fsattr(5),
lf64(5), standards(5)
SunOS 5.11 Last change: 10 Oct 2007 9
|