=========
FCNTL.H
=========
Defines "open flags" for open and similar
library functions.
_fmode
O_APPEND
O_BINARY
O_CHANGED
O_CREAT
O_DENYALL
O_DENYNONE
O_DENYREAD
O_DENYWRITE
O_DEVICE
O_EXCL
O_NOINHERIT
O_RDONLY
O_RDWR
O_TEXT
O_TRUNC
O_WRONLY
See Also
========
List of all Header files
==========
LIMITS.H
==========
Constants, data types, and global variables
===========================================
CHAR_BIT
CHAR_MAX
CHAR_MIN
INT_MAX
INT_MIN
LONG_MAX
LONG_MIN
SCHAR_MAX
SCHAR_MIN
SHRT_MAX
SHRT_MIN
UCHAR_MAX
UINT_MAX
ULONG_MAX
USHRT_MAX
See Also
========
List of all Header files
=========
SHARE.H
=========
Constants, data types, and global variables
===========================================
SH_COMPAT
SH_DENYNO
SH_DENYNONE
SH_DENYRD
SH_DENYRW
SH_DENYWR
See Also
========
List of all Header files
============
SYS\STAT.H
============
Functions
=========
fstat
stat
Constants, data types, and global variables
===========================================
file status bits
stat structure
See Also
========
List of all Header files
===============
SYS\LOCKING.H
===============
Constants
=========
LK_LOCK
LK_NBLCK
LK_NBRLCK
LK_RLCK
LK_UNLCK
O_xxxx <FCNTL.H>
========
#defines that are bit definitions for
file-access argument.
These RTL file-open functions use some (not
all) of these definitions:
_dos_open
fdopen
fopen
freopen
_fsopen
_open
open
sopen
_dos_open and sopen also use file-sharing
symbolic constants in the file-access
argument.
Symbolic |
Constant | Description
--------------+-----------------------------------------------------------
Read/Write flag (Used by _dos_open, _open, open, and sopen)
NOTE: For DOS 2.x, _dos_open and_open are limited to these values.
--------------+-----------------------------------------------------------
O_RDONLY | Open for reading only
O_WRONLY | Open for writing only
O_RDWR | Open for reading and writing
--------------------------------------------------------------------------
Other access flags (Used by open and sopen)
--------------+-----------------------------------------------------------
O_NDELAY | Not used; for UNIX compatibility.
O_APPEND | Append to end of file
| If set, the file pointer is set to the end of the file
| prior to each write.
O_CREAT | Create and open file
| If the file already exists, has no effect.
| If the file does not exist, the file is created.
O_EXCL | Exclusive open: Used only with O_CREAT.
| If the file already exists, an error is returned.
O_TRUNC | Open with truncation
| If the file already exists, its length is truncated to 0.
| The file attributes remain unchanged.
--------------------------------------------------------------------------
Binary-mode/Text-mode flags (Used by fdopen, fopen, freopen, _fsopen,
open and sopen)
--------------+-----------------------------------------------------------
O_BINARY | No translation
| Explicitly opens the file in binary mode
O_TEXT | CR-LF translation
| Explicitly opens the file in text mode
---------------------------------------------------------------------------
Additional values available under DOS 3.x (used by _open)
NOTE: Only one of the O_DENYxxx options can be included in
a single open. These file-sharing attributes are in addition
to any locking performed on the files.
--------------+------------------------------------------------------------
O_NOINHERIT | Child processes inherit file
O_DENYALL | Error if opened for read/write
O_DENYWRITE | Error if opened for write
O_DENYREAD | Error if opened for read
O_DENYNONE | Allow concurrent access
--------------------------------------------------------------------------
DO NOT MODIFY these special read-only bits described in DOS documentation
--------------+-----------------------------------------------------------
O_CHANGED | Special DOS read-only bit
O_DEVICE | Special DOS read-only bit
_fmode <FCNTL.H>, <STDLIB.H>
========
Global variable: Default file-translation mode
Declaration: extern int _fmode;
Remarks:
Defaults to O_TEXT on startup. You can
override the default on open file calls by
explicitly passing either O_TEXT or O_BINARY
to the routine.
See Also:
fopen
List of all global variables
CHAR_xxx <LIMITS.H>
==========
Name | Meaning
----------+-----------------------------
CHAR_BIT | Type char, number of bits
CHAR_MAX | Type char, minimum value
CHAR_MIN | Type char, maximum value
These values are independent of whether type
char is defined as signed or unsigned by
default.
SCHAR_xxx <LIMITS.H>
===========
Name | Meaning
-----------+---------------------------
SCHAR_MAX | Type char, maximum value
SCHAR_MIN | Type char, minimum value
Uxxxx_MAX <LIMITS.H>
===========
Name | Maximum value for type ...
-----------+----------------------------
UCHAR_MAX | unsigned char
USHRT_MAX | unsigned short
UINT_MAX | unsigned integer
ULONG_MAX | unsigned long
SHRT_xxx <LIMITS.H>
=====================
Name | Meaning
----------+---------------------------
SHRT_MAX | Type short, maximum value
SHRT_MIN | Type short, minimum value
INT_xxx <LIMITS.H>
=========
Maximum and minimum value for type int.
Name | Meaning
----------+---------------------------
INT_MAX | Type int, maximum value
INT_MIN | Type int, minimum value
LONG_xxx <LIMITS.H>
==========
Maximum and minimum value for type long.
Name | Meaning
----------+---------------------------
LONG_MAX | Type long, maximum value
LONG_MIN | Type long, minimum value
SH_xxxx <SHARE.H>
=========
File-sharing mode for use with _dos_open and
sopen (under DOS 3.0 or later).
Constant | Meaning
-------------+-------------------------------------------------------------
SH_COMPAT | Sets compatibility mode:
| Allows other opens with SH_COMPAT. The call will fail if
| the file has already been opened in any other shared mode.
SH_DENYNONE | Permits read/write access
| Allows other shared opens to the file, but not other
| SH_COMPAT opens
SH_DENYNO | Permits read/write access (provided for compatibility)
SH_DENYRD | Denies read access
| Allows only writes from any other open to the file
SH_DENYRW | Denies read/write access
| Only the current handle may have access to the file
SH_DENYWR | Denies write access
| Allows only reads from any other open to the file
-------------+-------------------------------------------------------------
O_NOINHERIT | The file is not passed to child programs
These file-sharing attributes are in addition
to any locking performed on the files.
S_Ixxxx <SYS\STAT.H>
=========
Definitions used for file status and
directory functions.
Name | Meaning
----------+----------------------------------
S_IFMT | File type mask
S_IFDIR | Directory
S_IFIFO | FIFO special
S_IFCHR | Character special
S_IFBLK | Block special
S_IFREG | Regular file
S_IREAD | Owner can read
S_IWRITE | Owner can write
S_IEXEC | Owner can execute
stat <SYS\STAT.H>
======
A structure containing information about a
file or directory. Used by the fstat and stat
functions.
struct stat {
short st_dev, st_ino;
short st_mode, st_nlink;
int st_uid, st_gid;
short st_rdev;
long st_size, st_atime;
long st_mtime, st_ctime;
};
Element | What It Is
----------+----------------------------------
st_dev | Drive number of disk containing
| the file, or file handle if the
| file is on a device
st_mode | Bit mask giving information
| about the open file's mode
st_nlink | Set to the integer constant 1
st_rdev | Same as st_dev
st_size | Size of the open file in bytes
st_atime | Most recent time the open file
| was modified
st_mtime | Same as st_atime
st_ctime | Same as st_atime
----------+----------------------------------
st_ino | These elements contain values
st_uid | that are not meaningful under
st_gid | DOS.
=============
fstat, stat <SYS\STAT.H>
=============
* fstat gets information about an open file
* stat gets information about a file
Declaration:
* int fstat(int handle, struct stat *statbuf);
* int stat(char *path, struct stat *statbuf);
Remarks:
fstat and stat store information about an
open file or directory in a stat structure.
Argument |Function| What Argument Is/Does
----------+--------+-----------------------------------------------------
handle | fstat | Handle associated with open file or directory
statbuf | both | Points to a stat structure (defined in sys\STAT.H)
path | stat | Path name of open file or directory
The bit mask that gives information about the
mode of the open file includes these bits:
Bit |Function| Bit Is Set If...
----------+--------+-------------------------------------------
User-Execute Bits: One of these bits will be set:
----------+--------+-------------------------------------------
S_IFCHR | fstat | handle refers to a device
S_IFDIR | stat | path specifies a directory.
S_IFREG | both | handle or path refers to an ordinary file
---------------------------------------------------------------
Read/Write Bits: One or both of these bits will be set:
----------+--------+-------------------------------------------
S_IWRITE | both | User has permission to write to file
S_IREAD | both | User has permission to read from file
Return Value:
* On success, both functions return 0 (information about the open
file or directory successfully retrieved)
* On error (failure to get the information),
* fstat returns -1 and sets errno to EBADF (bad file handle)
* stat returns -1 and sets errno to ENOENT (file or path not found)
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | Yes | Yes | | |
+-----+------+---------+--------+----------+
See Also:
access
chmod
Examples:
fstat example
stat example
fstat example
===============
#include <sys\stat.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
struct stat statbuf;
FILE *stream;
/* open a file for update */
if ((stream = fopen("DUMMY.FIL", "w+"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return(1);
}
fprintf(stream, "This is a test");
fflush(stream);
/* get information about the file */
fstat(fileno(stream), &statbuf);
fclose(stream);
/* display the information returned */
if (statbuf.st_mode & S_IFCHR)
printf("Handle refers to a device.\n");
if (statbuf.st_mode & S_IFREG)
printf("Handle refers to an ordinary file.\n");
if (statbuf.st_mode & S_IREAD)
printf("User has read permission on file.\n");
if (statbuf.st_mode & S_IWRITE)
printf("User has write permission on file.\n");
printf("Drive letter of file: %c\n", 'A'+statbuf.st_dev);
printf("Size of file in bytes: %ld\n", statbuf.st_size);
printf("Time file last opened: %s\n", ctime(&statbuf.st_ctime));
return 0;
}
stat example
==============
#include <sys\stat.h>
#include <stdio.h>
#include <time.h>
#define FILENAME "TEST.$$$"
int main(void)
{
struct stat statbuf;
FILE *stream;
/* open a file for update */
if ((stream = fopen(FILENAME, "w+")) == NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return(1);
}
/* get information about the file */
stat(FILENAME, &statbuf);
fclose(stream);
/* display the information returned */
if (statbuf.st_mode & S_IFCHR)
printf("Handle refers to a device.\n");
if (statbuf.st_mode & S_IFREG)
printf("Handle refers to an ordinary file.\n");
if (statbuf.st_mode & S_IREAD)
printf("User has read permission on file.\n");
if (statbuf.st_mode & S_IWRITE)
printf("User has write permission on file.\n");
printf("Drive letter of file: %c\n", 'A'+statbuf.st_dev);
printf("Size of file in bytes: %ld\n", statbuf.st_size);
printf("Time file last opened: %s\n", ctime(&statbuf.st_ctime));
return 0;
}