open a file for shared access
#include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <share.h> int sopen( const char *filename, int access, int share, ... );
The sopen() function opens a file at the operating system level for shared access. The name of the file to be opened is given by filename. The file can be accessed according to the access mode specified by access.
The sharing mode of the file is given by the share argument. The optional argument is the file permissions to be used when O_CREAT flag is on in the access mode; you must provide this when the file is to be created.
The access mode is established by a combination of the bits defined in the <fcntl.h> header file. The following bits may be set:
O_CREAT must be specified when the file doesn't exist.
When the file is to be created (O_CREAT is specified), an additional argument must be passed that contains the file permissions to be used for the new file, specified as a combination of bits. These are defined in the <sys/stat.h> header file, and are described in the Header Files chapter.
The sopen() function applies the current file permission mask to the specified permissions (see umask()).
The shared access for the file, share, is established by a combination of bits defined in the <share.h> header file. The following values may be set:
Note that
open( path, oflag, ... );
is the same as:
sopen( path, oflag, SH_COMPAT, ... );
The sopen() function ignores advisory locks that may have been set by the fcntl(), lock(), or locking() functions. |
A descriptor for the file. If an error occurs while opening the file, -1 is returned and errno indicates the type of error detected.
#include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <share.h> int main( void ) { int filedes ; /* open a file for output */ /* replace existing file if it exists */ filedes = sopen( "file", O_WRONLY | O_CREAT | O_TRUNC, SH_DENYWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP ); /* read a file which is assumed to exist */ filedes = sopen( "file", O_RDONLY, SH_DENYWR ); /* append to the end of an existing file */ /* write a new file if file does not exist */ filedes = sopen( "file", O_WRONLY | O_CREAT | O_APPEND, SH_DENYWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP ); return( EXIT_SUCCESS ); }
WATCOM
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
chsize(), close(), creat(), dup(), dup2(), errno, eof(), exec... functions, fcntl(), filelength(), fileno(), fstat(), isatty(), lseek(), open(), read(), setmode(), stat(), tell(), umask(), write()