digitalmars.com                      
Last update Sun Mar 4 12:00:59 2018

sys\stat.h


_fstat

Header
sys\stat.h
Prototype
int _fstat(int fd, struct stat *buf);
Description
The _fstat function gets information about an open file fd and stores it in the structure pointed to by buf. Some of the fields in the _stat structure are:

Field Description
st_dev Drive number of disk containing file fd or value of fd if fd is a device.
st_mode Bit map containing mode information on the open file. The bits in this mask are described in the following table.
st_nlink Number of links (always 1).
st_rdev Same as st_dev.
st_size Size of open file in bytes.
st_mtime Time last modified.
st_atime Same as st_mtime
st_ctime Same as st_mtime

st_mode will be a combination of these values:

Value Is set if fd refers to...
_S_IFCHR a character device.
_S_IFREG a regular file, not a device.
_S_IREAD a readable file or device.
_S_IWRITE a wriatable file or device.
Synonym
Function: fstat
Structure: stat
Modes: S_IFCHR, S_IFREG, S_IREAD, S_IWRITE
Return Value
A value of 0 if file information is successfully obtained. If the return value is -1, errno is set to EBADF indicating a bad file handle.
Compatibility
DOS Windows 3.x Phar Lap DOSX Win32
See Also
_stat
findfirst
findnext
_isatty
Example
/* Example of _fstat */

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

void main()
{
 char filen[_MAX_PATH];
 char *date;
 int res;
 struct _stat fstat;
 int fh;

 printf("Enter a filename: ");
 gets(filen);

 fh = _open(filen, _O_RDONLY);
 if (fh < 0)
 {
    perror("Error opening file");
    exit(EXIT_FAILURE);
 }

 res = _fstat(fh, &fstat);
 if (res != 0)
 {
    perror("Failure calling _fstat");
    exit(EXIT_FAILURE);
 }

 date = asctime(localtime(&fstat.st_ctime));
 printf("\nDate: %s", date);
 printf("Mode: %d\n", fstat.st_mode);
 printf("Size: %ld\n", fstat.st_size);
}
Output
Enter a filename: _fstat.c

Date: Tue Jun 28 14: 11: 56 1994
Mode: -32330
Size: 760

_stat

Header
sys/stat.h
sys/types.h
Prototype
int _stat(char *path, struct _stat *buf);
Description
The _stat function gets information about a file or directory specified by path and store the information in the structure that buf points to. The structure contains the following fields:

Field Description
st_dev The drive number of the drive specified in path, or the default drive, if none is given.
st_mode Bit map containing mode information on the open file, made up of the values below.
st_nlink Always 1.
st_rdev Same as st_dev.
st_size Size of file in bytes.
st_mtime Time last modified.
st_atime For NT, time last accessed; or same as st_mtime.
st_ctime For NT, time created; otherwise same as st_mtime.
st_ino Always 0. (stat structure only)
st_uid Always 0. (stat structure only)
st_gid Always 0. (stat structure only)
Use the following values for st_mode:

_S_IFREG Set if path refers to an ordinary file, not a directory.
_S_IREAD Set if path refers to a readable file or directory.
_S_IWRITE Set if path refers to a writable file or directory.
_S_IFDIR Set if path refers to a directory.
_S_IEXEC Set if path refers to an executable file or a directory.
Synonym
Function: stat
Values: S_IFREG, S_IREAD, S_IWRITE, S_IFDIR, S_IEXEC
Return Value
stat returns 0 if the status information is retrieved. On error the function returns -1 and errno is set.
Compatibility
DOS Windows 3.x Phar Lap DOSX Win32
See Also
_fstat
Example
/* Example of _stat */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys\stat.h>

void main()
{
 char filen[_MAX_PATH];
 char *date;
 int res;
 struct _stat fstat;

 printf("Enter a filename: ");
 gets(filen);
 res = _stat(filen, &fstat);
 if (res != 0)
 {
   perror("Failure calling _stat");
   exit(EXIT_FAILURE);
 }
 date = asctime(localtime(&fstat.st_ctime));
 printf("\nDate: %s", date);
 printf("Mode: %d\n", fstat. st_mode);
 printf("Size: %ld\n", fstat. st_size);
}
Output
Enter a filename: _stat.c
Date: Thu Jun 23 09:42:04 1994
Mode: -32330
Size: 585
Home | Compiler & Tools | IDDE Reference | STL | Search | Download | Forums