Get file information, given a file description
#include <sys/types.h> #include <sys/stat.h> int fstat( int filedes, struct stat* buf ); int fstat64( int filedes, struct stat64* buf );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
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 arguments to the macros are:
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>. For more information, see “Access permissions” in the documentation for stat().
The st_mode field also encodes the following bits:
#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 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()