Get file information
#include <sys/types.h>
#include <sys/stat.h>
int fstat( int filedes,
struct stat* buf );
int fstat64( int filedes,
struct stat64* buf );
libc
The fstat() and fstat64() functions get information from the file specified by filedes and stores it in the structure pointed to by buf.
The file <sys/stat.h> contains definitions for struct stat, as well as following macros:
The m supplied to the macros is the value of st_mode in a stat structure. The value buf is a pointer to a stat structure. The macros evaluate to nonzero if the test is true, and zero if the test is false.
Access permissions are specified as a combination of bits in the st_mode field of the stat structure. These bits are defined in <sys/stat.h>.
The st_mode field also encodes the following bits:
Zero for success, or nonzero if an error occurs (errno is set).
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main( void )
{
int filedes;
int rc;
struct stat buf;
filedes = open( "file", O_RDONLY );
if( filedes != -1 ) {
rc = fstat( filedes , &buf );
if( rc != -1 ) {
printf( "File size = %d\n", buf.st_size );
}
close( filedes );
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
fstat() is POSIX 1003.1; fstat64() is for large-file support
| Safety: | |
|---|---|
| Cancellation point | No |
| Interrupt handler | No |
| Signal handler | Yes |
| Thread | Yes |
creat(), dup(), dup2(), errno, fcntl(), lstat(), open(), pipe(), sopen(), stat()