Name
fstat - Saves file-status information.
Syntax
#include <sys/types.h>
#include <sys/stat.h>
int fstat(handle, buffer)
int handle;
struct stat *buffer;
Description
The fstat function obtains information about the open file
associated with handle and stores it in the structure that
buffer points to. The structure, whose type stat is defined
in sys/stat.h, contains the following fields:
Field Value
st_atime Time of last modification of file
(same as st_mtime and st_ctime).
st_ctime Time of last modification of file
(same as st_atime and st_mtime).
st_dev Either the drive number of the disk
containing the file, or handle in the
case of a device (same as st_rdev).
st_mode Bit mask for file-mode information.
S_IFCHR bit set if handle refers to a
device. S_IFREG bit set if handle
refers to an ordinary file. User
read/write bits set according to the
file's permission mode.
st_mtime Time of last modification of file
(same as st_atime and st_ctime).
st_nlink Always 1.
st_rdev Either the drive number of the disk
containing the file, or handle in the
case of a device (same as st_dev).
st_size Size of the file in bytes.
Return Value
The fstat function returns the value 0 if the file-status
information is obtained. A return value of -1 indicates an
error; in this case, errno is set to EBADF, indicating an
invalid file handle.
See Also
access(DOS), chmod(DOS), filelength(DOS), stat(DOS)
Notes
If handle refers to a device, the size and time fields in
the stat structure are not meaningful.
Example
#include <fcntl.h> #include <time.h> #include <sys/types.h>
#include <sys/stat.h> #include <stdio.h> #include <io.h>
struct stat buf; int fh, result; char *buffer = "A line to
output"; main()
{
fh = open("data",
O_CREAT | O_WRONLY | O_TRUNC);
write(fh,buffer,strlen(buffer));
/* Get data associated with "fh": */
result = fstat(fh,&buf);
/* Check if statistics are valid: */
if (result != 0)
printf("Bad file handle\n" );
else
/* Output some of */
/* the statistics: */
{
printf("File size : %ld\n",buf.st_size);
printf("Drive number : %d\n",buf.st_dev);
printf("Time modified : %s",
ctime(&buf.st_atime));
}
}
This program uses fstat to report the size of a file named
data.
(printed 6/18/89)