Opens a file for file sharing.
#include <fcntl.h> | ||
#include <sys\types.h> | ||
#include <sys\stat.h> | ||
#include <share.h> | ||
#include <io.h> | Required only for function declarations |
int _sopen( char *filename,int oflag, int shflag[[, int pmode]] );
filename | Filename | |
oflag | Type of operations allowed | |
shflag | Type of sharing allowed | |
pmode | Permission setting |
The _sopen function opens the file specified by filename and prepares the file for subsequent shared reading or writing, as defined by oflag and shflag. The integer expression oflag is formed by combining one or more of the following manifest constants, defined in the file FCNTL.H. When two or more constants are used to form the argument oflag, the constants are combined with the bitwise-OR operator (|).
Constant | Meaning |
_O_APPEND | Repositions the file pointer to the end of the file before every write operation. |
_O_BINARY | Opens file in binary (untranslated) mode. (See fopen for a description of binary mode.) |
_O_CREAT | Creates and opens a new file. This has no effect if the file specified by filename exists. |
_O_EXCL | Returns an error value if the file specified by filename exists. This applies only when used with _O_CREAT. |
_O_RDONLY | Opens file for reading only. If this flag is given, neither the _O_RDWR flag nor the _O_WRONLY flag can be given. |
_O_RDWR | Opens file for both reading and writing. If this flag is given, neither _O_RDONLY nor _O_WRONLY can be given. |
_O_TEXT | Opens file in text (translated) mode. (See fopen for a description of text mode.) |
_O_TRUNC | Opens and truncates an existing file to 0 bytes. The file must have write permission; the contents of the file are destroyed. |
_O_WRONLY | Opens file for writing only. If this flag is given, neither _O_RDONLY nor _O_RDWR can be given. |
The argument shflag is a constant expression consisting of one of the following manifest constants, defined in SHARE.H. If SHARE.COM (or SHARE.EXE for some versions of DOS) is not installed, DOS ignores the sharing mode. (See your system documentation for detailed information about sharing modes.)
Constant | Meaning |
_SH_COMPAT | Sets compatibility mode. This is the sharing mode used in the _open function in DOS. |
_SH_DENYRW | Denies read and write access to file. |
_SH_DENYWR | Denies write access to file. |
_SH_DENYRD | Denies read access to file. |
_SH_DENYNO | Permits read and write access. |
The _sopen function should be used only with DOS version 3.0 and later. Under earlier versions of DOS, the shflag argument is ignored.
The pmode argument is required only when _O_CREAT is specified. If the file does not exist, pmode specifies the file's permission settings, which are set when the new file is closed for the first time. Otherwise, the pmode argument is ignored. The pmode argument is an integer expression that contains one or both of the manifest constants _S_IWRITE and _S_IREAD, defined in SYS\STAT.H. When both constants are given, they are combined with the bitwise-OR operator (|). The meaning of the pmode argument is as follows:
Value | Meaning |
_S_IWRITE | Writing permitted |
_S_IREAD | Reading permitted |
_S_IREAD | _S_IWRITE | Reading and writing permitted |
If write permission is not given, the file is read-only. With DOS, all files are readable; it is not possible to give write-only permission. Thus, the modes _S_IWRITE and _S_IREAD | _S_IWRITE are equivalent.
Note that with DOS versions 3.x with SHARE installed, a problem occurs when opening a new file with _sopen under the following sets of conditions:
With oflag set to _O_CREAT | _O_RDONLY or _O_CREAT | _O_WRONLY, pmode set to _S_IREAD, and shflag set to _SH_COMPAT.
With oflag set to any combination that includes _O_CREAT | _O_RDWR, pmode set to _S_IREAD, and shflag set to anything other than _SH_COMPAT.
In either case, the operating system will prematurely close the file during system calls made within _sopen, or the system will generate a sharing violation (INT 24H). To avoid the problem, open the file with pmode set to _S_IWRITE. After closing the file, call _chmod and change the mode back to _S_IREAD. Another solution is to open the file with pmode set to _S_IREAD, oflag set to _O_CREAT | _O_RDWR, and shflag set to _SH_COMPAT.
The _sopen function applies the current file-permission mask to pmode before setting the permissions (see _umask).
The _sopen function returns a file handle for the opened file. A return value of –1 indicates an error, and errno is set to one of the following values:
Value | Meaning |
EACCES | Given path name is a directory; or the file is read-only but an open for writing was attempted; or a sharing violation occurred (the file's sharing mode does not allow the specified operations; DOS versions 3.0 and later only). |
EEXIST | The _O_CREAT and _O_EXCL flags are specified, but the named file already exists. |
EINVAL | An invalid oflag or shflag argument was given. |
EMFILE | No more file handles available (too many open files). |
ENOENT | File or path name not found. |
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
_close, _creat, fopen, _fsopen, _open, _umask
See the example for _locking.