stat -- Get Information about File or Directory

Format

#include <sys\types.h>
#include <sys\stat.h>
int stat(const char *pathname, struct stat *buffer);

Language Level: XPG4, Extension
stat stores information about the file or directory specified by pathname in the structure to which buffer points. The stat structure, defined in <sys\stat.h>, contains the following fields:

Field Value
st_mode Bit mask for file-mode information. The S_IFDIR bit is set if pathname specifies a directory. The S_IFREG bit is set if pathname specifies an ordinary file. User read/write bits are set according to the permission mode of the file. The S_IEXEC bit is set using the file name extension. The other bits are undefined.

st_dev Drive number of the disk containing the file.
st_rdev Drive number of the disk containing the file (same as st_dev).
st_nlink Always 1.
st_size Size of the file in bytes.
st_atime Time of last access of file.
st_mtime Time of last modification of file.
st_ctime Time of file creation.

Note: If the given pathname specifies only a device (for example C:\), time information is not available.

Note: In earlier releases of the C/C++ run-time library, stat began with an underscore (_stat). Because it is defined by the X/Open standard, the underscore has been removed. For compatibility, IBM C and C++ Compilers will map _stat to stat for you.

Return Value
stat returns the value 0 if the file status information is obtained. A return value of -1 indicates an error, and errno is set to ENOENT, indicating that the file name or path name could not be found.

Example
This example requests that the status information for the file test.exe be placed into the structure buf. If the request is successful and the file is executable, the example runs test.exe.

#include <sys\types.h>
#include <sys\stat.h>
#include <process.h>
#include <stdio.h>
int main(void)
{
   struct stat buf;
   if (0 == stat("test.exe", &buf)) {
      if ((buf.st_mode&S_IFREG) && (buf.st_mode&S_IEXEC))
         execl("test.exe", "test", NULL);         /* file is executable */
   }
   else
      printf("File could not be found\n");
   return 0;
   /**********************************************************************
      The source for test.exe is:
      #include <stdio.h>
      int main(void)
      {
          puts("test.exe is an executable file");
      }
      The output should be:
      test.exe is an executable file
   **********************************************************************/
}


fstat -- Information about Open File
<sys\stat.h>
<sys\types.h>