stat(), stat64()

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. The stat64() function is a large-file support version of stat().

Note: In QNX Neutrino 6.6 or later, the large-file support functions and data types appear in the name space only if you define _LARGEFILE64_SOURCE when you compile your code. For more information, see Classification in 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.
EINTR
This function was interrupted by a signal.
EINVAL
(QNX Neutrino 7.0 or later) 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 argument path exceeds PATH_MAX in length, or a pathname component is longer than NAME_MAX. These manifests are defined in the <limits.h> header file.
ENOENT
The named file doesn't exist, or path is an empty string.
ENOSYS
The stat() function isn't implemented for the filesystem 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.

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
Interrupt handler No
Signal handler Yes
Thread Yes