gtpc2m3dC/C++ Language Support User's Guide

fstat-Get Status Information about a File

This function gets status information about a file.

Format

#include <sys/stat.h>
int fstat(int fildes,struct stat *info);

fildes
An open file descriptor.

info
The address of an object of type struct stat in which file status will be returned.

This function gets status information about the file specified by the open file descriptor, fildes, and stores it in the area of memory indicated by the info argument. The status information is returned in a stat structure as defined in the sys/stat.h header file.

Normal Return

If successful, the fstat function returns a 0 value.

Error Return

If unsuccessful, the fstat function returns -1 and sets errno to one of the following:

EBADF
fildes is not a valid open file descriptor.

EINVAL
info is a null pointer.

Programming Considerations

See Table 15 for more information about the stat data structure, which is defined in the sys/stat.h header file.

Examples

The following example gets status information for the file called temp.file.

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
 
main() {
  char fn[]="temp.file";
  struct stat info;
  int fd;
 
  if ((fd = creat(fn, S_IWUSR)) < 0)
    perror("creat() error");
  else {
    if (fstat(fd, &info) != 0)
      perror("fstat() error");
    else {
      puts("fstat() returned:");
      printf("   inode:   %d\n",   (int) info.st_ino);
      printf("  dev id:   %.8x\n'        info.st_dev);
      printf("    mode:   %.8x\n",       info.st_mode);
      printf("   links:   %d\n",         info.st_nlink);
      printf("     uid:   %d\n",   (int) info.st_uid);
      printf("     gid:   %d\n",   (int) info.st_gid);
      printf("modified:   %s",           ctime(&info.st_mtime));
    }
    close(fd);
    unlink(fn);
  }
}

Output

fstat returned:
   inode:   3057
  dev id:   C2E2E240
    mode:   02000080
   links:   1
     uid:   256
     gid:   257
modified:   Fri Jan 16 16:03:16 1998

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.