Name
stat - Gets file status.
Syntax
#include <sys/stat.h>
int stat(pathname, buffer)
char *pathname;
struct stat *buffer;
Description
The stat function obtains information about the file or
directory specified by pathname and stores it in the
structure pointed to by buffer. The stat structure, defined
in the file sys/stat.h, includes the following fields:
Field Value
st_mode Bit mask for file-mode information. The
S_IFDIR bit is set if pathname specifies
a directory; the S_IFREG bit is set if
pathname specifies an ordinary file.
User read/write bits are set according
to the file's permission mode; user
execute bits are set according to the
file-name extension.
st_dev Drive number of the disk containing the
file (same as st_rdev). Real mode only.
st_rdev Drive number of the disk containing the
file (same as st_dev). Real mode only.
st_nlink Always 1.
st_size Size of the file in bytes.
st_atime Time of last modification of file (same
as st_mtime and st_ctime).
st_mtime Time of last modification of file (same
as st_atime and st_ctime).
st_ctime Time of last modification of file (same
as st_atime and st_mtime).
Note that if the given pathname refers to a device, the size
and time fields in the stat structure are not meaningful.
Return Value
The stat function returns the value 0 if the file-status
information is obtained. A return value of -1 indicates an
error, and errno is set to ENOENT, indicating that the file
name or path name could not be found.
See Also
access(DOS), fstat(DOS)
Example
#include <time.h> #include <sys/types.h> #include
<sys/stat.h> #include <stdio.h>
struct stat buf; int fh, result; char *buffer = "A line to
output";
main()
{
/* Get data associated with "data": */
result = stat("data",&buf);
/* Check if statistics are valid: */
if (result != 0)
perror("Problem getting information");
else
{
/* Output some of the statistics: */
printf("File size : %ld\n",buf.st_size);
printf("Time modified : %s",
ctime(&buf.st_atime));
}
}
This program uses the stat function to report the size and
last modification time for the file named data.
(printed 6/18/89)