sopen
Opens a shared file.
Defined in header <fcntl.h>
Syntax
#include <fcntl.h>
#include <sys\stat.h>
#inGlude <share.h>
#include <io.h>
int sopen(char *path, int access,intshflag[,intmode]);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ | ■ |
Remarks
sopen opens the file given by path and prepares it for shared reading or writing, as determined by access, shflag, and mode.
For sopen, access is constructed by ORing flags bitwise from the following two lists. Only one flag from the first list can be used; the remaining flags can be used in any logical combination.
List 1: Read/write flags
| 0_RDONLY | Open for reading only. |
| 0_WRONLY | Open for writing only. |
| 0_RDWR | Open for reading and writing. |
List 2: Other access flags
| 0_NDELAY | Not used; for UNIX compatibility. | |
| 0_APPEND | If set, the file pointer is set to the end of the file prior to each write. | |
| 0_CREAT 0_TRUNC If the file exists, its length is truncated to 0. The file | If the file exists, this flag has no effect. If the file does not exist, the file is created, and the bits of mode are used to set the file attribute bits as in chmod. attributes remain unchanged. | |
| 0_EXCL O_BINARY This flag can be given to explicitly open the file in binary | Used only with 0_CREAT. error is returned. mode. | If the file already exists, an |
| O_TEXT | This flag can be given to explicitly open the file in text mode. |
These O_... symbolic constants are defined in fcntl.h.
| If neither O_BINARY | nor O_TEXT is given, the file is opened in the |
translation mode set by the global variable _fmode.
| If the 0_CREAT | flag is used in constructing access, you need to supply the |
mode argument to sopen from the following symbolic constants defined in sysXstat.h.
| Value of mode | Access permission |
| S_IWRITE | Permission to write |
| SJREAD | Permission to read |
| S IREADIS IWRITE | Permission to read/write |
shflag specifies the type of file-sharing allowed on the file pat/z. Symbolic constants for shflag are defined in share.h.
| Value of shflag | What it does |
| SH_COMPAT | Sets compatibility mode |
| SH_DENYRW | Denies read/write access |
| SH_DENYWR | Denies write access |
| SH_DENYRD | Denies read access |
| SH_DENYNONE | Permits read/write access |
| SH DENYNO | Permits read/write access |
Return value
On successful completion, sopen returns a nonnegative integer (the file handle), and the file pointer (that marks the current position in the file) is set to the beginning of the file. On error, it returns -1, and the global variable errno is set to
| ENOENT | Path or file function not found |
| EMFILE | Too many open files |
| EACCES | Permission denied |
| EINVACC | Invalid access code |
See also
Example
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <process.h>
#include <share.h>
#include <stdio.h>
int main (void)
{
int handle, status;
handle = sopen( "c:\\autoexec.bat", 0_RDONLY, SH_DENYNO, S_IREAD);
if (handle < 0) {
printf ("sopen failed\n");
exit(l);
}
status = access ("c:\\autoexec.bat", 6);
if (status == 0)
printf ("read/write access allowed\n");
else
printf ("read/write access not allowed\n");
close (handle);
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/fcntl.h/sopen/
· short form /3.1/sopen/