Borland C++ reference

fstat, stat

partly corrected

Gets open file information.

Defined in header <sys/stat.h>

Syntax

#include <sys\stat.h>
int fstat(int handle, struct stat *statbuf);
int stat(char  *path, struct stat *statbuf);

Portability

DOSUNIXWindowsANSI CC++ only
■■■

Remarks

*®*^* stores information in the stat structure about the open file or directory associated with handle.

stat stores information about a given file or directory in the stat structure.

statbuf points to the stat structure (defined in sysXstat.h). That structure contains the following fields:

stjnode Bit mask giving information about the open file's mode

st_dev Drive number of disk containing the file, or file handle if the file is on a device

st_rdevSame as st_dev

stjilink Set to the integer constant 1

st_sizeSize of the open file in bytes

st_atime Most recent time the open file was modified

stjntime Same as st_atime

stjotime Same as st_atime

The stat structure contains three more fields not mentioned here. They contain values that are not meaningful under DOS.

The bit mask that gives information about the mode of the open file includes the following bits:

One of the following bits will be set

S_IFCHR If handle refers to a device. S_IFREG If an ordinary file is referred to by handle.

One or both of the following bits will be set

S_IWRITE If user has permission to write to file. S_IREAD If user has permission to read to file.

The bit mask also includes the read/write bits; these are set according to the file's permission mode.

Return value

fstat and stat return 0 if they successfully retrieved the information about the open file. On error (failure to get the information), these functions return -1 and set the global variable errno to

EBADFBad file handle

See also

Example

#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys\stat.h>

struct Stat statbuf;
void pstat (void)
{
    if  (statbuf.st_mode & S_IWRITE)
        printfC'File  is writable\n");
    if  (statbuf.st_mode & S_IREAD)
        printfC'File  is readable\n");
    if  (statbuf.st_mode & S_IFREG)
        printfC'File  is a regular file\n");
    if  (statbuf.st_mode & S_IFCHR)
        printfC'File  is a character device\n");
    if  (statbuf.st_mode & S_IFDIR)
        printfC'File  is a directory\n") ;
}
void main(int argc, char  **argv) {
    char *infilename;
    int infile;

    if  (argc != 2) {
        printf( "Usage: fstatest filename\n");
        exit(l);
    }
    infilename = argv[l];

    if  ((infile = open(infilename,0_RDONLY)) == -1)
        perror( "Unable to open file for reading");
    else  {
        if  (fstat(infile,&statbuf) != 0) {
            perror ("Unable to fstat");
            exit(l);
        }
        close (infile);
        printf ("Results of fstat:\n");
        pstat 0 ;
    }
    if  (stat(infilename,&statbuf) != 0)
        perror ("Unable to stat");
    else  {
        printf ("Results of stat:\n"
        pstatO;

    exit I

See strcat, strchr, strcspn,  strdup,  stricmp, strlen, strlwr, strncat,
strncmp,   strncpy,  strnicmp,  strnset, strpbrk,  strrchr, strrev, strset,
strspn,  strstr, strtok, and strupr for descriptions  of the far versions  of each
of these functions.

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.