stat(), stat64()
QNX SDP8.0C Library ReferenceAPIDeveloper
Get information about a file or directory, given a path
Synopsis:
#include <sys/stat.h>
int stat( const char * path,
struct stat * buf );
int stat64( const char * path,
struct stat64 * buf );
Arguments:
- path
- The path of the file or directory that you want information about.
- buf
- A pointer to a struct stat or struct stat64 buffer where the function can store the information.
Library:
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
Description:
The stat() and stat64() functions obtain information about the file or directory referenced in path. This information is placed in the structure located at the address indicated by buf.
Note:
The stat64() function is a large-file support version of
stat() provided for backwards compatibility.
If you're using large-file support functions and data types, you should define
_LARGEFILE64_SOURCE with a value of 1 to ensure they appear in the name space.
For more information, see
Classificationin
What's in a Function Description?.
Returns:
- 0
- Success.
- -1
- An error occurred (errno is set).
Errors:
- EACCES
- Search permission is denied for a component of path.
- EAGAIN
- The function is temporarily unavailable. Try again.
- EINTR
- This function was interrupted by a signal.
- EINVAL
- The function was unable to convert the format of the stat structure received from the resource manager to _STAT_FORM_PREFERRED; see stat_convert_form().
- EIO
- A physical error occurred on the block device.
- ELOOP
- Too many levels of symbolic links or prefixes.
- ENAMETOOLONG
- The length of the path string exceeds PATH_MAX.
- ENOENT
- The named file doesn't exist, or path is an empty string.
- ENOMEM
- Not enough memory is available.
- ENOSYS
- The stat() function isn't implemented for the filesystem underlying the path specified in path.
- ENOTDIR
- A component of path isn't a directory.
- EOVERFLOW
- The file size in bytes or the number of blocks allocated to the file or the file serial number can't be represented correctly in the structure pointed to by buf.
- ESRCH (procfs only)
- There is no running process that corresponds to /proc/pid.
Examples:
Determine the size of a file:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int main( void )
{
struct stat buf;
if( stat( "file", &buf ) != -1 ) {
printf( "File size = %d\n", buf.st_size );
}
return EXIT_SUCCESS;
}
Determine the amount of free memory:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int main () {
struct stat buf;
if ( stat( "/proc", &buf ) == -1) {
perror ("stat" );
return EXIT_FAILURE;
} else {
printf ("Free memory: %d bytes\n", buf.st_size);
return EXIT_SUCCESS;
}
}
Classification:
stat() is POSIX 1003.1; stat64() is Large-file support
Safety: | |
---|---|
Cancellation point | No |
Signal handler | Yes |
Thread | Yes |
Page updated: